Class: RKelly::Visitors::ECMAVisitor

Inherits:
Visitor
  • Object
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

#initializeECMAVisitor

Returns a new instance of ECMAVisitor.



4
5
6
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 4

def initialize
  @indent = 0
end

Instance Method Details

#visit_ArgumentsNode(o) ⇒ Object



72
73
74
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 72

def visit_ArgumentsNode(o)
  o.value.map { |x| x.accept(self) }.join(', ')
end

#visit_ArrayNode(o) ⇒ Object



139
140
141
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 139

def visit_ArrayNode(o)
  "[#{o.value.map { |x| x ? x.accept(self) : '' }.join(', ')}]"
end

#visit_AssignExprNode(o) ⇒ Object



24
25
26
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 24

def visit_AssignExprNode(o)
  " = #{o.value.accept(self)}"
end

#visit_BitwiseNotNode(o) ⇒ Object



131
132
133
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 131

def visit_BitwiseNotNode(o)
  "~#{o.value.accept(self)}"
end

#visit_BlockNode(o) ⇒ Object



55
56
57
58
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 55

def visit_BlockNode(o)
  @indent += 1
  "{\n#{o.value.accept(self)}\n#{@indent -=1; indent}}"
end

#visit_BracketAccessorNode(o) ⇒ Object



302
303
304
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 302

def visit_BracketAccessorNode(o)
  "#{o.value.accept(self)}[#{o.accessor.accept(self)}]"
end

#visit_BreakNode(o) ⇒ Object



99
100
101
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 99

def visit_BreakNode(o)
  "break" + (o.value ? " #{o.value}" : '') + ';'
end

#visit_CaseBlockNode(o) ⇒ Object



225
226
227
228
229
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 225

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



231
232
233
234
235
236
237
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 231

def visit_CaseClauseNode(o)
  case_code = "#{indent}case #{o.left ? o.left.accept(self) : nil}:\n"
  @indent += 1
  case_code += "#{o.value.accept(self)}\n"
  @indent -= 1
  case_code
end

#visit_CommaNode(o) ⇒ Object



277
278
279
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 277

def visit_CommaNode(o)
  "#{o.left.accept(self)}, #{o.value.accept(self)}"
end

#visit_ConditionalNode(o) ⇒ Object



286
287
288
289
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 286

def visit_ConditionalNode(o)
  "#{o.conditions.accept(self)} ? #{o.value.accept(self)} : " +
    "#{o.else.accept(self)}"
end

#visit_ConstStatementNode(o) ⇒ Object



16
17
18
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 16

def visit_ConstStatementNode(o)
  "const #{o.value.map { |x| x.accept(self) }.join(', ')};"
end

#visit_ContinueNode(o) ⇒ Object



103
104
105
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 103

def visit_ContinueNode(o)
  "continue" + (o.value ? " #{o.value}" : '') + ';'
end

#visit_DeleteNode(o) ⇒ Object



135
136
137
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 135

def visit_DeleteNode(o)
  "delete #{o.value.accept(self)}"
end

#visit_DotAccessorNode(o) ⇒ Object



123
124
125
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 123

def visit_DotAccessorNode(o)
  "#{o.value.accept(self)}.#{o.accessor}"
end

#visit_DoWhileNode(o) ⇒ Object



239
240
241
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 239

def visit_DoWhileNode(o)
  "do #{o.left.accept(self)} while(#{o.value.accept(self)});"
end

#visit_ElementNode(o) ⇒ Object



143
144
145
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 143

def visit_ElementNode(o)
  o.value.accept(self)
end

#visit_EmptyStatementNode(o) ⇒ Object



115
116
117
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 115

def visit_EmptyStatementNode(o)
  ';'
end

#visit_ExpressionStatementNode(o) ⇒ Object



60
61
62
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 60

def visit_ExpressionStatementNode(o)
  "#{o.value.accept(self)};"
end

#visit_FalseNode(o) ⇒ Object



111
112
113
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 111

def visit_FalseNode(o)
  "false"
end

#visit_ForInNode(o) ⇒ Object



291
292
293
294
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 291

def visit_ForInNode(o)
  "for(#{o.left.accept(self)} in #{o.right.accept(self)}) " +
    "#{o.value.accept(self)}"
end

#visit_ForNode(o) ⇒ Object



32
33
34
35
36
37
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 32

def visit_ForNode(o)
  init    = o.init ? o.init.accept(self) : ';'
  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



94
95
96
97
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 94

def visit_FunctionBodyNode(o)
  @indent += 1
  "{\n#{o.value.accept(self)}\n#{@indent -=1; indent}}"
end

#visit_FunctionCallNode(o) ⇒ Object



68
69
70
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 68

def visit_FunctionCallNode(o)
  "#{o.value.accept(self)}(#{o.arguments.accept(self)})"
end

#visit_FunctionDeclNode(o) ⇒ Object



84
85
86
87
88
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 84

def visit_FunctionDeclNode(o)
  "#{indent}function #{o.value}(" +
    "#{o.arguments.map { |x| x.accept(self) }.join(', ')})" +
    "#{o.function_body.accept(self)}"
end

#visit_FunctionExprNode(o) ⇒ Object



272
273
274
275
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 272

