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