author | Claus Gittinger <cg@exept.de> |
Tue, 12 Nov 1996 13:18:57 +0100 | |
changeset 441 | fa5637faa969 |
parent 394 | 728e3b942ad5 |
child 458 | 0e032b203043 |
permissions | -rw-r--r-- |
0 | 1 |
" |
4 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
45 | 3 |
All Rights Reserved |
0 | 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 |
" |
|
12 |
||
13 |
ParseNode subclass:#BlockNode |
|
168 | 14 |
instanceVariableNames:'blockArgs statements home inlineBlock exitBlock blockVars |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
15 |
needsHome lineNr endLineNr' |
168 | 16 |
classVariableNames:'' |
17 |
poolDictionaries:'' |
|
18 |
category:'System-Compiler-Support' |
|
0 | 19 |
! |
20 |
||
366 | 21 |
!BlockNode class methodsFor:'documentation'! |
20 | 22 |
|
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
45 | 26 |
All Rights Reserved |
0 | 27 |
|
20 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
0 | 36 |
|
20 | 37 |
documentation |
38 |
" |
|
39 |
node for parse-trees, representing blocks |
|
261 | 40 |
This is a helper class for the compiler. |
263 | 41 |
|
42 |
[author:] |
|
43 |
Claus Gittinger |
|
20 | 44 |
" |
45 |
! ! |
|
0 | 46 |
|
366 | 47 |
!BlockNode class methodsFor:'instance creation'! |
0 | 48 |
|
3 | 49 |
arguments:argList home:h variables:vars |
50 |
^ (self basicNew) setArguments:argList home:h variables:vars |
|
0 | 51 |
! ! |
52 |
||
53 |
!BlockNode methodsFor:'accessing'! |
|
54 |
||
55 |
arguments |
|
56 |
^ blockArgs |
|
57 |
! |
|
58 |
||
26 | 59 |
arguments:argList |
60 |
blockArgs := argList |
|
61 |
! |
|
62 |
||
394 | 63 |
endLineNumber |
64 |
^ endLineNr |
|
65 |
||
66 |
"Created: 23.10.1996 / 15:51:32 / cg" |
|
67 |
! |
|
68 |
||
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
69 |
endLineNumber:aNumber |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
70 |
endLineNr := aNumber |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
71 |
|
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
72 |
"Created: 21.10.1996 / 14:17:57 / cg" |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
73 |
! |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
74 |
|
140 | 75 |
home |
76 |
^ home |
|
0 | 77 |
! |
78 |
||
79 |
home:aBlock |
|
80 |
home := aBlock |
|
81 |
! |
|
82 |
||
83 |
inlineBlock |
|
84 |
^ inlineBlock |
|
85 |
! |
|
86 |
||
87 |
inlineBlock:aBoolean |
|
88 |
inlineBlock := aBoolean |
|
89 |
! |
|
90 |
||
394 | 91 |
lineNumber |
92 |
^ lineNr |
|
93 |
||
94 |
"Created: 23.10.1996 / 15:51:50 / cg" |
|
95 |
! |
|
96 |
||
140 | 97 |
lineNumber:aNumber |
98 |
lineNr := aNumber |
|
99 |
! |
|
100 |
||
0 | 101 |
needsHome |
102 |
^ needsHome |
|
103 |
! |
|
104 |
||
105 |
needsHome:aBoolean |
|
106 |
needsHome := aBoolean |
|
6 | 107 |
! |
108 |
||
140 | 109 |
statements |
110 |
^ statements |
|
111 |
! |
|
112 |
||
113 |
statements:s |
|
114 |
statements := s |
|
115 |
! |
|
116 |
||
117 |
variables |
|
118 |
^ blockVars |
|
119 |
! |
|
120 |
||
121 |
variables:varList |
|
122 |
blockVars := varList |
|
123 |
! ! |
|
124 |
||
125 |
!BlockNode methodsFor:'block messages'! |
|
126 |
||
127 |
doesNotUnderstand:aMessage |
|
128 |
|numArgs kludgeBlock| |
|
129 |
||
130 |
(Block implements:(aMessage selector)) ifTrue:[ |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
199
diff
changeset
|
131 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
199
diff
changeset
|
132 |
"/ mhmh - a message which I dont understand, but Block implements |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
199
diff
changeset
|
133 |
"/ send it to a kludgeblock, which will evaluate me again ..." |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
199
diff
changeset
|
134 |
|
140 | 135 |
numArgs := blockArgs size. |
136 |
numArgs == 0 ifTrue:[ |
|
137 |
kludgeBlock := [self value] |
|
138 |
] ifFalse:[ |
|
139 |
numArgs == 1 ifTrue:[ |
|
140 |
kludgeBlock := [:a1 | self value:a1]. |
|
141 |
] ifFalse:[ |
|
142 |
numArgs == 2 ifTrue:[ |
|
143 |
kludgeBlock := [:a1 :a2 | self value:a1 value:a2]. |
|
144 |
] ifFalse:[ |
|
145 |
numArgs == 3 ifTrue:[ |
|
146 |
kludgeBlock := [:a1 :a2 :a3| self value:a1 value:a2 value:a3]. |
|
147 |
] ifFalse:[ |
|
148 |
numArgs == 4 ifTrue:[ |
|
149 |
kludgeBlock := [:a1 :a2 :a3 :a4| self value:a1 value:a2 value:a3 value:a4]. |
|
150 |
] ifFalse:[ |
|
151 |
^ self error:'only support blocks with up-to 4 args' |
|
152 |
] |
|
153 |
] |
|
154 |
] |
|
155 |
] |
|
156 |
]. |
|
157 |
^ kludgeBlock perform:aMessage selector withArguments:aMessage arguments |
|
158 |
]. |
|
159 |
^ super doesNotUnderstand:aMessage |
|
0 | 160 |
! ! |
161 |
||
140 | 162 |
!BlockNode methodsFor:'code generation'! |
163 |
||
164 |
checkForSimpleBlock |
|
165 |
"simple things can be made cheap blocks right now - |
|
166 |
resulting in a simple pushLit instruction ..." |
|
167 |
||
168 |
|cheapy e val code| |
|
169 |
||
170 |
statements isNil ifTrue:[ |
|
366 | 171 |
"a []-block" |
140 | 172 |
|
366 | 173 |
val := nil |
140 | 174 |
] ifFalse:[ |
366 | 175 |
statements nextStatement notNil ifTrue:[^ nil]. |
176 |
(statements isMemberOf:StatementNode) ifFalse:[^ nil]. |
|
140 | 177 |
|
366 | 178 |
e := statements expression. |
179 |
e isConstant ifFalse:[^ nil]. |
|
140 | 180 |
|
366 | 181 |
val := e value. |
140 | 182 |
]. |
183 |
||
184 |
val == 0 ifTrue:[ |
|
366 | 185 |
"a [0]-block" |
140 | 186 |
|
366 | 187 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#ret0). |
140 | 188 |
]. |
189 |
val == 1 ifTrue:[ |
|
366 | 190 |
"a [1]-block" |
140 | 191 |
|
366 | 192 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#push1) |
193 |
with:(ByteCodeCompiler byteCodeFor:#retTop). |
|
140 | 194 |
]. |
195 |
||
196 |
val == true ifTrue:[ |
|
366 | 197 |
"a [true]-block" |
140 | 198 |
|
366 | 199 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#retTrue). |
140 | 200 |
]. |
201 |
||
202 |
val == false ifTrue:[ |
|
366 | 203 |
"a [false]-block" |
140 | 204 |
|
366 | 205 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#retFalse). |
140 | 206 |
]. |
207 |
||
208 |
val == nil ifTrue:[ |
|
366 | 209 |
"a [nil]-block" |
140 | 210 |
|
366 | 211 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#retNil). |
140 | 212 |
]. |
213 |
||
214 |
code notNil ifTrue:[ |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
215 |
cheapy := CheapBlock |
366 | 216 |
byteCode:code |
217 |
numArgs:(blockArgs size) |
|
218 |
sourcePosition:nil |
|
219 |
initialPC:nil |
|
220 |
literals:nil. |
|
221 |
^ ConstantNode type:#Block value:cheapy |
|
140 | 222 |
]. |
223 |
||
224 |
^ nil |
|
366 | 225 |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
226 |
"Modified: 21.10.1996 / 13:55:52 / cg" |
140 | 227 |
! |
228 |
||
229 |
codeForSideEffectOn:aStream inBlock:b for:aCompiler |
|
230 |
"generate code for this statement - value not needed. |
|
231 |
For blocks, no code is generated at all." |
|
232 |
||
233 |
^ self |
|
234 |
! |
|
235 |
||
236 |
codeInlineOn:aStream inBlock:b for:aCompiler |
|
237 |
self codeInlineOn:aStream inBlock:b valueNeeded:true for:aCompiler |
|
238 |
! |
|
239 |
||
240 |
codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded for:aCompiler |
|
241 |
|thisStatement nextStatement| |
|
242 |
||
243 |
blockVars notNil ifTrue:[ |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
244 |
"cannot currently compile this block inline (have to move blockvars into |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
245 |
surrounding method. generate a make-block and send it value" |
0 | 246 |
|
278 | 247 |
"/ Transcript showCR:'cannot (yet) compile block with blockvars inline'. |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
248 |
self codeOn:aStream inBlock:b for:aCompiler. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
249 |
aStream nextPut:#value. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
250 |
(aCompiler hasLineNumber:#value) ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
251 |
aStream nextPut:lineNr. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
252 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
253 |
valueNeeded ifFalse:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
254 |
aStream nextPut:#drop |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
255 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
256 |
^ self |
140 | 257 |
]. |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
258 |
|
140 | 259 |
inlineBlock := true. |
260 |
statements isNil ifTrue:[ |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
261 |
valueNeeded ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
262 |
aStream nextPut:#pushNil |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
263 |
] |
140 | 264 |
] ifFalse:[ |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
265 |
thisStatement := statements. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
266 |
[thisStatement notNil] whileTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
267 |
nextStatement := thisStatement nextStatement. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
268 |
(nextStatement notNil or:[valueNeeded not]) ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
269 |
thisStatement codeForSideEffectOn:aStream inBlock:b for:aCompiler |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
270 |
] ifFalse:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
271 |
thisStatement codeOn:aStream inBlock:b for:aCompiler |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
272 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
273 |
thisStatement := nextStatement |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
274 |
] |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
275 |
]. |
386 | 276 |
"/ endLineNr notNil ifTrue:[ |
277 |
"/ ParseNode codeLineNumber:endLineNr on:aStream for:aCompiler |
|
278 |
"/ ]. |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
279 |
|
386 | 280 |
"Modified: 21.10.1996 / 16:57:53 / cg" |
140 | 281 |
! |
282 |
||
283 |
codeOn:aStream inBlock:b for:aCompiler |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
284 |
|thisStatement nextStatement lastStatement pos code cheapy p0| |
140 | 285 |
|
286 |
cheapy := self checkForSimpleBlock. |
|
287 |
cheapy notNil ifTrue:[ |
|
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
288 |
cheapy codeOn:aStream inBlock:b for:aCompiler. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
289 |
^ self |
140 | 290 |
]. |
291 |
||
292 |
pos := aStream position. |
|
62 | 293 |
|
140 | 294 |
aStream nextPut:#makeBlock. "+0" |
295 |
aStream nextPut:0. "+1" |
|
296 |
aStream nextPut:(blockVars size). "+2" |
|
297 |
aStream nextPut:(blockArgs size). "+3" |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
298 |
"+4" |
394 | 299 |
p0 := pos+4. |
300 |
||
140 | 301 |
statements isNil ifTrue:[ |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
302 |
endLineNr notNil ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
303 |
ParseNode codeLineNumber:endLineNr on:aStream for:aCompiler |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
304 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
305 |
aStream nextPut:#pushNil. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
306 |
aStream nextPut:#retTop. |
140 | 307 |
] ifFalse:[ |
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
308 |
thisStatement := statements. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
309 |
[thisStatement notNil] whileTrue:[ |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
310 |
nextStatement := thisStatement nextStatement. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
311 |
nextStatement notNil ifTrue:[ |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
312 |
thisStatement codeForSideEffectOn:aStream inBlock:self for:aCompiler |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
313 |
] ifFalse:[ |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
314 |
lastStatement := thisStatement |
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
315 |
]. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
316 |
thisStatement := nextStatement |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
317 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
318 |
|
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
319 |
lastStatement isPrimary ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
320 |
ReturnNode |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
321 |
codeSimpleReturnFor:lastStatement expression |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
322 |
inBlock:nil |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
323 |
on:aStream |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
324 |
inLine:endLineNr |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
325 |
for:aCompiler |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
326 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
327 |
lastStatement codeOn:aStream inBlock:self for:aCompiler. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
328 |
endLineNr notNil ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
329 |
ParseNode codeLineNumber:endLineNr on:aStream for:aCompiler |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
330 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
331 |
aStream nextPut:#retTop. |
140 | 332 |
]. |
333 |
||
334 |
"check for [0]-block; |
|
335 |
these are sometimes used as in ... ifAbsent:[0] |
|
336 |
" |
|
337 |
code := (aStream contents). |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
338 |
(code at:p0) == #lineno ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
339 |
p0 := p0 + 2 |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
340 |
] ifFalse:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
341 |
(code at:p0) == #lineno16 ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
342 |
p0 := p0 + 3 |
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
343 |
] |
140 | 344 |
]. |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
345 |
|
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
346 |
(code at:p0) == #ret0 ifTrue:[ |
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
347 |
aStream position:pos. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
348 |
code grow:pos. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
349 |
aStream nextPut:#mk0Block. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
350 |
^ self |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
351 |
]. |
140 | 352 |
|
353 |
"check for [nil]-block; |
|
354 |
these come to play when code in blocks is commented |
|
355 |
out, or as dummy exception blocks |
|
356 |
" |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
357 |
(code at:p0) == #retNil ifTrue:[ |
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
358 |
aStream position:pos. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
359 |
code grow:pos. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
360 |
aStream nextPut:#mkNilBlock. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
361 |
^ self |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
362 |
]. |
140 | 363 |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
364 |
code at:pos+1 put:(aStream position) |
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
365 |
|
394 | 366 |
"Modified: 23.10.1996 / 15:40:46 / cg" |
0 | 367 |
! ! |
368 |
||
369 |
!BlockNode methodsFor:'evaluating'! |
|
370 |
||
140 | 371 |
evaluate |
372 |
^ self |
|
373 |
! |
|
374 |
||
0 | 375 |
exitWith:something |
376 |
"return via return-statement" |
|
377 |
||
378 |
home notNil ifTrue:[ |
|
45 | 379 |
home exitWith:something |
0 | 380 |
]. |
381 |
exitBlock value:something. |
|
382 |
^ something |
|
383 |
! |
|
384 |
||
385 |
value |
|
386 |
(blockArgs size ~~ 0) ifTrue:[ |
|
45 | 387 |
^ self wrongNumberOfArguments:0 |
0 | 388 |
]. |
389 |
statements isNil ifTrue:[^ nil]. |
|
390 |
exitBlock := [:val | ^ val]. |
|
391 |
^ statements evaluate |
|
392 |
! |
|
393 |
||
394 |
value:anArg |
|
395 |
|oldValue val| |
|
396 |
||
397 |
(blockArgs size ~~ 1) ifTrue:[ |
|
45 | 398 |
^ self wrongNumberOfArguments:1 |
0 | 399 |
]. |
400 |
statements isNil ifTrue:[^ nil]. |
|
401 |
||
402 |
oldValue := (blockArgs at:1) value. |
|
403 |
(blockArgs at:1) value:anArg. |
|
404 |
||
15 | 405 |
exitBlock := [:v | |
45 | 406 |
(blockArgs at:1) value:oldValue. |
407 |
^ v |
|
0 | 408 |
]. |
409 |
||
410 |
val := statements evaluate. |
|
411 |
||
412 |
(blockArgs at:1) value:oldValue. |
|
413 |
^ val |
|
414 |
! |
|
415 |
||
416 |
value:arg1 value:arg2 |
|
417 |
|oldValue1 oldValue2 val| |
|
418 |
||
419 |
(blockArgs size ~~ 2) ifTrue:[ |
|
45 | 420 |
^ self wrongNumberOfArguments:2 |
0 | 421 |
]. |
422 |
statements isNil ifTrue:[^ nil]. |
|
423 |
||
424 |
oldValue1 := (blockArgs at:1) value. |
|
425 |
oldValue2 := (blockArgs at:2) value. |
|
426 |
(blockArgs at:1) value:arg1. |
|
427 |
(blockArgs at:2) value:arg2. |
|
428 |
||
15 | 429 |
exitBlock := [:v | |
45 | 430 |
(blockArgs at:1) value:oldValue1. |
431 |
(blockArgs at:2) value:oldValue2. |
|
432 |
^ v |
|
0 | 433 |
]. |
434 |
||
435 |
val := statements evaluate. |
|
436 |
||
437 |
(blockArgs at:1) value:oldValue1. |
|
438 |
(blockArgs at:2) value:oldValue2. |
|
439 |
^ val |
|
440 |
! |
|
441 |
||
442 |
value:arg1 value:arg2 value:arg3 |
|
443 |
|oldValue1 oldValue2 oldValue3 val| |
|
444 |
||
445 |
(blockArgs size ~~ 3) ifTrue:[ |
|
45 | 446 |
^ self wrongNumberOfArguments:3 |
0 | 447 |
]. |
448 |
statements isNil ifTrue:[^ nil]. |
|
449 |
||
450 |
oldValue1 := (blockArgs at:1) value. |
|
451 |
oldValue2 := (blockArgs at:2) value. |
|
452 |
oldValue3 := (blockArgs at:3) value. |
|
453 |
(blockArgs at:1) value:arg1. |
|
454 |
(blockArgs at:2) value:arg2. |
|
455 |
(blockArgs at:3) value:arg3. |
|
456 |
||
15 | 457 |
exitBlock := [:v | |
45 | 458 |
(blockArgs at:1) value:oldValue1. |
459 |
(blockArgs at:2) value:oldValue2. |
|
460 |
(blockArgs at:3) value:oldValue3. |
|
461 |
^ v |
|
0 | 462 |
]. |
463 |
||
464 |
val := statements evaluate. |
|
465 |
||
466 |
(blockArgs at:1) value:oldValue1. |
|
467 |
(blockArgs at:2) value:oldValue2. |
|
468 |
(blockArgs at:3) value:oldValue3. |
|
469 |
^ val |
|
470 |
! |
|
471 |
||
472 |
value:arg1 value:arg2 value:arg3 value:arg4 |
|
473 |
|oldValue1 oldValue2 oldValue3 oldValue4 val| |
|
474 |
||
475 |
(blockArgs size ~~ 4) ifTrue:[ |
|
45 | 476 |
^ self wrongNumberOfArguments:4 |
0 | 477 |
]. |
478 |
statements isNil ifTrue:[^ nil]. |
|
479 |
||
480 |
oldValue1 := (blockArgs at:1) value. |
|
481 |
oldValue2 := (blockArgs at:2) value. |
|
482 |
oldValue3 := (blockArgs at:3) value. |
|
483 |
oldValue4 := (blockArgs at:4) value. |
|
484 |
(blockArgs at:1) value:arg1. |
|
485 |
(blockArgs at:2) value:arg2. |
|
486 |
(blockArgs at:3) value:arg3. |
|
487 |
(blockArgs at:4) value:arg4. |
|
488 |
||
15 | 489 |
exitBlock := [:v | |
45 | 490 |
(blockArgs at:1) value:oldValue1. |
491 |
(blockArgs at:2) value:oldValue2. |
|
492 |
(blockArgs at:3) value:oldValue3. |
|
493 |
(blockArgs at:4) value:oldValue4. |
|
494 |
^ v |
|
0 | 495 |
]. |
496 |
||
497 |
val := statements evaluate. |
|
498 |
||
499 |
(blockArgs at:1) value:oldValue1. |
|
500 |
(blockArgs at:2) value:oldValue2. |
|
501 |
(blockArgs at:3) value:oldValue3. |
|
502 |
(blockArgs at:4) value:oldValue4. |
|
503 |
^ val |
|
140 | 504 |
! |
3 | 505 |
|
506 |
valueWithArguments:argArray |
|
507 |
|oldValues val| |
|
508 |
||
509 |
(blockArgs size ~~ argArray size) ifTrue:[ |
|
45 | 510 |
^ self wrongNumberOfArguments:argArray size |
3 | 511 |
]. |
512 |
statements isNil ifTrue:[^ nil]. |
|
513 |
||
514 |
oldValues := Array new:(argArray size). |
|
515 |
1 to:argArray size do:[:i | |
|
45 | 516 |
oldValues at:i put:(blockArgs at:i) value. |
517 |
(blockArgs at:i) value:(argArray at:i). |
|
3 | 518 |
]. |
15 | 519 |
exitBlock := [:v | |
45 | 520 |
1 to:argArray size do:[:i | |
521 |
( blockArgs at:i) value:(oldValues at:i) |
|
522 |
]. |
|
523 |
^ v |
|
3 | 524 |
]. |
525 |
||
526 |
val := statements evaluate. |
|
527 |
||
528 |
1 to:argArray size do:[:i | |
|
45 | 529 |
(blockArgs at:i) value:(oldValues at:i) |
3 | 530 |
]. |
531 |
^ val |
|
140 | 532 |
! |
533 |
||
534 |
wrongNumberOfArguments:numberGiven |
|
535 |
Block argumentSignal |
|
536 |
raiseRequestWith:self |
|
537 |
errorString:('block got ' , numberGiven printString , |
|
538 |
' args while ' , blockArgs size printString , ' where expected') |
|
0 | 539 |
! ! |
540 |
||
541 |
!BlockNode methodsFor:'looping'! |
|
542 |
||
543 |
whileFalse:aBlock |
|
26 | 544 |
self value ifTrue:[^ nil]. |
545 |
aBlock value. |
|
546 |
thisContext restart |
|
0 | 547 |
! |
548 |
||
140 | 549 |
whileTrue:aBlock |
550 |
self value ifFalse:[^ nil]. |
|
551 |
aBlock value. |
|
552 |
thisContext restart |
|
0 | 553 |
! ! |
554 |
||
555 |
!BlockNode methodsFor:'printing'! |
|
556 |
||
557 |
printOn:aStream indent:i |
|
98 | 558 |
|n "{Class: SmallInteger }"| |
559 |
||
0 | 560 |
aStream nextPut:$[. |
98 | 561 |
(n := blockArgs size) > 0 ifTrue:[ |
562 |
1 to:n do:[:index | |
|
96 | 563 |
aStream nextPut:$:. |
564 |
aStream nextPutAll:(blockArgs at:index) name. |
|
565 |
aStream space. |
|
566 |
]. |
|
98 | 567 |
aStream nextPut:$|. |
568 |
aStream space. |
|
569 |
]. |
|
570 |
(n := blockVars size) > 0 ifTrue:[ |
|
571 |
aStream nextPut:$|. |
|
572 |
1 to:n do:[:index | |
|
573 |
aStream nextPutAll:(blockVars at:index) name. |
|
574 |
aStream space. |
|
575 |
]. |
|
576 |
aStream nextPut:$|. |
|
0 | 577 |
]. |
578 |
statements notNil ifTrue:[ |
|
45 | 579 |
aStream cr. |
580 |
statements printAllOn:aStream indent:i + 4. |
|
581 |
aStream cr. |
|
582 |
aStream spaces:i. |
|
0 | 583 |
]. |
584 |
aStream nextPut:$] |
|
585 |
! ! |
|
140 | 586 |
|
587 |
!BlockNode methodsFor:'private accessing'! |
|
588 |
||
589 |
setArguments:argList home:h variables:vars |
|
590 |
inlineBlock := false. |
|
591 |
needsHome := false. |
|
592 |
blockArgs := argList. |
|
593 |
home := h. |
|
594 |
blockVars := vars |
|
595 |
! ! |
|
596 |
||
597 |
!BlockNode methodsFor:'queries'! |
|
598 |
||
394 | 599 |
collectBlocksInto:aCollection |
600 |
aCollection add:self. |
|
601 |
statements collectBlocksInto:aCollection |
|
602 |
||
603 |
"Created: 23.10.1996 / 15:45:16 / cg" |
|
604 |
"Modified: 23.10.1996 / 16:02:57 / cg" |
|
605 |
! |
|
606 |
||
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
607 |
endsWithReturn |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
608 |
statements isNil ifTrue:[ |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
609 |
^ false |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
610 |
]. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
611 |
^ statements listEndsWithReturn |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
612 |
|
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
613 |
"Created: 19.8.1996 / 14:36:32 / cg" |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
614 |
! |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
615 |
|
140 | 616 |
isBlock |
617 |
"a kludge, to have blocknodes mimic blocks" |
|
618 |
||
619 |
^ true |
|
168 | 620 |
! |
621 |
||
394 | 622 |
numArgs |
623 |
^ blockArgs size |
|
624 |
||
625 |
"Created: 23.10.1996 / 15:57:04 / cg" |
|
626 |
! |
|
627 |
||
628 |
numVars |
|
629 |
^ blockVars size |
|
630 |
||
631 |
"Created: 23.10.1996 / 16:17:07 / cg" |
|
632 |
! |
|
633 |
||
169 | 634 |
simpleSendBlockExpression |
635 |
blockVars notNil ifTrue:[^ nil]. |
|
636 |
statements isNil ifTrue:[^ nil]. |
|
637 |
statements nextStatement notNil ifTrue:[^ nil]. |
|
638 |
^ statements expression |
|
639 |
||
640 |
"Created: 13.12.1995 / 20:06:09 / cg" |
|
140 | 641 |
! ! |
642 |
||
366 | 643 |
!BlockNode class methodsFor:'documentation'! |
148 | 644 |
|
645 |
version |
|
394 | 646 |
^ '$Header: /cvs/stx/stx/libcomp/BlockNode.st,v 1.33 1996-10-23 15:32:42 cg Exp $' |
148 | 647 |
! ! |