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