def visit_FunctionExprNode(o)
  "#{o.value}(#{o.arguments.map { |x| x.accept(self) }.join(', ')}) " +
    "#{o.function_body.accept(self)}"
end

#visit_GetterPropertyNode(o) ⇒ Object



264
265
266
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 264

def visit_GetterPropertyNode(o)
  "get #{o.name}#{o.value.accept(self)}"
end

#visit_IfNode(o) ⇒ Object



281
282
283
284
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 281

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



247
248
249
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 247

def visit_LabelNode(o)
  "#{o.name}: #{o.value.accept(self)}"
end

#visit_LessNode(o) ⇒ Object



39
40
41
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 39

def visit_LessNode(o)
  "#{o.left.accept(self)} < #{o.value.accept(self)}"
end

#visit_LogicalNotNode(o) ⇒ Object



147
148
149
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 147

def visit_LogicalNotNode(o)
  "!#{o.value.accept(self)}"
end

#visit_NewExprNode(o) ⇒ Object



306
307
308
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 306

def visit_NewExprNode(o)
  "new #{o.value.accept(self)}(#{o.arguments.accept(self)})"
end

#visit_NullNode(o) ⇒ Object



80
81
82
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 80

def visit_NullNode(o)
  "null"
end

#visit_NumberNode(o) ⇒ Object



28
29
30
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 28

def visit_NumberNode(o)
  o.value.to_s
end

#visit_ObjectLiteralNode(o) ⇒ Object



251
252
253
254
255
256
257
258
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 251

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



64
65
66
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 64

def visit_OpEqualNode(o)
  "#{o.left.accept(self)} = #{o.value.accept(self)}"
end

#visit_ParameterNode(o) ⇒ Object



90
91
92
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 90

def visit_ParameterNode(o)
  o.value
end

#visit_PostfixNode(o) ⇒ Object



47
48
49
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 47

def visit_PostfixNode(o)
  "#{o.operand.accept(self)}#{o.value}"
end

#visit_PrefixNode(o) ⇒ Object



51
52
53
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 51

def visit_PrefixNode(o)
  "#{o.value}#{o.operand.accept(self)}"
end

#visit_PropertyNode(o) ⇒ Object



260
261
262
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 260

def visit_PropertyNode(o)
  "#{o.name}: #{o.value.accept(self)}"
end

#visit_RegexpNode(o) ⇒ Object



119
120
121
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 119

def visit_RegexpNode(o)
  o.value
end

#visit_ResolveNode(o) ⇒ Object



43
44
45
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 43

def visit_ResolveNode(o)
  o.value
end

#visit_ReturnNode(o) ⇒ Object



159
160
161
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 159

def visit_ReturnNode(o)
  "return" + (o.value ? " #{o.value.accept(self)}" : '') + ';'
end

#visit_SetterPropertyNode(o) ⇒ Object



268
269
270
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 268

def visit_SetterPropertyNode(o)
  "set #{o.name}#{o.value.accept(self)}"
end

#visit_SourceElementsNode(o) ⇒ Object



8
9
10
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 8

def visit_SourceElementsNode(o)
  o.value.map { |x| "#{indent}#{x.accept(self)}" }.join("\n")
end

#visit_StringNode(o) ⇒ Object



76
77
78
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 76

def visit_StringNode(o)
  o.value
end

#visit_SwitchNode(o) ⇒ Object



221
222
223
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 221

def visit_SwitchNode(o)
  "switch(#{o.left.accept(self)}) #{o.value.accept(self)}"
end

#visit_ThisNode(o) ⇒ Object



127
128
129
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 127

def visit_ThisNode(o)
  "this"
end

#visit_ThrowNode(o) ⇒ Object



163
164
165
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 163

def visit_ThrowNode(o)
  "throw #{o.value.accept(self)};"
end

#visit_TrueNode(o) ⇒ Object



107
108
109
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 107

def visit_TrueNode(o)
  "true"
end

#visit_TryNode(o) ⇒ Object



296
297
298
299
300
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 296

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



167
168
169
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 167

def visit_TypeOfNode(o)
  "typeof #{o.value.accept(self)}"
end

#visit_UnaryMinusNode(o) ⇒ Object



151
152
153
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 151

def visit_UnaryMinusNode(o)
  "-#{o.value.accept(self)}"
end

#visit_UnaryPlusNode(o) ⇒ Object



155
156
157
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 155

def visit_UnaryPlusNode(o)
  "+#{o.value.accept(self)}"
end

#visit_VarDeclNode(o) ⇒ Object



20
21
22
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 20

def visit_VarDeclNode(o)
  "#{o.name}#{o.value ? o.value.accept(self) : nil}"
end

#visit_VarStatementNode(o) ⇒ Object



12
13
14
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 12

def visit_VarStatementNode(o)
  "var #{o.value.map { |x| x.accept(self) }.join(', ')};"
end

#visit_VoidNode(o) ⇒ Object



171
172
173
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 171

def visit_VoidNode(o)
  "void(#{o.value.accept(self)})"
end

#visit_WhileNode(o) ⇒ Object



217
218
219
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 217

def visit_WhileNode(o)
  "while(#{o.left.accept(self)}) #{o.value.accept(self)}"
end

#visit_WithNode(o) ⇒ Object



243
244
245
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 243

def visit_WithNode(o)
  "with(#{o.left.accept(self)}) #{o.value.accept(self)}"
end