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