author | Claus Gittinger <cg@exept.de> |
Wed, 25 Jun 1997 13:16:52 +0200 | |
changeset 553 | 59e4fcf722f6 |
parent 544 | fcfff6f8bc31 |
child 555 | 987b766b8b6e |
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 |
|
553 | 268 |
self numArgs ~~ 0 ifTrue:[ |
269 |
self halt:'unexpected: inlineBlock with args'. |
|
270 |
]. |
|
271 |
||
278 | 272 |
"/ 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
|
273 |
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
|
274 |
aStream nextPut:#value. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
275 |
(aCompiler hasLineNumber:#value) ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
276 |
aStream nextPut:lineNr. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
277 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
278 |
valueNeeded ifFalse:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
279 |
aStream nextPut:#drop |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
280 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
281 |
^ self |
140 | 282 |
]. |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
283 |
|
140 | 284 |
inlineBlock := true. |
285 |
statements isNil ifTrue:[ |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
286 |
valueNeeded ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
287 |
aStream nextPut:#pushNil |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
288 |
] |
140 | 289 |
] ifFalse:[ |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
290 |
thisStatement := statements. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
291 |
[thisStatement notNil] whileTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
292 |
nextStatement := thisStatement nextStatement. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
293 |
(nextStatement notNil or:[valueNeeded not]) ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
294 |
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
|
295 |
] ifFalse:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
296 |
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
|
297 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
298 |
thisStatement := nextStatement |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
299 |
] |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
300 |
]. |
386 | 301 |
"/ endLineNr notNil ifTrue:[ |
302 |
"/ ParseNode codeLineNumber:endLineNr on:aStream for:aCompiler |
|
303 |
"/ ]. |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
304 |
|
553 | 305 |
"Modified: 25.6.1997 / 13:06:21 / cg" |
140 | 306 |
! |
307 |
||
308 |
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
|
309 |
|thisStatement nextStatement lastStatement pos code cheapy p0| |
140 | 310 |
|
311 |
cheapy := self checkForSimpleBlock. |
|
312 |
cheapy notNil ifTrue:[ |
|
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
313 |
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
|
314 |
^ self |
140 | 315 |
]. |
316 |
||
317 |
pos := aStream position. |
|
62 | 318 |
|
140 | 319 |
aStream nextPut:#makeBlock. "+0" |
320 |
aStream nextPut:0. "+1" |
|
321 |
aStream nextPut:(blockVars size). "+2" |
|
322 |
aStream nextPut:(blockArgs size). "+3" |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
323 |
"+4" |
394 | 324 |
p0 := pos+4. |
325 |
||
140 | 326 |
statements isNil ifTrue:[ |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
327 |
endLineNr notNil ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
328 |
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
|
329 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
330 |
aStream nextPut:#pushNil. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
331 |
aStream nextPut:#retTop. |
140 | 332 |
] ifFalse:[ |
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
333 |
thisStatement := statements. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
334 |
[thisStatement notNil] whileTrue:[ |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
335 |
nextStatement := thisStatement nextStatement. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
336 |
nextStatement notNil ifTrue:[ |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
337 |
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
|
338 |
] ifFalse:[ |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
339 |
lastStatement := thisStatement |
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
340 |
]. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
341 |
thisStatement := nextStatement |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
342 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
343 |
|
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
344 |
lastStatement isPrimary ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
345 |
ReturnNode |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
346 |
codeSimpleReturnFor:lastStatement expression |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
347 |
inBlock:nil |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
348 |
on:aStream |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
349 |
inLine:endLineNr |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
350 |
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 |
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
|
353 |
endLineNr notNil ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
354 |
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
|
355 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
356 |
aStream nextPut:#retTop. |
140 | 357 |
]. |
358 |
||
359 |
"check for [0]-block; |
|
360 |
these are sometimes used as in ... ifAbsent:[0] |
|
361 |
" |
|
362 |
code := (aStream contents). |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
363 |
(code at:p0) == #lineno ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
364 |
p0 := p0 + 2 |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
365 |
] ifFalse:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
366 |
(code at:p0) == #lineno16 ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
367 |
p0 := p0 + 3 |
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
368 |
] |
140 | 369 |
]. |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
370 |
|
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
371 |
(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
|
372 |
aStream position:pos. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
373 |
code grow:pos. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
374 |
aStream nextPut:#mk0Block. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
375 |
^ self |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
376 |
]. |
140 | 377 |
|
378 |
"check for [nil]-block; |
|
379 |
these come to play when code in blocks is commented |
|
380 |
out, or as dummy exception blocks |
|
381 |
" |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
382 |
(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
|
383 |
aStream position:pos. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
384 |
code grow:pos. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
385 |
aStream nextPut:#mkNilBlock. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
386 |
^ self |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
387 |
]. |
140 | 388 |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
389 |
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
|
390 |
|
394 | 391 |
"Modified: 23.10.1996 / 15:40:46 / cg" |
0 | 392 |
! ! |
393 |
||
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
394 |
!BlockNode methodsFor:'enumeration'! |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
395 |
|
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
396 |
nodeDo:anEnumerator |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
397 |
"helper for parse tree walking" |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
398 |
|
544
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
399 |
|args| |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
400 |
|
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
401 |
args := blockArgs ? #(). |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
402 |
args := args collect:[:var | |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
403 |
|p| |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
404 |
|
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
405 |
p := ParameterNode new. |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
406 |
p variable:var |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
407 |
]. |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
408 |
^ anEnumerator doBlock:self arguments:args body:statements |
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
409 |
|
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
410 |
"Created: 19.6.1997 / 16:38:30 / cg" |
544
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
411 |
"Modified: 19.6.1997 / 17:17:57 / cg" |
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
412 |
! ! |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
413 |
|
0 | 414 |
!BlockNode methodsFor:'evaluating'! |
415 |
||
140 | 416 |
evaluate |
417 |
^ self |
|
418 |
! |
|
419 |
||
0 | 420 |
exitWith:something |
421 |
"return via return-statement" |
|
422 |
||
423 |
home notNil ifTrue:[ |
|
45 | 424 |
home exitWith:something |
0 | 425 |
]. |
426 |
exitBlock value:something. |
|
427 |
^ something |
|
428 |
! |
|
429 |
||
430 |
value |
|
431 |
(blockArgs size ~~ 0) ifTrue:[ |
|
45 | 432 |
^ self wrongNumberOfArguments:0 |
0 | 433 |
]. |
434 |
statements isNil ifTrue:[^ nil]. |
|
435 |
exitBlock := [:val | ^ val]. |
|
436 |
^ statements evaluate |
|
437 |
! |
|
438 |
||
439 |
value:anArg |
|
440 |
|oldValue val| |
|
441 |
||
442 |
(blockArgs size ~~ 1) ifTrue:[ |
|
45 | 443 |
^ self wrongNumberOfArguments:1 |
0 | 444 |
]. |
445 |
statements isNil ifTrue:[^ nil]. |
|
446 |
||
447 |
oldValue := (blockArgs at:1) value. |
|
448 |
(blockArgs at:1) value:anArg. |
|
449 |
||
15 | 450 |
exitBlock := [:v | |
45 | 451 |
(blockArgs at:1) value:oldValue. |
452 |
^ v |
|
0 | 453 |
]. |
454 |
||
455 |
val := statements evaluate. |
|
456 |
||
457 |
(blockArgs at:1) value:oldValue. |
|
458 |
^ val |
|
459 |
! |
|
460 |
||
461 |
value:arg1 value:arg2 |
|
462 |
|oldValue1 oldValue2 val| |
|
463 |
||
464 |
(blockArgs size ~~ 2) ifTrue:[ |
|
45 | 465 |
^ self wrongNumberOfArguments:2 |
0 | 466 |
]. |
467 |
statements isNil ifTrue:[^ nil]. |
|
468 |
||
469 |
oldValue1 := (blockArgs at:1) value. |
|
470 |
oldValue2 := (blockArgs at:2) value. |
|
471 |
(blockArgs at:1) value:arg1. |
|
472 |
(blockArgs at:2) value:arg2. |
|
473 |
||
15 | 474 |
exitBlock := [:v | |
45 | 475 |
(blockArgs at:1) value:oldValue1. |
476 |
(blockArgs at:2) value:oldValue2. |
|
477 |
^ v |
|
0 | 478 |
]. |
479 |
||
480 |
val := statements evaluate. |
|
481 |
||
482 |
(blockArgs at:1) value:oldValue1. |
|
483 |
(blockArgs at:2) value:oldValue2. |
|
484 |
^ val |
|
485 |
! |
|
486 |
||
487 |
value:arg1 value:arg2 value:arg3 |
|
488 |
|oldValue1 oldValue2 oldValue3 val| |
|
489 |
||
490 |
(blockArgs size ~~ 3) ifTrue:[ |
|
45 | 491 |
^ self wrongNumberOfArguments:3 |
0 | 492 |
]. |
493 |
statements isNil ifTrue:[^ nil]. |
|
494 |
||
495 |
oldValue1 := (blockArgs at:1) value. |
|
496 |
oldValue2 := (blockArgs at:2) value. |
|
497 |
oldValue3 := (blockArgs at:3) value. |
|
498 |
(blockArgs at:1) value:arg1. |
|
499 |
(blockArgs at:2) value:arg2. |
|
500 |
(blockArgs at:3) value:arg3. |
|
501 |
||
15 | 502 |
exitBlock := [:v | |
45 | 503 |
(blockArgs at:1) value:oldValue1. |
504 |
(blockArgs at:2) value:oldValue2. |
|
505 |
(blockArgs at:3) value:oldValue3. |
|
506 |
^ v |
|
0 | 507 |
]. |
508 |
||
509 |
val := statements evaluate. |
|
510 |
||
511 |
(blockArgs at:1) value:oldValue1. |
|
512 |
(blockArgs at:2) value:oldValue2. |
|
513 |
(blockArgs at:3) value:oldValue3. |
|
514 |
^ val |
|
515 |
! |
|
516 |
||
517 |
value:arg1 value:arg2 value:arg3 value:arg4 |
|
518 |
|oldValue1 oldValue2 oldValue3 oldValue4 val| |
|
519 |
||
520 |
(blockArgs size ~~ 4) ifTrue:[ |
|
45 | 521 |
^ self wrongNumberOfArguments:4 |
0 | 522 |
]. |
523 |
statements isNil ifTrue:[^ nil]. |
|
524 |
||
525 |
oldValue1 := (blockArgs at:1) value. |
|
526 |
oldValue2 := (blockArgs at:2) value. |
|
527 |
oldValue3 := (blockArgs at:3) value. |
|
528 |
oldValue4 := (blockArgs at:4) value. |
|
529 |
(blockArgs at:1) value:arg1. |
|
530 |
(blockArgs at:2) value:arg2. |
|
531 |
(blockArgs at:3) value:arg3. |
|
532 |
(blockArgs at:4) value:arg4. |
|
533 |
||
15 | 534 |
exitBlock := [:v | |
45 | 535 |
(blockArgs at:1) value:oldValue1. |
536 |
(blockArgs at:2) value:oldValue2. |
|
537 |
(blockArgs at:3) value:oldValue3. |
|
538 |
(blockArgs at:4) value:oldValue4. |
|
539 |
^ v |
|
0 | 540 |
]. |
541 |
||
542 |
val := statements evaluate. |
|
543 |
||
544 |
(blockArgs at:1) value:oldValue1. |
|
545 |
(blockArgs at:2) value:oldValue2. |
|
546 |
(blockArgs at:3) value:oldValue3. |
|
547 |
(blockArgs at:4) value:oldValue4. |
|
548 |
^ val |
|
140 | 549 |
! |
3 | 550 |
|
551 |
valueWithArguments:argArray |
|
552 |
|oldValues val| |
|
553 |
||
554 |
(blockArgs size ~~ argArray size) ifTrue:[ |
|
45 | 555 |
^ self wrongNumberOfArguments:argArray size |
3 | 556 |
]. |
557 |
statements isNil ifTrue:[^ nil]. |
|
558 |
||
559 |
oldValues := Array new:(argArray size). |
|
560 |
1 to:argArray size do:[:i | |
|
45 | 561 |
oldValues at:i put:(blockArgs at:i) value. |
562 |
(blockArgs at:i) value:(argArray at:i). |
|
3 | 563 |
]. |
15 | 564 |
exitBlock := [:v | |
45 | 565 |
1 to:argArray size do:[:i | |
566 |
( blockArgs at:i) value:(oldValues at:i) |
|
567 |
]. |
|
568 |
^ v |
|
3 | 569 |
]. |
570 |
||
571 |
val := statements evaluate. |
|
572 |
||
573 |
1 to:argArray size do:[:i | |
|
45 | 574 |
(blockArgs at:i) value:(oldValues at:i) |
3 | 575 |
]. |
576 |
^ val |
|
140 | 577 |
! |
578 |
||
579 |
wrongNumberOfArguments:numberGiven |
|
580 |
Block argumentSignal |
|
581 |
raiseRequestWith:self |
|
582 |
errorString:('block got ' , numberGiven printString , |
|
583 |
' args while ' , blockArgs size printString , ' where expected') |
|
0 | 584 |
! ! |
585 |
||
586 |
!BlockNode methodsFor:'looping'! |
|
587 |
||
588 |
whileFalse:aBlock |
|
26 | 589 |
self value ifTrue:[^ nil]. |
590 |
aBlock value. |
|
591 |
thisContext restart |
|
0 | 592 |
! |
593 |
||
140 | 594 |
whileTrue:aBlock |
595 |
self value ifFalse:[^ nil]. |
|
596 |
aBlock value. |
|
597 |
thisContext restart |
|
0 | 598 |
! ! |
599 |
||
600 |
!BlockNode methodsFor:'printing'! |
|
601 |
||
602 |
printOn:aStream indent:i |
|
98 | 603 |
|n "{Class: SmallInteger }"| |
604 |
||
0 | 605 |
aStream nextPut:$[. |
98 | 606 |
(n := blockArgs size) > 0 ifTrue:[ |
607 |
1 to:n do:[:index | |
|
96 | 608 |
aStream nextPut:$:. |
609 |
aStream nextPutAll:(blockArgs at:index) name. |
|
610 |
aStream space. |
|
611 |
]. |
|
98 | 612 |
aStream nextPut:$|. |
613 |
aStream space. |
|
614 |
]. |
|
615 |
(n := blockVars size) > 0 ifTrue:[ |
|
616 |
aStream nextPut:$|. |
|
617 |
1 to:n do:[:index | |
|
618 |
aStream nextPutAll:(blockVars at:index) name. |
|
619 |
aStream space. |
|
620 |
]. |
|
621 |
aStream nextPut:$|. |
|
0 | 622 |
]. |
623 |
statements notNil ifTrue:[ |
|
45 | 624 |
aStream cr. |
625 |
statements printAllOn:aStream indent:i + 4. |
|
626 |
aStream cr. |
|
627 |
aStream spaces:i. |
|
0 | 628 |
]. |
629 |
aStream nextPut:$] |
|
630 |
! ! |
|
140 | 631 |
|
632 |
!BlockNode methodsFor:'private accessing'! |
|
633 |
||
634 |
setArguments:argList home:h variables:vars |
|
635 |
inlineBlock := false. |
|
636 |
needsHome := false. |
|
637 |
blockArgs := argList. |
|
638 |
home := h. |
|
639 |
blockVars := vars |
|
640 |
! ! |
|
641 |
||
642 |
!BlockNode methodsFor:'queries'! |
|
643 |
||
394 | 644 |
collectBlocksInto:aCollection |
645 |
aCollection add:self. |
|
458 | 646 |
statements notNil ifTrue:[statements collectBlocksInto:aCollection] |
394 | 647 |
|
648 |
"Created: 23.10.1996 / 15:45:16 / cg" |
|
649 |
"Modified: 23.10.1996 / 16:02:57 / cg" |
|
650 |
! |
|
651 |
||
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
652 |
endsWithReturn |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
653 |
statements isNil ifTrue:[ |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
654 |
^ false |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
655 |
]. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
656 |
^ statements listEndsWithReturn |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
657 |
|
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
658 |
"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
|
659 |
! |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
660 |
|
140 | 661 |
isBlock |
662 |
"a kludge, to have blocknodes mimic blocks" |
|
663 |
||
664 |
^ true |
|
168 | 665 |
! |
666 |
||
394 | 667 |
numArgs |
522 | 668 |
"return the number of arguments the block represented by myself |
669 |
expects for evaluation" |
|
670 |
||
394 | 671 |
^ blockArgs size |
672 |
||
673 |
"Created: 23.10.1996 / 15:57:04 / cg" |
|
522 | 674 |
"Modified: 7.5.1997 / 15:34:35 / cg" |
394 | 675 |
! |
676 |
||
677 |
numVars |
|
678 |
^ blockVars size |
|
679 |
||
680 |
"Created: 23.10.1996 / 16:17:07 / cg" |
|
681 |
! |
|
682 |
||
169 | 683 |
simpleSendBlockExpression |
684 |
blockVars notNil ifTrue:[^ nil]. |
|
685 |
statements isNil ifTrue:[^ nil]. |
|
686 |
statements nextStatement notNil ifTrue:[^ nil]. |
|
687 |
^ statements expression |
|
688 |
||
689 |
"Created: 13.12.1995 / 20:06:09 / cg" |
|
140 | 690 |
! ! |
691 |
||
366 | 692 |
!BlockNode class methodsFor:'documentation'! |
148 | 693 |
|
694 |
version |
|
553 | 695 |
^ '$Header: /cvs/stx/stx/libcomp/BlockNode.st,v 1.40 1997-06-25 11:16:52 cg Exp $' |
148 | 696 |
! ! |