Class: Crokus::Transformer
- Inherits:
-
Object
- Object
- Crokus::Transformer
show all
- Defined in:
- lib/crokus/transformer.rb
Overview
here we transform an AST into another AST. we don’t use Marshalling.
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#initialize ⇒ Transformer
constructor
A new instance of Transformer.
-
#transform(ast) ⇒ Object
(also: #visit)
-
#visitAddressOf(ao, args = nil) ⇒ Object
-
#visitArrayOf(aof, args = nil) ⇒ Object
-
#visitArrayOrStructInit(init, args = nil) ⇒ Object
-
#visitArrow(arrow, args = nil) ⇒ Object
-
#visitAssign(assign, args = nil) ⇒ Object
-
#visitBinary(expr, args = nil) ⇒ Object
-
#visitBody(body, args = nil) ⇒ Object
-
#visitBreak(brk, args = nil) ⇒ Object
-
#visitCase(case_, args = nil) ⇒ Object
-
#visitCastedExpr(cexpr, args = nil) ⇒ Object
-
#visitCasting(cast, args = nil) ⇒ Object
-
#visitCharLit(lit, args = nil) ⇒ Object
-
#visitCommaStmt(comma, args = nil) ⇒ Object
-
#visitCondExpr(ternary, args = nil) ⇒ Object
-
#visitContinue(cont, args = nil) ⇒ Object
-
#visitDecl(decl, args = nil) ⇒ Object
-
#visitDefine(define, args = nil) ⇒ Object
-
#visitDeref(deref, args = nil) ⇒ Object
-
#visitDesignUnit(du, args = nil) ⇒ Object
-
#visitDotted(dotted, args = nil) ⇒ Object
-
#visitDoWhile(while_, args = nil) ⇒ Object
-
#visitElse(else_, args = nil) ⇒ Object
-
#visitFloatLit(lit, args = nil) ⇒ Object
-
#visitFor(for_, args = nil) ⇒ Object
-
#visitFormalArg(formalArg, args = nil) ⇒ Object
-
#visitFunCall(fcall, args = nil) ⇒ Object
-
#visitFunction(func, args = nil) ⇒ Object
-
#visitFunctionProto(func, args = nil) ⇒ Object
-
#visitGoto(goto, args = nil) ⇒ Object
-
#visitIdent(ident, args = nil) ⇒ Object
-
#visitIf(if_, args = nil) ⇒ Object
-
#visitInclude(incl, args = nil) ⇒ Object
-
#visitIndexed(index, args = nil) ⇒ Object
-
#visitIntLit(lit, args = nil) ⇒ Object
-
#visitLabeledStmt(lstmt, args = nil) ⇒ Object
-
#visitLabelledStmt(label, args = nil) ⇒ Object
-
#visitParenth(par, args = nil) ⇒ Object
-
#visitPointerTo(pto, args = nil) ⇒ Object
-
#visitPostFixAccu(accu, args = nil) ⇒ Object
-
#visitPreFixAccu(accu, args = nil) ⇒ Object
-
#visitReturn(ret, args = nil) ⇒ Object
-
#visitSizeof(sizeof, args = nil) ⇒ Object
-
#visitStrLit(lit, args = nil) ⇒ Object
-
#visitStruct(struct, args = nil) ⇒ Object
-
#visitSwitch(sw_, args = nil) ⇒ Object
-
#visitToken(tok, args = nil) ⇒ Object
-
#visitType(type, args = nil) ⇒ Object
-
#visitTypedef(typdef, args = nil) ⇒ Object
-
#visitUnary(unary, args = nil) ⇒ Object
-
#visitWhile(while_, args = nil) ⇒ Object
Constructor Details
Returns a new instance of Transformer.
13
14
15
16
17
|
# File 'lib/crokus/transformer.rb', line 13
def initialize
@ind=-2
@verbose=true
@verbose=false
end
|
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
11
12
13
|
# File 'lib/crokus/transformer.rb', line 11
def code
@code
end
|
Instance Method Details
19
20
21
|
# File 'lib/crokus/transformer.rb', line 19
def transform ast
new_ast=ast.accept(self)
end
|
#visitAddressOf(ao, args = nil) ⇒ Object
288
289
290
291
|
# File 'lib/crokus/transformer.rb', line 288
def visitAddressOf ao,args=nil
e=ao.expr.accept(self)
AddressOf.new(e)
end
|
#visitArrayOf(aof, args = nil) ⇒ Object
69
70
71
72
73
|
# File 'lib/crokus/transformer.rb', line 69
def visitArrayOf aof,args=nil
type=aof.type.accept(self)
size=aof.size.accept(self) if aof.size
ArrayOf.new(type,size)
end
|
#visitArrayOrStructInit(init, args = nil) ⇒ Object
283
284
285
286
|
# File 'lib/crokus/transformer.rb', line 283
def visitArrayOrStructInit init,args=nil
elements=init.elements.map{|e| e.accept(self)}
ArrayOrStructInit.new(elements)
end
|
#visitArrow(arrow, args = nil) ⇒ Object
271
272
273
274
275
|
# File 'lib/crokus/transformer.rb', line 271
def visitArrow arrow,args=nil
lhs=arrow.lhs.accept(self)
rhs=arrow.rhs.accept(self)
Arrow.new(lhs,rhs)
end
|
#visitAssign(assign, args = nil) ⇒ Object
129
130
131
132
133
134
|
# File 'lib/crokus/transformer.rb', line 129
def visitAssign assign,args=nil
lhs=assign.lhs.accept(self)
op=assign.op.accept(self)
rhs=assign.rhs.accept(self)
Assign.new(lhs,op,rhs)
end
|
#visitBinary(expr, args = nil) ⇒ Object
246
247
248
249
250
251
|
# File 'lib/crokus/transformer.rb', line 246
def visitBinary expr,args=nil
lhs=expr.lhs.accept(self)
op=expr.op.accept(self)
rhs=expr.rhs.accept(self)
Binary.new(lhs,op,rhs)
end
|
#visitBody(body, args = nil) ⇒ Object
309
310
311
312
|
# File 'lib/crokus/transformer.rb', line 309
def visitBody body,args=nil
stmts=body.stmts.map{|stmt| stmt.accept(self)}
Body.new(stmts)
end
|
#visitBreak(brk, args = nil) ⇒ Object
203
204
205
|
# File 'lib/crokus/transformer.rb', line 203
def visitBreak brk,args=nil
Break.new
end
|
#visitCase(case_, args = nil) ⇒ Object
185
186
187
188
189
|
# File 'lib/crokus/transformer.rb', line 185
def visitCase case_,args=nil
expr=case_.expr.accept(self)
body=case_.body.accept(self)
Case.new(expr,body)
end
|
#visitCastedExpr(cexpr, args = nil) ⇒ Object
87
88
89
90
91
|
# File 'lib/crokus/transformer.rb', line 87
def visitCastedExpr cexpr,args=nil
type=cexpr.type.accept(self)
expr=cexpr.expr.accept(self)
CastedExpr.new(type,expr)
end
|
#visitCasting(cast, args = nil) ⇒ Object
81
82
83
84
85
|
# File 'lib/crokus/transformer.rb', line 81
def visitCasting cast,args=nil
type=cast.type.accept(self)
modifier=cast.modifier.accept(self)
Casting.new(type,modifier)
end
|
#visitCharLit(lit, args = nil) ⇒ Object
236
237
238
239
|
# File 'lib/crokus/transformer.rb', line 236
def visitCharLit lit,args=nil
tok=lit.tok.accept(self)
CharLit.new(tok)
end
|
#visitCommaStmt(comma, args = nil) ⇒ Object
123
124
125
126
127
|
# File 'lib/crokus/transformer.rb', line 123
def visitCommaStmt comma,args=nil
lhs=comma.lhs.accept(self)
rhs=comma.rhs.accept(self)
CommaStmt.new(lhs,rhs)
end
|
#visitCondExpr(ternary, args = nil) ⇒ Object
259
260
261
262
263
264
|
# File 'lib/crokus/transformer.rb', line 259
def visitCondExpr ternary,args=nil
cond=ternary.cond.accept(self)
lhs=ternary.lhs.accept(self)
rhs=ternary.rhs.accept(self)
CondExpr.new(cond,lhs,rhs)
end
|
#visitContinue(cont, args = nil) ⇒ Object
207
208
209
|
# File 'lib/crokus/transformer.rb', line 207
def visitContinue cont,args=nil
Continue.new
end
|
#visitDecl(decl, args = nil) ⇒ Object
30
31
32
33
34
35
|
# File 'lib/crokus/transformer.rb', line 30
def visitDecl decl,args=nil
type=decl.type.accept(self)
var=decl.var.accept(self) if decl.var
init=decl.init.accept(self) if decl.init
Decl.new(type,var,init)
end
|
#visitDefine(define, args = nil) ⇒ Object
43
44
45
46
47
48
|
# File 'lib/crokus/transformer.rb', line 43
def visitDefine define,args=nil
name=define.name.accept(self)
args=define.args.map{|arg| arg.accept(self)}
expr=define.expr.accept(self)
Define.new(name,args,expr)
end
|
#visitDeref(deref, args = nil) ⇒ Object
304
305
306
307
|
# File 'lib/crokus/transformer.rb', line 304
def visitDeref deref,args=nil
e=deref.expr.accept(self)
Deref.new(e)
end
|
#visitDesignUnit(du, args = nil) ⇒ Object
25
26
27
28
|
# File 'lib/crokus/transformer.rb', line 25
def visitDesignUnit du,args=nil
list=du.list.collect{|e| e.accept(self)}
DesignUnit.new(list)
end
|
#visitDotted(dotted, args = nil) ⇒ Object
293
294
295
296
297
|
# File 'lib/crokus/transformer.rb', line 293
def visitDotted dotted,args=nil
lhs=dotted.lhs.accept(self)
rhs=dotted.rhs.accept(self)
Dotted.new(lhs,rhs)
end
|
#visitDoWhile(while_, args = nil) ⇒ Object
197
198
199
200
201
|
# File 'lib/crokus/transformer.rb', line 197
def visitDoWhile while_,args=nil
cond=while_.cond.accept(self)
body=while_.body.each{|stmt| stmt.accept(self)}
DoWhile.new(cond,body)
end
|
#visitElse(else_, args = nil) ⇒ Object
174
175
176
177
|
# File 'lib/crokus/transformer.rb', line 174
def visitElse else_,args=nil
body=else_.body.accept(self)
Else.new(body)
end
|
#visitFloatLit(lit, args = nil) ⇒ Object
241
242
243
244
|
# File 'lib/crokus/transformer.rb', line 241
def visitFloatLit lit,args=nil
tok=lit.tok.accept(self)
FloatLit.new(tok)
end
|
#visitFor(for_, args = nil) ⇒ Object
154
155
156
157
158
159
160
|
# File 'lib/crokus/transformer.rb', line 154
def visitFor for_,args=nil
init=for_.init.collect{|stmt| stmt.accept(self)}
cond=for_.cond.accept(self)
increment=for_.increment.accept(self)
body=for_.body.accept(self)
For.new(init,cond,increment,body)
end
|
110
111
112
113
114
|
# File 'lib/crokus/transformer.rb', line 110
def visitFormalArg formalArg,args=nil
name=formalArg.name.accept(self) if formalArg.name type=formalArg.type.accept(self)
FormalArg.new(type,name)
end
|
#visitFunCall(fcall, args = nil) ⇒ Object
148
149
150
151
152
|
# File 'lib/crokus/transformer.rb', line 148
def visitFunCall fcall,args=nil
name=fcall.name.accept(self)
args=fcall.args.collect{|arg| arg.accept(self)}
FunCall.new(name,args)
end
|
#visitFunction(func, args = nil) ⇒ Object
95
96
97
98
99
100
101
|
# File 'lib/crokus/transformer.rb', line 95
def visitFunction func,args=nil
type=func.type.accept(self)
name=func.name.accept(self)
args=func.args.collect{|arg| arg.accept(self)}
body=func.body.accept(self)
Function.new(name,type,args,body)
end
|
#visitFunctionProto(func, args = nil) ⇒ Object
103
104
105
106
107
108
|
# File 'lib/crokus/transformer.rb', line 103
def visitFunctionProto func,args=nil
type=func.type.accept(self)
name=func.name.accept(self)
args=func.args.collect{|arg| arg.accept(self)}
FunctionProto.new(name,type,args)
end
|
#visitGoto(goto, args = nil) ⇒ Object
216
217
218
219
|
# File 'lib/crokus/transformer.rb', line 216
def visitGoto goto,args=nil
label=goto.label.accept(self)
Goto.new(label)
end
|
#visitIdent(ident, args = nil) ⇒ Object
221
222
223
224
|
# File 'lib/crokus/transformer.rb', line 221
def visitIdent ident,args=nil
tok=ident.tok.accept(self)
Ident.new(tok)
end
|
#visitIf(if_, args = nil) ⇒ Object
167
168
169
170
171
172
|
# File 'lib/crokus/transformer.rb', line 167
def visitIf if_,args=nil
cond=if_.cond.accept(self)
body=if_.body.accept(self)
else_=if_.else.accept(self) if if_.else
If.new(cond,body,else_)
end
|
#visitInclude(incl, args = nil) ⇒ Object
37
38
39
40
41
|
# File 'lib/crokus/transformer.rb', line 37
def visitInclude incl,args=nil
name=incl.name.accept(self)
env=incl.env
Include.new(name,env)
end
|
#visitIndexed(index, args = nil) ⇒ Object
277
278
279
280
281
|
# File 'lib/crokus/transformer.rb', line 277
def visitIndexed index,args=nil
lhs=index.lhs.accept(self)
rhs=index.rhs.accept(self)
Indexed.new(lhs,rhs)
end
|
#visitIntLit(lit, args = nil) ⇒ Object
226
227
228
229
|
# File 'lib/crokus/transformer.rb', line 226
def visitIntLit lit,args=nil
tok=lit.tok.accept(self)
IntLit.new(tok)
end
|
#visitLabeledStmt(lstmt, args = nil) ⇒ Object
117
118
119
120
121
|
# File 'lib/crokus/transformer.rb', line 117
def visitLabeledStmt lstmt,args=nil
label=lstmt.label.accept(self)
stmt=lstmt.stmt.accept(self)
LabeledStmt.new(label,stmt)
end
|
#visitLabelledStmt(label, args = nil) ⇒ Object
211
212
213
214
|
# File 'lib/crokus/transformer.rb', line 211
def visitLabelledStmt label,args=nil
stmt=label.stmt.accept(self)
LabelledStmt.new(stmt)
end
|
#visitParenth(par, args = nil) ⇒ Object
266
267
268
269
|
# File 'lib/crokus/transformer.rb', line 266
def visitParenth par,args=nil
e=par.expr.accept(self)
Parenth.new(e)
end
|
#visitPointerTo(pto, args = nil) ⇒ Object
64
65
66
67
|
# File 'lib/crokus/transformer.rb', line 64
def visitPointerTo pto,args=nil
type=pto.type.accept(self)
PointerTo.new(type)
end
|
#visitPostFixAccu(accu, args = nil) ⇒ Object
136
137
138
139
140
|
# File 'lib/crokus/transformer.rb', line 136
def visitPostFixAccu accu,args=nil
lhs=accu.lhs.accept(self) if accu.lhs op=accu.op.accept(self)
PostFixAccu.new(lhs,op)
end
|
#visitPreFixAccu(accu, args = nil) ⇒ Object
142
143
144
145
146
|
# File 'lib/crokus/transformer.rb', line 142
def visitPreFixAccu accu,args=nil
lhs=accu.lhs.accept(self) if accu.lhs op=accu.op.accept(self)
PreFixAccu.new(lhs,op)
end
|
#visitReturn(ret, args = nil) ⇒ Object
162
163
164
165
|
# File 'lib/crokus/transformer.rb', line 162
def visitReturn ret,args=nil
expr=ret.expr.accept(self) if ret.expr
Return.new(expr)
end
|
#visitSizeof(sizeof, args = nil) ⇒ Object
299
300
301
302
|
# File 'lib/crokus/transformer.rb', line 299
def visitSizeof sizeof,args=nil
type=sizeof.type.accept(self)
Sizeof.new(type)
end
|
#visitStrLit(lit, args = nil) ⇒ Object
231
232
233
234
|
# File 'lib/crokus/transformer.rb', line 231
def visitStrLit lit,args=nil
tok=lit.tok.accept(self)
StrLit.new(tok)
end
|
#visitStruct(struct, args = nil) ⇒ Object
75
76
77
78
79
|
# File 'lib/crokus/transformer.rb', line 75
def visitStruct struct,args=nil
name=struct.name.accept(self) if struct.name
decls=struct.decls.collect{|decl| decl.accept(self)}
Struct.new(name,decls)
end
|
#visitSwitch(sw_, args = nil) ⇒ Object
179
180
181
182
183
|
# File 'lib/crokus/transformer.rb', line 179
def visitSwitch sw_,args=nil
expr =sw_.expr.accept(self)
cases=sw_.cases.collect{|case_| case_.accept(self)}
Switch.new(expr,cases)
end
|
#visitToken(tok, args = nil) ⇒ Object
314
315
316
|
# File 'lib/crokus/transformer.rb', line 314
def visitToken tok,args=nil
Token.new [tok.kind,tok.val,tok.pos]
end
|
#visitType(type, args = nil) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/crokus/transformer.rb', line 56
def visitType type,args=nil
precisions=type.precisions.collect{|prc| prc.accept(self)}
name=type.name.accept(self)
ret=Type.new(name)
ret.precisions=precisions
ret
end
|
#visitTypedef(typdef, args = nil) ⇒ Object
50
51
52
53
54
|
# File 'lib/crokus/transformer.rb', line 50
def visitTypedef typdef,args=nil
type=typdef.type.accept(self)
name=typdef.name.accept(self)
Typedef.new(type,name)
end
|
#visitUnary(unary, args = nil) ⇒ Object
253
254
255
256
257
|
# File 'lib/crokus/transformer.rb', line 253
def visitUnary unary,args=nil
op=unary.op.accept(self)
rhs=unary.rhs.accept(self)
Unary.new(op,rhs,unary.postfix)
end
|
#visitWhile(while_, args = nil) ⇒ Object
191
192
193
194
195
|
# File 'lib/crokus/transformer.rb', line 191
def visitWhile while_,args=nil
cond=while_.cond.accept(self)
body=while_.body.accept(self)
While.new(cond,body)
end
|