summaryrefslogtreecommitdiff
path: root/ipl/progs/interpp.icn
blob: 1718cc5bfdc68d37dd45c09a72f857711f92715d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
############################################################################
#
#	File:     interpp.icn
#
#	Subject:  Program to interpret Icon programs
#
#	Author:   Jerry Nowlin
#
#	Date:     December 30, 1991
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#  
#    This program is kind of like an interactive version of BASIC in that Icon
#  expressions are entered with line numbers and you can resequence them list
#  them etc. and execute all the lines entered.  There is no editor built
#  in.  You have to retype a line to change it.
#
#    Documentation is lacking but there is a "?" help command that lists all
#  the other commands.
#
############################################################################
#
#  See also:  interpe.icn
#
############################################################################

global	WHITE,	# the white space cset
	MFLAG,	# the modified flag
	PRTBL	# the program table

procedure main(arg)
   local line, lno, pline

#	define the needed cset
	WHITE := ' \t\n\f'

#	initialize the program table
	PRTBL := table()

#	initialize the modified flag
	MFLAG := 0

#	get all the input
	writes("Icon> ")
	while line := read() do {

#		scan the input line
		line ? {

#			skip any initial white space
			tab(many(WHITE))

#			check for program lines (they have line numbers)
			if lno := tab(many(&digits)) & tab(many(WHITE)) then {

#				get the program line
				pline := tab(0)

#				store the line in the program table
				PRTBL[numeric(lno)] := pline

#				set the modified flag
				MFLAG +:= 1
			}

#			read command
			else if (tab(upto(WHITE)) | tab(0)) ==
				("read" | "r") then {
				readprog()

#				clear the modified flag
				MFLAG := 0
			}

#			write command
			else if (tab(upto(WHITE)) | tab(0)) ==
				("write" | "w") then {
				writeprog()

#				clear the modified flag
				MFLAG := 0
			}

#			delete command
			else if (tab(upto(WHITE)) | tab(0)) ==
				("delete" | "d") then {
				delprog()

#				set the modified flag
				MFLAG +:= 1
			}

#			sequence command
			else if (tab(upto(WHITE)) | tab(0)) ==
				("sequence" | "s") then {
				seqprog()
			}

#			list command
			else if (tab(upto(WHITE)) | tab(0)) ==
				("list" | "l") then {
				listprog()
			}

#			execute command
			else if (tab(upto(WHITE)) | tab(0)) ==
				("execute" | "e") then {
				execprog()
			}

#			help command
			else if (tab(upto(WHITE)) | tab(0)) ==
				("help" | "h" | "?") then {
				helpprog()
			}

#			quit command
			else if (tab(upto(WHITE)) | tab(0)) ==
				("quit" | "q") then {
				quitprog()
			}

#			invalid syntax input
			else {
				write("Syntax Error: ",line)
				helpprog()
			}
		}
		writes("Icon> ")
	}

end

procedure execprog()
   local runargs, out, prog, line, command

	static	tmpfile

	initial tmpfile := "TMPFILE.icn"

#	get any runtime arguments
	runargs := tab(0)

#	create the temporary Icon file
	(out := open(tmpfile,"w")) |

#	or mention the problem and fail
	(write("I can't open '",tmpfile,"' for writing") & fail)

#	sort the program table
	prog := sort(PRTBL)

#	put the program in the file
	every line := !prog do {
		write(out,line[2])
	}
	close(out)

#	format the command to execute the program
	command := "icont -s " || tmpfile || " -x " || runargs

#	add the command to remove the temporary file
	command ||:= " ; rm -f " || tmpfile

#	execute the command
	system(command)

end

procedure seqprog()
   local begno, incno, prog, lno, l

#	initialize the sequencing numbers
	begno := incno := 10

#	skip any white space
	tab(many(WHITE))

#	get an initial line number
	begno := numeric(tab(many(&digits)))

