author | Claus Gittinger <cg@exept.de> |
Tue, 08 Nov 2016 19:10:04 +0100 | |
changeset 20922 | 26a835f3689a |
parent 20920 | 41aec62f6587 |
child 20923 | ce3f5d5e8b54 |
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 |
|
20915 | 373 |
currentDirectory := currentDirectory ? (Filename currentDirectory). |
374 |
||
16087 | 375 |
filename := filename asFilename. |
376 |
filename isAbsolute ifFalse:[ |
|
20909 | 377 |
filename := currentDirectory construct:filename. |
16087 | 378 |
]. |
379 |
||
20922 | 380 |
StreamError ignoreIn:[ |
381 |
newInput := filename readStream. |
|
382 |
]. |
|
16087 | 383 |
newInput isNil ifTrue:[ |
20915 | 384 |
('Could not find file: "',filename pathName,'"') errorPrintCR. |
20909 | 385 |
^ self. |
16087 | 386 |
]. |
387 |
||
388 |
[ |
|
20909 | 389 |
savedCurrentDirectory := currentDirectory. |
20912 | 390 |
savedDoChunkFormat := doChunkFormat. |
20909 | 391 |
savedInput := inputStream. |
392 |
savedPrint := printFlag. |
|
393 |
savedPrompt := prompt. |
|
16087 | 394 |
|
20909 | 395 |
currentDirectory := filename directory. |
396 |
inputStream := newInput. |
|
20910 | 397 |
|
20909 | 398 |
self |
399 |
basicReadEvalPrintLoopWithInput:newInput |
|
400 |
output:outputStream |
|
401 |
error:errorStream |
|
20910 | 402 |
compiler:(compiler ? Compiler ? Parser) |
20909 | 403 |
prompt:false |
404 |
print:false. |
|
20912 | 405 |
'after read1' errorPrintCR. |
16087 | 406 |
] ensure:[ |
20914 | 407 |
newInput close. |
20912 | 408 |
doChunkFormat := savedDoChunkFormat. |
20909 | 409 |
currentDirectory := savedCurrentDirectory. |
410 |
inputStream := savedInput. |
|
411 |
printFlag := savedPrint. |
|
20912 | 412 |
prompt := savedPrompt. |
413 |
||
414 |
'ensure done' errorPrintCR. |
|
16087 | 415 |
]. |
20912 | 416 |
'after read2' errorPrintCR. |
16087 | 417 |
! |
418 |
||
10463 | 419 |
cmd_set:lineStream |
10255 | 420 |
self cmd_setOrClear:lineStream to:true |
421 |
||
422 |
"Modified: / 07-12-2006 / 19:04:46 / cg" |
|
423 |
! |
|
424 |
||
10463 | 425 |
cmd_setOrClear:lineStream to:aBoolean |
426 |
|what| |
|
427 |
||
428 |
lineStream skipSeparators. |
|
429 |
what := lineStream nextAlphaNumericWord. |
|
16133 | 430 |
what notNil ifTrue:[ |
20771 | 431 |
(what startsWith:'tra') ifTrue:[ |
432 |
traceFlag := aBoolean. |
|
433 |
^ self. |
|
434 |
]. |
|
435 |
(what startsWith:'tim') ifTrue:[ |
|
436 |
timingFlag := aBoolean. |
|
437 |
^ self. |
|
438 |
]. |
|
439 |
(what startsWith:'pro') ifTrue:[ |
|
440 |
profilingFlag := aBoolean. |
|
441 |
^ self. |
|
442 |
]. |
|
443 |
(what startsWith:'chunk') ifTrue:[ |
|
444 |
doChunkFormat := aBoolean. |
|
445 |
^ self. |
|
446 |
]. |
|
10463 | 447 |
]. |
16133 | 448 |
self errorStream nextPutLine:'? which flag ?'. |
10255 | 449 |
|
10256 | 450 |
"Modified: / 07-12-2006 / 19:13:34 / cg" |
10255 | 451 |
! |
452 |
||
10463 | 453 |
cmd_show:lineStream |
20901 | 454 |
|errStream what showAll ok| |
10463 | 455 |
|
16087 | 456 |
errStream := self errorStream. |
457 |
||
10463 | 458 |
lineStream skipSeparators. |
459 |
what := lineStream nextAlphaNumericWord. |
|
20834 | 460 |
ok := false. |
461 |
||
16133 | 462 |
what notNil ifTrue:[ |
20834 | 463 |
showAll := (what startsWith:'all'). |
464 |
||
20835 | 465 |
(showAll or:[ what startsWith:'var' ]) ifTrue:[ |
466 |
showAll ifTrue:[ errStream nextPutLine:'Variables:'; nextPutLine:'----------' ]. |
|
20901 | 467 |
self showVariables. |
20845 | 468 |
ok := true. |
20768 | 469 |
]. |
20845 | 470 |
|
20835 | 471 |
(showAll or:[ what startsWith:'proc' ]) ifTrue:[ |
20836 | 472 |
showAll ifTrue:[ errStream cr; nextPutLine:'Threads:'; nextPutLine:'--------' ]. |
20768 | 473 |
MiniDebugger basicNew showProcesses. |
20845 | 474 |
ok := true. |
20768 | 475 |
]. |
20845 | 476 |
|
20902 | 477 |
("showAll or:[" what startsWith:'pack' "]") ifTrue:[ |
20905 | 478 |
showAll ifTrue:[ errStream cr; nextPutLine:'Available Packages:'; nextPutLine:'--------' ]. |
20900 | 479 |
self showPackages. |
480 |
ok := true. |
|
481 |
]. |
|
482 |
||
20834 | 483 |
(showAll or:[ what startsWith:'mod' ]) ifTrue:[ |
20836 | 484 |
showAll ifTrue:[ errStream cr; nextPutLine:'Modules:'; nextPutLine:'--------' ]. |
20901 | 485 |
self showModules. |
13865 | 486 |
|
20845 | 487 |
ok := true. |
20768 | 488 |
]. |
20845 | 489 |
|
20834 | 490 |
(showAll or:[ what startsWith:'mem' ]) ifTrue:[ |
491 |
|allMem| |
|
492 |
||
20836 | 493 |
showAll ifTrue:[ errStream cr; nextPutLine:'Memory:'; nextPutLine:'-------' ]. |
20891 | 494 |
"/ allMem := ObjectMemory oldSpaceUsed + ObjectMemory symSpaceUsed |
495 |
"/ + ObjectMemory newSpaceUsed. |
|
20768 | 496 |
errStream |
20891 | 497 |
"/ nextPutLine:('overall: ',(allMem // 1024) printString,' Kb'); |
20900 | 498 |
nextPutLine:('used : ',(ObjectMemory bytesUsed // 1024) printString,' Kb'); |
20768 | 499 |
nextPutLine:('free : ',(ObjectMemory freeSpace // 1024) printString,' Kb'); |
20900 | 500 |
nextPutAll:('minorGC: ',(ObjectMemory scavengeCount) printString); |
501 |
nextPutLine:(' majorGC: ',(ObjectMemory garbageCollectCount) printString). |
|
20845 | 502 |
ok := true. |
20768 | 503 |
]. |
20845 | 504 |
|
20834 | 505 |
(showAll or:[ what startsWith:'flag' ]) ifTrue:[ |
20836 | 506 |
showAll ifTrue:[ errStream cr; nextPutLine:'Flags:'; nextPutLine:'------' ]. |
20768 | 507 |
errStream |
20892 | 508 |
nextPutLine:('trace : ',(traceFlag ? false) printString); |
509 |
nextPutLine:('timing: ',(timingFlag ? false) printString); |
|
510 |
nextPutLine:('profiling: ',(profilingFlag ? false) printString); |
|
511 |
nextPutLine:('chunkFormat: ',(doChunkFormat ? false) printString). |
|
20845 | 512 |
ok := true. |
20768 | 513 |
]. |
10463 | 514 |
]. |
515 |
||
20834 | 516 |
ok ifFalse:[ |
517 |
errStream nextPutLine:'? show what ?'. |
|
518 |
]. |
|
519 |
||
520 |
" |
|
521 |
self basicNew cmd_show:'packages' readStream |
|
522 |
" |
|
10255 | 523 |
|
13866 | 524 |
"Modified: / 07-12-2011 / 22:15:07 / cg" |
10255 | 525 |
! |
526 |
||
10463 | 527 |
cmd_use:lineStream |
16145 | 528 |
|pkg| |
10463 | 529 |
|
530 |
lineStream skipSeparators. |
|
16133 | 531 |
pkg := lineStream upToEnd. |
532 |
pkg isNil ifTrue:[ |
|
16565 | 533 |
'? which package?' errorPrintCR. |
534 |
^ self. |
|
16133 | 535 |
]. |
536 |
pkg := pkg withoutSeparators. |
|
537 |
pkg isEmpty ifTrue:[ |
|
16565 | 538 |
'? which package?' errorPrintCR. |
539 |
^ self. |
|
16133 | 540 |
]. |
541 |
||
16145 | 542 |
[ |
16565 | 543 |
Smalltalk loadPackage:pkg. |
16145 | 544 |
] on:PackageLoadError do:[:ex| |
16565 | 545 |
"/ allow for some shortcuts... |
546 |
(pkg includes:$:) ifTrue:[ |
|
547 |
self errorStream nextPutLine:('Failed to load package: "',pkg,'"'). |
|
548 |
] ifFalse:[ |
|
549 |
"/ try stx standard package |
|
550 |
pkg := 'stx:', pkg. |
|
551 |
ex restart. |
|
552 |
]. |
|
13852 | 553 |
]. |
10255 | 554 |
|
555 |
"Created: / 07-12-2006 / 19:07:56 / cg" |
|
10463 | 556 |
! |
557 |
||
558 |
directive:line |
|
559 |
|s cmd| |
|
560 |
||
561 |
s := line readStream. |
|
562 |
s next. "/ skip the hash |
|
20913 | 563 |
s peek == $!! ifTrue:[ |
564 |
"/ skip shebang line |
|
565 |
^ self. |
|
566 |
]. |
|
10463 | 567 |
s skipSeparators. |
568 |
||
569 |
cmd := s nextAlphaNumericWord. |
|
16087 | 570 |
cmd notNil ifTrue:[ |
20919 | 571 |
AbortAllOperationRequest handle:[:ex | |
572 |
self errorStream nextPutLine:('Directive aborted: ', ex description) |
|
573 |
] do:[ |
|
20920 | 574 |
Error handle:[:ex | |
20919 | 575 |
self errorStream nextPutLine:('Ignored in directive: ', ex description). |
576 |
] do:[ |
|
577 |
self |
|
578 |
perform:('cmd_',cmd) asMutator with:s |
|
579 |
ifNotUnderstood:[ |
|
580 |
self errorStream |
|
581 |
nextPutAll:'?? invalid command: '; |
|
582 |
nextPutAll:cmd; |
|
583 |
nextPutAll:'. Type "#help" for help.'; |
|
584 |
cr. |
|
585 |
]. |
|
20913 | 586 |
]. |
20919 | 587 |
]. |
16087 | 588 |
]. |
10463 | 589 |
|
590 |
"Created: / 07-12-2006 / 18:49:17 / cg" |
|
20900 | 591 |
! |
592 |
||
20901 | 593 |
showModules |
594 |
|errStream printModule| |
|
595 |
||
596 |
errStream := self errorStream. |
|
597 |
||
598 |
printModule := |
|
599 |
[:mod | |
|
600 |
errStream |
|
601 |
nextPutAll:' '; |
|
602 |
nextPutAll:(mod package "libraryName"); |
|
603 |
nextPutLine:' (',(mod type),')'. |
|
604 |
]. |
|
605 |
||
606 |
errStream nextPutLine:'builtIn:'. |
|
607 |
((ObjectMemory binaryModuleInfo |
|
608 |
reject:[:m | m dynamic]) |
|
609 |
asSortedCollection:[:a :b | a name < b name]) do:printModule. |
|
610 |
||
611 |
errStream nextPutLine:'dynamic:'. |
|
612 |
((ObjectMemory binaryModuleInfo |
|
613 |
select:[:m | m dynamic]) |
|
614 |
asSortedCollection:[:a :b | a name < b name]) do:printModule. |
|
615 |
||
616 |
" |
|
617 |
ReadEvalPrintLoop basicNew showModules |
|
618 |
" |
|
619 |
! |
|
620 |
||
20900 | 621 |
showPackages |
622 |
|all| |
|
623 |
||
624 |
all := Set new. |
|
625 |
Smalltalk knownLoadablePackagesDo:[:packageID :type :path | |
|
626 |
all add:packageID |
|
627 |
]. |
|
628 |
all := all asOrderedCollection sort. |
|
629 |
all do:[:eachPackage | |
|
20901 | 630 |
self errorStream nextPutAll:eachPackage. |
631 |
(Smalltalk isPackageLoaded:eachPackage) ifTrue:[ |
|
632 |
self errorStream nextPutAll:' (loaded)'. |
|
633 |
]. |
|
634 |
self errorStream cr. |
|
20900 | 635 |
]. |
636 |
||
637 |
" |
|
638 |
ReadEvalPrintLoop basicNew showPackages |
|
20901 | 639 |
ReadEvalPrintLoop basicNew showModules |
640 |
" |
|
641 |
! |
|
642 |
||
643 |
showVariables |
|
644 |
Workspace notNil ifTrue:[ |
|
645 |
Workspace workspaceVariables keys asOrderedCollection sort do:[:nm | |
|
646 |
|holder| |
|
647 |
holder := Workspace workspaceVariables at:nm. |
|
648 |
self errorStream |
|
649 |
nextPutAll:nm; |
|
650 |
nextPutAll:' -> '; |
|
651 |
nextPutLine:holder value printString. |
|
652 |
]. |
|
653 |
]. |
|
654 |
||
655 |
" |
|
656 |
ReadEvalPrintLoop basicNew showVariables |
|
20900 | 657 |
" |
10255 | 658 |
! ! |
659 |
||
10248 | 660 |
!ReadEvalPrintLoop methodsFor:'evaluation'! |
661 |
||
16565 | 662 |
basicReadEvalPrintLoopWithInput:input output:output error:error |
16087 | 663 |
compiler:compilerClass prompt:prompt print:doPrint |
664 |
||
665 |
"{ Pragma: +optSpace }" |
|
666 |
||
667 |
"the core of the interpreter loop; extracted and parametrized, so it can be called recursive |
|
668 |
for included scripts. |
|
669 |
If chunkFormat is true, chunks are read. |
|
670 |
Otherwise, lines up to an empty line (or EOF) or a line ending in '.' are read. |
|
20774 | 671 |
A '#' character appearing in the first column of the first line turns off chunkmode, |
672 |
which allows for convenient shell scripts containing a #/bin/stx as the first line." |
|
16087 | 673 |
|
20911 | 674 |
exitAction := [^ self]. |
675 |
||
16087 | 676 |
[ |
20769 | 677 |
|lines chunk| |
16087 | 678 |
|
20769 | 679 |
prompt notNil ifTrue:[ |
680 |
error nextPutAll:prompt. |
|
681 |
]. |
|
16087 | 682 |
|
20769 | 683 |
input atEnd ifTrue:[ |
20912 | 684 |
doPrint ifTrue:[ error cr ]. |
20769 | 685 |
^ self. |
686 |
]. |
|
16087 | 687 |
|
20769 | 688 |
input peek == $# ifTrue:[ |
689 |
self doChunkFormat:false. |
|
690 |
]. |
|
16087 | 691 |
|
20769 | 692 |
self doChunkFormat ifTrue:[ |
693 |
input skipSeparators. |
|
694 |
chunk := input nextChunk. |
|
695 |
] ifFalse:[ |
|
696 |
lines := OrderedCollection new. |
|
697 |
[ |
|
698 |
|line| |
|
16087 | 699 |
|
20769 | 700 |
line := input nextLine. |
701 |
line notEmptyOrNil ifTrue:[ |
|
702 |
line = '?' ifTrue:[ |
|
703 |
self cmd_help:nil. |
|
704 |
prompt notNil ifTrue:[ |
|
705 |
error nextPutAll:prompt. |
|
706 |
]. |
|
707 |
] ifFalse:[ |
|
708 |
(line startsWith:'#') ifTrue:[ |
|
709 |
self directive:line. |
|
710 |
prompt notNil ifTrue:[ |
|
711 |
error nextPutAll:prompt. |
|
712 |
]. |
|
713 |
] ifFalse:[ |
|
714 |
lines add:line. |
|
715 |
] |
|
716 |
] |
|
717 |
]. |
|
718 |
line notEmptyOrNil and:[(line endsWith:$.) not]. |
|
719 |
] whileTrue. |
|
720 |
chunk := lines asStringWith:Character cr. |
|
721 |
]. |
|
16087 | 722 |
|
20890 | 723 |
(chunk notEmptyOrNil |
724 |
and:[chunk withoutSeparators notEmpty |
|
725 |
and:[chunk withoutSeparators ~= '.']] |
|
726 |
) ifTrue:[ |
|
20769 | 727 |
"abortAll is handled, but not asked for here!!" |
728 |
AbortAllOperationRequest handle:[:ex | |
|
729 |
error nextPutLine:('Evaluation aborted: ', ex description) |
|
730 |
] do:[ |
|
731 |
(Error, ControlInterrupt) handle:[:ex | |
|
732 |
prompt isNil ifTrue:[ |
|
733 |
ex reject |
|
734 |
]. |
|
735 |
MiniDebugger enterWithMessage:(ex errorString) mayProceed:true. |
|
736 |
ex mayProceed ifTrue:[ |
|
737 |
ex proceed. |
|
738 |
]. |
|
739 |
error nextPutLine:('Evaluation aborted: ', ex description). |
|
740 |
ex return. |
|
741 |
] do:[ |
|
742 |
|value ms us| |
|
16087 | 743 |
|
20771 | 744 |
profilingFlag == true ifTrue:[ |
745 |
MessageTally spyDetailedOn:[ |
|
746 |
value := (compilerClass new requestor:self) |
|
747 |
evaluate:chunk |
|
748 |
compile:true. |
|
749 |
]. |
|
750 |
doPrint ifTrue:[ |
|
751 |
value printOn:output. output cr. |
|
752 |
]. |
|
753 |
] ifFalse:[ |
|
754 |
us := Time microsecondsToRun:[ |
|
755 |
value := (compilerClass new requestor:self) |
|
756 |
evaluate:chunk compile:true. |
|
757 |
]. |
|
758 |
doPrint ifTrue:[ |
|
759 |
value printOn:output. output cr. |
|
760 |
]. |
|
16087 | 761 |
|
20771 | 762 |
timingFlag == true ifTrue:[ |
763 |
'execution time: ' printOn:error. |
|
764 |
us < 1000 ifTrue:[ |
|
765 |
us < 1 ifTrue:[ |
|
766 |
'too small to measure (<1us)' printOn:error. |
|
767 |
] ifFalse:[ |
|
768 |
us printOn:output. 'us' printOn:error. |
|
769 |
] |
|
20769 | 770 |
] ifFalse:[ |
20771 | 771 |
((us / 1000) asFixedPoint:2) printOn:output. 'ms' printOn:error. |
772 |
]. |
|
773 |
error cr. |
|
20769 | 774 |
]. |
775 |
]. |
|
776 |
Workspace notNil ifTrue:[ |
|
777 |
Workspace rememberResultAsWorkspaceVariable:value. |
|
778 |
]. |
|
779 |
]. |
|
780 |
]. |
|
781 |
]. |
|
16087 | 782 |
] loop. |
783 |
||
784 |
" |
|
785 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
|
786 |
" |
|
787 |
||
788 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
789 |
"Modified: / 06-12-2011 / 15:29:03 / cg" |
|
790 |
! |
|
791 |
||
10463 | 792 |
readEvalPrintLoop |
793 |
"{ Pragma: +optSpace }" |
|
794 |
||
795 |
"simple read-eval-print loop for non-graphical Minitalk. |
|
796 |
If the chunkFormat-argument is true, chunks are read. |
|
16087 | 797 |
Otherwise, lines up to an empty line (or EOF) are read. |
798 |
A '#' character appearing in the first column of the first line |
|
799 |
switches to chunkmode." |
|
10463 | 800 |
|
801 |
ControlInterrupt handle:[:ex | |
|
20774 | 802 |
self errorStream nextPutLine:('Caught: ', ex description). |
20833 | 803 |
self inputStream atEnd ifTrue:[ |
804 |
ex return. |
|
805 |
]. |
|
20774 | 806 |
ex restart. |
10463 | 807 |
] do:[ |
20774 | 808 |
|input output error compilerClass| |
809 |
||
810 |
"/ re-evaluate these in the loop, so they can be changed dynamically |
|
811 |
input := self inputStream. |
|
812 |
output := self outputStream. |
|
813 |
error := self errorStream. |
|
10463 | 814 |
|
20774 | 815 |
compilerClass := compiler ? Compiler ? Parser. |
816 |
compilerClass isNil ifTrue:[ |
|
817 |
self errorStream nextPutLine:('oops - no Compiler class found'). |
|
818 |
^ self. |
|
819 |
]. |
|
820 |
StreamError handle:[:ex | |
|
821 |
(input isOpen not or:[input atEnd]) ifTrue:[ |
|
822 |
error nextPutLine:'EOF on input'. |
|
20833 | 823 |
ex return. |
20774 | 824 |
]. |
825 |
(output isOpen not) ifTrue:[ |
|
826 |
error nextPutLine:'no output'. |
|
827 |
]. |
|
828 |
(error isOpen not) ifTrue:[ |
|
829 |
]. |
|
830 |
] do:[ |
|
831 |
input signalAtEnd:true. |
|
832 |
self |
|
833 |
basicReadEvalPrintLoopWithInput:input output:output error:error |
|
834 |
compiler:compilerClass prompt:prompt print:(printFlag ? true). |
|
835 |
] |
|
10463 | 836 |
] |
837 |
||
838 |
" |
|
839 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
|
840 |
" |
|
841 |
||
842 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
13859
81451e09af8d
changed: #readEvalPrintLoop
Claus Gittinger <cg@exept.de>
parents:
13858
diff
changeset
|
843 |
"Modified: / 06-12-2011 / 15:29:03 / cg" |
10248 | 844 |
! ! |
845 |
||
16089 | 846 |
!ReadEvalPrintLoop methodsFor:'queries'! |
847 |
||
848 |
autoDefineVariables |
|
849 |
"when evaluating with --eval, auto define any variables" |
|
850 |
||
851 |
^ #workspace |
|
852 |
! ! |
|
853 |
||
10248 | 854 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
855 |
||
856 |
version |
|
19379 | 857 |
^ '$Header$' |
13121
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
858 |
! |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
859 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
860 |
version_CVS |
19379 | 861 |
^ '$Header$' |
10248 | 862 |
! ! |
19379 | 863 |