author | Claus Gittinger <cg@exept.de> |
Tue, 08 Nov 2016 22:24:59 +0100 | |
changeset 20934 | 36e46558156d |
parent 20933 | 2a6274961920 |
child 20935 | 7803512423cb |
permissions | -rw-r--r-- |
20933 | 1 |
"{ Encoding: utf8 }" |
2 |
||
10249 | 3 |
" |
4 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
16565 | 5 |
All Rights Reserved |
10249 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
10248 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
19379 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
10248 | 18 |
Object subclass:#ReadEvalPrintLoop |
16228 | 19 |
instanceVariableNames:'inputStream outputStream errorStream compiler prompt |
20771 | 20 |
doChunkFormat traceFlag timingFlag profilingFlag printFlag |
20926 | 21 |
exitAction currentDirectory lastEditedClass lastEditedSelector' |
16228 | 22 |
classVariableNames:'' |
23 |
poolDictionaries:'' |
|
24 |
category:'System-Support' |
|
10248 | 25 |
! |
26 |
||
10249 | 27 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
28 |
||
29 |
copyright |
|
30 |
" |
|
31 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
16565 | 32 |
All Rights Reserved |
10249 | 33 |
|
34 |
This software is furnished under a license and may be used |
|
35 |
only in accordance with the terms of that license and with the |
|
36 |
inclusion of the above copyright notice. This software may not |
|
37 |
be provided or otherwise made available to, or used by, any |
|
38 |
other person. No title to or ownership of the software is |
|
39 |
hereby transferred. |
|
40 |
" |
|
10461
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
41 |
! |
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 |
documentation |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
44 |
" |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
45 |
A simple read-eval-print loop for non-GUI or stscript operation. |
16081 | 46 |
Invoked, for example if stx is started with a --repl argument. |
20833 | 47 |
|
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
48 |
A line starting with '?' shows the usage message. |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
49 |
Lines starting with '#' are directives: |
20833 | 50 |
#exit - exit the rep-loop |
51 |
type '?' to see more. |
|
52 |
||
16087 | 53 |
The input can be in one of two formats: |
20833 | 54 |
1) traditional chunk format (bang-separated chunks, bangs duplicated) |
55 |
this is the traditional fileIn format, as generated by fileOut from the browser |
|
16087 | 56 |
|
20833 | 57 |
2) interactive line mode. Chunks are any number of lines up to either an empty line or |
58 |
a line ending in a period. This is more useful for an interactive REPL, where statements/expressions |
|
59 |
are entered linewise by a user. |
|
16087 | 60 |
|
61 |
The input can is switched to non-chunk format whenever a line with a '#' in the first column appears. |
|
20833 | 62 |
|
20933 | 63 |
Try it (but only if you have a console): |
64 |
Smalltalk readEvalPrintLoop |
|
65 |
||
20833 | 66 |
[Author:] |
67 |
Claus Gittinger |
|
10461
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
68 |
" |
10249 | 69 |
! ! |
10248 | 70 |
|
71 |
!ReadEvalPrintLoop methodsFor:'accessing'! |
|
72 |
||
10463 | 73 |
compiler:something |
16087 | 74 |
"assign a compiler to use;could be used to change the language" |
75 |
||
10463 | 76 |
compiler := something. |
10248 | 77 |
! |
78 |
||
10632 | 79 |
doChunkFormat |
16087 | 80 |
"true if currently reading chunk format" |
81 |
||
10632 | 82 |
^ doChunkFormat ? true |
83 |
||
84 |
"Created: / 07-12-2006 / 18:24:04 / cg" |
|
85 |
! |
|
86 |
||
16087 | 87 |
doChunkFormat:aBoolean |
88 |
"enable/disable chunk format" |
|
89 |
||
90 |
doChunkFormat := aBoolean. |
|
10254 | 91 |
|
92 |
"Created: / 07-12-2006 / 18:24:04 / cg" |
|
10248 | 93 |
! |
94 |
||
16087 | 95 |
error:aStream |
96 |
"assign an error stream" |
|
97 |
||
98 |
errorStream := aStream. |
|
10248 | 99 |
|
100 |
"Created: / 07-12-2006 / 17:33:39 / cg" |
|
101 |
! |
|
102 |
||
10463 | 103 |
errorStream |
16087 | 104 |
"return the current error stream" |
105 |
||
19379 | 106 |
errorStream notNil ifTrue:[^ errorStream]. |
107 |
^ Processor activeProcess stderr |
|
10256 | 108 |
|
19379 | 109 |
"Created: / 07-12-2006 / 19:12:27 / cg" |
10256 | 110 |
! |
111 |
||
16087 | 112 |
input:aStream |
113 |
"assign an input stream" |
|
114 |
||
115 |
inputStream := aStream asLineNumberReadStream. |
|
10248 | 116 |
|
117 |
"Modified: / 07-12-2006 / 17:33:31 / cg" |
|
118 |
! |
|
119 |
||
10463 | 120 |
inputStream |
16087 | 121 |
"get the current input stream" |
122 |
||
19379 | 123 |
inputStream notNil ifTrue:[^ inputStream]. |
124 |
^ Processor activeProcess stdin |
|
10463 | 125 |
|
126 |
"Created: / 07-12-2006 / 19:12:13 / cg" |
|
10256 | 127 |
! |
128 |
||
16087 | 129 |
output:aStream |
130 |
"assign an output stream" |
|
131 |
||
132 |
outputStream := aStream. |
|
10248 | 133 |
|
134 |
"Created: / 07-12-2006 / 17:27:48 / cg" |
|
135 |
! |
|
136 |
||
10463 | 137 |
outputStream |
19379 | 138 |
"return the current output stream" |
16087 | 139 |
|
19379 | 140 |
outputStream notNil ifTrue:[^ outputStream]. |
141 |
^ Processor activeProcess stdout |
|
10463 | 142 |
|
143 |
"Created: / 07-12-2006 / 19:12:27 / cg" |
|
144 |
! |
|
145 |
||
16087 | 146 |
prompt:aString |
147 |
"set the prompt" |
|
148 |
||
149 |
prompt := aString. |
|
10248 | 150 |
! ! |
151 |
||
10631 | 152 |
!ReadEvalPrintLoop methodsFor:'compiler interface-error handling'! |
153 |
||
154 |
correctableError:message position:pos1 to:pos2 from:aCompiler |
|
155 |
"compiler notifies us of an error - ignore it" |
|
156 |
||
15702 | 157 |
^ false "/ no correction |
10631 | 158 |
! |
159 |
||
160 |
correctableSelectorWarning:aString position:relPos to:relEndPos from:aCompiler |
|
161 |
"compiler notifies us of a warning - ignore it" |
|
162 |
||
163 |
^ false |
|
164 |
! |
|
165 |
||
13121
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
166 |
correctableWarning:message position:pos1 to:pos2 from:aCompiler |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
167 |
"compiler notifies us of an error - ignore it" |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
168 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
169 |
^ false |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
170 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
171 |
"Created: / 02-11-2010 / 13:29:22 / cg" |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
172 |
! |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
173 |
|
10631 | 174 |
error:aString position:relPos to:relEndPos from:aCompiler |
175 |
"compiler notifies us of a warning - ignore it" |
|
176 |
||
177 |
^ false |
|
178 |
! |
|
179 |
||
180 |
unusedVariableWarning:aString position:relPos to:relEndPos from:aCompiler |
|
181 |
"compiler notifies us of a warning - ignore it" |
|
182 |
||
183 |
^ false |
|
184 |
! |
|
185 |
||
186 |
warning:aString position:relPos to:relEndPos from:aCompiler |
|
187 |
"compiler notifies us of a warning - ignore it" |
|
188 |
||
189 |
^ self |
|
190 |
! ! |
|
191 |
||
10255 | 192 |
!ReadEvalPrintLoop methodsFor:'directives'! |
193 |
||
10463 | 194 |
cmd_clear:lineStream |
10255 | 195 |
self cmd_setOrClear:lineStream to:false |
196 |
||
197 |
"Created: / 07-12-2006 / 19:04:50 / cg" |
|
198 |
! |
|
199 |
||
20900 | 200 |
cmd_debug:lineStream |
201 |
MiniDebugger enter. |
|
202 |
! |
|
203 |
||
204 |
cmd_edit:lineStream |
|
205 |
"edit a class or selector" |
|
206 |
||
207 |
|errStream editor classOrMethodName cls methodName selector |
|
20906 | 208 |
code isNewClass editFullClass tmpFile modifiedTime| |
20900 | 209 |
|
210 |
errStream := self errorStream. |
|
211 |
||
212 |
editor := OperatingSystem getEnvironment:'STX_EDITOR'. |
|
213 |
editor isNil ifTrue:[ |
|
214 |
editor := OperatingSystem getEnvironment:'EDITOR'. |
|
215 |
editor isNil ifTrue:[ |
|
216 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
|
217 |
editor := 'notepad'. |
|
218 |
] ifFalse:[ |
|
219 |
editor := 'vi'. |
|
220 |
]. |
|
221 |
]. |
|
222 |
]. |
|
223 |
||
20906 | 224 |
isNewClass := editFullClass := false. |
20900 | 225 |
|
226 |
lineStream skipSeparators. |
|
20926 | 227 |
lineStream atEnd ifTrue:[ |
228 |
cls := lastEditedClass. |
|
229 |
methodName := lastEditedSelector. |
|
230 |
] ifFalse:[ |
|
20900 | 231 |
|
20926 | 232 |
classOrMethodName := lineStream |
233 |
upToElementForWhich:[:ch | |
|
234 |
ch isLetterOrDigit not and:[ch ~~ $_] |
|
235 |
]. |
|
236 |
"/ |
|
237 |
(classOrMethodName isUppercaseFirst) ifTrue:[ |
|
238 |
(cls := Smalltalk classNamed:classOrMethodName) isNil ifTrue:[ |
|
239 |
errStream show:'edit: no such class: ',classOrMethodName,' ; create (y/n)? '. |
|
240 |
(self inputStream nextLine withoutSeparators startsWith:'y') ifFalse:[^ self]. |
|
241 |
isNewClass := true. |
|
242 |
code := |
|
243 |
'"/ change the code as required, then save and exit the editor. |
|
244 |
"/ To cancel this edit, leave the editor WITHOUT saving. |
|
245 |
"/ |
|
246 |
||
247 |
Object |
|
20900 | 248 |
subclass:#%1 |
249 |
instanceVariableNames:'''' |
|
250 |
classVariableNames:'''' |
|
251 |
poolDictionaries:'''' |
|
252 |
category:''user classes'' |
|
253 |
' bindWith:classOrMethodName. |
|
20926 | 254 |
] ifFalse:[ |
255 |
lineStream skipSeparators. |
|
256 |
lineStream atEnd ifFalse:[ |
|
257 |
methodName := lineStream upToSeparator. |
|
258 |
]. |
|
20900 | 259 |
]. |
20926 | 260 |
] ifFalse:[ |
261 |
methodName := classOrMethodName |
|
20900 | 262 |
]. |
263 |
]. |
|
20926 | 264 |
|
20900 | 265 |
isNewClass ifFalse:[ |
266 |
cls := cls ? lastEditedClass. |
|
267 |
cls isNil ifTrue:[ |
|
20923 | 268 |
errStream showCR:'edit usage:'. |
269 |
errStream showCR:' #edit className selector'. |
|
270 |
errStream showCR:' #edit className '. |
|
271 |
errStream showCR:' #edit selector (class as in previous edit)'. |
|
20926 | 272 |
errStream showCR:' #edit (class/method as in previous edit)'. |
20900 | 273 |
^ self. |
274 |
]. |
|
275 |
lastEditedClass := cls. |
|
20926 | 276 |
lastEditedSelector := methodName. |
277 |
||
20906 | 278 |
methodName isNil ifTrue:[ |
279 |
editFullClass := true. |
|
280 |
code := cls source asString |
|
281 |
] ifFalse:[ |
|
282 |
((selector := methodName asSymbolIfInterned) isNil |
|
283 |
or:[ (cls implements:selector) not]) ifTrue:[ |
|
20923 | 284 |
errStream show:('"',methodName,'" is a new method; create (y/n)? '). |
20906 | 285 |
(self inputStream nextLine withoutSeparators startsWith:'y') ifFalse:[^ self]. |
20926 | 286 |
code := |
287 |
'"/ change the code as required, then save and exit the editor. |
|
288 |
"/ To cancel this edit, leave the editor WITHOUT saving. |
|
289 |
||
20900 | 290 |
%1 |
291 |
"this is a new method" |
|
292 |
self halt |
|
20906 | 293 |
' bindWith:methodName. |
294 |
] ifFalse:[ |
|
20926 | 295 |
code := (cls compiledMethodAt:selector) source. |
20906 | 296 |
]. |
20900 | 297 |
]. |
298 |
]. |
|
299 |
||
20906 | 300 |
[ |
20933 | 301 |
|ok| |
302 |
||
20906 | 303 |
tmpFile := Filename newTemporary. |
304 |
tmpFile contents:code. |
|
305 |
modifiedTime := tmpFile modificationTime. |
|
20933 | 306 |
|
307 |
ok := OperatingSystem |
|
20934 | 308 |
executeCommand:('%1 "%2"' bindWith:editor with:tmpFile pathName) |
309 |
inputFrom:nil "/ Stdin |
|
310 |
outputTo:nil "/ Stdout |
|
311 |
errorTo:nil "/ Stderr |
|
20933 | 312 |
auxFrom:nil |
313 |
environment:nil |
|
314 |
inDirectory:nil |
|
315 |
lineWise:false |
|
316 |
newPgrp:false |
|
317 |
showWindow:true |
|
318 |
onError:[:status | false]. |
|
319 |
||
320 |
(ok and:[tmpFile modificationTime ~= modifiedTime]) ifTrue:[ |
|
20906 | 321 |
isNewClass ifTrue:[ |
322 |
Compiler evaluate:tmpFile contentsOfEntireFile. |
|
323 |
] ifFalse:[ |
|
324 |
editFullClass ifTrue:[ |
|
325 |
tmpFile fileIn. |
|
326 |
] ifFalse:[ |
|
327 |
cls compile:tmpFile contentsOfEntireFile classified:'*as yet uncategorized'. |
|
328 |
]. |
|
329 |
]. |
|
330 |
]. |
|
331 |
] ensure:[ |
|
332 |
tmpFile notNil ifTrue:[ |
|
333 |
tmpFile remove |
|
334 |
] |
|
20900 | 335 |
]. |
20906 | 336 |
|
20900 | 337 |
" |
20934 | 338 |
Smalltalk readEvalPrintLoop |
339 |
||
20900 | 340 |
self new |
341 |
input:Stdin; |
|
342 |
cmd_edit:'MyClass foo' readStream |
|
343 |
" |
|
20933 | 344 |
|
20934 | 345 |
"Modified (comment): / 08-11-2016 / 22:24:00 / cg" |
20900 | 346 |
! |
347 |
||
10463 | 348 |
cmd_exit:lineStream |
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
349 |
exitAction value |
10463 | 350 |
|
351 |
"Created: / 07-12-2006 / 18:55:46 / cg" |
|
10255 | 352 |
! |
353 |
||
10463 | 354 |
cmd_help:lineStream |
355 |
self errorStream |
|
20771 | 356 |
nextPutAll: |
16084 | 357 |
'Everything entered up to an empty line or a line ending in "." is called a "chunk" and evaluated. |
10463 | 358 |
Lines starting with "#" are commands to the read-eval-print interpreter. |
359 |
||
360 |
Valid commands are: |
|
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
361 |
#help ............... this text |
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
362 |
#exit ............... exit interpreter loop |
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
363 |
#use <package>....... use (load) a package |
20771 | 364 |
stx:libwidg .............. GUI package |
365 |
stx:libtool .............. IDE tool package |
|
366 |
stx:goodies/regex ........ regex package |
|
367 |
stx:goodies/petitparser .. peg parser package |
|
16087 | 368 |
#read <filename>..... read another script or source file |
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
369 |
#show <what> ........ show info |
20771 | 370 |
variables .......... interpreter variables |
371 |
processes .......... processes |
|
372 |
memory ............. memory usage |
|
373 |
flags .............. flags |
|
374 |
modules ............ loaded modules |
|
20900 | 375 |
packages ........... available packages to load |
20834 | 376 |
all ................ all of the above |
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
377 |
#set/clear <flag> ... set or clear a flag |
20771 | 378 |
trace .............. tracing execution |
379 |
timing ............. timing execution |
|
380 |
profiling .......... show execution profile |
|
381 |
chunkFormat ........ traditional bang chunk format input mode |
|
20900 | 382 |
#debug ................. enter a MiniDebugger |
10257 | 383 |
|
16087 | 384 |
The MiniDebugger (if entered) shows its own help with "?". |
10255 | 385 |
' |
386 |
||
387 |
"Created: / 07-12-2006 / 18:54:20 / cg" |
|
388 |
! |
|
389 |
||
16087 | 390 |
cmd_read:lineStream |
20912 | 391 |
|filename newInput savedPrompt savedPrint savedInput savedCurrentDirectory savedDoChunkFormat| |
16087 | 392 |
|
393 |
lineStream skipSeparators. |
|
394 |
filename := lineStream upToEnd withoutSeparators. |
|
16133 | 395 |
filename isNil ifTrue:[ |
20923 | 396 |
self errorStream showCR:'? which file?'. |
20909 | 397 |
^ self. |
16133 | 398 |
]. |
399 |
filename := filename withoutSeparators. |
|
400 |
filename isEmpty ifTrue:[ |
|
20923 | 401 |
self errorStream showCR:'? which file?'. |
20909 | 402 |
^ self. |
16133 | 403 |
]. |
16087 | 404 |
|
20915 | 405 |
currentDirectory := currentDirectory ? (Filename currentDirectory). |
406 |
||
16087 | 407 |
filename := filename asFilename. |
408 |
filename isAbsolute ifFalse:[ |
|
20909 | 409 |
filename := currentDirectory construct:filename. |
16087 | 410 |
]. |
411 |
||
20922 | 412 |
StreamError ignoreIn:[ |
413 |
newInput := filename readStream. |
|
414 |
]. |
|
16087 | 415 |
newInput isNil ifTrue:[ |
20923 | 416 |
self errorStream showCR:('Could not find file: "',filename pathName,'"'). |
20909 | 417 |
^ self. |
16087 | 418 |
]. |
419 |
||
420 |
[ |
|
20909 | 421 |
savedCurrentDirectory := currentDirectory. |
20912 | 422 |
savedDoChunkFormat := doChunkFormat. |
20909 | 423 |
savedInput := inputStream. |
424 |
savedPrint := printFlag. |
|
425 |
savedPrompt := prompt. |
|
16087 | 426 |
|
20909 | 427 |
currentDirectory := filename directory. |
428 |
inputStream := newInput. |
|
20910 | 429 |
|
20909 | 430 |
self |
431 |
basicReadEvalPrintLoopWithInput:newInput |
|
432 |
output:outputStream |
|
433 |
error:errorStream |
|
20910 | 434 |
compiler:(compiler ? Compiler ? Parser) |
20909 | 435 |
prompt:false |
436 |
print:false. |
|
16087 | 437 |
] ensure:[ |
20914 | 438 |
newInput close. |
20912 | 439 |
doChunkFormat := savedDoChunkFormat. |
20909 | 440 |
currentDirectory := savedCurrentDirectory. |
441 |
inputStream := savedInput. |
|
442 |
printFlag := savedPrint. |
|
20912 | 443 |
prompt := savedPrompt. |
16087 | 444 |
]. |
445 |
! |
|
446 |
||
10463 | 447 |
cmd_set:lineStream |
10255 | 448 |
self cmd_setOrClear:lineStream to:true |
449 |
||
450 |
"Modified: / 07-12-2006 / 19:04:46 / cg" |
|
451 |
! |
|
452 |
||
10463 | 453 |
cmd_setOrClear:lineStream to:aBoolean |
454 |
|what| |
|
455 |
||
456 |
lineStream skipSeparators. |
|
457 |
what := lineStream nextAlphaNumericWord. |
|
16133 | 458 |
what notNil ifTrue:[ |
20771 | 459 |
(what startsWith:'tra') ifTrue:[ |
460 |
traceFlag := aBoolean. |
|
461 |
^ self. |
|
462 |
]. |
|
463 |
(what startsWith:'tim') ifTrue:[ |
|
464 |
timingFlag := aBoolean. |
|
465 |
^ self. |
|
466 |
]. |
|
467 |
(what startsWith:'pro') ifTrue:[ |
|
468 |
profilingFlag := aBoolean. |
|
469 |
^ self. |
|
470 |
]. |
|
471 |
(what startsWith:'chunk') ifTrue:[ |
|
472 |
doChunkFormat := aBoolean. |
|
473 |
^ self. |
|
474 |
]. |
|
10463 | 475 |
]. |
20923 | 476 |
self errorStream showCR:'? which flag ?'. |
10255 | 477 |
|
10256 | 478 |
"Modified: / 07-12-2006 / 19:13:34 / cg" |
10255 | 479 |
! |
480 |
||
10463 | 481 |
cmd_show:lineStream |
20901 | 482 |
|errStream what showAll ok| |
10463 | 483 |
|
16087 | 484 |
errStream := self errorStream. |
485 |
||
10463 | 486 |
lineStream skipSeparators. |
487 |
what := lineStream nextAlphaNumericWord. |
|
20834 | 488 |
ok := false. |
489 |
||
16133 | 490 |
what notNil ifTrue:[ |
20834 | 491 |
showAll := (what startsWith:'all'). |
492 |
||
20835 | 493 |
(showAll or:[ what startsWith:'var' ]) ifTrue:[ |
20923 | 494 |
showAll ifTrue:[ errStream showCR:'Variables:'; showCR:'----------' ]. |
20901 | 495 |
self showVariables. |
20845 | 496 |
ok := true. |
20768 | 497 |
]. |
20845 | 498 |
|
20835 | 499 |
(showAll or:[ what startsWith:'proc' ]) ifTrue:[ |
20923 | 500 |
showAll ifTrue:[ errStream cr; showCR:'Threads:'; showCR:'--------' ]. |
20768 | 501 |
MiniDebugger basicNew showProcesses. |
20845 | 502 |
ok := true. |
20768 | 503 |
]. |
20845 | 504 |
|
20902 | 505 |
("showAll or:[" what startsWith:'pack' "]") ifTrue:[ |
20923 | 506 |
showAll ifTrue:[ errStream cr; showCR:'Available Packages:'; showCR:'--------' ]. |
20900 | 507 |
self showPackages. |
508 |
ok := true. |
|
509 |
]. |
|
510 |
||
20834 | 511 |
(showAll or:[ what startsWith:'mod' ]) ifTrue:[ |
20923 | 512 |
showAll ifTrue:[ errStream cr; showCR:'Modules:'; showCR:'--------' ]. |
20901 | 513 |
self showModules. |
13865 | 514 |
|
20845 | 515 |
ok := true. |
20768 | 516 |
]. |
20845 | 517 |
|
20834 | 518 |
(showAll or:[ what startsWith:'mem' ]) ifTrue:[ |
519 |
|allMem| |
|
520 |
||
20923 | 521 |
showAll ifTrue:[ errStream cr; showCR:'Memory:'; showCR:'-------' ]. |
20891 | 522 |
"/ allMem := ObjectMemory oldSpaceUsed + ObjectMemory symSpaceUsed |
523 |
"/ + ObjectMemory newSpaceUsed. |
|
20768 | 524 |
errStream |
20923 | 525 |
"/ showCR:('overall: ',(allMem // 1024) printString,' Kb'); |
526 |
showCR:('used : ',(ObjectMemory bytesUsed // 1024) printString,' Kb'); |
|
527 |
showCR:('free : ',(ObjectMemory freeSpace // 1024) printString,' Kb'); |
|
528 |
show:('minorGC: ',(ObjectMemory scavengeCount) printString); |
|
529 |
showCR:(' majorGC: ',(ObjectMemory garbageCollectCount) printString). |
|
20845 | 530 |
ok := true. |
20768 | 531 |
]. |
20845 | 532 |
|
20834 | 533 |
(showAll or:[ what startsWith:'flag' ]) ifTrue:[ |
20923 | 534 |
showAll ifTrue:[ errStream cr; showCR:'Flags:'; showCR:'------' ]. |
20768 | 535 |
errStream |
20923 | 536 |
showCR:('trace : ',(traceFlag ? false) printString); |
537 |
showCR:('timing: ',(timingFlag ? false) printString); |
|
538 |
showCR:('profiling: ',(profilingFlag ? false) printString); |
|
539 |
showCR:('chunkFormat: ',(doChunkFormat ? false) printString). |
|
20845 | 540 |
ok := true. |
20768 | 541 |
]. |
10463 | 542 |
]. |
543 |
||
20834 | 544 |
ok ifFalse:[ |
20923 | 545 |
errStream showCR:'? show what ?'. |
20834 | 546 |
]. |
547 |
||
548 |
" |
|
549 |
self basicNew cmd_show:'packages' readStream |
|
550 |
" |
|
10255 | 551 |
|
13866 | 552 |
"Modified: / 07-12-2011 / 22:15:07 / cg" |
10255 | 553 |
! |
554 |
||
10463 | 555 |
cmd_use:lineStream |
16145 | 556 |
|pkg| |
10463 | 557 |
|
558 |
lineStream skipSeparators. |
|
16133 | 559 |
pkg := lineStream upToEnd. |
560 |
pkg isNil ifTrue:[ |
|
20923 | 561 |
self errorStream showCR:'? which package?'. |
562 |
^ self. |
|
16133 | 563 |
]. |
564 |
pkg := pkg withoutSeparators. |
|
565 |
pkg isEmpty ifTrue:[ |
|
20923 | 566 |
self errorStream showCR:'? which package?'. |
567 |
^ self. |
|
16133 | 568 |
]. |
569 |
||
16145 | 570 |
[ |
20923 | 571 |
Smalltalk loadPackage:pkg. |
16145 | 572 |
] on:PackageLoadError do:[:ex| |
20923 | 573 |
"/ allow for some shortcuts... |
574 |
(pkg includes:$:) ifTrue:[ |
|
575 |
self errorStream showCR:('Failed to load package: "',pkg,'"'). |
|
576 |
] ifFalse:[ |
|
577 |
"/ try stx standard package |
|
578 |
pkg := 'stx:', pkg. |
|
579 |
ex restart. |
|
580 |
]. |
|
13852 | 581 |
]. |
10255 | 582 |
|
583 |
"Created: / 07-12-2006 / 19:07:56 / cg" |
|
10463 | 584 |
! |
585 |
||
586 |
directive:line |
|
587 |
|s cmd| |
|
588 |
||
589 |
s := line readStream. |
|
590 |
s next. "/ skip the hash |
|
20913 | 591 |
s peek == $!! ifTrue:[ |
592 |
"/ skip shebang line |
|
593 |
^ self. |
|
594 |
]. |
|
10463 | 595 |
s skipSeparators. |
596 |
||
597 |
cmd := s nextAlphaNumericWord. |
|
16087 | 598 |
cmd notNil ifTrue:[ |
20919 | 599 |
AbortAllOperationRequest handle:[:ex | |
20923 | 600 |
self errorStream showCR:('Directive aborted: ', ex description) |
20919 | 601 |
] do:[ |
20920 | 602 |
Error handle:[:ex | |
20926 | 603 |
self errorStream showCR:('Caught in directive: ', ex description). |
604 |
ex suspendedContext fullPrintAll. |
|
20919 | 605 |
] do:[ |
20926 | 606 |
ControlInterrupt handle:[:ex | |
607 |
self errorStream showCR:('Ignored in directive: ', ex description). |
|
20933 | 608 |
ex reject. |
20926 | 609 |
ex proceed. |
610 |
] do:[ |
|
611 |
self |
|
612 |
perform:('cmd_',cmd) asMutator with:s |
|
613 |
ifNotUnderstood:[ |
|
614 |
self errorStream |
|
615 |
show:'?? invalid command: '; show:cmd; |
|
616 |
showCR:'. Type "#help" for help.' |
|
617 |
]. |
|
618 |
]. |
|
20913 | 619 |
]. |
20919 | 620 |
]. |
16087 | 621 |
]. |
10463 | 622 |
|
623 |
"Created: / 07-12-2006 / 18:49:17 / cg" |
|
20933 | 624 |
"Modified: / 08-11-2016 / 21:59:16 / cg" |
20900 | 625 |
! |
626 |
||
20901 | 627 |
showModules |
628 |
|errStream printModule| |
|
629 |
||
630 |
errStream := self errorStream. |
|
631 |
||
632 |
printModule := |
|
633 |
[:mod | |
|
634 |
errStream |
|
20923 | 635 |
show:' '; |
636 |
show:(mod package "libraryName"); |
|
637 |
showCR:' (',(mod type),')'. |
|
20901 | 638 |
]. |
639 |
||
640 |
errStream nextPutLine:'builtIn:'. |
|
641 |
((ObjectMemory binaryModuleInfo |
|
642 |
reject:[:m | m dynamic]) |
|
643 |
asSortedCollection:[:a :b | a name < b name]) do:printModule. |
|
644 |
||
645 |
errStream nextPutLine:'dynamic:'. |
|
646 |
((ObjectMemory binaryModuleInfo |
|
647 |
select:[:m | m dynamic]) |
|
648 |
asSortedCollection:[:a :b | a name < b name]) do:printModule. |
|
649 |
||
650 |
" |
|
651 |
ReadEvalPrintLoop basicNew showModules |
|
652 |
" |
|
653 |
! |
|
654 |
||
20900 | 655 |
showPackages |
656 |
|all| |
|
657 |
||
658 |
all := Set new. |
|
659 |
Smalltalk knownLoadablePackagesDo:[:packageID :type :path | |
|
660 |
all add:packageID |
|
661 |
]. |
|
662 |
all := all asOrderedCollection sort. |
|
663 |
all do:[:eachPackage | |
|
20923 | 664 |
self errorStream show:eachPackage. |
20901 | 665 |
(Smalltalk isPackageLoaded:eachPackage) ifTrue:[ |
20923 | 666 |
self errorStream show:' (loaded)'. |
20901 | 667 |
]. |
668 |
self errorStream cr. |
|
20900 | 669 |
]. |
670 |
||
671 |
" |
|
672 |
ReadEvalPrintLoop basicNew showPackages |
|
20901 | 673 |
ReadEvalPrintLoop basicNew showModules |
674 |
" |
|
675 |
! |
|
676 |
||
677 |
showVariables |
|
678 |
Workspace notNil ifTrue:[ |
|
679 |
Workspace workspaceVariables keys asOrderedCollection sort do:[:nm | |
|
680 |
|holder| |
|
681 |
holder := Workspace workspaceVariables at:nm. |
|
682 |
self errorStream |
|
20923 | 683 |
show:nm; |
684 |
show:' -> '; |
|
685 |
showCR:holder value printString. |
|
20901 | 686 |
]. |
687 |
]. |
|
688 |
||
689 |
" |
|
690 |
ReadEvalPrintLoop basicNew showVariables |
|
20900 | 691 |
" |
10255 | 692 |
! ! |
693 |
||
10248 | 694 |
!ReadEvalPrintLoop methodsFor:'evaluation'! |
695 |
||
16565 | 696 |
basicReadEvalPrintLoopWithInput:input output:output error:error |
16087 | 697 |
compiler:compilerClass prompt:prompt print:doPrint |
698 |
||
699 |
"{ Pragma: +optSpace }" |
|
700 |
||
701 |
"the core of the interpreter loop; extracted and parametrized, so it can be called recursive |
|
702 |
for included scripts. |
|
703 |
If chunkFormat is true, chunks are read. |
|
704 |
Otherwise, lines up to an empty line (or EOF) or a line ending in '.' are read. |
|
20774 | 705 |
A '#' character appearing in the first column of the first line turns off chunkmode, |
706 |
which allows for convenient shell scripts containing a #/bin/stx as the first line." |
|
16087 | 707 |
|
20911 | 708 |
exitAction := [^ self]. |
709 |
||
16087 | 710 |
[ |
20769 | 711 |
|lines chunk| |
16087 | 712 |
|
20769 | 713 |
prompt notNil ifTrue:[ |
714 |
error nextPutAll:prompt. |
|
715 |
]. |
|
16087 | 716 |
|
20769 | 717 |
input atEnd ifTrue:[ |
20912 | 718 |
doPrint ifTrue:[ error cr ]. |
20769 | 719 |
^ self. |
720 |
]. |
|
16087 | 721 |
|
20769 | 722 |
input peek == $# ifTrue:[ |
723 |
self doChunkFormat:false. |
|
724 |
]. |
|
16087 | 725 |
|
20769 | 726 |
self doChunkFormat ifTrue:[ |
727 |
input skipSeparators. |
|
728 |
chunk := input nextChunk. |
|
729 |
] ifFalse:[ |
|
730 |
lines := OrderedCollection new. |
|
731 |
[ |
|
732 |
|line| |
|
16087 | 733 |
|
20769 | 734 |
line := input nextLine. |
735 |
line notEmptyOrNil ifTrue:[ |
|
736 |
line = '?' ifTrue:[ |
|
737 |
self cmd_help:nil. |
|
738 |
prompt notNil ifTrue:[ |
|
739 |
error nextPutAll:prompt. |
|
740 |
]. |
|
741 |
] ifFalse:[ |
|
742 |
(line startsWith:'#') ifTrue:[ |
|
743 |
self directive:line. |
|
744 |
prompt notNil ifTrue:[ |
|
745 |
error nextPutAll:prompt. |
|
746 |
]. |
|
747 |
] ifFalse:[ |
|
748 |
lines add:line. |
|
749 |
] |
|
750 |
] |
|
751 |
]. |
|
752 |
line notEmptyOrNil and:[(line endsWith:$.) not]. |
|
753 |
] whileTrue. |
|
754 |
chunk := lines asStringWith:Character cr. |
|
755 |
]. |
|
16087 | 756 |
|
20890 | 757 |
(chunk notEmptyOrNil |
758 |
and:[chunk withoutSeparators notEmpty |
|
759 |
and:[chunk withoutSeparators ~= '.']] |
|
760 |
) ifTrue:[ |
|
20769 | 761 |
"abortAll is handled, but not asked for here!!" |
762 |
AbortAllOperationRequest handle:[:ex | |
|
763 |
error nextPutLine:('Evaluation aborted: ', ex description) |
|
764 |
] do:[ |
|
765 |
(Error, ControlInterrupt) handle:[:ex | |
|
766 |
prompt isNil ifTrue:[ |
|
767 |
ex reject |
|
768 |
]. |
|
769 |
MiniDebugger enterWithMessage:(ex errorString) mayProceed:true. |
|
770 |
ex mayProceed ifTrue:[ |
|
771 |
ex proceed. |
|
772 |
]. |
|
773 |
error nextPutLine:('Evaluation aborted: ', ex description). |
|
774 |
ex return. |
|
775 |
] do:[ |
|
776 |
|value ms us| |
|
16087 | 777 |
|
20771 | 778 |
profilingFlag == true ifTrue:[ |
779 |
MessageTally spyDetailedOn:[ |
|
780 |
value := (compilerClass new requestor:self) |
|
781 |
evaluate:chunk |
|
782 |
compile:true. |
|
783 |
]. |
|
784 |
doPrint ifTrue:[ |
|
785 |
value printOn:output. output cr. |
|
786 |
]. |
|
787 |
] ifFalse:[ |
|
788 |
us := Time microsecondsToRun:[ |
|
789 |
value := (compilerClass new requestor:self) |
|
790 |
evaluate:chunk compile:true. |
|
791 |
]. |
|
792 |
doPrint ifTrue:[ |
|
793 |
value printOn:output. output cr. |
|
794 |
]. |
|
16087 | 795 |
|
20771 | 796 |
timingFlag == true ifTrue:[ |
797 |
'execution time: ' printOn:error. |
|
798 |
us < 1000 ifTrue:[ |
|
799 |
us < 1 ifTrue:[ |
|
800 |
'too small to measure (<1us)' printOn:error. |
|
801 |
] ifFalse:[ |
|
802 |
us printOn:output. 'us' printOn:error. |
|
803 |
] |
|
20769 | 804 |
] ifFalse:[ |
20771 | 805 |
((us / 1000) asFixedPoint:2) printOn:output. 'ms' printOn:error. |
806 |
]. |
|
807 |
error cr. |
|
20769 | 808 |
]. |
809 |
]. |
|
810 |
Workspace notNil ifTrue:[ |
|
811 |
Workspace rememberResultAsWorkspaceVariable:value. |
|
812 |
]. |
|
813 |
]. |
|
814 |
]. |
|
815 |
]. |
|
16087 | 816 |
] loop. |
817 |
||
818 |
" |
|
819 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
|
820 |
" |
|
821 |
||
822 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
823 |
"Modified: / 06-12-2011 / 15:29:03 / cg" |
|
824 |
! |
|
825 |
||
10463 | 826 |
readEvalPrintLoop |
827 |
"{ Pragma: +optSpace }" |
|
828 |
||
829 |
"simple read-eval-print loop for non-graphical Minitalk. |
|
830 |
If the chunkFormat-argument is true, chunks are read. |
|
16087 | 831 |
Otherwise, lines up to an empty line (or EOF) are read. |
832 |
A '#' character appearing in the first column of the first line |
|
833 |
switches to chunkmode." |
|
10463 | 834 |
|
835 |
ControlInterrupt handle:[:ex | |
|
20774 | 836 |
self errorStream nextPutLine:('Caught: ', ex description). |
20833 | 837 |
self inputStream atEnd ifTrue:[ |
838 |
ex return. |
|
839 |
]. |
|
20933 | 840 |
MiniDebugger enter. |
841 |
ex proceed. |
|
842 |
"/ ex restart. |
|
10463 | 843 |
] do:[ |
20774 | 844 |
|input output error compilerClass| |
845 |
||
846 |
"/ re-evaluate these in the loop, so they can be changed dynamically |
|
847 |
input := self inputStream. |
|
848 |
output := self outputStream. |
|
849 |
error := self errorStream. |
|
10463 | 850 |
|
20774 | 851 |
compilerClass := compiler ? Compiler ? Parser. |
852 |
compilerClass isNil ifTrue:[ |
|
853 |
self errorStream nextPutLine:('oops - no Compiler class found'). |
|
854 |
^ self. |
|
855 |
]. |
|
856 |
StreamError handle:[:ex | |
|
857 |
(input isOpen not or:[input atEnd]) ifTrue:[ |
|
858 |
error nextPutLine:'EOF on input'. |
|
20833 | 859 |
ex return. |
20774 | 860 |
]. |
861 |
(output isOpen not) ifTrue:[ |
|
862 |
error nextPutLine:'no output'. |
|
863 |
]. |
|
864 |
(error isOpen not) ifTrue:[ |
|
865 |
]. |
|
866 |
] do:[ |
|
867 |
input signalAtEnd:true. |
|
868 |
self |
|
869 |
basicReadEvalPrintLoopWithInput:input output:output error:error |
|
870 |
compiler:compilerClass prompt:prompt print:(printFlag ? true). |
|
871 |
] |
|
20933 | 872 |
]. |
873 |
self errorStream nextPutLine:('done.'). |
|
10463 | 874 |
|
875 |
" |
|
20933 | 876 |
Stdin atEnd |
877 |
Stdin clearEOF |
|
878 |
Smalltalk readEvalPrintLoop |
|
10463 | 879 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
880 |
" |
|
881 |
||
882 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
20933 | 883 |
"Modified (comment): / 08-11-2016 / 22:04:03 / cg" |
10248 | 884 |
! ! |
885 |
||
16089 | 886 |
!ReadEvalPrintLoop methodsFor:'queries'! |
887 |
||
888 |
autoDefineVariables |
|
889 |
"when evaluating with --eval, auto define any variables" |
|
890 |
||
891 |
^ #workspace |
|
892 |
! ! |
|
893 |
||
10248 | 894 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
895 |
||
896 |
version |
|
19379 | 897 |
^ '$Header$' |
13121
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
898 |
! |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
899 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
900 |
version_CVS |
19379 | 901 |
^ '$Header$' |
10248 | 902 |
! ! |
19379 | 903 |