#	skip any white space
	tab(many(WHITE))

#	get a increment number
	incno := numeric(tab(many(&digits)))

#	sort the program table
	prog := sort(PRTBL)

#	reinitialize it
	PRTBL := table()

#	sequence the program lines starting with begno by incno
	lno := begno
	every l := !prog do {
		PRTBL[lno] := l[2]
		lno +:= incno
	}

end

procedure readprog()
   local readfile, response, in, lno, line

#	get a possible command line file name
	tab(many(WHITE))
	readfile := tab(upto(WHITE) | 0)

#	if there was no file with the command get one
	if /readfile | *readfile = 0 then {
		writes("Read file name: ")
		readfile := read()
	}

#	make sure a modified file has been written
	if MFLAG > 0 then {
		writes("Write before reading over current program? ")
		response := read()
		if any('yY',response) then
			writeprog()
	}

#	initialize the program table
	PRTBL := table()

#	read the program from the read file
	in := open(readfile,"r")
	lno := 10
	every line := !in do {
		PRTBL[lno] := line
		lno +:= 10
	}
	close(in)

#	tell them what you did
	write("Read '",readfile,"'...",*PRTBL," lines")

end

procedure writeprog()
   local writefile, prog, out, l

#	get a possible command line file name
	tab(many(WHITE))
	writefile := tab(upto(WHITE) | 0)

#	if there was no file with the command get one
	if /writefile | *writefile = 0 then {
		writes("Write file name: ")
		writefile := read()
	}

#	sort the program table
	prog := sort(PRTBL)

#	write the program to the write file
	out := open(writefile,"w")
	every l := !prog do {
		write(out,l[2])
	}
	close(out)

#	tell them what you did
	write("Write '",writefile,"'...",*PRTBL," lines")

end

procedure delprog()
   local begno, endno, prog, l, lno

#	initialize the line numbers
	begno := 0
	endno := 99999

#	skip any white space
	tab(many(WHITE))

#	get an initial line number
	begno := endno := numeric(tab(many(&digits)))

#	skip any white space
	tab(many(WHITE))

#	get a final line number
	endno := numeric(tab(many(&digits)))

#	sort the program table
	prog := sort(PRTBL)

#	reinitialize it
	PRTBL := table()

#	delete the program lines between the optional numbers
	every l := !prog do {
		lno := numeric(l[1])
		if (lno < begno) | (lno > endno) then PRTBL[lno] := l[2]
	}

end

procedure listprog()
   local begno, endno, prog, l, lno

#	initialize the line numbers
	begno := 0
	endno := 99999

#	skip any white space
	tab(many(WHITE))

#	get an initial line number
	begno := endno := numeric(tab(many(&digits)))

#	skip any white space
	tab(many(WHITE))

#	get a final line number
	endno := numeric(tab(many(&digits)))

#	sort the program table
	prog := sort(PRTBL)

#	list the program lines between the optional numbers
	every l := !prog do {
		lno := numeric(l[1])
		if (lno >= begno) & (lno <= endno) then
			write(right(lno,5),": ",l[2])
		if lno > endno then break
	}

end

procedure helpprog()

	static helpmsg

#	define the help message
	initial {
		helpmsg := [
		"<<< Icon Expression Syntax >>>",
		"",
		"lineno expression",
		"",
		"<<< Command Summary >>>",
		" (1st character works)",
		"",
		"read [ file ]",
		"write [ file ]",
		"list [ begno [ endno ] ]",
		"delete [ begno [ endno ] ]",
		"sequence [ begno [ increment ] ]",
		"execute [ args ]",
		"quit",
		"help"
		]
	}

#	print it
	every write(!helpmsg)

end

procedure quitprog()
   local response

#	make sure a modified file has been written
	if MFLAG > 0 then {
		writes("Write before quitting? ")
		response := read()
		if any('yY',response) then
			writeprog()
	}

	stop("Goodbye.")

end