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