-
Notifications
You must be signed in to change notification settings - Fork 67
/
CSlangPrettyPrinter.class.st
567 lines (459 loc) · 14.9 KB
/
CSlangPrettyPrinter.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
Class {
#name : #CSlangPrettyPrinter,
#superclass : #CAbstractNodeVisitor,
#instVars : [
'stream',
'level'
],
#category : #'Slang-AST'
}
{ #category : #accessing }
CSlangPrettyPrinter >> contents [
^ stream contents
]
{ #category : #initialization }
CSlangPrettyPrinter >> initialize [
super initialize.
stream := '' writeStream.
level := 0
]
{ #category : #visiting }
CSlangPrettyPrinter >> printComments: aListOfComments [
aListOfComments do: [ :e |
stream
nextPutAll: '/* ';
nextPutAll: e;
nextPutAll: ' */'.
] separatedBy: [ stream cr ].
]
{ #category : #generated }
CSlangPrettyPrinter >> printExpression: cAST [
(cAST isLeaf
"Cast expressions already contain their own parentheses"
or: [ cAST isCastExpression
"An unary expression that has a leaf expression dosen't need parentheses aroud expression"
or: [cAST isUnaryOperation and: [cAST expression isLeaf or: [ cAST needsParentheses]]]])
ifTrue: [ cAST acceptVisitor: self ]
ifFalse: [
stream nextPut: $(.
cAST acceptVisitor: self.
stream nextPut: $) ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitArray: anArrayAcess [
anArrayAcess array acceptVisitor: self.
stream nextPut: $[.
anArrayAcess index acceptVisitor: self.
stream nextPut: $]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitArrayDeclarator: anArrayDeclarator [
anArrayDeclarator declarator acceptVisitor: self.
stream nextPut: $[.
anArrayDeclarator size ifNotNil: [ :size |
stream nextPutAll: size asString ].
stream nextPut: $]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitArrow: aPointerAccess [
aPointerAccess needsParentheses ifTrue: [ stream nextPut: $(].
self printExpression: aPointerAccess structurePointer.
stream nextPutAll: '->'.
aPointerAccess member acceptVisitor: self.
aPointerAccess needsParentheses ifTrue: [ stream nextPut: $)].
]
{ #category : #generated }
CSlangPrettyPrinter >> visitAssignment: anAssignment [
anAssignment needsParentheses ifTrue: [ stream nextPut: $( ].
anAssignment lvalue acceptVisitor: self.
stream
nextPutAll: ' ';
nextPutAll: anAssignment operator;
nextPutAll: ' '.
anAssignment rvalue acceptVisitor: self.
anAssignment needsParentheses ifTrue: [ stream nextPut: $) ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitBinary: aBinary [
aBinary needsParentheses ifTrue: [ stream nextPut: $( ].
self printExpression: aBinary left.
stream
nextPutAll: ' ';
nextPutAll: aBinary operator;
nextPutAll: ' '.
self printExpression: aBinary right.
aBinary needsParentheses ifTrue: [ stream nextPut: $) ].
]
{ #category : #generated }
CSlangPrettyPrinter >> visitBreakStatement: aBreakStatement [
stream nextPutAll: 'break'
]
{ #category : #generated }
CSlangPrettyPrinter >> visitCall: aCall [
aCall identifier isIdentifier ifFalse: [ stream nextPut: $( ].
aCall identifier acceptVisitor: self.
aCall identifier isIdentifier ifFalse: [ stream nextPut: $) ].
stream nextPutAll: '('.
aCall arguments do: [ :arg | arg acceptVisitor: self ] separatedBy: [ stream nextPutAll: ', '].
stream nextPutAll: ')'.
]
{ #category : #generated }
CSlangPrettyPrinter >> visitCastExpression: aCastExpression [
| nbParentheses |
nbParentheses := aCastExpression needsParentheses ifTrue: [ 2 ] ifFalse: [ 1 ].
"Wrap everything between parentheses to avoid ambiguities"
stream next: nbParentheses put: $(.
"Print the type between parentheses"
stream nextPut: $(.
aCastExpression type acceptVisitor: self.
stream nextPutAll: ') '.
"Print the expression to cast"
self printExpression: aCastExpression expression.
stream space.
stream next: nbParentheses put: $)
]
{ #category : #generated }
CSlangPrettyPrinter >> visitComment: aComment [
stream nextPutAll: '/*'.
stream nextPutAll: aComment comment.
stream nextPutAll: '*/'
]
{ #category : #generated }
CSlangPrettyPrinter >> visitCompoundStatement: aCompoundStatement [
| first |
first := true.
aCompoundStatement needsBrackets ifTrue: [
"If we print brackets, then the first was already printed"
first := false.
stream nextPutAll: '{'.
stream cr.
level := level + 1 ].
aCompoundStatement declarations do: [ :e |
first
ifTrue: [ first := false. ]
ifFalse: [ stream tab: level ].
e acceptVisitor: self.
e needsSeparator ifTrue: [ stream nextPut: $; ].
stream cr. ].
"Put at least one empty line to separate declarations and statements.
This is required for the current GNUisation to work, which searches for empty lines"
aCompoundStatement declarations ifNotEmpty: [
stream cr.
].
aCompoundStatement statements do: [ :e |
first
ifTrue: [ first := false. ]
ifFalse: [ stream tab: level ].
e acceptVisitor: self.
e needsSeparator ifTrue: [ stream nextPut: $; ]]
separatedBy: [ stream cr ].
aCompoundStatement needsBrackets ifTrue: [
level := level - 1.
(aCompoundStatement statements, aCompoundStatement declarations)
ifNotEmpty: [ stream cr ].
stream tab: level.
stream nextPut: $} ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitConstant: aConstant [
stream nextPutAll: aConstant value asString
]
{ #category : #generated }
CSlangPrettyPrinter >> visitDeclaration: aDeclaration [
aDeclaration specifiers do: [ :e |
stream nextPutAll: e.
stream space ].
aDeclaration hasRawPrototype ifFalse: [
aDeclaration declarators
do: [ :e | e acceptVisitor: self ]
separatedBy: [ stream nextPutAll: ', ' ] ].
aDeclaration isFunctionPrototype ifTrue: [ stream nextPut: $; ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitDeclarator: aDeclarator [
aDeclarator qualifiers do: [ :e |
stream nextPutAll: e.
stream space ].
aDeclarator pointers do: [ :e | stream nextPutAll: e ].
aDeclarator declarator acceptVisitor: self
]
{ #category : #generated }
CSlangPrettyPrinter >> visitDecrement: anDecrementNode [
| visit |
visit := [ anDecrementNode object acceptVisitor: self ].
anDecrementNode prefix ifFalse: visit.
stream nextPutAll: '--'.
anDecrementNode prefix ifTrue: visit.
]
{ #category : #generated }
CSlangPrettyPrinter >> visitDoStatement: aDoStatement [
stream nextPutAll: 'do'.
aDoStatement statement acceptVisitor: self.
stream nextPutAll: 'while('.
aDoStatement while acceptVisitor: self.
stream nextPut: $)
]
{ #category : #generated }
CSlangPrettyPrinter >> visitEmptyStatement: anEmptyStatement [
"Nothing to print."
self printComments: anEmptyStatement comments
]
{ #category : #generated }
CSlangPrettyPrinter >> visitExpressionList: anExpressionList [
| lastRequiredSeparator |
stream nextPut: $(.
anExpressionList expressions do: [ :e |
e acceptVisitor: self.
lastRequiredSeparator := e isEmpty not ]
separatedBy: [
lastRequiredSeparator
ifFalse: [ stream nextPutAll: ' ' ]
ifTrue: [
stream nextPutAll: ', '.
anExpressionList printOnMultipleLines ifTrue: [ stream cr ] ] ].
stream nextPut: $)
]
{ #category : #generated }
CSlangPrettyPrinter >> visitForStatement: aForStatement [
stream nextPutAll: 'for ('.
aForStatement init
do: [ :e | e acceptVisitor: self ]
separatedBy: [ stream nextPutAll: ', ' ].
stream nextPutAll: '; '.
aForStatement until acceptVisitor: self.
stream nextPutAll: '; '.
aForStatement step
do: [ :e | e acceptVisitor: self ]
separatedBy: [ stream nextPutAll: ', ' ].
stream nextPutAll: ') '.
aForStatement statement acceptVisitor: self
]
{ #category : #generated }
CSlangPrettyPrinter >> visitFunctionDeclarator: aFunctionDeclarator [
aFunctionDeclarator declarator acceptVisitor: self.
stream nextPut: $(.
aFunctionDeclarator parameters
do: [ :arg | arg acceptVisitor: self ]
separatedBy: [ stream nextPutAll: ', ' ].
stream nextPut: $)
]
{ #category : #generated }
CSlangPrettyPrinter >> visitFunctionDefinition: aFunction [
aFunction specifiers
do: [ :e | stream nextPutAll: e ]
separatedBy: [ stream space ].
stream cr.
aFunction hasRawPrototype ifFalse: [
aFunction declarator acceptVisitor: self ].
stream cr.
aFunction body acceptVisitor: self.
stream cr.
]
{ #category : #generated }
CSlangPrettyPrinter >> visitGotoStatement: aGoToStatement [
stream nextPutAll: 'goto ';
nextPutAll: aGoToStatement label name
]
{ #category : #generated }
CSlangPrettyPrinter >> visitIdentifier: anIdentifier [
stream nextPutAll: anIdentifier name
]
{ #category : #generated }
CSlangPrettyPrinter >> visitIfStatement: anIf [
stream nextPutAll: 'if ('.
anIf if acceptVisitor: self.
stream nextPutAll: ') '.
anIf then isCompoundStatement ifFalse: [ stream crtab: level + 1 ].
anIf then acceptVisitor: self.
anIf then needsTrailingSemicolon ifTrue: [ stream nextPut: $; ].
anIf else ifNotNil: [ :e |
anIf then isCompoundStatement ifFalse: [ stream crtab: level ].
stream nextPutAll: ' else '.
anIf else isCompoundStatement ifFalse: [ stream crtab: level + 1 ].
e acceptVisitor: self.
anIf else needsTrailingSemicolon ifTrue: [ stream nextPut: $; ]]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitIncrement: anIncrementNode [
| visit |
visit := [ anIncrementNode object acceptVisitor: self ].
anIncrementNode prefix ifFalse: visit.
stream nextPutAll: '++'.
anIncrementNode prefix ifTrue: visit.
]
{ #category : #generated }
CSlangPrettyPrinter >> visitInitDeclarator: anInitDeclarator [
anInitDeclarator declarator acceptVisitor: self.
stream nextPutAll: ' = '.
anInitDeclarator initializer acceptVisitor: self.
]
{ #category : #generated }
CSlangPrettyPrinter >> visitInitializer: anInitializer [
stream
nextPut: ${;
crtab: level + 1.
anInitializer initializers
do: [ :e | e acceptVisitor: self ]
separatedBy: [ stream nextPutAll: ', ' ].
stream crtab: level.
stream nextPut: $}
]
{ #category : #generated }
CSlangPrettyPrinter >> visitLabeledStatement: aLabeledStatement [
"Case and labels are overloaded in the same node..."
aLabeledStatement case ifNotNil: [
aLabeledStatement case = 'default'
ifTrue: [ stream nextPutAll: 'default' ]
ifFalse: [
stream nextPutAll: 'case '.
aLabeledStatement case acceptVisitor: self ] ].
aLabeledStatement label ifNotNil: [
stream nextPutAll: aLabeledStatement label name ].
(aLabeledStatement case notNil or: [ aLabeledStatement label notNil ])
ifTrue: [ stream nextPutAll: ':'.
aLabeledStatement comments ifNotEmpty: [ stream cr ] ].
self printComments: aLabeledStatement comments.
stream crtab: level.
aLabeledStatement statement acceptVisitor: self.
"Close the statement here to keep compatibility with slang"
((aLabeledStatement case notNil or: [ aLabeledStatement label notNil ])
and: [ aLabeledStatement statement needsSeparator ]) ifTrue: [
stream nextPutAll: ';' ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitParameterDeclaration: aParameter [
"Raw declaration wins over a normal declaration."
aParameter rawDeclaration
ifNotNil: [ stream nextPutAll: aParameter rawDeclaration ]
ifNil: [
aParameter specifiers do: [ :e |
stream nextPutAll: e.
stream space ].
aParameter declarator acceptVisitor: self ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitPoint: aMemberAccess [
aMemberAccess needsParentheses ifTrue: [ stream nextPut: $(].
self printExpression: aMemberAccess object.
stream nextPut: $..
aMemberAccess member acceptVisitor: self.
aMemberAccess needsParentheses ifTrue: [ stream nextPut: $)].
]
{ #category : #generated }
CSlangPrettyPrinter >> visitPreprocessorDefine: aPreprocessorDefine [
stream nextPutAll: '#define '.
aPreprocessorDefine token acceptVisitor: self.
stream space.
"Raw declaration wins over a normal declaration."
aPreprocessorDefine rawMacro
ifNotNil: [ stream nextPutAll: aPreprocessorDefine rawMacro ]
ifNil: [ aPreprocessorDefine expression acceptVisitor: self ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitPreprocessorIf: anIf [
| previousLevel tabbing |
previousLevel := level.
level := anIf isArgument ifTrue: [
tabbing := level + 1.
level + 2 ]
ifFalse: [
tabbing := level ].
stream
cr;
nextPut: $#;
next: tabbing * 2 put: Character space;
nextPutAll: 'if '.
anIf if acceptVisitor: self.
stream crtab: level.
anIf then acceptVisitor: self.
anIf else ifNotNil: [ :e |
stream
cr;
nextPut: $#;
next: tabbing * 2 put: Character space;
nextPutAll: 'else /* '.
anIf if acceptVisitor: self.
stream
nextPutAll: ' */';
crtab: level.
e acceptVisitor: self ].
stream
cr;
nextPut: $#;
next: tabbing * 2 put: Character space;
nextPutAll: 'endif /* '.
anIf if acceptVisitor: self.
stream
nextPutAll: ' */'.
anIf isArgument ifTrue: [ stream crtab: level ].
level := previousLevel.
]
{ #category : #visiting }
CSlangPrettyPrinter >> visitRawCode: aCRawCodeNode [
stream nextPutAll: aCRawCodeNode code asString
]
{ #category : #generated }
CSlangPrettyPrinter >> visitReturnStatement: aReturn [
stream nextPutAll: 'return'.
aReturn expression ifNotNil: [ :exp |
stream space.
exp acceptVisitor: self ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitStringLiteral: aStringLiteral [
aStringLiteral needsDoubleQuotes ifTrue: [ stream nextPut: $" ].
stream nextPutAll: aStringLiteral value.
aStringLiteral needsDoubleQuotes ifTrue: [ stream nextPut: $" ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitSwitchStatement: aSwitchStatement [
stream nextPutAll: 'switch ('.
aSwitchStatement if acceptVisitor: self.
stream nextPutAll: ') '.
aSwitchStatement statement acceptVisitor: self.
]
{ #category : #generated }
CSlangPrettyPrinter >> visitTernary: aTernary [
stream nextPutAll: '(('.
aTernary condition acceptVisitor: self.
stream nextPut: $).
aTernary printOnMultipleLines ifTrue: [
level := level + 1.
stream crtab: level ].
stream nextPutAll: ' ? '.
aTernary then acceptVisitor: self.
aTernary printOnMultipleLines ifTrue: [ stream crtab: level ].
stream nextPutAll: ' : '.
aTernary else acceptVisitor: self.
stream nextPut: $).
aTernary printOnMultipleLines ifTrue: [ level := level - 1 ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitTypename: aTypename [
stream nextPutAll: aTypename symbol
]
{ #category : #generated }
CSlangPrettyPrinter >> visitUnaryOperator: anUnary [
| avoidParentheses |
anUnary needsParentheses ifTrue: [ stream nextPut: $( ].
avoidParentheses := anUnary expression isCastExpression or: [
anUnary expression isConstant or: [
anUnary expression isIdentifier or: [
anUnary expression isLeaf ] ] ].
stream nextPutAll: anUnary operator.
avoidParentheses ifFalse: [ stream nextPut: $( ].
anUnary expression acceptVisitor: self.
avoidParentheses ifFalse: [ stream nextPut: $) ].
anUnary needsParentheses ifTrue: [ stream nextPut: $) ]
]
{ #category : #generated }
CSlangPrettyPrinter >> visitWhileStatement: aWhileStatement [
stream nextPutAll: 'while ('.
aWhileStatement while acceptVisitor: self.
stream nextPutAll: ') '.
aWhileStatement statement acceptVisitor: self
]
{ #category : #accessing }
CSlangPrettyPrinter >> writeStream: aWriteStream [
stream := aWriteStream
]