-
Notifications
You must be signed in to change notification settings - Fork 69
/
TSwitchStmtNode.class.st
469 lines (417 loc) · 15.1 KB
/
TSwitchStmtNode.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
"
I implement a Smalltalk
foo caseOf: { [IntegerConstant | GlobalVariable] -> [expr] }
statement converting it into a C switch statement. I make some effort to discover identical right-hand-side cases.
"
Class {
#name : #TSwitchStmtNode,
#superclass : #TParseNode,
#instVars : [
'expression',
'cases',
'otherwiseOrNil',
'switchVariable'
],
#category : #'Slang-AST'
}
{ #category : #visiting }
TSwitchStmtNode >> accept: aVisitor [
^ aVisitor visitSwitchStatementNode: self
]
{ #category : #transforming }
TSwitchStmtNode >> asCASTExpressionIn: aBuilder [
| var ternary |
ternary := nil.
self assert: (expression isVariable or: [ switchVariable notNil ]).
var := [ :i |
switchVariable
ifNil: [ expression asCASTIn: aBuilder ]
ifNotNil: [
i = 1
ifTrue: [
CAssignmentNode
lvalue: (CIdentifierNode name: switchVariable)
rvalue: (expression asCASTIn: aBuilder) ]
ifFalse: [ CIdentifierNode name: switchVariable ] ] ].
cases size to: 1 by: -1 do: [ :i |
[ :labels :case |
labels size to: 1 by: -1 do: [ :j |
ternary := ternary
ifNil: [
(CTernaryNode
condition: (CBinaryOperatorNode
operator: #==
left: (var value: i)
right: ((labels at: j) asCASTExpressionIn: aBuilder))
then:
((TStatementListNode new
setArguments: #( )
statements: case statements)
asCASTExpressionIn: aBuilder)
else: (otherwiseOrNil
ifNotNil: [
otherwiseOrNil asCASTExpressionIn: aBuilder ]
ifNil: [
CExpressionListNode new expressions: {
(CCallNode
identifier: (CIdentifierNode name: 'error')
arguments:
{ (CStringLiteralNode value:
'Case not found and no otherwise clause') }).
(CConstantNode value: 0) } ]))
printOnMultipleLines: true;
yourself ]
ifNotNil: [
(CTernaryNode
condition: (CBinaryOperatorNode
operator: #==
left: (var value: i)
right: ((labels at: j) asCASTExpressionIn: aBuilder))
then:
((TStatementListNode new
setArguments: #( )
statements: case statements)
asCASTExpressionIn: aBuilder)
else: ternary)
printOnMultipleLines: true;
yourself ] ] ] valueWithArguments: (cases at: i) ].
^ ternary
]
{ #category : #transforming }
TSwitchStmtNode >> asCASTIn: aBuilder [
| statements |
statements := CCompoundStatementNode new.
cases do: [ :tuple |
[ :labels :case |
| nestedCase statement |
statement := case asCASTIn: aBuilder.
labels size to: 1 by: -1 do: [ :i |
nestedCase := CLabeledStatementNode
case: ((labels at: i) asCASTExpressionIn: aBuilder)
statement: (i = labels size
ifTrue: [ statement ]
ifFalse: [ nestedCase ]) ].
statements add: nestedCase.
case endsWithReturn ifFalse: [
statements add: CBreakStatementNode new ] ] valueWithArguments:
tuple ].
statements add: (CLabeledStatementNode
case: 'default'
statement: (otherwiseOrNil
ifNotNil: [ otherwiseOrNil asCASTIn: aBuilder ]
ifNil: [
CCallNode
identifier: (CIdentifierNode name: 'error')
arguments:
{ (CStringLiteralNode value:
'Case not found and no otherwise clause') } ])).
^ CSwitchStatementNode
if: (expression asCASTExpressionIn: aBuilder)
statement: statements
]
{ #category : #transforming }
TSwitchStmtNode >> asCASTIn: aBuilder addToEndOfCases: aNodeToPrependOrNil [
| statements |
statements := CCompoundStatementNode new.
cases do: [ :tuple |
[ :labels :case |
| nestedCase statement |
statement := case
asCASTIn: aBuilder
prependToEnd: aNodeToPrependOrNil.
labels size to: 1 by: -1 do: [ :i |
nestedCase := CLabeledStatementNode
case: ((labels at: i) asCASTExpressionIn: aBuilder)
statement: (i = labels size
ifTrue: [ statement ]
ifFalse: [ nestedCase ]) ].
statements add: nestedCase ] valueWithArguments: tuple.
(aNodeToPrependOrNil notNil and: [ aNodeToPrependOrNil isReturn ])
ifFalse: [ statements add: CBreakStatementNode new ] ].
statements add:
(CLabeledStatementNode case: 'default' statement: (otherwiseOrNil
ifNotNil: [
otherwiseOrNil
asCASTIn: aBuilder
prependToEnd: aNodeToPrependOrNil ]
ifNil: [
CCallNode
identifier: (CIdentifierNode name: 'error')
arguments:
{ (CStringLiteralNode value:
'Case not found and no otherwise clause') } ])).
(otherwiseOrNil isNil and: [ aNodeToPrependOrNil isNotNil ]) ifTrue: [
| defaultExpr type |
defaultExpr := TConstantNode value: -1.
(aNodeToPrependOrNil isAssignment and: [
(type := aBuilder
typeFor: aNodeToPrependOrNil variable
in: aBuilder currentMethod) notNil and: [
aBuilder isPointerCType: type ] ]) ifTrue: [
defaultExpr := TSendNode new
setSelector: #cCoerceSimple:to:
receiver: (TVariableNode new setName: 'self')
arguments: {
defaultExpr.
(TConstantNode value: type) } ].
statements add:
((aNodeToPrependOrNil copy expression: defaultExpr) asCASTIn:
aBuilder) ].
^ CSwitchStatementNode
if: (expression asCASTExpressionIn: aBuilder)
statement: statements
]
{ #category : #transformations }
TSwitchStmtNode >> bindVariableUsesIn: aDictionary [
expression := expression bindVariableUsesIn: aDictionary.
cases := (cases collect:
[:pair| "<Array with: <Array with: ParseTreeNode> with: TStmtListNode"
{ pair first collect: [:labelNode| labelNode bindVariableUsesIn: aDictionary].
pair last bindVariableUsesIn: aDictionary }]).
otherwiseOrNil ifNotNil:
[otherwiseOrNil := otherwiseOrNil bindVariableUsesIn: aDictionary]
]
{ #category : #transformations }
TSwitchStmtNode >> bindVariableUsesIn: aDictionary andConstantFoldIf: constantFold in: codeGen [
"Answer either the receiver, if it contains no references to the given variables, or a new node with the given variables rebound."
| newExpression newCases newOtherwise |
newExpression := expression bindVariableUsesIn: aDictionary andConstantFoldIf: constantFold in: codeGen.
newCases := cases collect:
[:pair| "<Array with: <Array with: ParseTreeNode> with: TStmtListNode"
{ pair first collect: [:labelNode| labelNode bindVariableUsesIn: aDictionary andConstantFoldIf: constantFold in: codeGen].
pair last bindVariableUsesIn: aDictionary andConstantFoldIf: constantFold in: codeGen}].
newOtherwise := otherwiseOrNil ifNotNil:
[otherwiseOrNil bindVariableUsesIn: aDictionary andConstantFoldIf: constantFold in: codeGen].
^(newExpression = expression
and: [newCases = cases
and: [newOtherwise = otherwiseOrNil]])
ifTrue: [self]
ifFalse:
[self shallowCopy
expression: newExpression;
cases: newCases;
otherwiseOrNil: newOtherwise;
yourself]
]
{ #category : #transformations }
TSwitchStmtNode >> bindVariablesIn: aDictionary [
expression := expression bindVariablesIn: aDictionary.
cases := (cases collect:
[:pair| "<Array with: <Array with: ParseTreeNode> with: TStmtListNode"
{ pair first collect: [:labelNode| labelNode bindVariablesIn: aDictionary].
pair last bindVariablesIn: aDictionary }]).
otherwiseOrNil ifNotNil:
[otherwiseOrNil := otherwiseOrNil bindVariablesIn: aDictionary]
]
{ #category : #transformations }
TSwitchStmtNode >> bindVariablesIn: aDictionary unless: cautionaryBlock [
(cautionaryBlock value: self) ifTrue: [^self].
expression := expression bindVariablesIn: aDictionary unless: cautionaryBlock.
cases := (cases collect:
[:pair| "<Array with: <Array with: ParseTreeNode> with: TStmtListNode"
{ pair first collect: [:labelNode| labelNode bindVariablesIn: aDictionary unless: cautionaryBlock].
pair last bindVariablesIn: aDictionary unless: cautionaryBlock }]).
otherwiseOrNil ifNotNil:
[otherwiseOrNil := otherwiseOrNil bindVariablesIn: aDictionary unless: cautionaryBlock]
]
{ #category : #accessing }
TSwitchStmtNode >> cases [
"Answer the value of cases"
^ cases
]
{ #category : #accessing }
TSwitchStmtNode >> cases: anObject [
"Set the value of cases"
cases := anObject
]
{ #category : #accessing }
TSwitchStmtNode >> children [
^ { expression },
cases flattened,
(otherwiseOrNil ifNil: [ #() ] ifNotNil: [ {otherwiseOrNil} ])
]
{ #category : #'instance initialization' }
TSwitchStmtNode >> createCasesFromBraceNode: aTBraceNode [
"Answer a sequence of tuples of { labels. case } for a TBraceNode, making
sure to collect equivalent cases together under a signle sequence of labels."
| casesToStrings stringsToLabels newCases |
casesToStrings := Dictionary new.
stringsToLabels := Dictionary new.
newCases := OrderedCollection new: aTBraceNode caseLabels size.
aTBraceNode caseLabels with: aTBraceNode cases do:
[:label :case| | printString |
printString := casesToStrings at: case put: case printString.
(stringsToLabels at: printString ifAbsentPut: [OrderedCollection new]) addLast: label].
aTBraceNode caseLabels with: aTBraceNode cases do:
[:label :case| | printString labels |
printString := casesToStrings at: case.
label = (labels := (stringsToLabels at: printString) asArray) first ifTrue:
[newCases addLast: { labels collect: [:ea| ea statements first]. case}]].
^newCases
]
{ #category : #accessing }
TSwitchStmtNode >> expression [
"Answer the value of expression"
^ expression
]
{ #category : #accessing }
TSwitchStmtNode >> expression: anObject [
"Set the value of expression"
expression := anObject
]
{ #category : #'instance initialization' }
TSwitchStmtNode >> expression: expr cases: aTBraceNode otherwiseOrNil: otherwiseOrNilNode [
self expression: expr.
self cases: (self createCasesFromBraceNode: aTBraceNode).
self otherwiseOrNil: otherwiseOrNilNode
]
{ #category : #comparing }
TSwitchStmtNode >> isSameAs: anotherNode [
anotherNode isSwitch ifFalse: [ ^ false ].
(expression isSameAs: anotherNode expression)
ifFalse: [ ^ false ].
cases size ~= anotherNode cases size ifTrue: [ ^ false ].
cases with: anotherNode cases do: [ :case1 :case2 |
case1 first = case2 first
ifFalse: [ ^ false ].
(case1 second isSameAs: case2 second)
ifFalse: [ ^ false ]
].
"Apparently it went ok after all this"
^ true
]
{ #category : #testing }
TSwitchStmtNode >> isSwitch [
^true
]
{ #category : #enumerating }
TSwitchStmtNode >> nodesDo: aBlock [
"Apply aBlock to all nodes in the receiver.
N.B. This is assumed to be bottom-up, leaves first."
expression nodesDo: aBlock.
cases do:
[:pair|
pair first do: [:node| node nodesDo: aBlock].
pair last nodesDo: aBlock].
otherwiseOrNil ifNotNil:
[otherwiseOrNil nodesDo: aBlock].
aBlock value: self
]
{ #category : #enumerating }
TSwitchStmtNode >> nodesDo: aBlock parent: parent [
"Apply aBlock to all nodes in the receiver with each node's parent.
N.B. This is assumed to be bottom-up, leaves first."
expression nodesDo: aBlock parent: self.
cases do:
[:pair|
pair first do: [:node| node nodesDo: aBlock parent: self.].
pair last nodesDo: aBlock parent: self.].
otherwiseOrNil ifNotNil:
[otherwiseOrNil nodesDo: aBlock parent: self].
aBlock value: self value: parent
]
{ #category : #enumerating }
TSwitchStmtNode >> nodesDo: aBlock parent: parent unless: cautionaryBlock [
(cautionaryBlock value: self value: parent) ifTrue: [^self].
expression nodesDo: aBlock parent: self unless: cautionaryBlock.
cases do:
[:pair|
pair first do: [:node| node nodesDo: aBlock parent: self unless: cautionaryBlock].
pair last nodesDo: aBlock parent: self unless: cautionaryBlock].
otherwiseOrNil ifNotNil:
[otherwiseOrNil nodesDo: aBlock parent: self unless: cautionaryBlock]
]
{ #category : #enumerating }
TSwitchStmtNode >> nodesDo: aBlock unless: cautionaryBlock [
(cautionaryBlock value: self) ifTrue: [^self].
expression nodesDo: aBlock unless: cautionaryBlock.
cases do:
[:pair|
pair first do: [:node| node nodesDo: aBlock unless: cautionaryBlock].
pair last nodesDo: aBlock unless: cautionaryBlock].
otherwiseOrNil ifNotNil:
[otherwiseOrNil nodesDo: aBlock unless: cautionaryBlock]
]
{ #category : #accessing }
TSwitchStmtNode >> otherwiseOrNil [
"Answer the value of otherwiseOrNil"
^ otherwiseOrNil
]
{ #category : #accessing }
TSwitchStmtNode >> otherwiseOrNil: anObject [
"Set the value of otherwiseOrNil"
otherwiseOrNil := anObject
]
{ #category : #copying }
TSwitchStmtNode >> postCopy [
expression := expression copy.
cases := (cases collect:
[:pair| "<Array with: <Array with: ParseTreeNode> with: TStmtListNode"
{ pair first collect: [:labelNode| labelNode copy].
pair last copy }]).
otherwiseOrNil := otherwiseOrNil copy
]
{ #category : #printing }
TSwitchStmtNode >> printOn: aStream level: level [
aStream crtab: level.
aStream nextPutAll: 'switch ('.
expression printOn: aStream level: level.
aStream nextPutAll: ') {'.
cases do:
[:tuple|
[:labels :case|
labels do:
[:label|
aStream
crtab: level;
nextPutAll: 'case '.
label printOn: aStream level: level + 1.
aStream nextPut: $:].
aStream crtab: level + 1.
case printOn: aStream level: level + 1.
aStream crtab: level + 1; nextPutAll: 'break;']
valueWithArguments: tuple].
otherwiseOrNil ifNotNil:
[aStream
crtab: level;
nextPutAll: 'default:';
crtab: level + 1.
otherwiseOrNil printOn: aStream level: level + 1].
aStream
crtab: level;
nextPut: $}
]
{ #category : #transformations }
TSwitchStmtNode >> removeAssertions [
expression removeAssertions.
cases do:
[:pair| "<Array with: <Array with: ParseTreeNode> with: TStmtListNode"
pair first do: [:labelNode| labelNode removeAssertions].
pair last removeAssertions].
otherwiseOrNil ifNotNil:
[otherwiseOrNil removeAssertions]
]
{ #category : #transformations }
TSwitchStmtNode >> replaceNodesIn: aDictionary [
^aDictionary
at: self
ifAbsent:
[expression := expression replaceNodesIn: aDictionary.
cases := (cases collect:
[:pair| "<Array with: <Array with: ParseTreeNode> with: TStmtListNode"
{ pair first collect: [:labelNode| labelNode replaceNodesIn: aDictionary].
pair last replaceNodesIn: aDictionary }]).
otherwiseOrNil ifNotNil:
[otherwiseOrNil := otherwiseOrNil replaceNodesIn: aDictionary].
self]
]
{ #category : #accessing }
TSwitchStmtNode >> switchVariable [
"Answer the value of switchVariable"
^ switchVariable
]
{ #category : #accessing }
TSwitchStmtNode >> switchVariable: anObject [
"Set the value of switchVariable"
switchVariable := anObject
]