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