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