author | Claus Gittinger <cg@exept.de> |
Wed, 04 Jul 2007 17:36:42 +0200 | |
changeset 10628 | 284266d54dbe |
parent 10500 | 3106f50bd9d7 |
child 10631 | 39ebd04452ac |
permissions | -rw-r--r-- |
10249 | 1 |
" |
2 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
10248 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
14 |
Object subclass:#ReadEvalPrintLoop |
|
10254 | 15 |
instanceVariableNames:'inputStream outputStream errorStream compiler prompt |
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
16 |
doChunkFormat traceFlag timingFlag exitAction' |
10248 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
10277 | 19 |
category:'System-Support' |
10248 | 20 |
! |
21 |
||
10249 | 22 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
10461
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
36 |
! |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
37 |
|
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
38 |
documentation |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
39 |
" |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
40 |
A simple read-eval-print loop for non-GUI or stscript operation. |
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
41 |
A line starting with '?' shows the usage message. |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
42 |
Lines starting with '#' are directives: |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
43 |
#exit - exit the rep-loop |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
44 |
|
10461
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
45 |
" |
10249 | 46 |
! ! |
10248 | 47 |
|
48 |
!ReadEvalPrintLoop methodsFor:'accessing'! |
|
49 |
||
10463 | 50 |
compiler:something |
51 |
compiler := something. |
|
10248 | 52 |
! |
53 |
||
10463 | 54 |
doChunkFormat:something |
10254 | 55 |
doChunkFormat := something. |
56 |
||
57 |
"Created: / 07-12-2006 / 18:24:04 / cg" |
|
10248 | 58 |
! |
59 |
||
60 |
error:something |
|
61 |
errorStream := something. |
|
62 |
||
63 |
"Created: / 07-12-2006 / 17:33:39 / cg" |
|
64 |
! |
|
65 |
||
10463 | 66 |
errorStream |
10256 | 67 |
^ errorStream ? Transcript ? Stderr |
68 |
||
69 |
"Created: / 07-12-2006 / 19:11:56 / cg" |
|
70 |
! |
|
71 |
||
10463 | 72 |
input:something |
10248 | 73 |
inputStream := something. |
74 |
||
75 |
"Modified: / 07-12-2006 / 17:33:31 / cg" |
|
76 |
! |
|
77 |
||
10463 | 78 |
inputStream |
79 |
^ inputStream ? Stdin |
|
80 |
||
81 |
"Created: / 07-12-2006 / 19:12:13 / cg" |
|
10256 | 82 |
! |
83 |
||
10463 | 84 |
output:something |
10248 | 85 |
outputStream := something. |
86 |
||
87 |
"Created: / 07-12-2006 / 17:27:48 / cg" |
|
88 |
! |
|
89 |
||
10463 | 90 |
outputStream |
91 |
^ outputStream ? Stdout |
|
92 |
||
93 |
"Created: / 07-12-2006 / 19:12:27 / cg" |
|
94 |
! |
|
95 |
||
96 |
prompt:something |
|
97 |
prompt := something. |
|
10248 | 98 |
! ! |
99 |
||
10255 | 100 |
!ReadEvalPrintLoop methodsFor:'directives'! |
101 |
||
10463 | 102 |
cmd_clear:lineStream |
10255 | 103 |
self cmd_setOrClear:lineStream to:false |
104 |
||
105 |
"Created: / 07-12-2006 / 19:04:50 / cg" |
|
106 |
! |
|
107 |
||
10463 | 108 |
cmd_exit:lineStream |
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
109 |
exitAction value |
10463 | 110 |
|
111 |
"Created: / 07-12-2006 / 18:55:46 / cg" |
|
10255 | 112 |
! |
113 |
||
10463 | 114 |
cmd_help:lineStream |
115 |
self errorStream |
|
116 |
nextPutAll: |
|
117 |
'Everything entered up to an empty line is called a "chunk" and evaluated as a block. |
|
118 |
Lines starting with "#" are commands to the read-eval-print interpreter. |
|
119 |
||
120 |
Valid commands are: |
|
121 |
help ............... this text |
|
122 |
exit ............... exit smalltalk |
|
123 |
use <package>....... use a package |
|
124 |
stx:libwidg ........ GUI package |
|
125 |
stx:libtool ........ IDE tool package |
|
126 |
show <what> ........ show info |
|
127 |
variables .......... interpreter variables |
|
128 |
processes .......... processes |
|
129 |
memory ............. memory usage |
|
130 |
flags .............. flags |
|
131 |
set/clear <flag> ... set or clear a flag |
|
132 |
trace .............. tracing execution |
|
133 |
timing ............. timing execution |
|
10257 | 134 |
|
135 |
MiniDebugger shows its help with "?". |
|
10255 | 136 |
' |
137 |
||
138 |
"Created: / 07-12-2006 / 18:54:20 / cg" |
|
139 |
! |
|
140 |
||
10463 | 141 |
cmd_set:lineStream |
10255 | 142 |
self cmd_setOrClear:lineStream to:true |
143 |
||
144 |
"Modified: / 07-12-2006 / 19:04:46 / cg" |
|
145 |
! |
|
146 |
||
10463 | 147 |
cmd_setOrClear:lineStream to:aBoolean |
148 |
|what| |
|
149 |
||
150 |
lineStream skipSeparators. |
|
151 |
what := lineStream nextAlphaNumericWord. |
|
152 |
(what startsWith:'tra') ifTrue:[ |
|
153 |
traceFlag := aBoolean. |
|
154 |
^ self. |
|
155 |
]. |
|
156 |
(what startsWith:'tim') ifTrue:[ |
|
157 |
timingFlag := aBoolean. |
|
158 |
^ self. |
|
159 |
]. |
|
160 |
||
161 |
self errorStream nextPutLine:'?? which flag ?'. |
|
10255 | 162 |
|
10256 | 163 |
"Modified: / 07-12-2006 / 19:13:34 / cg" |
10255 | 164 |
! |
165 |
||
10463 | 166 |
cmd_show:lineStream |
167 |
|what| |
|
168 |
||
169 |
lineStream skipSeparators. |
|
170 |
what := lineStream nextAlphaNumericWord. |
|
171 |
(what startsWith:'var') ifTrue:[ |
|
172 |
^ self. |
|
173 |
]. |
|
174 |
(what startsWith:'proc') ifTrue:[ |
|
175 |
MiniDebugger basicNew showProcesses. |
|
176 |
^ self. |
|
177 |
]. |
|
178 |
(what startsWith:'mem') ifTrue:[ |
|
179 |
self errorStream |
|
180 |
nextPutLine:('overall: ',(ObjectMemory bytesUsed // 1024) printString,' Kb'); |
|
181 |
nextPutLine:('free : ',(ObjectMemory freeSpace // 1024) printString,' Kb'); |
|
182 |
nextPutLine:('minorGC: ',(ObjectMemory scavengeCount) printString); |
|
183 |
nextPutLine:('majorGC: ',(ObjectMemory garbageCollectCount) printString). |
|
184 |
^ self. |
|
185 |
]. |
|
186 |
(what startsWith:'flag') ifTrue:[ |
|
187 |
self errorStream |
|
188 |
nextPutLine:('trace : ',traceFlag printString); |
|
189 |
nextPutLine:('timing: ',timingFlag printString). |
|
190 |
^ self. |
|
191 |
]. |
|
192 |
||
193 |
self errorStream nextPutLine:'?? show what ?'. |
|
10255 | 194 |
|
10261 | 195 |
"Modified: / 07-12-2006 / 19:23:15 / cg" |
10255 | 196 |
! |
197 |
||
10463 | 198 |
cmd_use:lineStream |
199 |
|pkg| |
|
200 |
||
201 |
lineStream skipSeparators. |
|
202 |
pkg := lineStream upToEnd withoutSeparators. |
|
10255 | 203 |
Smalltalk loadPackage:pkg |
204 |
||
205 |
"Created: / 07-12-2006 / 19:07:56 / cg" |
|
10463 | 206 |
! |
207 |
||
208 |
directive:line |
|
209 |
|s cmd| |
|
210 |
||
211 |
s := line readStream. |
|
212 |
s next. "/ skip the hash |
|
213 |
s skipSeparators. |
|
214 |
||
215 |
cmd := s nextAlphaNumericWord. |
|
216 |
self |
|
217 |
perform:('cmd_',cmd,':') asSymbol with:s |
|
218 |
ifNotUnderstood:[ |
|
219 |
self errorStream |
|
220 |
nextPutAll:'?? invalid command: '; |
|
221 |
nextPutAll:cmd; |
|
222 |
nextPutAll:'. Type "#help" for help.'; |
|
223 |
cr. |
|
224 |
]. |
|
225 |
||
226 |
"Created: / 07-12-2006 / 18:49:17 / cg" |
|
10255 | 227 |
! ! |
228 |
||
10248 | 229 |
!ReadEvalPrintLoop methodsFor:'evaluation'! |
230 |
||
10462
1db807a0f9e5
+correctableError:position:to:from
Claus Gittinger <cg@exept.de>
parents:
10461
diff
changeset
|
231 |
correctableError:message position:pos1 to:pos2 from:aCompiler |
10461
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
232 |
^ false |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
233 |
! |
0b2935207fec
+correctableError:position:to:
Claus Gittinger <cg@exept.de>
parents:
10277
diff
changeset
|
234 |
|
10463 | 235 |
readEvalPrintLoop |
236 |
"{ Pragma: +optSpace }" |
|
237 |
||
238 |
"simple read-eval-print loop for non-graphical Minitalk. |
|
239 |
If the chunkFormat-argument is true, chunks are read. |
|
240 |
Otherwise, lines up to an empty line (or EOF) are read." |
|
241 |
||
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
242 |
exitAction := [^ self]. |
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
243 |
|
10463 | 244 |
ControlInterrupt handle:[:ex | |
245 |
self errorStream nextPutLine:('Cought: ', ex description). |
|
246 |
ex restart. |
|
247 |
] do:[ |
|
248 |
[ |
|
249 |
|input output error lines chunk compilerClass| |
|
250 |
||
251 |
"/ re-evaluate these in the loop, so they can be changed dynamically |
|
252 |
input := self inputStream. |
|
253 |
output := self outputStream. |
|
254 |
error := self errorStream. |
|
255 |
compilerClass := compiler ? Compiler. |
|
256 |
||
257 |
prompt notNil ifTrue:[ |
|
258 |
error nextPutAll:prompt. |
|
259 |
]. |
|
260 |
||
261 |
input atEnd ifTrue:[ |
|
262 |
^ self. |
|
263 |
]. |
|
264 |
||
265 |
(doChunkFormat ? true) ifTrue:[ |
|
266 |
input skipSeparators. |
|
267 |
chunk := input nextChunk. |
|
268 |
] ifFalse:[ |
|
269 |
lines := OrderedCollection new. |
|
270 |
[ |
|
271 |
|line| |
|
272 |
||
273 |
line := input nextLine. |
|
274 |
line notEmptyOrNil ifTrue:[ |
|
275 |
line = '?' ifTrue:[ |
|
276 |
self cmd_help:nil. |
|
277 |
prompt notNil ifTrue:[ |
|
278 |
error nextPutAll:prompt. |
|
279 |
]. |
|
280 |
] ifFalse:[ |
|
281 |
(line startsWith:'#') ifTrue:[ |
|
282 |
self directive:line. |
|
283 |
prompt notNil ifTrue:[ |
|
284 |
error nextPutAll:prompt. |
|
285 |
]. |
|
286 |
] ifFalse:[ |
|
287 |
lines add:line. |
|
288 |
] |
|
289 |
] |
|
290 |
]. |
|
291 |
line notEmptyOrNil. |
|
292 |
] whileTrue. |
|
293 |
chunk := lines asStringWith:Character cr. |
|
294 |
]. |
|
295 |
||
296 |
chunk notEmptyOrNil ifTrue:[ |
|
10500
3106f50bd9d7
AbortOperationRequest is a child of AbortAllOperationRequest
Stefan Vogel <sv@exept.de>
parents:
10463
diff
changeset
|
297 |
AbortAllOperationRequest handle:[:ex | |
10463 | 298 |
error nextPutLine:('Evaluation aborted: ', ex description) |
299 |
] do:[ |
|
300 |
(Error, ControlInterrupt) handle:[:ex | |
|
301 |
prompt isNil ifTrue:[ |
|
302 |
ex reject |
|
303 |
]. |
|
304 |
MiniDebugger enterWithMessage:(ex errorString) mayProceed:true. |
|
305 |
ex mayProceed ifTrue:[ |
|
306 |
ex proceed. |
|
307 |
]. |
|
308 |
error nextPutLine:('Evaluation aborted: ', ex description). |
|
309 |
ex return. |
|
310 |
] do:[ |
|
311 |
|value| |
|
312 |
||
313 |
value := (compilerClass new requestor:self) evaluate:chunk compile:true. |
|
314 |
value printOn:output. |
|
315 |
output cr. |
|
316 |
]. |
|
317 |
]. |
|
318 |
]. |
|
319 |
] loop. |
|
320 |
] |
|
321 |
||
322 |
" |
|
323 |
(ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop |
|
324 |
" |
|
325 |
||
326 |
"Created: / 07-12-2006 / 17:27:21 / cg" |
|
327 |
"Modified: / 07-12-2006 / 19:41:20 / cg" |
|
10248 | 328 |
! ! |
329 |
||
330 |
!ReadEvalPrintLoop class methodsFor:'documentation'! |
|
331 |
||
332 |
version |
|
10628
284266d54dbe
#exit only leaves the rep-loop
Claus Gittinger <cg@exept.de>
parents:
10500
diff
changeset
|
333 |
^ '$Header: /cvs/stx/stx/libbasic/ReadEvalPrintLoop.st,v 1.23 2007-07-04 15:36:42 cg Exp $' |
10248 | 334 |
! ! |