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