Class: RKelly::Visitors::ECMAVisitor
- Inherits:
-
Visitor
- Object
- Visitor
- RKelly::Visitors::ECMAVisitor
show all
- Defined in:
- lib/rkelly/visitors/ecma_visitor.rb
Constant Summary
Constants inherited
from Visitor
Visitor::ALL_NODES, Visitor::ARRAY_VALUE_NODES, Visitor::BINARY_NODES, Visitor::CONDITIONAL_NODES, Visitor::FUNC_CALL_NODES, Visitor::FUNC_DECL_NODES, Visitor::NAME_VALUE_NODES, Visitor::PREFIX_POSTFIX_NODES, Visitor::SINGLE_VALUE_NODES, Visitor::TERMINAL_NODES
Instance Method Summary
collapse
Methods inherited from Visitor
#accept
Constructor Details
Returns a new instance of ECMAVisitor.
6
7
8
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 6
def initialize
@indent = 0
end
|
Instance Method Details
#function_params_and_body(o) ⇒ Object
Helper for all the various function nodes
285
286
287
288
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 285
def function_params_and_body(o)
"(#{o.arguments.map { |x| x.accept(self) }.join(', ')}) " <<
"#{o.function_body.accept(self)}"
end
|
#visit_ArgumentsNode(o) ⇒ Object
79
80
81
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 79
def visit_ArgumentsNode(o)
o.value.map { |x| x.accept(self) }.join(', ')
end
|
#visit_ArrayNode(o) ⇒ Object
144
145
146
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 144
def visit_ArrayNode(o)
"[#{o.value.map { |x| x ? x.accept(self) : '' }.join(', ')}]"
end
|
#visit_AssignExprNode(o) ⇒ Object
30
31
32
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 30
def visit_AssignExprNode(o)
" = #{o.value.accept(self)}"
end
|
#visit_BitwiseNotNode(o) ⇒ Object
136
137
138
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 136
def visit_BitwiseNotNode(o)
"~#{o.value.accept(self)}"
end
|
#visit_BlockNode(o) ⇒ Object
62
63
64
65
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 62
def visit_BlockNode(o)
@indent += 1
"{\n#{o.value.accept(self)}\n#{@indent -=1; indent}}"
end
|
#visit_BracketAccessorNode(o) ⇒ Object
316
317
318
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 316
def visit_BracketAccessorNode(o)
"#{o.value.accept(self)}[#{o.accessor.accept(self)}]"
end
|
#visit_BreakNode(o) ⇒ Object
104
105
106
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 104
def visit_BreakNode(o)
+"break" << (o.value ? " #{o.value}" : '') << ';'
end
|
#visit_CaseBlockNode(o) ⇒ Object
228
229
230
231
232
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 228
def visit_CaseBlockNode(o)
@indent += 1
+"{\n" << (o.value ? o.value.map { |x| x.accept(self) }.join('') : '') <<
"#{@indent -=1; indent}}"
end
|
#visit_CaseClauseNode(o) ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 234
def visit_CaseClauseNode(o)
if o.left
case_code = "#{indent}case #{o.left.accept(self)}:\n"
else
case_code = "#{indent}default:\n"
end
@indent += 1
case_code << "#{o.value.accept(self)}\n"
@indent -= 1
case_code
end
|
#visit_CommaNode(o) ⇒ Object
290
291
292
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 290
def visit_CommaNode(o)
"#{o.left.accept(self)}, #{o.value.accept(self)}"
end
|
#visit_ConditionalNode(o) ⇒ Object
299
300
301
302
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 299
def visit_ConditionalNode(o)
"#{o.conditions.accept(self)} ? #{o.value.accept(self)} : " <<
"#{o.else.accept(self)}"
end
|
#visit_ConstStatementNode(o) ⇒ Object
22
23
24
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 22
def visit_ConstStatementNode(o)
"const #{o.value.map { |x| x.accept(self) }.join(', ')};"
end
|
#visit_ContinueNode(o) ⇒ Object
108
109
110
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 108
def visit_ContinueNode(o)
+"continue" << (o.value ? " #{o.value}" : '') << ';'
end
|
#visit_DeleteNode(o) ⇒ Object
140
141
142
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 140
def visit_DeleteNode(o)
"delete #{o.value.accept(self)}"
end
|
#visit_DotAccessorNode(o) ⇒ Object
128
129
130
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 128
def visit_DotAccessorNode(o)
"#{o.value.accept(self)}.#{o.accessor}"
end
|
#visit_DoWhileNode(o) ⇒ Object
246
247
248
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 246
def visit_DoWhileNode(o)
"do #{o.left.accept(self)} while(#{o.value.accept(self)});"
end
|
#visit_ElementNode(o) ⇒ Object
148
149
150
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 148
def visit_ElementNode(o)
o.value.accept(self)
end
|
#visit_EmptyStatementNode(o) ⇒ Object
120
121
122
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 120
def visit_EmptyStatementNode(o)
';'
end
|
#visit_ExpressionStatementNode(o) ⇒ Object
67
68
69
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 67
def visit_ExpressionStatementNode(o)
"#{o.value.accept(self)};"
end
|
#visit_FalseNode(o) ⇒ Object
116
117
118
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 116
def visit_FalseNode(o)
"false"
end
|
#visit_ForInNode(o) ⇒ Object
304
305
306
307
308
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 304
def visit_ForInNode(o)
var = o.left.is_a?(RKelly::Nodes::VarDeclNode) ? 'var ' : ''
"for(#{var}#{o.left.accept(self)} in #{o.right.accept(self)}) " <<
"#{o.value.accept(self)}"
end
|
#visit_ForNode(o) ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 38
def visit_ForNode(o)
init = o.init ? o.init.accept(self) : ';'
init << ';' unless init.end_with? ';' test = o.test ? o.test.accept(self) : ''
counter = o.counter ? o.counter.accept(self) : ''
"for(#{init} #{test}; #{counter}) #{o.value.accept(self)}"
end
|
#visit_FunctionBodyNode(o) ⇒ Object
99
100
101
102
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 99
def visit_FunctionBodyNode(o)
@indent += 1
"{\n#{o.value.accept(self)}\n#{@indent -=1; indent}}"
end
|
#visit_FunctionCallNode(o) ⇒ Object
75
76
77
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 75
def visit_FunctionCallNode(o)
"#{o.value.accept(self)}(#{o.arguments.accept(self)})"
end
|
#visit_FunctionDeclNode(o) ⇒ Object
91
92
93
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 91
def visit_FunctionDeclNode(o)
"#{indent}function #{o.value}" << function_params_and_body(o)
end
|
#visit_FunctionExprNode(o) ⇒ Object
279
280
281
282
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 279
def visit_FunctionExprNode(o)
name = (o.value == 'function') ? '' : +' ' << o.value
+"function" << name << function_params_and_body(o)
end
|
#visit_GetterPropertyNode(o) ⇒ Object
271
272
273
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 271
def visit_GetterPropertyNode(o)
"get #{o.name}" << function_params_and_body(o.value)
end
|
#visit_IfNode(o) ⇒ Object
294
295
296
297
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 294
def visit_IfNode(o)
"if(#{o.conditions.accept(self)}) #{o.value.accept(self)}" <<
(o.else ? " else #{o.else.accept(self)}" : '')
end
|
#visit_LabelNode(o) ⇒ Object
254
255
256
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 254
def visit_LabelNode(o)
"#{o.name}: #{o.value.accept(self)}"
end
|
#visit_LessNode(o) ⇒ Object
46
47
48
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 46
def visit_LessNode(o)
"#{o.left.accept(self)} < #{o.value.accept(self)}"
end
|
#visit_LogicalNotNode(o) ⇒ Object
152
153
154
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 152
def visit_LogicalNotNode(o)
"!#{o.value.accept(self)}"
end
|
#visit_NewExprNode(o) ⇒ Object
320
321
322
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 320
def visit_NewExprNode(o)
"new #{o.value.accept(self)}(#{o.arguments.accept(self)})"
end
|
#visit_NullNode(o) ⇒ Object
87
88
89
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 87
def visit_NullNode(o)
"null"
end
|
#visit_NumberNode(o) ⇒ Object
34
35
36
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 34
def visit_NumberNode(o)
o.value.to_s
end
|
#visit_ObjectLiteralNode(o) ⇒ Object
258
259
260
261
262
263
264
265
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 258
def visit_ObjectLiteralNode(o)
@indent += 1
lit = +"{" << (o.value.length > 0 ? "\n" : ' ') <<
o.value.map { |x| "#{indent}#{x.accept(self)}" }.join(",\n") <<
(o.value.length > 0 ? "\n" : '') << '}'
@indent -= 1
lit
end
|
#visit_OpEqualNode(o) ⇒ Object
71
72
73
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 71
def visit_OpEqualNode(o)
"#{o.left.accept(self)} = #{o.value.accept(self)}"
end
|
#visit_ParameterNode(o) ⇒ Object
95
96
97
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 95
def visit_ParameterNode(o)
o.value
end
|
#visit_ParentheticalNode(o) ⇒ Object
10
11
12
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 10
def visit_ParentheticalNode(o)
"(#{o.value.accept(self)})"
end
|
#visit_PostfixNode(o) ⇒ Object
54
55
56
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 54
def visit_PostfixNode(o)
"#{o.operand.accept(self)}#{o.value}"
end
|
#visit_PrefixNode(o) ⇒ Object
58
59
60
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 58
def visit_PrefixNode(o)
"#{o.value}#{o.operand.accept(self)}"
end
|
#visit_PropertyNode(o) ⇒ Object
267
268
269
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 267
def visit_PropertyNode(o)
"#{o.name}: #{o.value.accept(self)}"
end
|
#visit_RegexpNode(o) ⇒ Object
124
125
126
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 124
def visit_RegexpNode(o)
o.value
end
|
#visit_ResolveNode(o) ⇒ Object
50
51
52
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 50
def visit_ResolveNode(o)
o.value
end
|
#visit_ReturnNode(o) ⇒ Object
164
165
166
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 164
def visit_ReturnNode(o)
+"return" << (o.value ? " #{o.value.accept(self)}" : '') << ';'
end
|
#visit_SetterPropertyNode(o) ⇒ Object
275
276
277
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 275
def visit_SetterPropertyNode(o)
"set #{o.name}" << function_params_and_body(o.value)
end
|
#visit_SourceElementsNode(o) ⇒ Object
14
15
16
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 14
def visit_SourceElementsNode(o)
o.value.map { |x| "#{indent}#{x.accept(self)}" }.join("\n")
end
|
#visit_StringNode(o) ⇒ Object
83
84
85
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 83
def visit_StringNode(o)
o.value
end
|
#visit_SwitchNode(o) ⇒ Object
224
225
226
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 224
def visit_SwitchNode(o)
"switch(#{o.left.accept(self)}) #{o.value.accept(self)}"
end
|
#visit_ThisNode(o) ⇒ Object
132
133
134
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 132
def visit_ThisNode(o)
"this"
end
|
#visit_ThrowNode(o) ⇒ Object
168
169
170
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 168
def visit_ThrowNode(o)
"throw #{o.value.accept(self)};"
end
|
#visit_TrueNode(o) ⇒ Object
112
113
114
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 112
def visit_TrueNode(o)
"true"
end
|
#visit_TryNode(o) ⇒ Object
310
311
312
313
314
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 310
def visit_TryNode(o)
"try #{o.value.accept(self)}" <<
(o.catch_block ? " catch(#{o.catch_var}) #{o.catch_block.accept(self)}" : '') <<
(o.finally_block ? " finally #{o.finally_block.accept(self)}" : '')
end
|
#visit_TypeOfNode(o) ⇒ Object
172
173
174
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 172
def visit_TypeOfNode(o)
"typeof #{o.value.accept(self)}"
end
|
#visit_UnaryMinusNode(o) ⇒ Object
156
157
158
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 156
def visit_UnaryMinusNode(o)
"-#{o.value.accept(self)}"
end
|
#visit_UnaryPlusNode(o) ⇒ Object
160
161
162
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 160
def visit_UnaryPlusNode(o)
"+#{o.value.accept(self)}"
end
|
#visit_VarDeclNode(o) ⇒ Object
26
27
28
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 26
def visit_VarDeclNode(o)
"#{o.name}#{o.value ? o.value.accept(self) : nil}"
end
|
#visit_VarStatementNode(o) ⇒ Object
18
19
20
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 18
def visit_VarStatementNode(o)
"var #{o.value.map { |x| x.accept(self) }.join(', ')};"
end
|
#visit_VoidNode(o) ⇒ Object
176
177
178
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 176
def visit_VoidNode(o)
"void(#{o.value.accept(self)})"
end
|
#visit_WhileNode(o) ⇒ Object
220
221
222
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 220
def visit_WhileNode(o)
"while(#{o.left.accept(self)}) #{o.value.accept(self)}"
end
|
#visit_WithNode(o) ⇒ Object
250
251
252
|
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 250
def visit_WithNode(o)
"with(#{o.left.accept(self)}) #{o.value.accept(self)}"
end
|