author | Claus Gittinger <cg@exept.de> |
Tue, 08 Nov 2016 17:26:31 +0100 | |
changeset 20902 | 342c68aa8e9a |
parent 20901 | 56be0b5cd5d7 |
child 20905 | a0c73010fb0f |
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 |
20900 | 19 |
exitAction currentDirectory lastEditedClass' |
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 |
||
20900 | 195 |
cmd_debug:lineStream |
196 |
MiniDebugger enter. |
|
197 |
! |
|
198 |
||
199 |
cmd_edit:lineStream |
|
200 |
"edit a class or selector" |
|
201 |
||
202 |
|errStream editor classOrMethodName cls methodName selector |
|
203 |
code isNewClass tmpFile modifiedTime| |
|
204 |
||
205 |
errStream := self errorStream. |
|
206 |
||
207 |
editor := OperatingSystem getEnvironment:'STX_EDITOR'. |
|
208 |
editor isNil ifTrue:[ |
|
209 |
editor := OperatingSystem getEnvironment:'EDITOR'. |
|
210 |
editor isNil ifTrue:[ |
|
211 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
|
212 |
editor := 'notepad'. |
|
213 |
] ifFalse:[ |
|
214 |
editor := 'vi'. |
|
215 |
]. |
|
216 |
]. |
|
217 |
]. |
|
218 |
||
219 |
isNewClass := false. |
|
220 |
||
221 |
lineStream skipSeparators. |
|
222 |
lineStream atEnd ifTrue:[^ self]. |
|
223 |
||
224 |
classOrMethodName := lineStream |
|
225 |
upToElementForWhich:[:ch | |
|
226 |
ch isLetterOrDigit not and:[ch ~~ $_] |
|
227 |
]. |
|
228 |
"/ |
|
229 |
(classOrMethodName isUppercaseFirst) ifTrue:[ |
|
230 |
(cls := Smalltalk classNamed:classOrMethodName) isNil ifTrue:[ |
|
231 |
errStream nextPutAll:'edit: no such class: ',classOrMethodName,' ; create (y/n)? '. |
|
232 |
(self inputStream nextLine withoutSeparators startsWith:'y') ifFalse:[^ self]. |
|
233 |
isNewClass := true. |
|
234 |
code := |
|
235 |
'Object |
|
236 |
subclass:#%1 |
|
237 |
instanceVariableNames:'''' |
|
238 |
classVariableNames:'''' |
|
239 |
poolDictionaries:'''' |
|
240 |
category:''user classes'' |
|
241 |
' bindWith:classOrMethodName. |
|
242 |
] ifFalse:[ |
|
243 |
lineStream skipSeparators. |
|
244 |
lineStream atEnd ifFalse:[ |
|
245 |
methodName := lineStream upToSeparator. |
|
246 |
]. |
|
247 |
]. |
|
248 |
] ifFalse:[ |
|
249 |
methodName := classOrMethodName |
|
250 |
]. |
|
251 |
||
252 |
isNewClass ifFalse:[ |
|
253 |
cls := cls ? lastEditedClass. |
|
254 |
cls isNil ifTrue:[ |
|
255 |
errStream nextPutLine:'edit usage:'. |
|
256 |
errStream nextPutLine:' #edit className selector'. |
|
257 |
errStream nextPutLine:' #edit className '. |
|
258 |
errStream nextPutLine:' #edit selector (class as in previous edit)'. |
|
259 |
^ self. |
|
260 |
]. |
|
261 |
lastEditedClass := cls. |
|
262 |
((selector := methodName asSymbolIfInterned) isNil |
|
263 |
or:[ (cls implements:selector) not]) ifTrue:[ |
|
264 |
errStream nextPutAll:('"',methodName,'" is a new method; create (y/n)? '). |
|
265 |
(self inputStream nextLine withoutSeparators startsWith:'y') ifFalse:[^ self]. |
|
266 |
code := ' |
|
267 |
%1 |
|
268 |
"this is a new method" |
|
269 |
self halt |
|
270 |
' bindWith:methodName. |
|
271 |
] ifFalse:[ |
|
272 |
code := cls compiledMethodAt:selector. |
|
273 |
]. |
|
274 |
]. |
|
275 |
||
276 |
tmpFile := Filename newTemporary. |
|
277 |
tmpFile contents:code. |
|
278 |
modifiedTime := tmpFile modificationTime. |
|
20901 | 279 |
OperatingSystem executeCommand:('%1 "%2"' bindWith:editor with:tmpFile pathName). |
20900 | 280 |
tmpFile modificationTime ~= modifiedTime ifTrue:[ |
281 |
isNewClass ifTrue:[ |
|
282 |
Compiler evaluate:tmpFile contentsOfEntireFile. |
|
283 |
] ifFalse:[ |
|
284 |
cls compile:tmpFile contentsOfEntireFile classified:'*as yet uncategorized'. |
|
285 |
]. |
|
286 |
]. |
|
287 |
||
288 |
" |
|
289 |
self new |
|
290 |
input:Stdin; |
|
291 |
cmd_edit:'MyClass foo' readStream |
|
292 |
" |
|
293 |
! |
|
294 |
||
10463 | 295 |
cmd_exit:lineStream |
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
296 |
exitAction value |
10463 | 297 |
|
298 |
"Created: / 07-12-2006 / 18:55:46 / cg" |
|
10255 | 299 |
! |
300 |
||
10463 | 301 |
cmd_help:lineStream |
302 |
self errorStream |
|
20771 | 303 |
nextPutAll: |
16084 | 304 |
'Everything entered up to an empty line or a line ending in "." is called a "chunk" and evaluated. |
10463 | 305 |
Lines starting with "#" are commands to the read-eval-print interpreter. |
306 |
||
307 |
Valid commands are: |
|
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
308 |
#help ............... this text |
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
309 |
#exit ............... exit interpreter loop |
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
310 |
#use <package>....... use (load) a package |
20771 | 311 |
stx:libwidg .............. GUI package |
312 |
stx:libtool .............. IDE tool package |
|
313 |
stx:goodies/regex ........ regex package |
|
314 |
stx:goodies/petitparser .. peg parser package |
|
16087 | 315 |
#read <filename>..... read another script or source file |
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
316 |
#show <what> ........ show info |
20771 | 317 |
variables .......... interpreter variables |
318 |
processes .......... processes |
|
319 |
memory ............. memory usage |
|
320 |
flags .............. flags |
|
321 |
modules ............ loaded modules |
|
20900 | 322 |
packages ........... available packages to load |
20834 | 323 |
all ................ all of the above |
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
324 |
#set/clear <flag> ... set or clear a flag |
20771 | 325 |
trace .............. tracing execution |
326 |
timing ............. timing execution |
|
327 |
profiling .......... show execution profile |
|
328 |
chunkFormat ........ traditional bang chunk format input mode |
|
20900 | 329 |
#debug ................. enter a MiniDebugger |
10257 | 330 |
|
16087 | 331 |
The MiniDebugger (if entered) shows its own help with "?". |
10255 | 332 |
' |
333 |
||
334 |
"Created: / 07-12-2006 / 18:54:20 / cg" |
|
335 |
! |
|
336 |
||
16087 | 337 |
cmd_read:lineStream |
338 |
|filename newInput savedPrompt savedPrint savedInput savedCurrentDirectory| |
|
339 |
||
340 |
lineStream skipSeparators. |
|
341 |
filename := lineStream upToEnd withoutSeparators. |
|
16133 | 342 |
filename isNil ifTrue:[ |
16565 | 343 |
'? which file?' errorPrintCR. |
344 |
^ self. |
|
16133 | 345 |
]. |
346 |
filename := filename withoutSeparators. |
|
347 |
filename isEmpty ifTrue:[ |
|
16565 | 348 |
'? which file?' errorPrintCR. |
349 |
^ self. |
|
16133 | 350 |
]. |
16087 | 351 |
|
352 |
filename := filename asFilename. |
|
353 |
filename isAbsolute ifFalse:[ |
|
16565 | 354 |
filename := currentDirectory construct:filename. |
16087 | 355 |
]. |
356 |
||
357 |
newInput := filename readStream. |
|
358 |
newInput isNil ifTrue:[ |
|
16565 | 359 |
('Could not find file: "',filename,'"') errorPrintCR. |
360 |
^ self. |
|
16087 | 361 |
]. |
362 |
||
363 |
[ |
|
16565 | 364 |
savedCurrentDirectory := currentDirectory. |
365 |
savedInput := inputStream. |
|
366 |
savedPrint := printFlag. |
|
367 |
savedPrompt := prompt. |
|
16087 | 368 |
|
16565 | 369 |
currentDirectory := filename directory. |
370 |
inputStream := newInput. |
|
16087 | 371 |
|
16565 | 372 |
self |
373 |
basicReadEvalPrintLoopWithInput:newInput |
|
374 |
output:outputStream |
|
375 |
error:errorStream |
|
376 |
compiler:compiler |
|
377 |
prompt:false |
|
378 |
print:false. |
|
16087 | 379 |
] ensure:[ |
16565 | 380 |
currentDirectory := savedCurrentDirectory. |
381 |
inputStream := savedInput. |
|
382 |
printFlag := savedPrint. |
|
383 |
prompt := savedPrompt |
|
16087 | 384 |
]. |
385 |
! |
|
386 |
||
10463 | 387 |
cmd_set:lineStream |
10255 | 388 |
self cmd_setOrClear:lineStream to:true |
389 |
||
390 |
"Modified: / 07-12-2006 / 19:04:46 / cg" |
|
391 |
! |
|
392 |
||
10463 | 393 |
cmd_setOrClear:lineStream to:aBoolean |
394 |
|what| |
|
395 |
||
396 |
lineStream skipSeparators. |
|
397 |
what := lineStream nextAlphaNumericWord. |
|
16133 | 398 |
what notNil ifTrue:[ |
20771 | 399 |
(what startsWith:'tra') ifTrue:[ |
400 |
traceFlag := aBoolean. |
|
401 |
^ self. |
|
402 |
]. |
|
403 |
(what startsWith:'tim') ifTrue:[ |
|
404 |
timingFlag := aBoolean. |
|
405 |
^ self. |
|
406 |
]. |
|
407 |
(what startsWith:'pro') ifTrue:[ |
|
408 |
profilingFlag := aBoolean. |
|
409 |
^ self. |
|
410 |
]. |
|
411 |
(what startsWith:'chunk') ifTrue:[ |
|
412 |
doChunkFormat := aBoolean. |
|
413 |
^ self. |
|
414 |
]. |
|
10463 | 415 |
]. |
16133 | 416 |
self errorStream nextPutLine:'? which flag ?'. |
10255 | 417 |
|
10256 | 418 |
"Modified: / 07-12-2006 / 19:13:34 / cg" |
10255 | 419 |
! |
420 |
||
10463 | 421 |
cmd_show:lineStream |
20901 | 422 |
|errStream what showAll ok| |
10463 | 423 |
|
16087 | 424 |
errStream := self errorStream. |
425 |
||
10463 | 426 |
lineStream skipSeparators. |
427 |
what := lineStream nextAlphaNumericWord. |
|
20834 | 428 |
ok := false. |
429 |
||
16133 | 430 |
what notNil ifTrue:[ |
20834 | 431 |
showAll := (what startsWith:'all'). |
432 |
||
20835 | 433 |
(showAll or:[ what startsWith:'var' ]) ifTrue:[ |
434 |
showAll ifTrue:[ errStream nextPutLine:'Variables:'; nextPutLine:'----------' ]. |
|
20901 | 435 |
self showVariables. |
20845 | 436 |
ok := true. |
20768 | 437 |
]. |
20845 | 438 |
|
20835 | 439 |
(showAll or:[ what startsWith:'proc' ]) ifTrue:[ |
20836 | 440 |
showAll ifTrue:[ errStream cr; nextPutLine:'Threads:'; nextPutLine:'--------' ]. |
20768 | 441 |
MiniDebugger basicNew showProcesses. |
20845 | 442 |
ok := true. |
20768 | 443 |
]. |
20845 | 444 |
|
20902 | 445 |
("showAll or:[" what startsWith:'pack' "]") ifTrue:[ |
20900 | 446 |
showAll ifTrue:[ errStream cr; nextPutLine:'Packages:'; nextPutLine:'--------' ]. |
447 |
self showPackages. |
|
448 |
ok := true. |
|
449 |
]. |
|
450 |
||
20834 | 451 |
(showAll or:[ what startsWith:'mod' ]) ifTrue:[ |
20836 | 452 |
showAll ifTrue:[ errStream cr; nextPutLine:'Modules:'; nextPutLine:'--------' ]. |
20901 | 453 |
self showModules. |
13865 | 454 |
|
20845 | 455 |
ok := true. |
20768 | 456 |
]. |
20845 | 457 |
|
20834 | 458 |
(showAll or:[ what startsWith:'mem' ]) ifTrue:[ |
459 |
|allMem| |
|
460 |
||
20836 | 461 |
showAll ifTrue:[ errStream cr; nextPutLine:'Memory:'; nextPutLine:'-------' ]. |
20891 | 462 |
"/ allMem := ObjectMemory oldSpaceUsed + ObjectMemory symSpaceUsed |
463 |
"/ + ObjectMemory newSpaceUsed. |
|
20768 | 464 |
errStream |
20891 | 465 |
"/ nextPutLine:('overall: ',(allMem // 1024) printString,' Kb'); |
20900 | 466 |
nextPutLine:('used : ',(ObjectMemory bytesUsed // 1024) printString,' Kb'); |
20768 | 467 |
nextPutLine:('free : ',(ObjectMemory freeSpace // 1024) printString,' Kb'); |
20900 | 468 |
nextPutAll:('minorGC: ',(ObjectMemory scavengeCount) printString); |
469 |
nextPutLine:(' majorGC: ',(ObjectMemory garbageCollectCount) printString). |
|
20845 | 470 |
ok := true. |
20768 | 471 |
]. |
20845 | 472 |
|
20834 | 473 |
(showAll or:[ what startsWith:'flag' ]) ifTrue:[ |
20836 | 474 |
showAll ifTrue:[ errStream cr; nextPutLine:'Flags:'; nextPutLine:'------' ]. |
20768 | 475 |
errStream |
20892 | 476 |
nextPutLine:('trace : ',(traceFlag ? false) printString); |
477 |
nextPutLine:('timing: ',(timingFlag ? false) printString); |
|
478 |
nextPutLine:('profiling: ',(profilingFlag ? false) printString); |
|
479 |
nextPutLine:('chunkFormat: ',(doChunkFormat ? false) printString). |
|
20845 | 480 |
ok := true. |
20768 | 481 |
]. |
10463 | 482 |
]. |
483 |
||
20834 | 484 |
ok ifFalse:[ |
485 |
errStream nextPutLine:'? show what ?'. |
|
486 |
]. |
|
487 |
||
488 |
" |
|
489 |
self basicNew cmd_show:'packages' readStream |
|
490 |
" |
|
10255 | 491 |
|
13866 | 492 |
"Modified: / 07-12-2011 / 22:15:07 / cg" |
10255 | 493 |
! |
494 |
||
10463 | 495 |
cmd_use:lineStream |
16145 | 496 |
|pkg| |
10463 | 497 |
|
498 |
lineStream skipSeparators. |
|
16133 | 499 |
pkg := lineStream upToEnd. |
500 |
pkg isNil ifTrue:[ |
|
16565 | 501 |
'? which package?' errorPrintCR. |
502 |
^ self. |
|
16133 | 503 |
]. |
504 |
pkg := pkg withoutSeparators. |
|
505 |
pkg isEmpty ifTrue:[ |
|
16565 | 506 |
'? which package?' errorPrintCR. |
507 |
^ self. |
|
16133 | 508 |
]. |
509 |
||
16145 | 510 |
[ |
16565 | 511 |
Smalltalk loadPackage:pkg. |
16145 | 512 |
] on:PackageLoadError do:[:ex| |
16565 | 513 |
"/ allow for some shortcuts... |
514 |
(pkg includes:$:) ifTrue:[ |
|
515 |
self errorStream nextPutLine:('Failed to load package: "',pkg,'"'). |
|
516 |
] ifFalse:[ |
|
517 |
"/ try stx standard package |
|
518 |
pkg := 'stx:', pkg. |
|
519 |
ex restart. |
|
520 |
]. |
|
13852 | 521 |
]. |
10255 | 522 |
|
523 |
"Created: / 07-12-2006 / 19:07:56 / cg" |
|
10463 | 524 |
! |
525 |
||
526 |
directive:line |
|
527 |
|s cmd| |
|
528 |
||
529 |
s := line readStream. |
|
530 |
s next. "/ skip the hash |
|
531 |
s skipSeparators. |
|
532 |
||
533 |
cmd := s nextAlphaNumericWord. |
|
16087 | 534 |
cmd notNil ifTrue:[ |
16565 | 535 |
self |
536 |
perform:('cmd_',cmd) asMutator with:s |
|
537 |
ifNotUnderstood:[ |
|
538 |
self errorStream |
|
539 |
nextPutAll:'?? invalid command: '; |
|
540 |
nextPutAll:cmd; |
|
541 |
nextPutAll:'. Type "#help" for help.'; |
|
542 |
cr. |
|
543 |
]. |
|
16087 | 544 |
]. |
10463 | 545 |
|
546 |
"Created: / 07-12-2006 / 18:49:17 / cg" |
|
20900 | 547 |
! |
548 |
||
20901 | 549 |
showModules |
550 |
|errStream printModule| |
|
551 |
||
552 |
errStream := self errorStream. |
|
553 |
||
554 |
printModule := |
|
555 |
[:mod | |
|
556 |
errStream |
|
557 |
nextPutAll:' '; |
|
558 |
nextPutAll:(mod package "libraryName"); |
|
559 |
nextPutLine:' (',(mod type),')'. |
|
560 |
]. |
|
561 |
||
562 |
errStream nextPutLine:'builtIn:'. |
|
563 |
((ObjectMemory binaryModuleInfo |
|
564 |
reject:[:m | m dynamic]) |
|
565 |
asSortedCollection:[:a :b | a name < b name]) do:printModule. |
|
566 |
||
567 |
errStream nextPutLine:'dynamic:'. |
|
568 |
((ObjectMemory binaryModuleInfo |
|
569 |
select:[:m | m dynamic]) |
|
570 |
asSortedCollection:[:a :b | a name < b name]) do:printModule. |
|
571 |
||
572 |
" |
|
573 |
ReadEvalPrintLoop basicNew showModules |
|
574 |
" |
|
575 |
! |
|
576 |
||
20900 | 577 |
showPackages |
578 |
|all| |
|
579 |
||
580 |
all := Set new. |
|
581 |
Smalltalk knownLoadablePackagesDo:[:packageID :type :path | |
|
582 |
all add:packageID |
|
583 |
]. |
|
584 |
all := all asOrderedCollection sort. |
|
585 |
all do:[:eachPackage | |
|
20901 | 586 |
self errorStream nextPutAll:eachPackage. |
587 |
(Smalltalk isPackageLoaded:eachPackage) ifTrue:[ |
|
588 |
self errorStream nextPutAll:' (loaded)'. |
|
589 |
]. |
|
590 |
self errorStream cr. |
|
20900 | 591 |
]. |
592 |
||
593 |
" |
|
594 |
ReadEvalPrintLoop basicNew showPackages |
|
20901 | 595 |
ReadEvalPrintLoop basicNew showModules |
596 |
" |
|
597 |
! |
|
598 |
||
599 |
showVariables |
|
600 |
Workspace notNil ifTrue:[ |
|
601 |
Workspace workspaceVariables keys asOrderedCollection sort do:[:nm | |
|
602 |
|holder| |
|
603 |
holder := Workspace workspaceVariables at:nm. |
|
604 |
self errorStream |
|
605 |
nextPutAll:nm; |
|
606 |
nextPutAll:' -> '; |
|
607 |
nextPutLine:holder value printString. |
|
608 |
]. |
|
609 |
]. |
|
610 |
||
611 |
" |
|
612 |
ReadEvalPrintLoop basicNew showVariables |
|
20900 | 613 |
" |
10255 | 614 |
! ! |
615 |
||
10248 | 616 |
!ReadEvalPrintLoop methodsFor:'evaluation'! |
617 |
||
16565 | 618 |
basicReadEvalPrintLoopWithInput:input output:output error:error |
16087 | 619 |
compiler:compilerClass prompt:prompt print:doPrint |
620 |
||
621 |
"{ Pragma: +optSpace }" |
|
622 |
||
623 |
"the core of the interpreter loop; extracted and parametrized, so it can be called recursive |
|
624 |
for included scripts. |
|
625 |
If chunkFormat is true, chunks are read. |
|
626 |
Otherwise, lines up to an empty line (or EOF) or a line ending in '.' are read. |
|
20774 | 627 |
A '#' character appearing in the first column of the first line turns off chunkmode, |
628 |
which allows for convenient shell scripts containing a #/bin/stx as the first line." |
|
16087 | 629 |
|
630 |
[ |
|
20769 | 631 |
|lines chunk| |
16087 | 632 |
|
20769 | 633 |
prompt notNil ifTrue:[ |
634 |
error nextPutAll:prompt. |
|
635 |
]. |
|
16087 | 636 |
|
20769 | 637 |
input atEnd ifTrue:[ |
638 |
error cr. |
|
639 |
^ self. |
|
640 |
]. |
|
16087 | 641 |
|
20769 | 642 |
input peek == $# ifTrue:[ |
643 |
self doChunkFormat:false. |
|
644 |
]. |
|
16087 | 645 |
|
20769 | 646 |
self doChunkFormat ifTrue:[ |
647 |
input skipSeparators. |
|
648 |
chunk := input nextChunk. |
|
649 |
] ifFalse:[ |
|
650 |
lines := OrderedCollection new. |
|
651 |
[ |
|
652 |
|line| |
|
16087 | 653 |
|
20769 | 654 |
line := input nextLine. |
655 |
line notEmptyOrNil ifTrue:[ |
|
656 |
line = '?' ifTrue:[ |
|
657 |
self cmd_help:nil. |
|
658 |
prompt notNil ifTrue:[ |
|
659 |
error nextPutAll:prompt. |
|
660 |
]. |
|
661 |
] ifFalse:[ |
|
662 |
(line startsWith:'#') ifTrue:[ |
|
663 |
self directive:line. |
|
664 |
prompt notNil ifTrue:[ |
|
665 |
error nextPutAll:prompt. |
|
666 |
]. |
|
667 |
] ifFalse:[ |
|
668 |
lines add:line. |
|
669 |
] |
|
670 |
] |
|
671 |
]. |
|
672 |
line notEmptyOrNil and:[(line endsWith:$.) not]. |
|
673 |
] whileTrue. |
|
674 |
chunk := lines asStringWith:Character cr. |
|
675 |
]. |
|
16087 | 676 |
|
20890 | 677 |
(chunk notEmptyOrNil |
678 |
and:[chunk withoutSeparators notEmpty |
|
679 |
and:[chunk withoutSeparators ~= '.']] |
|
680 |
) ifTrue:[ |
|
20769 | 681 |
"abortAll is handled, but not asked for here!!" |
682 |
AbortAllOperationRequest handle:[:ex | |
|
683 |
error nextPutLine:('Evaluation aborted: ', ex description) |
|
684 |
] do:[ |
|
685 |
(Error, ControlInterrupt) handle:[:ex | |
|
686 |
prompt isNil ifTrue:[ |
|
687 |
ex reject |
|
688 |
]. |
|
689 |
MiniDebugger enterWithMessage:(ex errorString) mayProceed:true. |
|
690 |
ex mayProceed ifTrue:[ |
|
691 |
ex proceed. |
|
692 |
]. |
|
693 |
error nextPutLine:('Evaluation aborted: ', ex description). |
|
694 |
ex return. |
|
695 |
] do:[ |
|
696 |
|value ms us| |
|
16087 | 697 |
|
20771 | 698 |
profilingFlag == true ifTrue:[ |
699 |
MessageTally spyDetailedOn:[ |
|
700 |
value := (compilerClass new requestor:self) |
|
701 |
evaluate:chunk |
|
702 |
compile:true. |
|
703 |
]. |
|
704 |
doPrint ifTrue:[ |
|
705 |
value printOn:output. output cr. |
|
706 |
]. |
|
707 |
] ifFalse:[ |
|
708 |
us := Time microsecondsToRun:[ |
|
709 |
value := (compilerClass new requestor:self) |
|
710 |
evaluate:chunk compile:true. |
|
711 |
]. |
|
712 |
doPrint ifTrue:[ |
|
713 |
value printOn:output. output cr. |
|
714 |
]. |
|
16087 | 715 |
|
20771 | 716 |
timingFlag == true ifTrue:[ |
717 |
'execution time: ' printOn:error. |
|
718 |
us < 1000 ifTrue:[ |
|
719 |
us < 1 ifTrue:[ |
|
720 |
'too small to measure (<1us)' printOn:error. |
|
721 |
] ifFalse:[ |
|
722 |
us printOn:output. 'us' printOn:error. |
|
723 |
] |
|
20769 | 724 |
] ifFalse:[ |
20771 | 725 |
((us / 1000) asFixedPoint:2) printOn:output. 'ms' printOn:error. |
726 |
]. |
|
727 |
error cr. |
|
20769 | 728 |
]. |
729 |
]. |
|
730 |
Workspace notNil ifTrue:[ |
|
731 |
Workspace rememberResultAsWorkspaceVariable:value. |
|
732 |
]. |
|
733 |
]. |
|
734 |
]. |
|
735 |
]. |
|
16087 | 736 |
] loop. |
737 |
||
738 |
" |
|
739 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
|
740 |
" |
|
741 |
||
742 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
743 |
"Modified: / 06-12-2011 / 15:29:03 / cg" |
|
744 |
! |
|
745 |
||
10463 | 746 |
readEvalPrintLoop |
747 |
"{ Pragma: +optSpace }" |
|
748 |
||
749 |
"simple read-eval-print loop for non-graphical Minitalk. |
|
750 |
If the chunkFormat-argument is true, chunks are read. |
|
16087 | 751 |
Otherwise, lines up to an empty line (or EOF) are read. |
752 |
A '#' character appearing in the first column of the first line |
|
753 |
switches to chunkmode." |
|
10463 | 754 |
|
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
755 |
exitAction := [^ self]. |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
756 |
|
10463 | 757 |
ControlInterrupt handle:[:ex | |
20774 | 758 |
self errorStream nextPutLine:('Caught: ', ex description). |
20833 | 759 |
self inputStream atEnd ifTrue:[ |
760 |
ex return. |
|
761 |
]. |
|
20774 | 762 |
ex restart. |
10463 | 763 |
] do:[ |
20774 | 764 |
|input output error compilerClass| |
765 |
||
766 |
"/ re-evaluate these in the loop, so they can be changed dynamically |
|
767 |
input := self inputStream. |
|
768 |
output := self outputStream. |
|
769 |
error := self errorStream. |
|
10463 | 770 |
|
20774 | 771 |
compilerClass := compiler ? Compiler ? Parser. |
772 |
compilerClass isNil ifTrue:[ |
|
773 |
self errorStream nextPutLine:('oops - no Compiler class found'). |
|
774 |
^ self. |
|
775 |
]. |
|
776 |
StreamError handle:[:ex | |
|
777 |
(input isOpen not or:[input atEnd]) ifTrue:[ |
|
778 |
error nextPutLine:'EOF on input'. |
|
20833 | 779 |
ex return. |
20774 | 780 |
]. |
781 |
(output isOpen not) ifTrue:[ |
|
782 |
error nextPutLine:'no output'. |
|
783 |
]. |
|
784 |
(error isOpen not) ifTrue:[ |
|
785 |
]. |
|
786 |
] do:[ |
|
787 |
input signalAtEnd:true. |
|
788 |
self |
|
789 |
basicReadEvalPrintLoopWithInput:input output:output error:error |
|
790 |
compiler:compilerClass prompt:prompt print:(printFlag ? true). |
|
791 |
] |
|
10463 | 792 |
] |
793 |
||
794 |
" |
|
795 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
|
796 |
" |
|
797 |
||
798 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
13859
81451e09af8d
changed: #readEvalPrintLoop
Claus Gittinger <cg@exept.de>
parents:
13858
diff
changeset
|
799 |
"Modified: / 06-12-2011 / 15:29:03 / cg" |
10248 | 800 |
! ! |
801 |
||
16089 | 802 |
!ReadEvalPrintLoop methodsFor:'queries'! |
803 |
||
804 |
autoDefineVariables |
|
805 |
"when evaluating with --eval, auto define any variables" |
|
806 |
||
807 |
^ #workspace |
|
808 |
! ! |
|
809 |
||
10248 | 810 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
811 |
||
812 |
version |
|
19379 | 813 |
^ '$Header$' |
13121
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
814 |
! |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
815 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
816 |
version_CVS |
19379 | 817 |
^ '$Header$' |
10248 | 818 |
! ! |
19379 | 819 |