author | Claus Gittinger <cg@exept.de> |
Mon, 07 May 2018 08:47:43 +0200 | |
changeset 22759 | 21f54342dfc5 |
parent 22758 | c6bcf9b6e89a |
child 22760 | a86e884424b7 |
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 |
22746 | 22 |
editorCommand confirmDebugger noDebugger debuggerUsed |
23 |
returnValuePrinting' |
|
16228 | 24 |
classVariableNames:'' |
25 |
poolDictionaries:'' |
|
26 |
category:'System-Support' |
|
10248 | 27 |
! |
28 |
||
10249 | 29 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
30 |
||
31 |
copyright |
|
32 |
" |
|
33 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
16565 | 34 |
All Rights Reserved |
10249 | 35 |
|
36 |
This software is furnished under a license and may be used |
|
37 |
only in accordance with the terms of that license and with the |
|
38 |
inclusion of the above copyright notice. This software may not |
|
39 |
be provided or otherwise made available to, or used by, any |
|
40 |
other person. No title to or ownership of the software is |
|
41 |
hereby transferred. |
|
42 |
" |
|
10461
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 |
|
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
45 |
documentation |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
46 |
" |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
47 |
A simple read-eval-print loop for non-GUI or stscript operation. |
22735 | 48 |
Invoked, for example if stx is started with a --repl argument, |
49 |
or by the MiniDebugger with the 'I' command. |
|
20833 | 50 |
|
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
51 |
A line starting with '?' shows the usage message. |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
52 |
Lines starting with '#' are directives: |
22576 | 53 |
#exit - exit the rep-loop |
54 |
#show ... - show various infos |
|
55 |
#use package - show various infos |
|
20833 | 56 |
type '?' to see more. |
57 |
||
16087 | 58 |
The input can be in one of two formats: |
20833 | 59 |
1) traditional chunk format (bang-separated chunks, bangs duplicated) |
60 |
this is the traditional fileIn format, as generated by fileOut from the browser |
|
16087 | 61 |
|
20833 | 62 |
2) interactive line mode. Chunks are any number of lines up to either an empty line or |
63 |
a line ending in a period. This is more useful for an interactive REPL, where statements/expressions |
|
64 |
are entered linewise by a user. |
|
16087 | 65 |
|
66 |
The input can is switched to non-chunk format whenever a line with a '#' in the first column appears. |
|
20833 | 67 |
|
20933 | 68 |
Try it (but only if you have a console): |
69 |
Smalltalk readEvalPrintLoop |
|
70 |
||
20833 | 71 |
[Author:] |
72 |
Claus Gittinger |
|
10461
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
73 |
" |
10249 | 74 |
! ! |
10248 | 75 |
|
76 |
!ReadEvalPrintLoop methodsFor:'accessing'! |
|
77 |
||
10463 | 78 |
compiler:something |
16087 | 79 |
"assign a compiler to use;could be used to change the language" |
80 |
||
10463 | 81 |
compiler := something. |
10248 | 82 |
! |
83 |
||
22735 | 84 |
confirmDebugger |
85 |
"true if the user is asked for a debugger in case of errors" |
|
86 |
||
87 |
^ confirmDebugger ? true |
|
88 |
! |
|
89 |
||
90 |
confirmDebugger:aBoolean |
|
91 |
"true if the user is asked for a debugger in case of errors" |
|
92 |
||
93 |
confirmDebugger := aBoolean |
|
94 |
! |
|
95 |
||
96 |
debuggerUsed |
|
97 |
"by default, the miniDebugger is given control in case of an error; |
|
98 |
you may want to write (subclass) your own ;-)" |
|
99 |
||
100 |
^ debuggerUsed ? MiniDebugger |
|
101 |
! |
|
102 |
||
103 |
debuggerUsed:aDebuggerClass |
|
104 |
"by default, the miniDebugger is given control in case of an error; |
|
105 |
you may want to write (subclass) your own ;-)" |
|
106 |
||
107 |
debuggerUsed := aDebuggerClass |
|
108 |
! |
|
109 |
||
10632 | 110 |
doChunkFormat |
16087 | 111 |
"true if currently reading chunk format" |
112 |
||
10632 | 113 |
^ doChunkFormat ? true |
114 |
||
115 |
"Created: / 07-12-2006 / 18:24:04 / cg" |
|
116 |
! |
|
117 |
||
16087 | 118 |
doChunkFormat:aBoolean |
119 |
"enable/disable chunk format" |
|
120 |
||
121 |
doChunkFormat := aBoolean. |
|
10254 | 122 |
|
123 |
"Created: / 07-12-2006 / 18:24:04 / cg" |
|
10248 | 124 |
! |
125 |
||
20935 | 126 |
editorCommand |
21934 | 127 |
"the editor command to use with the #edit directive. |
128 |
Uses the STX_EDITOR or EDITOR shell variables' value, if defined; |
|
129 |
if not, the value in the classvar EDITORCOMMAND if non-nil; |
|
130 |
otheriwse vi (notepad on windows)" |
|
131 |
||
20935 | 132 |
|editor| |
133 |
||
134 |
(editor := editorCommand) isNil ifTrue:[ |
|
135 |
editor := OperatingSystem getEnvironment:'STX_EDITOR'. |
|
136 |
editor isNil ifTrue:[ |
|
137 |
editor := OperatingSystem getEnvironment:'EDITOR'. |
|
138 |
editor isNil ifTrue:[ |
|
139 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
|
140 |
editor := 'notepad'. |
|
141 |
] ifFalse:[ |
|
142 |
editor := 'vi'. |
|
143 |
]. |
|
144 |
]. |
|
145 |
]. |
|
146 |
]. |
|
147 |
^ editor |
|
148 |
||
149 |
"Created: / 08-11-2016 / 22:45:22 / cg" |
|
21934 | 150 |
"Modified: / 24-06-2017 / 09:54:10 / cg" |
20935 | 151 |
! |
152 |
||
16087 | 153 |
error:aStream |
154 |
"assign an error stream" |
|
155 |
||
156 |
errorStream := aStream. |
|
10248 | 157 |
|
158 |
"Created: / 07-12-2006 / 17:33:39 / cg" |
|
159 |
! |
|
160 |
||
10463 | 161 |
errorStream |
16087 | 162 |
"return the current error stream" |
163 |
||
19379 | 164 |
errorStream notNil ifTrue:[^ errorStream]. |
165 |
^ Processor activeProcess stderr |
|
10256 | 166 |
|
19379 | 167 |
"Created: / 07-12-2006 / 19:12:27 / cg" |
10256 | 168 |
! |
169 |
||
16087 | 170 |
input:aStream |
171 |
"assign an input stream" |
|
172 |
||
173 |
inputStream := aStream asLineNumberReadStream. |
|
10248 | 174 |
|
175 |
"Modified: / 07-12-2006 / 17:33:31 / cg" |
|
176 |
! |
|
177 |
||
10463 | 178 |
inputStream |
16087 | 179 |
"get the current input stream" |
180 |
||
19379 | 181 |
inputStream notNil ifTrue:[^ inputStream]. |
182 |
^ Processor activeProcess stdin |
|
10463 | 183 |
|
184 |
"Created: / 07-12-2006 / 19:12:13 / cg" |
|
10256 | 185 |
! |
186 |
||
22746 | 187 |
noDebugger |
188 |
"true if no debugger should be activated on errors (aborts)" |
|
189 |
||
190 |
^ noDebugger ? false |
|
191 |
! |
|
192 |
||
193 |
noDebugger:aBoolean |
|
194 |
"true if no debugger should be activated on errors (aborts)" |
|
195 |
||
196 |
noDebugger := aBoolean |
|
197 |
! |
|
198 |
||
16087 | 199 |
output:aStream |
200 |
"assign an output stream" |
|
201 |
||
202 |
outputStream := aStream. |
|
10248 | 203 |
|
204 |
"Created: / 07-12-2006 / 17:27:48 / cg" |
|
205 |
! |
|
206 |
||
10463 | 207 |
outputStream |
19379 | 208 |
"return the current output stream" |
16087 | 209 |
|
19379 | 210 |
outputStream notNil ifTrue:[^ outputStream]. |
211 |
^ Processor activeProcess stdout |
|
10463 | 212 |
|
213 |
"Created: / 07-12-2006 / 19:12:27 / cg" |
|
214 |
! |
|
215 |
||
22743 | 216 |
printFlag |
217 |
"true if the return value of expressions should be printed" |
|
218 |
||
219 |
^ printFlag ? true |
|
220 |
! |
|
221 |
||
222 |
printFlag:aBoolean |
|
223 |
"true if the return value of expressions should be printed" |
|
224 |
||
225 |
printFlag := aBoolean |
|
226 |
! |
|
227 |
||
16087 | 228 |
prompt:aString |
229 |
"set the prompt" |
|
230 |
||
231 |
prompt := aString. |
|
10248 | 232 |
! ! |
233 |
||
10631 | 234 |
!ReadEvalPrintLoop methodsFor:'compiler interface-error handling'! |
235 |
||
236 |
correctableError:message position:pos1 to:pos2 from:aCompiler |
|
237 |
"compiler notifies us of an error - ignore it" |
|
238 |
||
15702 | 239 |
^ false "/ no correction |
10631 | 240 |
! |
241 |
||
242 |
correctableSelectorWarning:aString position:relPos to:relEndPos from:aCompiler |
|
243 |
"compiler notifies us of a warning - ignore it" |
|
244 |
||
245 |
^ false |
|
246 |
! |
|
247 |
||
13121
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
248 |
correctableWarning:message position:pos1 to:pos2 from:aCompiler |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
249 |
"compiler notifies us of an error - ignore it" |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
250 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
251 |
^ false |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
252 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
253 |
"Created: / 02-11-2010 / 13:29:22 / cg" |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
254 |
! |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
255 |
|
10631 | 256 |
error:aString position:relPos to:relEndPos from:aCompiler |
257 |
"compiler notifies us of a warning - ignore it" |
|
258 |
||
22758 | 259 |
self errorStream nextPutLine:('Syntax Error: ',aString). |
10631 | 260 |
^ false |
261 |
! |
|
262 |
||
263 |
unusedVariableWarning:aString position:relPos to:relEndPos from:aCompiler |
|
264 |
"compiler notifies us of a warning - ignore it" |
|
265 |
||
266 |
^ false |
|
267 |
! |
|
268 |
||
269 |
warning:aString position:relPos to:relEndPos from:aCompiler |
|
270 |
"compiler notifies us of a warning - ignore it" |
|
271 |
||
272 |
^ self |
|
273 |
! ! |
|
274 |
||
10255 | 275 |
!ReadEvalPrintLoop methodsFor:'directives'! |
276 |
||
22735 | 277 |
askYesNo:message |
278 |
self errorStream show:message. |
|
279 |
^ (self inputStream nextLine withoutSeparators startsWith:'y'). |
|
280 |
! |
|
281 |
||
282 |
cmd_apropos:lineStream |
|
283 |
"apropos directive; i.e. |
|
284 |
#apropos collection [;more] |
|
285 |
" |
|
286 |
||
287 |
|words classNamesMatching selectorsMatching showList| |
|
288 |
||
289 |
lineStream skipSeparators. |
|
290 |
words := lineStream upToEnd asCollectionOfSubstringsSeparatedBy:$;. |
|
291 |
words := words select:[:each | each notEmpty]. |
|
292 |
words := words select:[:each | each isBlank not]. |
|
293 |
||
294 |
(words isEmpty) ifTrue:[ |
|
295 |
self errorStream showCR:'? usage: #apropos <word> [; morewords]'. |
|
296 |
^ self. |
|
297 |
]. |
|
298 |
||
299 |
"/ search in classes: |
|
300 |
classNamesMatching := Smalltalk allClasses |
|
301 |
select:[:cls | |
|
302 |
cls isPrivate not |
|
303 |
and:[ words conform:[:word | |
|
304 |
cls name matches:word caseSensitive:false]]] |
|
305 |
thenCollect:#name. |
|
306 |
"/ search in method names: |
|
307 |
selectorsMatching := (Smalltalk allClasses |
|
308 |
collectAll:[:cls | |
|
309 |
cls isPrivate |
|
310 |
ifTrue:[#()] |
|
311 |
ifFalse:[ |
|
312 |
cls selectors |
|
313 |
select:[:sel | |
|
314 |
words conform:[:word | |
|
315 |
sel matches:word caseSensitive:false]]]] |
|
316 |
) asSet. |
|
317 |
||
318 |
showList := |
|
319 |
[:list :listName | |
|
320 |
|showIt sortedList longest limit numCols| |
|
321 |
||
322 |
showIt := true. |
|
323 |
list notEmpty ifTrue:[ |
|
324 |
list size > 20 ifTrue:[ |
|
325 |
showIt := self askYesNo:( |
|
326 |
'apropos: there are %1 matching %2; list them all (y/n)? ' |
|
327 |
bindWith:list size |
|
328 |
with:listName) |
|
329 |
] ifFalse:[ |
|
330 |
self errorStream showCR:'matching %1:' with:listName. |
|
331 |
]. |
|
332 |
showIt ifTrue:[ |
|
333 |
sortedList := list asOrderedCollection sort. |
|
334 |
longest := (list collect:[:nm | nm size]) max. |
|
335 |
limit := 78. |
|
336 |
numCols := (80 // (longest min:limit)) max:1. |
|
337 |
sortedList slicesOf:numCols do:[:eachGroupOfN | |
|
338 |
self errorStream |
|
339 |
spaces:2; |
|
340 |
nextPutLine:( |
|
341 |
(eachGroupOfN |
|
342 |
collect:[:nm | |
|
343 |
(nm contractTo:limit) paddedTo:limit |
|
344 |
] |
|
345 |
) asStringWith:' '). |
|
346 |
]. |
|
347 |
]. |
|
348 |
]. |
|
349 |
]. |
|
350 |
||
351 |
showList value:classNamesMatching value:'classes'. |
|
352 |
showList value:selectorsMatching value:'method names'. |
|
353 |
" |
|
354 |
self basicNew |
|
355 |
input:Stdin; |
|
356 |
cmd_apropos:'Array' readStream |
|
357 |
||
358 |
self basicNew |
|
359 |
input:Stdin; |
|
360 |
cmd_apropos:'at:' readStream |
|
361 |
||
362 |
self basicNew |
|
363 |
input:Stdin; |
|
364 |
cmd_apropos:'*at:' readStream |
|
365 |
" |
|
366 |
! |
|
367 |
||
10463 | 368 |
cmd_clear:lineStream |
10255 | 369 |
self cmd_setOrClear:lineStream to:false |
370 |
||
371 |
"Created: / 07-12-2006 / 19:04:50 / cg" |
|
372 |
! |
|
373 |
||
20900 | 374 |
cmd_debug:lineStream |
375 |
MiniDebugger enter. |
|
376 |
! |
|
377 |
||
378 |
cmd_edit:lineStream |
|
20935 | 379 |
"edit a class or selector in an external editor" |
20900 | 380 |
|
20935 | 381 |
|errStream classOrMethodName cls methodName selector |
20906 | 382 |
code isNewClass editFullClass tmpFile modifiedTime| |
20900 | 383 |
|
384 |
errStream := self errorStream. |
|
385 |
||
20906 | 386 |
isNewClass := editFullClass := false. |
20900 | 387 |
|
388 |
lineStream skipSeparators. |
|
20926 | 389 |
lineStream atEnd ifTrue:[ |
390 |
cls := lastEditedClass. |
|
391 |
methodName := lastEditedSelector. |
|
392 |
] ifFalse:[ |
|
393 |
classOrMethodName := lineStream |
|
394 |
upToElementForWhich:[:ch | |
|
395 |
ch isLetterOrDigit not and:[ch ~~ $_] |
|
396 |
]. |
|
397 |
"/ |
|
398 |
(classOrMethodName isUppercaseFirst) ifTrue:[ |
|
399 |
(cls := Smalltalk classNamed:classOrMethodName) isNil ifTrue:[ |
|
400 |
errStream show:'edit: no such class: ',classOrMethodName,' ; create (y/n)? '. |
|
401 |
(self inputStream nextLine withoutSeparators startsWith:'y') ifFalse:[^ self]. |
|
402 |
isNewClass := true. |
|
403 |
code := |
|
404 |
'"/ change the code as required, then save and exit the editor. |
|
405 |
"/ To cancel this edit, leave the editor WITHOUT saving. |
|
406 |
"/ |
|
407 |
||
408 |
Object |
|
20900 | 409 |
subclass:#%1 |
410 |
instanceVariableNames:'''' |
|
411 |
classVariableNames:'''' |
|
412 |
poolDictionaries:'''' |
|
413 |
category:''user classes'' |
|
414 |
' bindWith:classOrMethodName. |
|
20926 | 415 |
] ifFalse:[ |
416 |
lineStream skipSeparators. |
|
417 |
lineStream atEnd ifFalse:[ |
|
418 |
methodName := lineStream upToSeparator. |
|
22735 | 419 |
methodName = 'class' ifTrue:[ |
420 |
cls := cls theMetaclass. |
|
421 |
lineStream skipSeparators. |
|
422 |
methodName := lineStream upToSeparator. |
|
423 |
]. |
|
20926 | 424 |
]. |
20900 | 425 |
]. |
20926 | 426 |
] ifFalse:[ |
427 |
methodName := classOrMethodName |
|
20900 | 428 |
]. |
429 |
]. |
|
20926 | 430 |
|
20900 | 431 |
isNewClass ifFalse:[ |
432 |
cls := cls ? lastEditedClass. |
|
433 |
cls isNil ifTrue:[ |
|
20923 | 434 |
errStream showCR:'edit usage:'. |
435 |
errStream showCR:' #edit className selector'. |
|
436 |
errStream showCR:' #edit className '. |
|
437 |
errStream showCR:' #edit selector (class as in previous edit)'. |
|
20926 | 438 |
errStream showCR:' #edit (class/method as in previous edit)'. |
20900 | 439 |
^ self. |
440 |
]. |
|
441 |
lastEditedClass := cls. |
|
20926 | 442 |
lastEditedSelector := methodName. |
443 |
||
20906 | 444 |
methodName isNil ifTrue:[ |
445 |
editFullClass := true. |
|
446 |
code := cls source asString |
|
447 |
] ifFalse:[ |
|
448 |
((selector := methodName asSymbolIfInterned) isNil |
|
449 |
or:[ (cls implements:selector) not]) ifTrue:[ |
|
22735 | 450 |
(self askYesNo:('"',methodName,'" is a new method; create (y/n)? ')) ifFalse:[^ self]. |
451 |
||
20926 | 452 |
code := |
453 |
'"/ change the code as required, then save and exit the editor. |
|
454 |
"/ To cancel this edit, leave the editor WITHOUT saving. |
|
455 |
||
20900 | 456 |
%1 |
457 |
"this is a new method" |
|
458 |
self halt |
|
20906 | 459 |
' bindWith:methodName. |
460 |
] ifFalse:[ |
|
20926 | 461 |
code := (cls compiledMethodAt:selector) source. |
20906 | 462 |
]. |
20900 | 463 |
]. |
464 |
]. |
|
465 |
||
20906 | 466 |
[ |
20936 | 467 |
|ok cmd| |
20933 | 468 |
|
20906 | 469 |
tmpFile := Filename newTemporary. |
470 |
tmpFile contents:code. |
|
471 |
modifiedTime := tmpFile modificationTime. |
|
20933 | 472 |
|
20936 | 473 |
cmd := '%1 "%2"'. |
474 |
OperatingSystem isUNIXlike ifTrue:[ |
|
475 |
cmd := '%1 "%2" </dev/tty'. |
|
476 |
]. |
|
477 |
||
20933 | 478 |
ok := OperatingSystem |
20936 | 479 |
executeCommand:(cmd bindWith:(self editorCommand) with:tmpFile pathName) |
480 |
inputFrom:Stdin |
|
481 |
outputTo:Stdout |
|
482 |
errorTo:Stderr |
|
20933 | 483 |
auxFrom:nil |
484 |
environment:nil |
|
485 |
inDirectory:nil |
|
486 |
lineWise:false |
|
487 |
newPgrp:false |
|
488 |
showWindow:true |
|
489 |
onError:[:status | false]. |
|
490 |
||
491 |
(ok and:[tmpFile modificationTime ~= modifiedTime]) ifTrue:[ |
|
20906 | 492 |
isNewClass ifTrue:[ |
493 |
Compiler evaluate:tmpFile contentsOfEntireFile. |
|
20937 | 494 |
errStream showCR:'Class (re)defined.' |
20906 | 495 |
] ifFalse:[ |
496 |
editFullClass ifTrue:[ |
|
497 |
tmpFile fileIn. |
|
20937 | 498 |
errStream showCR:'Class (re)compiled.' |
20906 | 499 |
] ifFalse:[ |
500 |
cls compile:tmpFile contentsOfEntireFile classified:'*as yet uncategorized'. |
|
20937 | 501 |
errStream showCR:'Method (re)compiled.' |
20906 | 502 |
]. |
503 |
]. |
|
20937 | 504 |
] ifFalse:[ |
505 |
errStream showCR:'No change.' |
|
506 |
]. |
|
20906 | 507 |
] ensure:[ |
508 |
tmpFile notNil ifTrue:[ |
|
509 |
tmpFile remove |
|
510 |
] |
|
20900 | 511 |
]. |
20906 | 512 |
|
20900 | 513 |
" |
20934 | 514 |
Smalltalk readEvalPrintLoop |
515 |
||
20900 | 516 |
self new |
517 |
input:Stdin; |
|
518 |
cmd_edit:'MyClass foo' readStream |
|
22735 | 519 |
|
520 |
self new |
|
521 |
input:Stdin; |
|
522 |
cmd_edit:'Array class new:' readStream |
|
20900 | 523 |
" |
20933 | 524 |
|
20935 | 525 |
"Modified: / 08-11-2016 / 22:46:12 / cg" |
20900 | 526 |
! |
527 |
||
10463 | 528 |
cmd_exit:lineStream |
22576 | 529 |
"exit directive - leaves the repl" |
530 |
||
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
531 |
exitAction value |
10463 | 532 |
|
533 |
"Created: / 07-12-2006 / 18:55:46 / cg" |
|
10255 | 534 |
! |
535 |
||
10463 | 536 |
cmd_help:lineStream |
537 |
self errorStream |
|
20771 | 538 |
nextPutAll: |
16084 | 539 |
'Everything entered up to an empty line or a line ending in "." is called a "chunk" and evaluated. |
22742 | 540 |
Lines starting with "#" (in the first column) are commands to the read-eval-print interpreter. |
10463 | 541 |
|
542 |
Valid commands are: |
|
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
543 |
#help ............... this text |
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
544 |
#exit ............... exit interpreter loop |
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
545 |
#use <package>....... use (load) a package |
20771 | 546 |
stx:libwidg .............. GUI package |
547 |
stx:libtool .............. IDE tool package |
|
548 |
stx:goodies/regex ........ regex package |
|
549 |
stx:goodies/petitparser .. peg parser package |
|
16087 | 550 |
#read <filename>..... read another script or source file |
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
551 |
#show <what> ........ show info |
20771 | 552 |
variables .......... interpreter variables |
553 |
processes .......... processes |
|
554 |
memory ............. memory usage |
|
555 |
flags .............. flags |
|
556 |
modules ............ loaded modules |
|
20900 | 557 |
packages ........... available packages to load |
20834 | 558 |
all ................ all of the above |
10635
c3c4fc186068
changed #interpreterLoopWith:
Claus Gittinger <cg@exept.de>
parents:
10632
diff
changeset
|
559 |
#set/clear <flag> ... set or clear a flag |
22754 | 560 |
print .............. print return values |
561 |
nodebug ............ no debugger on error |
|
562 |
confirmdebug ....... ask for debugger on error |
|
20771 | 563 |
trace .............. tracing execution |
564 |
timing ............. timing execution |
|
565 |
profiling .......... show execution profile |
|
566 |
chunkFormat ........ traditional bang chunk format input mode |
|
20935 | 567 |
editor ............. command used with #edit directive |
20900 | 568 |
#debug ................. enter a MiniDebugger |
20935 | 569 |
#edit <what> ........ open an external editor |
570 |
class .............. on a class |
|
571 |
class selector ..... on a method |
|
572 |
<empty> ............ on previously edited method/last class |
|
22735 | 573 |
#apropos word ....... list classes/selectors matching word |
574 |
#list <what> ........ show source |
|
575 |
class .............. class definition and comment |
|
576 |
class selector ..... method source |
|
10257 | 577 |
|
16087 | 578 |
The MiniDebugger (if entered) shows its own help with "?". |
10255 | 579 |
' |
580 |
||
581 |
"Created: / 07-12-2006 / 18:54:20 / cg" |
|
20935 | 582 |
"Modified: / 08-11-2016 / 22:53:53 / cg" |
10255 | 583 |
! |
584 |
||
22735 | 585 |
cmd_list:lineStream |
586 |
"list directive; i.e. |
|
587 |
#list <classname> ['class'] <selector> |
|
588 |
" |
|
589 |
||
590 |
|class selector source errStream| |
|
591 |
||
592 |
errStream := self errorStream. |
|
593 |
||
594 |
(self |
|
595 |
getClassNameAndSelectorFrom:lineStream |
|
596 |
into:[:classArg :selectorArg | |
|
597 |
class := classArg. |
|
598 |
selector := selectorArg. |
|
599 |
]) ifFalse:[^ self]. |
|
600 |
||
601 |
selector isNil ifTrue:[ |
|
602 |
errStream nextPutAll:(class definition). |
|
603 |
errStream nextPutAll:(class commentOrDocumentationString). |
|
604 |
] ifFalse:[ |
|
605 |
source := class sourceCodeAt:selector asSymbol. |
|
606 |
source isEmptyOrNil ifTrue:[ |
|
607 |
errStream nextPutLine:'Sorry, no sourcecode found' |
|
608 |
] ifFalse:[ |
|
609 |
errStream nextPutAll:source |
|
610 |
]. |
|
611 |
]. |
|
612 |
||
613 |
" |
|
614 |
self basicNew |
|
615 |
input:Stdin; |
|
616 |
cmd_list:'Array' readStream |
|
617 |
||
618 |
self basicNew |
|
619 |
input:Stdin; |
|
620 |
cmd_list:'Array at:put:' readStream |
|
621 |
" |
|
622 |
! |
|
623 |
||
16087 | 624 |
cmd_read:lineStream |
22577 | 625 |
"read directive; i.e. |
626 |
#read scriptFile |
|
627 |
" |
|
628 |
||
20912 | 629 |
|filename newInput savedPrompt savedPrint savedInput savedCurrentDirectory savedDoChunkFormat| |
16087 | 630 |
|
631 |
lineStream skipSeparators. |
|
632 |
filename := lineStream upToEnd withoutSeparators. |
|
22577 | 633 |
filename isEmptyOrNil ifTrue:[ |
634 |
self errorStream showCR:'? usage: #read <filename>'. |
|
20909 | 635 |
^ self. |
16133 | 636 |
]. |
16087 | 637 |
|
20915 | 638 |
currentDirectory := currentDirectory ? (Filename currentDirectory). |
639 |
||
16087 | 640 |
filename := filename asFilename. |
641 |
filename isAbsolute ifFalse:[ |
|
20909 | 642 |
filename := currentDirectory construct:filename. |
16087 | 643 |
]. |
644 |
||
20922 | 645 |
StreamError ignoreIn:[ |
646 |
newInput := filename readStream. |
|
647 |
]. |
|
16087 | 648 |
newInput isNil ifTrue:[ |
20923 | 649 |
self errorStream showCR:('Could not find file: "',filename pathName,'"'). |
20909 | 650 |
^ self. |
16087 | 651 |
]. |
652 |
||
653 |
[ |
|
20909 | 654 |
savedCurrentDirectory := currentDirectory. |
20912 | 655 |
savedDoChunkFormat := doChunkFormat. |
20909 | 656 |
savedInput := inputStream. |
657 |
savedPrint := printFlag. |
|
658 |
savedPrompt := prompt. |
|
16087 | 659 |
|
20909 | 660 |
currentDirectory := filename directory. |
661 |
inputStream := newInput. |
|
20910 | 662 |
|
20909 | 663 |
self |
664 |
basicReadEvalPrintLoopWithInput:newInput |
|
665 |
output:outputStream |
|
666 |
error:errorStream |
|
20910 | 667 |
compiler:(compiler ? Compiler ? Parser) |
20909 | 668 |
prompt:false |
669 |
print:false. |
|
16087 | 670 |
] ensure:[ |
20914 | 671 |
newInput close. |
20912 | 672 |
doChunkFormat := savedDoChunkFormat. |
20909 | 673 |
currentDirectory := savedCurrentDirectory. |
674 |
inputStream := savedInput. |
|
675 |
printFlag := savedPrint. |
|
20912 | 676 |
prompt := savedPrompt. |
16087 | 677 |
]. |
678 |
! |
|
679 |
||
10463 | 680 |
cmd_set:lineStream |
10255 | 681 |
self cmd_setOrClear:lineStream to:true |
682 |
||
683 |
"Modified: / 07-12-2006 / 19:04:46 / cg" |
|
684 |
! |
|
685 |
||
10463 | 686 |
cmd_setOrClear:lineStream to:aBoolean |
22735 | 687 |
"set/clear one of my internal flags" |
688 |
||
22753 | 689 |
|what param flag| |
690 |
||
691 |
flag := aBoolean. |
|
10463 | 692 |
|
693 |
lineStream skipSeparators. |
|
694 |
what := lineStream nextAlphaNumericWord. |
|
22753 | 695 |
flag ifTrue:[ |
696 |
lineStream atEnd ifFalse:[ |
|
697 |
lineStream skipSeparators. |
|
698 |
param := lineStream nextAlphaNumericWord. |
|
699 |
flag := (param startsWith: 'f') not. |
|
700 |
]. |
|
701 |
]. |
|
702 |
||
16133 | 703 |
what notNil ifTrue:[ |
20771 | 704 |
(what startsWith:'tra') ifTrue:[ |
22753 | 705 |
traceFlag := flag. |
20771 | 706 |
^ self. |
707 |
]. |
|
708 |
(what startsWith:'tim') ifTrue:[ |
|
22753 | 709 |
timingFlag := flag. |
20771 | 710 |
^ self. |
711 |
]. |
|
712 |
(what startsWith:'pro') ifTrue:[ |
|
22753 | 713 |
profilingFlag := flag. |
20771 | 714 |
^ self. |
715 |
]. |
|
716 |
(what startsWith:'chunk') ifTrue:[ |
|
22753 | 717 |
doChunkFormat := flag. |
20771 | 718 |
^ self. |
719 |
]. |
|
20935 | 720 |
(what startsWith:'edi') ifTrue:[ |
22753 | 721 |
flag ifTrue:[ |
20935 | 722 |
"/ set editor cmd |
723 |
lineStream skipSeparators. |
|
724 |
editorCommand := lineStream upToEnd. |
|
725 |
] ifFalse:[ |
|
726 |
editorCommand := nil. |
|
20938 | 727 |
]. |
728 |
^ self. |
|
20935 | 729 |
]. |
22735 | 730 |
(what startsWith:'con') ifTrue:[ |
22753 | 731 |
confirmDebugger := flag. |
22735 | 732 |
^ self. |
733 |
]. |
|
22747 | 734 |
(what startsWith:'node') ifTrue:[ |
22753 | 735 |
noDebugger := flag. |
22746 | 736 |
^ self. |
737 |
]. |
|
22743 | 738 |
(what startsWith:'pri') ifTrue:[ |
22753 | 739 |
printFlag := flag. |
22743 | 740 |
^ self. |
741 |
]. |
|
10463 | 742 |
]. |
22735 | 743 |
self errorStream |
744 |
showCR:'? usage: set/clear <flag>'; |
|
22754 | 745 |
showCR:'? (<flag> must be one of: print, nodebug, confirmdebug, trace, times, profile, chunk, editor)'. |
22749 | 746 |
self cmd_show:('flags' readStream). |
10255 | 747 |
|
20935 | 748 |
"Modified: / 08-11-2016 / 22:49:17 / cg" |
10255 | 749 |
! |
750 |
||
10463 | 751 |
cmd_show:lineStream |
22575 | 752 |
"show directive: |
753 |
show packages |
|
754 |
show modules (= loaded packages) |
|
755 |
show variables |
|
756 |
etc. |
|
757 |
" |
|
758 |
||
20901 | 759 |
|errStream what showAll ok| |
10463 | 760 |
|
16087 | 761 |
errStream := self errorStream. |
762 |
||
10463 | 763 |
lineStream skipSeparators. |
764 |
what := lineStream nextAlphaNumericWord. |
|
20834 | 765 |
ok := false. |
766 |
||
16133 | 767 |
what notNil ifTrue:[ |
20834 | 768 |
showAll := (what startsWith:'all'). |
769 |
||
20835 | 770 |
(showAll or:[ what startsWith:'var' ]) ifTrue:[ |
22575 | 771 |
errStream showCR:'Variables:'; showCR:'----------'. |
20901 | 772 |
self showVariables. |
20845 | 773 |
ok := true. |
20768 | 774 |
]. |
20845 | 775 |
|
20835 | 776 |
(showAll or:[ what startsWith:'proc' ]) ifTrue:[ |
22575 | 777 |
errStream cr; showCR:'Threads:'; showCR:'--------'. |
20768 | 778 |
MiniDebugger basicNew showProcesses. |
20845 | 779 |
ok := true. |
20768 | 780 |
]. |
20845 | 781 |
|
20902 | 782 |
("showAll or:[" what startsWith:'pack' "]") ifTrue:[ |
22575 | 783 |
errStream cr; showCR:'Available Packages:'; showCR:'--------'. |
20900 | 784 |
self showPackages. |
785 |
ok := true. |
|
786 |
]. |
|
787 |
||
20834 | 788 |
(showAll or:[ what startsWith:'mod' ]) ifTrue:[ |
22575 | 789 |
errStream cr; showCR:'Modules:'; showCR:'--------'. |
20901 | 790 |
self showModules. |
13865 | 791 |
|
20845 | 792 |
ok := true. |
20768 | 793 |
]. |
20845 | 794 |
|
20834 | 795 |
(showAll or:[ what startsWith:'mem' ]) ifTrue:[ |
796 |
|allMem| |
|
797 |
||
22575 | 798 |
errStream cr; showCR:'Memory:'; showCR:'-------'. |
20891 | 799 |
"/ allMem := ObjectMemory oldSpaceUsed + ObjectMemory symSpaceUsed |
800 |
"/ + ObjectMemory newSpaceUsed. |
|
20768 | 801 |
errStream |
20923 | 802 |
"/ showCR:('overall: ',(allMem // 1024) printString,' Kb'); |
803 |
showCR:('used : ',(ObjectMemory bytesUsed // 1024) printString,' Kb'); |
|
804 |
showCR:('free : ',(ObjectMemory freeSpace // 1024) printString,' Kb'); |
|
805 |
show:('minorGC: ',(ObjectMemory scavengeCount) printString); |
|
806 |
showCR:(' majorGC: ',(ObjectMemory garbageCollectCount) printString). |
|
20845 | 807 |
ok := true. |
20768 | 808 |
]. |
20845 | 809 |
|
20834 | 810 |
(showAll or:[ what startsWith:'flag' ]) ifTrue:[ |
22575 | 811 |
errStream cr; showCR:'Flags:'; showCR:'------'. |
20768 | 812 |
errStream |
22754 | 813 |
showCR:('print: ',self printFlag printString); |
814 |
showCR:('nodebug: ',self noDebugger printString); |
|
815 |
showCR:('confirmdebug:',self confirmDebugger printString); |
|
20923 | 816 |
showCR:('trace : ',(traceFlag ? false) printString); |
817 |
showCR:('timing: ',(timingFlag ? false) printString); |
|
818 |
showCR:('profiling: ',(profilingFlag ? false) printString); |
|
20935 | 819 |
showCR:('chunkFormat: ',(doChunkFormat ? false) printString); |
22735 | 820 |
showCR:('editor: ',self editorCommand printString); |
22754 | 821 |
yourself. |
20845 | 822 |
ok := true. |
20768 | 823 |
]. |
10463 | 824 |
]. |
825 |
||
20834 | 826 |
ok ifFalse:[ |
22576 | 827 |
errStream showCR:'? usage: show <what>'. |
22577 | 828 |
errStream showCR:'? (<what> must be one of: packages, modules, variables, flags, memory, processes)'. |
20834 | 829 |
]. |
830 |
||
831 |
" |
|
832 |
self basicNew cmd_show:'packages' readStream |
|
833 |
" |
|
10255 | 834 |
|
20935 | 835 |
"Modified: / 08-11-2016 / 22:46:51 / cg" |
10255 | 836 |
! |
837 |
||
10463 | 838 |
cmd_use:lineStream |
22574 | 839 |
"use directive; i.e. |
840 |
#use stx:goodies/xml |
|
841 |
#use exept:mqtt |
|
842 |
" |
|
843 |
||
16145 | 844 |
|pkg| |
10463 | 845 |
|
846 |
lineStream skipSeparators. |
|
16133 | 847 |
pkg := lineStream upToEnd. |
22736 | 848 |
(pkg isEmptyOrNil or:[(pkg := pkg withoutSeparators) isEmpty]) ifTrue:[ |
22577 | 849 |
self errorStream showCR:'? usage: #use <package>'. |
20923 | 850 |
^ self. |
16133 | 851 |
]. |
852 |
||
16145 | 853 |
[ |
20923 | 854 |
Smalltalk loadPackage:pkg. |
16145 | 855 |
] on:PackageLoadError do:[:ex| |
20923 | 856 |
"/ allow for some shortcuts... |
857 |
(pkg includes:$:) ifTrue:[ |
|
858 |
self errorStream showCR:('Failed to load package: "',pkg,'"'). |
|
859 |
] ifFalse:[ |
|
860 |
"/ try stx standard package |
|
861 |
pkg := 'stx:', pkg. |
|
862 |
ex restart. |
|
863 |
]. |
|
13852 | 864 |
]. |
10255 | 865 |
|
866 |
"Created: / 07-12-2006 / 19:07:56 / cg" |
|
10463 | 867 |
! |
868 |
||
869 |
directive:line |
|
870 |
|s cmd| |
|
871 |
||
872 |
s := line readStream. |
|
873 |
s next. "/ skip the hash |
|
20913 | 874 |
s peek == $!! ifTrue:[ |
875 |
"/ skip shebang line |
|
876 |
^ self. |
|
877 |
]. |
|
10463 | 878 |
s skipSeparators. |
879 |
||
880 |
cmd := s nextAlphaNumericWord. |
|
16087 | 881 |
cmd notNil ifTrue:[ |
20919 | 882 |
AbortAllOperationRequest handle:[:ex | |
20923 | 883 |
self errorStream showCR:('Directive aborted: ', ex description) |
20919 | 884 |
] do:[ |
20920 | 885 |
Error handle:[:ex | |
20926 | 886 |
self errorStream showCR:('Caught in directive: ', ex description). |
887 |
ex suspendedContext fullPrintAll. |
|
20919 | 888 |
] do:[ |
20926 | 889 |
ControlInterrupt handle:[:ex | |
20936 | 890 |
MiniDebugger enter. |
891 |
"/ self errorStream showCR:('Ignored in directive: ', ex description). |
|
892 |
"/ ex reject. |
|
893 |
"/ ex proceed. |
|
20926 | 894 |
] do:[ |
895 |
self |
|
896 |
perform:('cmd_',cmd) asMutator with:s |
|
897 |
ifNotUnderstood:[ |
|
898 |
self errorStream |
|
22739 | 899 |
showCR:'?? invalid command: %1. Type "#help" for help.' with:cmd. |
20926 | 900 |
]. |
901 |
]. |
|
20913 | 902 |
]. |
20919 | 903 |
]. |
16087 | 904 |
]. |
10463 | 905 |
|
906 |
"Created: / 07-12-2006 / 18:49:17 / cg" |
|
20933 | 907 |
"Modified: / 08-11-2016 / 21:59:16 / cg" |
20900 | 908 |
! |
909 |
||
22735 | 910 |
getClassNameAndSelectorFrom:lineStream into:aBlock |
911 |
"a helper for list and edit; parses class and selector name. |
|
912 |
returns false if nothing reasonable was entered" |
|
913 |
||
914 |
|words wordStream className class selector| |
|
915 |
||
916 |
lineStream skipSeparators. |
|
917 |
words := lineStream upToEnd asCollectionOfWords. |
|
918 |
||
919 |
(words isEmpty) ifTrue:[ |
|
920 |
^ false. |
|
921 |
]. |
|
922 |
||
923 |
wordStream := words readStream. |
|
924 |
||
925 |
"/ search in classes: |
|
926 |
className := wordStream next. |
|
927 |
class := Smalltalk classNamed:className. |
|
928 |
class isNil ifTrue:[ |
|
929 |
self errorStream showCR:'no such class: ',className. |
|
930 |
^ false. |
|
931 |
]. |
|
932 |
||
933 |
(wordStream atEnd not and:[wordStream peek = 'class']) ifTrue:[ |
|
934 |
wordStream next. |
|
935 |
class := class theMetaclass |
|
936 |
]. |
|
937 |
(wordStream atEnd) ifFalse:[ |
|
938 |
selector := wordStream next. |
|
939 |
]. |
|
940 |
aBlock value:class value:selector. |
|
941 |
^ true |
|
942 |
! |
|
943 |
||
20901 | 944 |
showModules |
945 |
|errStream printModule| |
|
946 |
||
947 |
errStream := self errorStream. |
|
948 |
||
949 |
printModule := |
|
950 |
[:mod | |
|
951 |
errStream |
|
20923 | 952 |
show:' '; |
953 |
show:(mod package "libraryName"); |
|
954 |
showCR:' (',(mod type),')'. |
|
20901 | 955 |
]. |
956 |
||
957 |
errStream nextPutLine:'builtIn:'. |
|
958 |
((ObjectMemory binaryModuleInfo |
|
959 |
reject:[:m | m dynamic]) |
|
960 |
asSortedCollection:[:a :b | a name < b name]) do:printModule. |
|
961 |
||
962 |
errStream nextPutLine:'dynamic:'. |
|
963 |
((ObjectMemory binaryModuleInfo |
|
964 |
select:[:m | m dynamic]) |
|
965 |
asSortedCollection:[:a :b | a name < b name]) do:printModule. |
|
966 |
||
967 |
" |
|
968 |
ReadEvalPrintLoop basicNew showModules |
|
969 |
" |
|
970 |
! |
|
971 |
||
20900 | 972 |
showPackages |
973 |
|all| |
|
974 |
||
975 |
all := Set new. |
|
976 |
Smalltalk knownLoadablePackagesDo:[:packageID :type :path | |
|
977 |
all add:packageID |
|
978 |
]. |
|
979 |
all := all asOrderedCollection sort. |
|
980 |
all do:[:eachPackage | |
|
20923 | 981 |
self errorStream show:eachPackage. |
20901 | 982 |
(Smalltalk isPackageLoaded:eachPackage) ifTrue:[ |
20923 | 983 |
self errorStream show:' (loaded)'. |
20901 | 984 |
]. |
985 |
self errorStream cr. |
|
20900 | 986 |
]. |
987 |
||
988 |
" |
|
989 |
ReadEvalPrintLoop basicNew showPackages |
|
20901 | 990 |
ReadEvalPrintLoop basicNew showModules |
991 |
" |
|
992 |
! |
|
993 |
||
994 |
showVariables |
|
995 |
Workspace notNil ifTrue:[ |
|
996 |
Workspace workspaceVariables keys asOrderedCollection sort do:[:nm | |
|
997 |
|holder| |
|
998 |
holder := Workspace workspaceVariables at:nm. |
|
999 |
self errorStream |
|
20923 | 1000 |
show:nm; |
1001 |
show:' -> '; |
|
1002 |
showCR:holder value printString. |
|
20901 | 1003 |
]. |
1004 |
]. |
|
1005 |
||
1006 |
" |
|
1007 |
ReadEvalPrintLoop basicNew showVariables |
|
20900 | 1008 |
" |
10255 | 1009 |
! ! |
1010 |
||
10248 | 1011 |
!ReadEvalPrintLoop methodsFor:'evaluation'! |
1012 |
||
16565 | 1013 |
basicReadEvalPrintLoopWithInput:input output:output error:error |
16087 | 1014 |
compiler:compilerClass prompt:prompt print:doPrint |
1015 |
||
1016 |
"{ Pragma: +optSpace }" |
|
1017 |
||
22735 | 1018 |
"the core of the interpreter loop; |
1019 |
extracted and parametrized, so it can be called recursively for included scripts. |
|
16087 | 1020 |
If chunkFormat is true, chunks are read. |
1021 |
Otherwise, lines up to an empty line (or EOF) or a line ending in '.' are read. |
|
20774 | 1022 |
A '#' character appearing in the first column of the first line turns off chunkmode, |
1023 |
which allows for convenient shell scripts containing a #/bin/stx as the first line." |
|
16087 | 1024 |
|
20911 | 1025 |
exitAction := [^ self]. |
1026 |
||
16087 | 1027 |
[ |
20769 | 1028 |
|lines chunk| |
16087 | 1029 |
|
20769 | 1030 |
prompt notNil ifTrue:[ |
20935 | 1031 |
error show:prompt. |
20769 | 1032 |
]. |
16087 | 1033 |
|
20769 | 1034 |
input atEnd ifTrue:[ |
20912 | 1035 |
doPrint ifTrue:[ error cr ]. |
20769 | 1036 |
^ self. |
1037 |
]. |
|
16087 | 1038 |
|
20769 | 1039 |
input peek == $# ifTrue:[ |
1040 |
self doChunkFormat:false. |
|
1041 |
]. |
|
16087 | 1042 |
|
20769 | 1043 |
self doChunkFormat ifTrue:[ |
1044 |
input skipSeparators. |
|
1045 |
chunk := input nextChunk. |
|
1046 |
] ifFalse:[ |
|
1047 |
lines := OrderedCollection new. |
|
1048 |
[ |
|
1049 |
|line| |
|
16087 | 1050 |
|
20769 | 1051 |
line := input nextLine. |
1052 |
line notEmptyOrNil ifTrue:[ |
|
1053 |
line = '?' ifTrue:[ |
|
1054 |
self cmd_help:nil. |
|
1055 |
prompt notNil ifTrue:[ |
|
20935 | 1056 |
error show:prompt. |
20769 | 1057 |
]. |
1058 |
] ifFalse:[ |
|
1059 |
(line startsWith:'#') ifTrue:[ |
|
1060 |
self directive:line. |
|
1061 |
prompt notNil ifTrue:[ |
|
20935 | 1062 |
error show:prompt. |
20769 | 1063 |
]. |
1064 |
] ifFalse:[ |
|
1065 |
lines add:line. |
|
1066 |
] |
|
1067 |
] |
|
1068 |
]. |
|
1069 |
line notEmptyOrNil and:[(line endsWith:$.) not]. |
|
1070 |
] whileTrue. |
|
1071 |
chunk := lines asStringWith:Character cr. |
|
1072 |
]. |
|
16087 | 1073 |
|
20890 | 1074 |
(chunk notEmptyOrNil |
1075 |
and:[chunk withoutSeparators notEmpty |
|
1076 |
and:[chunk withoutSeparators ~= '.']] |
|
1077 |
) ifTrue:[ |
|
20769 | 1078 |
"abortAll is handled, but not asked for here!!" |
1079 |
AbortAllOperationRequest handle:[:ex | |
|
22754 | 1080 |
ObjectMemory sendTraceOff. |
22753 | 1081 |
error nextPutLine:('Evaluation aborted.') |
20769 | 1082 |
] do:[ |
1083 |
(Error, ControlInterrupt) handle:[:ex | |
|
22754 | 1084 |
ObjectMemory sendTraceOff. |
20769 | 1085 |
prompt isNil ifTrue:[ |
1086 |
ex reject |
|
1087 |
]. |
|
22748 | 1088 |
(self noDebugger |
22752 | 1089 |
and:[ ex creator isControlInterrupt not]) ifTrue:[ |
1090 |
error showCR:('Evaluation aborted: ', ex description). |
|
1091 |
] ifFalse:[ |
|
22748 | 1092 |
(self confirmDebugger not |
1093 |
or:[ ex creator isControlInterrupt |
|
1094 |
or:[ self askYesNo:('Error encountered: %1\Debug? ' |
|
1095 |
withCRs bindWith:ex description)]] |
|
1096 |
) ifTrue:[ |
|
1097 |
MiniDebugger enterWithMessage:(ex errorString) mayProceed:true. |
|
1098 |
"/ if we arrive here, user typed 'c' - continue |
|
1099 |
ex mayProceed ifTrue:[ |
|
1100 |
ex proceed. |
|
1101 |
]. |
|
22738 | 1102 |
]. |
20769 | 1103 |
]. |
1104 |
ex return. |
|
1105 |
] do:[ |
|
22754 | 1106 |
|value ms us mthd codeBlock| |
1107 |
||
1108 |
traceFlag == true ifTrue:[ |
|
1109 |
mthd := Compiler |
|
22759 | 1110 |
compile:('doIt ^[',chunk,']') forClass:(self class) |
22754 | 1111 |
inCategory:'doit' notifying:self |
1112 |
install:false. |
|
16087 | 1113 |
|
22754 | 1114 |
mthd isMethod ifTrue:[ |
1115 |
codeBlock := mthd valueWithReceiver:nil arguments:nil. |
|
1116 |
[ |
|
1117 |
ObjectMemory sendTraceOn. |
|
1118 |
codeBlock value. |
|
1119 |
] ensure:[ |
|
1120 |
ObjectMemory sendTraceOff. |
|
1121 |
] |
|
1122 |
]. |
|
1123 |
] ifFalse:[ |
|
1124 |
profilingFlag == true ifTrue:[ |
|
1125 |
MessageTally spyDetailedOn:[ |
|
1126 |
value := (compilerClass new requestor:self) |
|
22759 | 1127 |
evaluate:chunk |
1128 |
in:self class receiver:self |
|
22754 | 1129 |
compile:true. |
1130 |
]. |
|
1131 |
] ifFalse:[ |
|
1132 |
us := Time microsecondsToRun:[ |
|
1133 |
value := (compilerClass new requestor:self) |
|
22759 | 1134 |
evaluate:chunk |
1135 |
in:self class receiver:self |
|
1136 |
compile:true. |
|
22754 | 1137 |
]. |
1138 |
||
1139 |
timingFlag == true ifTrue:[ |
|
1140 |
'execution time: ' printOn:error. |
|
1141 |
us < 1000 ifTrue:[ |
|
1142 |
us < 1 ifTrue:[ |
|
1143 |
'too small to measure (<1us)' printOn:error. |
|
1144 |
] ifFalse:[ |
|
1145 |
us printOn:output. 'us' printOn:error. |
|
1146 |
] |
|
1147 |
] ifFalse:[ |
|
1148 |
((us / 1000) asFixedPoint:2) printOn:output. 'ms' printOn:error. |
|
1149 |
]. |
|
1150 |
error cr. |
|
1151 |
]. |
|
1152 |
]. |
|
1153 |
]. |
|
1154 |
(doPrint and:[self printFlag]) ifTrue:[ |
|
1155 |
value isVoid ifFalse:[ |
|
22741 | 1156 |
'-> (Return value): ' print. |
22740 | 1157 |
value displayString printOn:output. output cr. |
20935 | 1158 |
output flush. |
20771 | 1159 |
]. |
20769 | 1160 |
]. |
1161 |
Workspace notNil ifTrue:[ |
|
1162 |
Workspace rememberResultAsWorkspaceVariable:value. |
|
1163 |
]. |
|
1164 |
]. |
|
1165 |
]. |
|
1166 |
]. |
|
16087 | 1167 |
] loop. |
1168 |
||
1169 |
" |
|
20935 | 1170 |
Smalltalk readEvalPrintLoop. |
1171 |
||
16087 | 1172 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
1173 |
" |
|
1174 |
||
1175 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
20935 | 1176 |
"Modified: / 08-11-2016 / 22:41:47 / cg" |
16087 | 1177 |
! |
1178 |
||
10463 | 1179 |
readEvalPrintLoop |
1180 |
"{ Pragma: +optSpace }" |
|
1181 |
||
1182 |
"simple read-eval-print loop for non-graphical Minitalk. |
|
1183 |
If the chunkFormat-argument is true, chunks are read. |
|
16087 | 1184 |
Otherwise, lines up to an empty line (or EOF) are read. |
1185 |
A '#' character appearing in the first column of the first line |
|
1186 |
switches to chunkmode." |
|
10463 | 1187 |
|
1188 |
ControlInterrupt handle:[:ex | |
|
20935 | 1189 |
self errorStream showCR:('Caught: ', ex description). |
20833 | 1190 |
self inputStream atEnd ifTrue:[ |
1191 |
ex return. |
|
1192 |
]. |
|
20933 | 1193 |
MiniDebugger enter. |
1194 |
ex proceed. |
|
1195 |
"/ ex restart. |
|
10463 | 1196 |
] do:[ |
20774 | 1197 |
|input output error compilerClass| |
1198 |
||
1199 |
"/ re-evaluate these in the loop, so they can be changed dynamically |
|
1200 |
input := self inputStream. |
|
1201 |
output := self outputStream. |
|
1202 |
error := self errorStream. |
|
10463 | 1203 |
|
20774 | 1204 |
compilerClass := compiler ? Compiler ? Parser. |
1205 |
compilerClass isNil ifTrue:[ |
|
20935 | 1206 |
error showCR:('oops - no Compiler class found'). |
20774 | 1207 |
^ self. |
1208 |
]. |
|
1209 |
StreamError handle:[:ex | |
|
1210 |
(input isOpen not or:[input atEnd]) ifTrue:[ |
|
20935 | 1211 |
error showCR:'EOF on input'. |
20833 | 1212 |
ex return. |
20774 | 1213 |
]. |
1214 |
(output isOpen not) ifTrue:[ |
|
20935 | 1215 |
error showCR:'no output'. |
20774 | 1216 |
]. |
1217 |
(error isOpen not) ifTrue:[ |
|
1218 |
]. |
|
1219 |
] do:[ |
|
1220 |
input signalAtEnd:true. |
|
22743 | 1221 |
|
20774 | 1222 |
self |
1223 |
basicReadEvalPrintLoopWithInput:input output:output error:error |
|
22743 | 1224 |
compiler:compilerClass prompt:prompt print:true. |
1225 |
||
1226 |
]. |
|
20933 | 1227 |
]. |
20935 | 1228 |
"/ self errorStream showCR:('done.'). |
10463 | 1229 |
|
1230 |
" |
|
20933 | 1231 |
Stdin atEnd |
1232 |
Stdin clearEOF |
|
1233 |
Smalltalk readEvalPrintLoop |
|
10463 | 1234 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
1235 |
" |
|
1236 |
||
1237 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
20935 | 1238 |
"Modified: / 08-11-2016 / 22:42:21 / cg" |
10248 | 1239 |
! ! |
1240 |
||
16089 | 1241 |
!ReadEvalPrintLoop methodsFor:'queries'! |
1242 |
||
1243 |
autoDefineVariables |
|
1244 |
"when evaluating with --eval, auto define any variables" |
|
1245 |
||
1246 |
^ #workspace |
|
1247 |
! ! |
|
1248 |
||
10248 | 1249 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
1250 |
||
1251 |
version |
|
19379 | 1252 |
^ '$Header$' |
13121
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
1253 |
! |
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
1254 |
|
e0cc829d4b4d
added: #correctableError:position:to:from:
Claus Gittinger <cg@exept.de>
parents:
10635
diff
changeset
|
1255 |
version_CVS |
19379 | 1256 |
^ '$Header$' |
10248 | 1257 |
! ! |
19379 | 1258 |