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