author | Claus Gittinger <cg@exept.de> |
Tue, 08 Nov 2016 14:42:49 +0100 | |
changeset 20892 | 765ca5daafd7 |
parent 20891 | 8d94557ec419 |
child 20900 | f2e647fa7eb1 |
permissions | -rw-r--r-- |
10249 | 1 |
" |
2 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
16565 | 3 |
All Rights Reserved |
10249 | 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 |
||
19379 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
10248 | 16 |
Object subclass:#ReadEvalPrintLoop |
16228 | 17 |
instanceVariableNames:'inputStream outputStream errorStream compiler prompt |
20771 | 18 |
doChunkFormat traceFlag timingFlag profilingFlag printFlag |
19 |
exitAction currentDirectory' |
|
16228 | 20 |
classVariableNames:'' |
21 |
poolDictionaries:'' |
|
22 |
category:'System-Support' |
|
10248 | 23 |
! |
24 |
||
10249 | 25 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
16565 | 30 |
All Rights Reserved |
10249 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
10461
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 |
|
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
41 |
documentation |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
42 |
" |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
43 |
A simple read-eval-print loop for non-GUI or stscript operation. |
16081 | 44 |
Invoked, for example if stx is started with a --repl argument. |
20833 | 45 |
|
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
46 |
A line starting with '?' shows the usage message. |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
47 |
Lines starting with '#' are directives: |
20833 | 48 |
#exit - exit the rep-loop |
49 |
type '?' to see more. |
|
50 |
||
16087 | 51 |
The input can be in one of two formats: |
20833 | 52 |
1) traditional chunk format (bang-separated chunks, bangs duplicated) |
53 |
this is the traditional fileIn format, as generated by fileOut from the browser |
|
16087 | 54 |
|
20833 | 55 |
2) interactive line mode. Chunks are any number of lines up to either an empty line or |
56 |
a line ending in a period. This is more useful for an interactive REPL, where statements/expressions |
|
57 |
are entered linewise by a user. |
|
16087 | 58 |
|
59 |
The input can is switched to non-chunk format whenever a line with a '#' in the first column appears. |
|
20833 | 60 |
|
61 |
[Author:] |
|
62 |
Claus Gittinger |
|
10461
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
63 |
" |
10249 | 64 |
! ! |
10248 | 65 |
|
66 |
!ReadEvalPrintLoop methodsFor:'accessing'! |
|
67 |
||
10463 | 68 |
compiler:something |
16087 | 69 |
"assign a compiler to use;could be used to change the language" |
70 |
||
10463 | 71 |
compiler := something. |
10248 | 72 |
! |
73 |
||
10632 | 74 |
doChunkFormat |
16087 | 75 |
"true if currently reading chunk format" |
76 |
||
10632 | 77 |
^ doChunkFormat ? true |
78 |
||
79 |
"Created: / 07-12-2006 / 18:24:04 / cg" |
|
80 |
! |
|
81 |
||
16087 | 82 |
doChunkFormat:aBoolean |
83 |
"enable/disable chunk format" |
|
84 |
||
85 |
doChunkFormat := aBoolean. |
|
10254 | 86 |
|
87 |
"Created: / 07-12-2006 / 18:24:04 / cg" |
|
10248 | 88 |
! |
89 |
||
16087 | 90 |
error:aStream |
91 |
"assign an error stream" |
|
92 |
||
93 |
errorStream := aStream. |
|
10248 | 94 |
|
95 |
"Created: / 07-12-2006 / 17:33:39 / cg" |
|
96 |
! |
|
97 |
||
10463 | 98 |
errorStream |
16087 | 99 |
"return the current error stream" |
100 |
||
19379 | 101 |
errorStream notNil ifTrue:[^ errorStream]. |
102 |
^ Processor activeProcess stderr |
|
10256 | 103 |
|
19379 | 104 |
"Created: / 07-12-2006 / 19:12:27 / cg" |
10256 | 105 |
! |
106 |
||
16087 | 107 |
input:aStream |
108 |
"assign an input stream" |
|
109 |
||
110 |
inputStream := aStream asLineNumberReadStream. |
|
10248 | 111 |
|
112 |
"Modified: / 07-12-2006 / 17:33:31 / cg" |
|
113 |
! |
|
114 |
||
10463 | 115 |
inputStream |
16087 | 116 |
"get the current input stream" |
117 |
||
19379 | 118 |
inputStream notNil ifTrue:[^ inputStream]. |
119 |
^ Processor activeProcess stdin |
|
10463 | 120 |
|
121 |
"Created: / 07-12-2006 / 19:12:13 / cg" |
|
10256 | 122 |
! |
123 |
||
16087 | 124 |
output:aStream |
125 |
"assign an output stream" |
|
126 |
||
127 |
outputStream := aStream. |
|
10248 | 128 |
|
129 |
"Created: / 07-12-2006 / 17:27:48 / cg" |
|
130 |
! |
|
131 |
||
10463 | 132 |
outputStream |
19379 | 133 |
"return the current output stream" |
16087 | 134 |
|
19379 | 135 |
outputStream notNil ifTrue:[^ outputStream]. |
136 |
^ Processor activeProcess stdout |
|
10463 | 137 |
|
138 |
"Created: / 07-12-2006 / 19:12:27 / cg" |
|
139 |
! |
|
140 |
||
16087 | 141 |
prompt:aString |
142 |
"set the prompt" |
|
143 |
||
144 |
prompt := aString. |
|
10248 | 145 |
! ! |
146 |
||
10631 | 147 |
!ReadEvalPrintLoop methodsFor:'compiler interface-error handling'! |
148 |
||
149 |
correctableError:message position:pos1 to:pos2 from:aCompiler |
|
150 |
"compiler notifies us of an error - ignore it" |
|
151 |
||
15702 | 152 |
^ false "/ no correction |
10631 | 153 |
! |
154 |
||
155 |
correctableSelectorWarning:aString position:relPos to:relEndPos from:aCompiler |
|
156 |
"compiler notifies us of a warning - ignore it" |
|
157 |
||
158 |
^ false |
|
159 |
! |
|
160 |
||
13121
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
161 |
correctableWarning:message position:pos1 to:pos2 from:aCompiler |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
162 |
"compiler notifies us of an error - ignore it" |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
163 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
164 |
^ false |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
165 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
166 |
"Created: / 02-11-2010 / 13:29:22 / cg" |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
167 |
! |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
168 |
|
10631 | 169 |
error:aString position:relPos to:relEndPos from:aCompiler |
170 |
"compiler notifies us of a warning - ignore it" |
|
171 |
||
172 |
^ false |
|
173 |
! |
|
174 |
||
175 |
unusedVariableWarning:aString position:relPos to:relEndPos from:aCompiler |
|
176 |
"compiler notifies us of a warning - ignore it" |
|
177 |
||
178 |
^ false |
|
179 |
! |
|
180 |
||
181 |
warning:aString position:relPos to:relEndPos from:aCompiler |
|
182 |
"compiler notifies us of a warning - ignore it" |
|
183 |
||
184 |
^ self |
|
185 |
! ! |
|
186 |
||
10255 | 187 |
!ReadEvalPrintLoop methodsFor:'directives'! |
188 |
||
10463 | 189 |
cmd_clear:lineStream |
10255 | 190 |
self cmd_setOrClear:lineStream to:false |
191 |
||
192 |
"Created: / 07-12-2006 / 19:04:50 / cg" |
|
193 |
! |
|
194 |
||
10463 | 195 |
cmd_exit:lineStream |
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
196 |
exitAction value |
10463 | 197 |
|
198 |
"Created: / 07-12-2006 / 18:55:46 / cg" |
|
10255 | 199 |
! |
200 |
||
10463 | 201 |
cmd_help:lineStream |
202 |
self errorStream |
|
20771 | 203 |
nextPutAll: |
16084 | 204 |
'Everything entered up to an empty line or a line ending in "." is called a "chunk" and evaluated. |
10463 | 205 |
Lines starting with "#" are commands to the read-eval-print interpreter. |
206 |
||
207 |
Valid commands are: |
|
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
208 |
#help ............... this text |
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
209 |
#exit ............... exit interpreter loop |
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
210 |
#use <package>....... use (load) a package |
20771 | 211 |
stx:libwidg .............. GUI package |
212 |
stx:libtool .............. IDE tool package |
|
213 |
stx:goodies/regex ........ regex package |
|
214 |
stx:goodies/petitparser .. peg parser package |
|
16087 | 215 |
#read <filename>..... read another script or source file |
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
216 |
#show <what> ........ show info |
20771 | 217 |
variables .......... interpreter variables |
218 |
processes .......... processes |
|
219 |
memory ............. memory usage |
|
220 |
flags .............. flags |
|
221 |
modules ............ loaded modules |
|
20834 | 222 |
all ................ all of the above |
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
223 |
#set/clear <flag> ... set or clear a flag |
20771 | 224 |
trace .............. tracing execution |
225 |
timing ............. timing execution |
|
226 |
profiling .......... show execution profile |
|
227 |
chunkFormat ........ traditional bang chunk format input mode |
|
10257 | 228 |
|
16087 | 229 |
The MiniDebugger (if entered) shows its own help with "?". |
10255 | 230 |
' |
231 |
||
232 |
"Created: / 07-12-2006 / 18:54:20 / cg" |
|
233 |
! |
|
234 |
||
16087 | 235 |
cmd_read:lineStream |
236 |
|filename newInput savedPrompt savedPrint savedInput savedCurrentDirectory| |
|
237 |
||
238 |
lineStream skipSeparators. |
|
239 |
filename := lineStream upToEnd withoutSeparators. |
|
16133 | 240 |
filename isNil ifTrue:[ |
16565 | 241 |
'? which file?' errorPrintCR. |
242 |
^ self. |
|
16133 | 243 |
]. |
244 |
filename := filename withoutSeparators. |
|
245 |
filename isEmpty ifTrue:[ |
|
16565 | 246 |
'? which file?' errorPrintCR. |
247 |
^ self. |
|
16133 | 248 |
]. |
16087 | 249 |
|
250 |
filename := filename asFilename. |
|
251 |
filename isAbsolute ifFalse:[ |
|
16565 | 252 |
filename := currentDirectory construct:filename. |
16087 | 253 |
]. |
254 |
||
255 |
newInput := filename readStream. |
|
256 |
newInput isNil ifTrue:[ |
|
16565 | 257 |
('Could not find file: "',filename,'"') errorPrintCR. |
258 |
^ self. |
|
16087 | 259 |
]. |
260 |
||
261 |
[ |
|
16565 | 262 |
savedCurrentDirectory := currentDirectory. |
263 |
savedInput := inputStream. |
|
264 |
savedPrint := printFlag. |
|
265 |
savedPrompt := prompt. |
|
16087 | 266 |
|
16565 | 267 |
currentDirectory := filename directory. |
268 |
inputStream := newInput. |
|
16087 | 269 |
|
16565 | 270 |
self |
271 |
basicReadEvalPrintLoopWithInput:newInput |
|
272 |
output:outputStream |
|
273 |
error:errorStream |
|
274 |
compiler:compiler |
|
275 |
prompt:false |
|
276 |
print:false. |
|
16087 | 277 |
] ensure:[ |
16565 | 278 |
currentDirectory := savedCurrentDirectory. |
279 |
inputStream := savedInput. |
|
280 |
printFlag := savedPrint. |
|
281 |
prompt := savedPrompt |
|
16087 | 282 |
]. |
283 |
! |
|
284 |
||
10463 | 285 |
cmd_set:lineStream |
10255 | 286 |
self cmd_setOrClear:lineStream to:true |
287 |
||
288 |
"Modified: / 07-12-2006 / 19:04:46 / cg" |
|
289 |
! |
|
290 |
||
10463 | 291 |
cmd_setOrClear:lineStream to:aBoolean |
292 |
|what| |
|
293 |
||
294 |
lineStream skipSeparators. |
|
295 |
what := lineStream nextAlphaNumericWord. |
|
16133 | 296 |
what notNil ifTrue:[ |
20771 | 297 |
(what startsWith:'tra') ifTrue:[ |
298 |
traceFlag := aBoolean. |
|
299 |
^ self. |
|
300 |
]. |
|
301 |
(what startsWith:'tim') ifTrue:[ |
|
302 |
timingFlag := aBoolean. |
|
303 |
^ self. |
|
304 |
]. |
|
305 |
(what startsWith:'pro') ifTrue:[ |
|
306 |
profilingFlag := aBoolean. |
|
307 |
^ self. |
|
308 |
]. |
|
309 |
(what startsWith:'chunk') ifTrue:[ |
|
310 |
doChunkFormat := aBoolean. |
|
311 |
^ self. |
|
312 |
]. |
|
10463 | 313 |
]. |
16133 | 314 |
self errorStream nextPutLine:'? which flag ?'. |
10255 | 315 |
|
10256 | 316 |
"Modified: / 07-12-2006 / 19:13:34 / cg" |
10255 | 317 |
! |
318 |
||
10463 | 319 |
cmd_show:lineStream |
20834 | 320 |
|errStream what showAll printModule ok| |
10463 | 321 |
|
16087 | 322 |
errStream := self errorStream. |
323 |
||
10463 | 324 |
lineStream skipSeparators. |
325 |
what := lineStream nextAlphaNumericWord. |
|
20834 | 326 |
ok := false. |
327 |
||
16133 | 328 |
what notNil ifTrue:[ |
20834 | 329 |
showAll := (what startsWith:'all'). |
330 |
||
20835 | 331 |
(showAll or:[ what startsWith:'var' ]) ifTrue:[ |
332 |
showAll ifTrue:[ errStream nextPutLine:'Variables:'; nextPutLine:'----------' ]. |
|
20768 | 333 |
Workspace notNil ifTrue:[ |
20770 | 334 |
Workspace workspaceVariables keys asOrderedCollection sort do:[:nm | |
335 |
|holder| |
|
336 |
holder := Workspace workspaceVariables at:nm. |
|
337 |
errStream nextPutAll:nm; nextPutAll:' -> '; nextPutLine:holder value printString. |
|
20768 | 338 |
]. |
339 |
]. |
|
20845 | 340 |
ok := true. |
20768 | 341 |
]. |
20845 | 342 |
|
20835 | 343 |
(showAll or:[ what startsWith:'proc' ]) ifTrue:[ |
20836 | 344 |
showAll ifTrue:[ errStream cr; nextPutLine:'Threads:'; nextPutLine:'--------' ]. |
20768 | 345 |
MiniDebugger basicNew showProcesses. |
20845 | 346 |
ok := true. |
20768 | 347 |
]. |
20845 | 348 |
|
20834 | 349 |
(showAll or:[ what startsWith:'mod' ]) ifTrue:[ |
20836 | 350 |
showAll ifTrue:[ errStream cr; nextPutLine:'Modules:'; nextPutLine:'--------' ]. |
20768 | 351 |
printModule := |
352 |
[:mod | |
|
353 |
errStream |
|
354 |
nextPutAll:' '; |
|
355 |
nextPutAll:(mod package "libraryName"); |
|
356 |
nextPutLine:' (',(mod type),')'. |
|
357 |
]. |
|
13865 | 358 |
|
20768 | 359 |
errStream nextPutLine:'builtIn:'. |
360 |
((ObjectMemory binaryModuleInfo |
|
361 |
reject:[:m | m dynamic]) |
|
362 |
asSortedCollection:[:a :b | a name < b name]) do:printModule. |
|
13865 | 363 |
|
20768 | 364 |
errStream nextPutLine:'dynamic:'. |
365 |
((ObjectMemory binaryModuleInfo |
|
366 |
select:[:m | m dynamic]) |
|
367 |
asSortedCollection:[:a :b | a name < b name]) do:printModule. |
|
13865 | 368 |
|
20845 | 369 |
ok := true. |
20768 | 370 |
]. |
20845 | 371 |
|
20834 | 372 |
(showAll or:[ what startsWith:'mem' ]) ifTrue:[ |
373 |
|allMem| |
|
374 |
||
20836 | 375 |
showAll ifTrue:[ errStream cr; nextPutLine:'Memory:'; nextPutLine:'-------' ]. |
20891 | 376 |
"/ allMem := ObjectMemory oldSpaceUsed + ObjectMemory symSpaceUsed |
377 |
"/ + ObjectMemory newSpaceUsed. |
|
20768 | 378 |
errStream |
20891 | 379 |
"/ nextPutLine:('overall: ',(allMem // 1024) printString,' Kb'); |
380 |
nextPutLine:('overall: ',(ObjectMemory bytesUsed // 1024) printString,' Kb'); |
|
20768 | 381 |
nextPutLine:('free : ',(ObjectMemory freeSpace // 1024) printString,' Kb'); |
382 |
nextPutLine:('minorGC: ',(ObjectMemory scavengeCount) printString); |
|
383 |
nextPutLine:('majorGC: ',(ObjectMemory garbageCollectCount) printString). |
|
20845 | 384 |
ok := true. |
20768 | 385 |
]. |
20845 | 386 |
|
20834 | 387 |
(showAll or:[ what startsWith:'flag' ]) ifTrue:[ |
20836 | 388 |
showAll ifTrue:[ errStream cr; nextPutLine:'Flags:'; nextPutLine:'------' ]. |
20768 | 389 |
errStream |
20892 | 390 |
nextPutLine:('trace : ',(traceFlag ? false) printString); |
391 |
nextPutLine:('timing: ',(timingFlag ? false) printString); |
|
392 |
nextPutLine:('profiling: ',(profilingFlag ? false) printString); |
|
393 |
nextPutLine:('chunkFormat: ',(doChunkFormat ? false) printString). |
|
20845 | 394 |
ok := true. |
20768 | 395 |
]. |
10463 | 396 |
]. |
397 |
||
20834 | 398 |
ok ifFalse:[ |
399 |
errStream nextPutLine:'? show what ?'. |
|
400 |
]. |
|
401 |
||
402 |
" |
|
403 |
self basicNew cmd_show:'packages' readStream |
|
404 |
" |
|
10255 | 405 |
|
13866 | 406 |
"Modified: / 07-12-2011 / 22:15:07 / cg" |
10255 | 407 |
! |
408 |
||
10463 | 409 |
cmd_use:lineStream |
16145 | 410 |
|pkg| |
10463 | 411 |
|
412 |
lineStream skipSeparators. |
|
16133 | 413 |
pkg := lineStream upToEnd. |
414 |
pkg isNil ifTrue:[ |
|
16565 | 415 |
'? which package?' errorPrintCR. |
416 |
^ self. |
|
16133 | 417 |
]. |
418 |
pkg := pkg withoutSeparators. |
|
419 |
pkg isEmpty ifTrue:[ |
|
16565 | 420 |
'? which package?' errorPrintCR. |
421 |
^ self. |
|
16133 | 422 |
]. |
423 |
||
16145 | 424 |
[ |
16565 | 425 |
Smalltalk loadPackage:pkg. |
16145 | 426 |
] on:PackageLoadError do:[:ex| |
16565 | 427 |
"/ allow for some shortcuts... |
428 |
(pkg includes:$:) ifTrue:[ |
|
429 |
self errorStream nextPutLine:('Failed to load package: "',pkg,'"'). |
|
430 |
] ifFalse:[ |
|
431 |
"/ try stx standard package |
|
432 |
pkg := 'stx:', pkg. |
|
433 |
ex restart. |
|
434 |
]. |
|
13852 | 435 |
]. |
10255 | 436 |
|
437 |
"Created: / 07-12-2006 / 19:07:56 / cg" |
|
10463 | 438 |
! |
439 |
||
440 |
directive:line |
|
441 |
|s cmd| |
|
442 |
||
443 |
s := line readStream. |
|
444 |
s next. "/ skip the hash |
|
445 |
s skipSeparators. |
|
446 |
||
447 |
cmd := s nextAlphaNumericWord. |
|
16087 | 448 |
cmd notNil ifTrue:[ |
16565 | 449 |
self |
450 |
perform:('cmd_',cmd) asMutator with:s |
|
451 |
ifNotUnderstood:[ |
|
452 |
self errorStream |
|
453 |
nextPutAll:'?? invalid command: '; |
|
454 |
nextPutAll:cmd; |
|
455 |
nextPutAll:'. Type "#help" for help.'; |
|
456 |
cr. |
|
457 |
]. |
|
16087 | 458 |
]. |
10463 | 459 |
|
460 |
"Created: / 07-12-2006 / 18:49:17 / cg" |
|
10255 | 461 |
! ! |
462 |
||
10248 | 463 |
!ReadEvalPrintLoop methodsFor:'evaluation'! |
464 |
||
16565 | 465 |
basicReadEvalPrintLoopWithInput:input output:output error:error |
16087 | 466 |
compiler:compilerClass prompt:prompt print:doPrint |
467 |
||
468 |
"{ Pragma: +optSpace }" |
|
469 |
||
470 |
"the core of the interpreter loop; extracted and parametrized, so it can be called recursive |
|
471 |
for included scripts. |
|
472 |
If chunkFormat is true, chunks are read. |
|
473 |
Otherwise, lines up to an empty line (or EOF) or a line ending in '.' are read. |
|
20774 | 474 |
A '#' character appearing in the first column of the first line turns off chunkmode, |
475 |
which allows for convenient shell scripts containing a #/bin/stx as the first line." |
|
16087 | 476 |
|
477 |
[ |
|
20769 | 478 |
|lines chunk| |
16087 | 479 |
|
20769 | 480 |
prompt notNil ifTrue:[ |
481 |
error nextPutAll:prompt. |
|
482 |
]. |
|
16087 | 483 |
|
20769 | 484 |
input atEnd ifTrue:[ |
485 |
error cr. |
|
486 |
^ self. |
|
487 |
]. |
|
16087 | 488 |
|
20769 | 489 |
input peek == $# ifTrue:[ |
490 |
self doChunkFormat:false. |
|
491 |
]. |
|
16087 | 492 |
|
20769 | 493 |
self doChunkFormat ifTrue:[ |
494 |
input skipSeparators. |
|
495 |
chunk := input nextChunk. |
|
496 |
] ifFalse:[ |
|
497 |
lines := OrderedCollection new. |
|
498 |
[ |
|
499 |
|line| |
|
16087 | 500 |
|
20769 | 501 |
line := input nextLine. |
502 |
line notEmptyOrNil ifTrue:[ |
|
503 |
line = '?' ifTrue:[ |
|
504 |
self cmd_help:nil. |
|
505 |
prompt notNil ifTrue:[ |
|
506 |
error nextPutAll:prompt. |
|
507 |
]. |
|
508 |
] ifFalse:[ |
|
509 |
(line startsWith:'#') ifTrue:[ |
|
510 |
self directive:line. |
|
511 |
prompt notNil ifTrue:[ |
|
512 |
error nextPutAll:prompt. |
|
513 |
]. |
|
514 |
] ifFalse:[ |
|
515 |
lines add:line. |
|
516 |
] |
|
517 |
] |
|
518 |
]. |
|
519 |
line notEmptyOrNil and:[(line endsWith:$.) not]. |
|
520 |
] whileTrue. |
|
521 |
chunk := lines asStringWith:Character cr. |
|
522 |
]. |
|
16087 | 523 |
|
20890 | 524 |
(chunk notEmptyOrNil |
525 |
and:[chunk withoutSeparators notEmpty |
|
526 |
and:[chunk withoutSeparators ~= '.']] |
|
527 |
) ifTrue:[ |
|
20769 | 528 |
"abortAll is handled, but not asked for here!!" |
529 |
AbortAllOperationRequest handle:[:ex | |
|
530 |
error nextPutLine:('Evaluation aborted: ', ex description) |
|
531 |
] do:[ |
|
532 |
(Error, ControlInterrupt) handle:[:ex | |
|
533 |
prompt isNil ifTrue:[ |
|
534 |
ex reject |
|
535 |
]. |
|
536 |
MiniDebugger enterWithMessage:(ex errorString) mayProceed:true. |
|
537 |
ex mayProceed ifTrue:[ |
|
538 |
ex proceed. |
|
539 |
]. |
|
540 |
error nextPutLine:('Evaluation aborted: ', ex description). |
|
541 |
ex return. |
|
542 |
] do:[ |
|
543 |
|value ms us| |
|
16087 | 544 |
|
20771 | 545 |
profilingFlag == true ifTrue:[ |
546 |
MessageTally spyDetailedOn:[ |
|
547 |
value := (compilerClass new requestor:self) |
|
548 |
evaluate:chunk |
|
549 |
compile:true. |
|
550 |
]. |
|
551 |
doPrint ifTrue:[ |
|
552 |
value printOn:output. output cr. |
|
553 |
]. |
|
554 |
] ifFalse:[ |
|
555 |
us := Time microsecondsToRun:[ |
|
556 |
value := (compilerClass new requestor:self) |
|
557 |
evaluate:chunk compile:true. |
|
558 |
]. |
|
559 |
doPrint ifTrue:[ |
|
560 |
value printOn:output. output cr. |
|
561 |
]. |
|
16087 | 562 |
|
20771 | 563 |
timingFlag == true ifTrue:[ |
564 |
'execution time: ' printOn:error. |
|
565 |
us < 1000 ifTrue:[ |
|
566 |
us < 1 ifTrue:[ |
|
567 |
'too small to measure (<1us)' printOn:error. |
|
568 |
] ifFalse:[ |
|
569 |
us printOn:output. 'us' printOn:error. |
|
570 |
] |
|
20769 | 571 |
] ifFalse:[ |
20771 | 572 |
((us / 1000) asFixedPoint:2) printOn:output. 'ms' printOn:error. |
573 |
]. |
|
574 |
error cr. |
|
20769 | 575 |
]. |
576 |
]. |
|
577 |
Workspace notNil ifTrue:[ |
|
578 |
Workspace rememberResultAsWorkspaceVariable:value. |
|
579 |
]. |
|
580 |
]. |
|
581 |
]. |
|
582 |
]. |
|
16087 | 583 |
] loop. |
584 |
||
585 |
" |
|
586 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
|
587 |
" |
|
588 |
||
589 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
590 |
"Modified: / 06-12-2011 / 15:29:03 / cg" |
|
591 |
! |
|
592 |
||
10463 | 593 |
readEvalPrintLoop |
594 |
"{ Pragma: +optSpace }" |
|
595 |
||
596 |
"simple read-eval-print loop for non-graphical Minitalk. |
|
597 |
If the chunkFormat-argument is true, chunks are read. |
|
16087 | 598 |
Otherwise, lines up to an empty line (or EOF) are read. |
599 |
A '#' character appearing in the first column of the first line |
|
600 |
switches to chunkmode." |
|
10463 | 601 |
|
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
602 |
exitAction := [^ self]. |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
603 |
|
10463 | 604 |
ControlInterrupt handle:[:ex | |
20774 | 605 |
self errorStream nextPutLine:('Caught: ', ex description). |
20833 | 606 |
self inputStream atEnd ifTrue:[ |
607 |
ex return. |
|
608 |
]. |
|
20774 | 609 |
ex restart. |
10463 | 610 |
] do:[ |
20774 | 611 |
|input output error compilerClass| |
612 |
||
613 |
"/ re-evaluate these in the loop, so they can be changed dynamically |
|
614 |
input := self inputStream. |
|
615 |
output := self outputStream. |
|
616 |
error := self errorStream. |
|
10463 | 617 |
|
20774 | 618 |
compilerClass := compiler ? Compiler ? Parser. |
619 |
compilerClass isNil ifTrue:[ |
|
620 |
self errorStream nextPutLine:('oops - no Compiler class found'). |
|
621 |
^ self. |
|
622 |
]. |
|
623 |
StreamError handle:[:ex | |
|
624 |
(input isOpen not or:[input atEnd]) ifTrue:[ |
|
625 |
error nextPutLine:'EOF on input'. |
|
20833 | 626 |
ex return. |
20774 | 627 |
]. |
628 |
(output isOpen not) ifTrue:[ |
|
629 |
error nextPutLine:'no output'. |
|
630 |
]. |
|
631 |
(error isOpen not) ifTrue:[ |
|
632 |
]. |
|
633 |
] do:[ |
|
634 |
input signalAtEnd:true. |
|
635 |
self |
|
636 |
basicReadEvalPrintLoopWithInput:input output:output error:error |
|
637 |
compiler:compilerClass prompt:prompt print:(printFlag ? true). |
|
638 |
] |
|
10463 | 639 |
] |
640 |
||
641 |
" |
|
642 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
|
643 |
" |
|
644 |
||
645 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
13859
81451e09af8d
changed: #readEvalPrintLoop
Claus Gittinger <cg@exept.de>
parents:
13858
diff
changeset
|
646 |
"Modified: / 06-12-2011 / 15:29:03 / cg" |
10248 | 647 |
! ! |
648 |
||
16089 | 649 |
!ReadEvalPrintLoop methodsFor:'queries'! |
650 |
||
651 |
autoDefineVariables |
|
652 |
"when evaluating with --eval, auto define any variables" |
|
653 |
||
654 |
^ #workspace |
|
655 |
! ! |
|
656 |
||
10248 | 657 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
658 |
||
659 |
version |
|
19379 | 660 |
^ '$Header$' |
13121
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
661 |
! |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
662 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
663 |
version_CVS |
19379 | 664 |
^ '$Header$' |
10248 | 665 |
! ! |
19379 | 666 |