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