-
Notifications
You must be signed in to change notification settings - Fork 71
/
TLabeledCommentNode.class.st
184 lines (141 loc) · 3.93 KB
/
TLabeledCommentNode.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
"
some comment
"
Class {
#name : #TLabeledCommentNode,
#superclass : #TParseNode,
#instVars : [
'label',
'asmLabel'
],
#category : #'Slang-AST'
}
{ #category : #accessing }
TLabeledCommentNode class >> label: aLabel [
^ self new
setLabel: aLabel;
yourself
]
{ #category : #accessing }
TLabeledCommentNode class >> withComment: aComment [
^ self new
setComment: aComment;
yourself
]
{ #category : #visiting }
TLabeledCommentNode >> accept: aVisitor [
^ aVisitor visitLabeledCommentNode: self
]
{ #category : #tranforming }
TLabeledCommentNode >> asCASTIn: aBuilder [
| result |
result := CCompoundStatementNode new.
result needsBrackets: false.
label ifNotNil: [ | labelledNode |
labelledNode := CLabeledStatementNode new.
labelledNode label: (CIdentifierNode name: label).
labelledNode statement: CEmptyStatementNode new.
result add: labelledNode ].
comment ifNotNil: [ | commentNode |
(aBuilder previousCommentMarksInlining: (label isNil and: [asmLabel isNil and: [comment beginsWith: 'begin ']]))
ifTrue: [ ^ result ].
commentNode := CEmptyStatementNode new.
commentNode comments: { self comment }.
commentNode needsSeparator: false.
result add: commentNode.
aBuilder previousCommenter: self].
(asmLabel notNil "only output labels in the interpret function."
and: [aBuilder currentMethod selector == #interpret]) ifTrue: [ | asmLabelNode |
asmLabelNode := aBuilder asmLabelNodeFor: asmLabel.
result add: asmLabelNode
].
^ result
]
{ #category : #accessing }
TLabeledCommentNode >> asmLabel [
^asmLabel
]
{ #category : #accessing }
TLabeledCommentNode >> asmLabel: labelString [
asmLabel := labelString
]
{ #category : #accessing }
TLabeledCommentNode >> children [
^ #()
]
{ #category : #testing }
TLabeledCommentNode >> isComment [
"Answer true if the receiver is just a comment (i.e., it has no label)."
^label = nil
]
{ #category : #testing }
TLabeledCommentNode >> isLabel [
^true
]
{ #category : #testing }
TLabeledCommentNode >> isLeaf [
^true
]
{ #category : #comparing }
TLabeledCommentNode >> isSameAs: anotherNode [
^anotherNode isLabel
and: [comment = anotherNode comment]
]
{ #category : #accessing }
TLabeledCommentNode >> label [
^label
]
{ #category : #testing }
TLabeledCommentNode >> needsTrailingSemicolon [
"Answer if, when emitted as a statement (in particular in a TStmtList), the
receiver needs a trailing semicolon. Comments do not. You'd think that
labels do not, but we put them at the end of blocks where there needs
to be a null statement following the label and before the end of block."
^self isComment not
]
{ #category : #enumerating }
TLabeledCommentNode >> nodesDo: aBlock parent: parent [
aBlock value: self value: parent
]
{ #category : #printing }
TLabeledCommentNode >> printOn: aStream level: level [
self printOptionalLabelOn: aStream.
comment ifNotNil:
[aStream nextPut: $".
aStream nextPutAll: comment.
aStream nextPut: $"]
]
{ #category : #printing }
TLabeledCommentNode >> printOptionalLabelOn: aStream [
label ifNotNil: [
self unindentTabs: aStream.
aStream
nextPutAll: label;
nextPut: $:;
cr;
nextPut: $; ]
]
{ #category : #accessing }
TLabeledCommentNode >> setComment: commentString [
label := nil.
comment := commentString.
]
{ #category : #accessing }
TLabeledCommentNode >> setLabel: labelString [
label := labelString.
]
{ #category : #accessing }
TLabeledCommentNode >> setLabel: labelString comment: commentString [
label := labelString.
comment := commentString.
]
{ #category : #'C code generation' }
TLabeledCommentNode >> unindentTabs: aStream [
"Remove all but one tab up to the beginning of line from the given stream if possible."
(aStream isKindOf: ReadWriteStream) ifFalse: [ ^self ].
[aStream position > 0] whileTrue:
[aStream position: aStream position - 1.
"restore stream position if previous char was not a tab"
aStream peek == Character tab ifFalse:
[^aStream next; tab]]
]