author | Stefan Vogel <sv@exept.de> |
Sat, 20 Jun 2009 11:16:54 +0200 | |
changeset 11778 | a4d708790f90 |
parent 10635 | c3c4fc186068 |
child 13121 | e0cc829d4b4d |
child 17711 | 39faaaf888b4 |
permissions | -rw-r--r-- |
10249 | 1 |
" |
2 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
10248 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
14 |
Object subclass:#ReadEvalPrintLoop |
|
10254 | 15 |
instanceVariableNames:'inputStream outputStream errorStream compiler prompt |
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
16 |
doChunkFormat traceFlag timingFlag exitAction' |
10248 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
10277 | 19 |
category:'System-Support' |
10248 | 20 |
! |
21 |
||
10249 | 22 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
10461
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
36 |
! |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
37 |
|
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
38 |
documentation |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
39 |
" |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
40 |
A simple read-eval-print loop for non-GUI or stscript operation. |
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
41 |
A line starting with '?' shows the usage message. |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
42 |
Lines starting with '#' are directives: |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
43 |
#exit - exit the rep-loop |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
44 |
|
10461
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
45 |
" |
10249 | 46 |
! ! |
10248 | 47 |
|
48 |
!ReadEvalPrintLoop methodsFor:'accessing'! |
|
49 |
||
10463 | 50 |
compiler:something |
51 |
compiler := something. |
|
10248 | 52 |
! |
53 |
||
10632 | 54 |
doChunkFormat |
55 |
^ doChunkFormat ? true |
|
56 |
||
57 |
"Created: / 07-12-2006 / 18:24:04 / cg" |
|
58 |
! |
|
59 |
||
10463 | 60 |
doChunkFormat:something |
10254 | 61 |
doChunkFormat := something. |
62 |
||
63 |
"Created: / 07-12-2006 / 18:24:04 / cg" |
|
10248 | 64 |
! |
65 |
||
66 |
error:something |
|
67 |
errorStream := something. |
|
68 |
||
69 |
"Created: / 07-12-2006 / 17:33:39 / cg" |
|
70 |
! |
|
71 |
||
10463 | 72 |
errorStream |
10256 | 73 |
^ errorStream ? Transcript ? Stderr |
74 |
||
75 |
"Created: / 07-12-2006 / 19:11:56 / cg" |
|
76 |
! |
|
77 |
||
10463 | 78 |
input:something |
10248 | 79 |
inputStream := something. |
80 |
||
81 |
"Modified: / 07-12-2006 / 17:33:31 / cg" |
|
82 |
! |
|
83 |
||
10463 | 84 |
inputStream |
85 |
^ inputStream ? Stdin |
|
86 |
||
87 |
"Created: / 07-12-2006 / 19:12:13 / cg" |
|
10256 | 88 |
! |
89 |
||
10463 | 90 |
output:something |
10248 | 91 |
outputStream := something. |
92 |
||
93 |
"Created: / 07-12-2006 / 17:27:48 / cg" |
|
94 |
! |
|
95 |
||
10463 | 96 |
outputStream |
97 |
^ outputStream ? Stdout |
|
98 |
||
99 |
"Created: / 07-12-2006 / 19:12:27 / cg" |
|
100 |
! |
|
101 |
||
102 |
prompt:something |
|
103 |
prompt := something. |
|
10248 | 104 |
! ! |
105 |
||
10631 | 106 |
!ReadEvalPrintLoop methodsFor:'compiler interface-error handling'! |
107 |
||
108 |
correctableError:message position:pos1 to:pos2 from:aCompiler |
|
109 |
"compiler notifies us of an error - ignore it" |
|
110 |
||
111 |
^ false |
|
112 |
! |
|
113 |
||
114 |
correctableSelectorWarning:aString position:relPos to:relEndPos from:aCompiler |
|
115 |
"compiler notifies us of a warning - ignore it" |
|
116 |
||
117 |
^ false |
|
118 |
! |
|
119 |
||
120 |
error:aString position:relPos to:relEndPos from:aCompiler |
|
121 |
"compiler notifies us of a warning - ignore it" |
|
122 |
||
123 |
^ false |
|
124 |
! |
|
125 |
||
126 |
unusedVariableWarning:aString position:relPos to:relEndPos from:aCompiler |
|
127 |
"compiler notifies us of a warning - ignore it" |
|
128 |
||
129 |
^ false |
|
130 |
! |
|
131 |
||
132 |
warning:aString position:relPos to:relEndPos from:aCompiler |
|
133 |
"compiler notifies us of a warning - ignore it" |
|
134 |
||
135 |
^ self |
|
136 |
! ! |
|
137 |
||
10255 | 138 |
!ReadEvalPrintLoop methodsFor:'directives'! |
139 |
||
10463 | 140 |
cmd_clear:lineStream |
10255 | 141 |
self cmd_setOrClear:lineStream to:false |
142 |
||
143 |
"Created: / 07-12-2006 / 19:04:50 / cg" |
|
144 |
! |
|
145 |
||
10463 | 146 |
cmd_exit:lineStream |
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
147 |
exitAction value |
10463 | 148 |
|
149 |
"Created: / 07-12-2006 / 18:55:46 / cg" |
|
10255 | 150 |
! |
151 |
||
10463 | 152 |
cmd_help:lineStream |
153 |
self errorStream |
|
154 |
nextPutAll: |
|
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
155 |
'Everything entered up to an empty line is called a "chunk" and evaluated. |
10463 | 156 |
Lines starting with "#" are commands to the read-eval-print interpreter. |
157 |
||
158 |
Valid commands are: |
|
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
159 |
#help ............... this text |
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
160 |
#exit ............... exit interpreter loop |
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
161 |
#use <package>....... use (load) a package |
10463 | 162 |
stx:libwidg ........ GUI package |
163 |
stx:libtool ........ IDE tool package |
|
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
164 |
#show <what> ........ show info |
10463 | 165 |
variables .......... interpreter variables |
166 |
processes .......... processes |
|
167 |
memory ............. memory usage |
|
168 |
flags .............. flags |
|
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
169 |
#set/clear <flag> ... set or clear a flag |
10463 | 170 |
trace .............. tracing execution |
171 |
timing ............. timing execution |
|
10257 | 172 |
|
173 |
MiniDebugger shows its help with "?". |
|
10255 | 174 |
' |
175 |
||
176 |
"Created: / 07-12-2006 / 18:54:20 / cg" |
|
177 |
! |
|
178 |
||
10463 | 179 |
cmd_set:lineStream |
10255 | 180 |
self cmd_setOrClear:lineStream to:true |
181 |
||
182 |
"Modified: / 07-12-2006 / 19:04:46 / cg" |
|
183 |
! |
|
184 |
||
10463 | 185 |
cmd_setOrClear:lineStream to:aBoolean |
186 |
|what| |
|
187 |
||
188 |
lineStream skipSeparators. |
|
189 |
what := lineStream nextAlphaNumericWord. |
|
190 |
(what startsWith:'tra') ifTrue:[ |
|
191 |
traceFlag := aBoolean. |
|
192 |
^ self. |
|
193 |
]. |
|
194 |
(what startsWith:'tim') ifTrue:[ |
|
195 |
timingFlag := aBoolean. |
|
196 |
^ self. |
|
197 |
]. |
|
198 |
||
199 |
self errorStream nextPutLine:'?? which flag ?'. |
|
10255 | 200 |
|
10256 | 201 |
"Modified: / 07-12-2006 / 19:13:34 / cg" |
10255 | 202 |
! |
203 |
||
10463 | 204 |
cmd_show:lineStream |
205 |
|what| |
|
206 |
||
207 |
lineStream skipSeparators. |
|
208 |
what := lineStream nextAlphaNumericWord. |
|
209 |
(what startsWith:'var') ifTrue:[ |
|
210 |
^ self. |
|
211 |
]. |
|
212 |
(what startsWith:'proc') ifTrue:[ |
|
213 |
MiniDebugger basicNew showProcesses. |
|
214 |
^ self. |
|
215 |
]. |
|
216 |
(what startsWith:'mem') ifTrue:[ |
|
217 |
self errorStream |
|
218 |
nextPutLine:('overall: ',(ObjectMemory bytesUsed // 1024) printString,' Kb'); |
|
219 |
nextPutLine:('free : ',(ObjectMemory freeSpace // 1024) printString,' Kb'); |
|
220 |
nextPutLine:('minorGC: ',(ObjectMemory scavengeCount) printString); |
|
221 |
nextPutLine:('majorGC: ',(ObjectMemory garbageCollectCount) printString). |
|
222 |
^ self. |
|
223 |
]. |
|
224 |
(what startsWith:'flag') ifTrue:[ |
|
225 |
self errorStream |
|
226 |
nextPutLine:('trace : ',traceFlag printString); |
|
227 |
nextPutLine:('timing: ',timingFlag printString). |
|
228 |
^ self. |
|
229 |
]. |
|
230 |
||
231 |
self errorStream nextPutLine:'?? show what ?'. |
|
10255 | 232 |
|
10261 | 233 |
"Modified: / 07-12-2006 / 19:23:15 / cg" |
10255 | 234 |
! |
235 |
||
10463 | 236 |
cmd_use:lineStream |
237 |
|pkg| |
|
238 |
||
239 |
lineStream skipSeparators. |
|
240 |
pkg := lineStream upToEnd withoutSeparators. |
|
10255 | 241 |
Smalltalk loadPackage:pkg |
242 |
||
243 |
"Created: / 07-12-2006 / 19:07:56 / cg" |
|
10463 | 244 |
! |
245 |
||
246 |
directive:line |
|
247 |
|s cmd| |
|
248 |
||
249 |
s := line readStream. |
|
250 |
s next. "/ skip the hash |
|
251 |
s skipSeparators. |
|
252 |
||
253 |
cmd := s nextAlphaNumericWord. |
|
254 |
self |
|
255 |
perform:('cmd_',cmd,':') asSymbol with:s |
|
256 |
ifNotUnderstood:[ |
|
257 |
self errorStream |
|
258 |
nextPutAll:'?? invalid command: '; |
|
259 |
nextPutAll:cmd; |
|
260 |
nextPutAll:'. Type "#help" for help.'; |
|
261 |
cr. |
|
262 |
]. |
|
263 |
||
264 |
"Created: / 07-12-2006 / 18:49:17 / cg" |
|
10255 | 265 |
! ! |
266 |
||
10248 | 267 |
!ReadEvalPrintLoop methodsFor:'evaluation'! |
268 |
||
10463 | 269 |
readEvalPrintLoop |
270 |
"{ Pragma: +optSpace }" |
|
271 |
||
272 |
"simple read-eval-print loop for non-graphical Minitalk. |
|
273 |
If the chunkFormat-argument is true, chunks are read. |
|
274 |
Otherwise, lines up to an empty line (or EOF) are read." |
|
275 |
||
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
276 |
exitAction := [^ self]. |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
277 |
|
10463 | 278 |
ControlInterrupt handle:[:ex | |
279 |
self errorStream nextPutLine:('Cought: ', ex description). |
|
280 |
ex restart. |
|
281 |
] do:[ |
|
282 |
[ |
|
283 |
|input output error lines chunk compilerClass| |
|
284 |
||
285 |
"/ re-evaluate these in the loop, so they can be changed dynamically |
|
286 |
input := self inputStream. |
|
287 |
output := self outputStream. |
|
288 |
error := self errorStream. |
|
289 |
compilerClass := compiler ? Compiler. |
|
290 |
||
291 |
prompt notNil ifTrue:[ |
|
292 |
error nextPutAll:prompt. |
|
293 |
]. |
|
294 |
||
295 |
input atEnd ifTrue:[ |
|
296 |
^ self. |
|
297 |
]. |
|
298 |
||
10632 | 299 |
self doChunkFormat ifTrue:[ |
10463 | 300 |
input skipSeparators. |
301 |
chunk := input nextChunk. |
|
302 |
] ifFalse:[ |
|
303 |
lines := OrderedCollection new. |
|
304 |
[ |
|
305 |
|line| |
|
306 |
||
307 |
line := input nextLine. |
|
308 |
line notEmptyOrNil ifTrue:[ |
|
309 |
line = '?' ifTrue:[ |
|
310 |
self cmd_help:nil. |
|
311 |
prompt notNil ifTrue:[ |
|
312 |
error nextPutAll:prompt. |
|
313 |
]. |
|
314 |
] ifFalse:[ |
|
315 |
(line startsWith:'#') ifTrue:[ |
|
316 |
self directive:line. |
|
317 |
prompt notNil ifTrue:[ |
|
318 |
error nextPutAll:prompt. |
|
319 |
]. |
|
320 |
] ifFalse:[ |
|
321 |
lines add:line. |
|
322 |
] |
|
323 |
] |
|
324 |
]. |
|
325 |
line notEmptyOrNil. |
|
326 |
] whileTrue. |
|
327 |
chunk := lines asStringWith:Character cr. |
|
328 |
]. |
|
329 |
||
330 |
chunk notEmptyOrNil ifTrue:[ |
|
10500
3106f50bd9d7
AbortOperationRequest is a child of AbortAllOperationRequest
Stefan Vogel <sv@exept.de>
parents:
10463
diff
changeset
|
331 |
AbortAllOperationRequest handle:[:ex | |
10463 | 332 |
error nextPutLine:('Evaluation aborted: ', ex description) |
333 |
] do:[ |
|
334 |
(Error, ControlInterrupt) handle:[:ex | |
|
335 |
prompt isNil ifTrue:[ |
|
336 |
ex reject |
|
337 |
]. |
|
338 |
MiniDebugger enterWithMessage:(ex errorString) mayProceed:true. |
|
339 |
ex mayProceed ifTrue:[ |
|
340 |
ex proceed. |
|
341 |
]. |
|
342 |
error nextPutLine:('Evaluation aborted: ', ex description). |
|
343 |
ex return. |
|
344 |
] do:[ |
|
345 |
|value| |
|
346 |
||
347 |
value := (compilerClass new requestor:self) evaluate:chunk compile:true. |
|
348 |
value printOn:output. |
|
349 |
output cr. |
|
350 |
]. |
|
351 |
]. |
|
352 |
]. |
|
353 |
] loop. |
|
354 |
] |
|
355 |
||
356 |
" |
|
357 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
|
358 |
" |
|
359 |
||
360 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
361 |
"Modified: / 07-12-2006 / 19:41:20 / cg" |
|
10248 | 362 |
! ! |
363 |
||
364 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
|
365 |
||
366 |
version |
|
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
367 |
^ '$Header: /cvs/stx/stx/libbasic/ReadEvalPrintLoop.st,v 1.26 2007-07-04 16:00:58 cg Exp $' |
10248 | 368 |
! ! |