Class: RBelly::Visitors::ECMAVisitor

Inherits:
Visitor
  • Object
show all
Defined in:
lib/rbelly/visitors/ecma_visitor.rb

Constant Summary

Constants inherited from Visitor

Visitor::ALL_NODES, Visitor::ARRAY_VALUE_NODES, Visitor::BELLEJS_FUNC_NODES, Visitor::BELLEJS_VAR_NODES, Visitor::BINARY_NODES, Visitor::CLASS_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/rbelly/visitors/ecma_visitor.rb', line 4

def initialize
  @indent = 0
end

Instance Method Details

#visit_ArgumentsNode(o) ⇒ Object



77
78
79
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 77

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

#visit_ArrayNode(o) ⇒ Object



165
166
167
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 165

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

#visit_AssignExprNode(o) ⇒ Object



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

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

#visit_BellejsFuncStatementNode(o) ⇒ Object



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

def visit_BellejsFuncStatementNode(o)
  "#{o.visibility} " +
      "#{o.func_statement.accept(self)}"
end

#visit_BellejsVarStatementNode(o) ⇒ Object



101
102
103
104
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 101

def visit_BellejsVarStatementNode(o)
  "#{o.visibility} " +
     "#{o.var_statement.accept(self)}"
end

#visit_BitwiseNotNode(o) ⇒ Object



157
158
159
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 157

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

#visit_BlockNode(o) ⇒ Object



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

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

#visit_BracketAccessorNode(o) ⇒ Object



339
340
341
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 339

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

#visit_BreakNode(o) ⇒ Object



125
126
127
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 125

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

#visit_CaseBlockNode(o) ⇒ Object



257
258
259
260
261
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 257

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



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 263

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_ClassBodyNode(o) ⇒ Object



120
121
122
123
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 120

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

#visit_ClassNode(o) ⇒ Object



95
96
97
98
99
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 95

def visit_ClassNode(o)
  "#{indent}class #{o.value}" +
      (o.parent ? " extends #{o.parent.value.to_s}" : "") +
      "#{o.class_body.accept(self)}"
end

#visit_CommaNode(o) ⇒ Object



313
314
315
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 313

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

#visit_ConditionalNode(o) ⇒ Object



322
323
324
325
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 322

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

#visit_ConstStatementNode(o) ⇒ Object



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

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

#visit_ContinueNode(o) ⇒ Object



129
130
131
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 129

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

#visit_DeleteNode(o) ⇒ Object



161
162
163
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 161

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

#visit_DotAccessorNode(o) ⇒ Object



149
150
151
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 149

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

#visit_DoWhileNode(o) ⇒ Object



275
276
277
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 275

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

#visit_ElementNode(o) ⇒ Object



169
170
171
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 169

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

#visit_EmptyStatementNode(o) ⇒ Object



141
142
143
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 141

def visit_EmptyStatementNode(o)
  ';'
end

#visit_ExpressionStatementNode(o) ⇒ Object



65
66
67
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 65

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

#visit_ExtendsNode(o) ⇒ Object



193
194
195
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 193

def visit_ExtendsNode(o)
  "extends #{o.value.accept(self)}"
end

#visit_FalseNode(o) ⇒ Object



137
138
139
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 137

def visit_FalseNode(o)
  "false"
end

#visit_ForInNode(o) ⇒ Object



327
328
329
330
331
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 327

def visit_ForInNode(o)
  var = o.left.is_a?(RBelly::Nodes::VarDeclNode) ? 'var ' : ''
  "for(#{var}#{o.left.accept(self)} in #{o.right.accept(self)}) " +
    "#{o.value.accept(self)}"
end

#visit_ForNode(o) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 36

def visit_ForNode(o)
  init    = o.init ? o.init.accept(self) : ';'
  init    << ';' unless init.end_with? ';' # make sure it has a ;
  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



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

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

#visit_FunctionCallNode(o) ⇒ Object



73
74
75
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 73

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

#visit_FunctionDeclNode(o) ⇒ Object



89
90
91
92
93
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 89

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



308
309
310
311
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 308

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

#visit_GetterPropertyNode(o) ⇒ Object



300
301
302
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 300

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

#visit_IfNode(o) ⇒ Object



317
318
319
320
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 317

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

#visit_ImportNode(o) ⇒ Object



189
190
191
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 189

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

#visit_LabelNode(o) ⇒ Object



283
284
285
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 283

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

#visit_LessNode(o) ⇒ Object



44
45
46
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 44

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

#visit_LogicalNotNode(o) ⇒ Object



173
174
175
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 173

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

#visit_NewExprNode(o) ⇒ Object



343
344
345
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 343

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

#visit_NullNode(o) ⇒ Object



85
86
87
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 85

def visit_NullNode(o)
  "null"
end

#visit_NumberNode(o) ⇒ Object



32
33
34
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 32

def visit_NumberNode(o)
  o.value.to_s
end

#visit_ObjectLiteralNode(o) ⇒ Object



287
288
289
290
291
292
293
294
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 287

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



69
70
71
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 69

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

#visit_ParameterNode(o) ⇒ Object



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

def visit_ParameterNode(o)
  o.value
end

#visit_ParentheticalNode(o) ⇒ Object



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

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

#visit_PostfixNode(o) ⇒ Object



52
53
54
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 52

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

#visit_PrefixNode(o) ⇒ Object



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

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

#visit_PropertyNode(o) ⇒ Object



296
297
298
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 296

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

#visit_RegexpNode(o) ⇒ Object



145
146
147
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 145

def visit_RegexpNode(o)
  o.value
end

#visit_ResolveNode(o) ⇒ Object



48
49
50
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 48

def visit_ResolveNode(o)
  o.value
end

#visit_ReturnNode(o) ⇒ Object



185
186
187
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 185

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

#visit_SetterPropertyNode(o) ⇒ Object



304
305
306
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 304

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

#visit_SourceElementsNode(o) ⇒ Object



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

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

#visit_StringNode(o) ⇒ Object



81
82
83
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 81

def visit_StringNode(o)
  o.value
end

#visit_SwitchNode(o) ⇒ Object



253
254
255
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 253

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

#visit_ThisNode(o) ⇒ Object



153
154
155
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 153

def visit_ThisNode(o)
  "this"
end

#visit_ThrowNode(o) ⇒ Object



197
198
199
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 197

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

#visit_TrueNode(o) ⇒ Object



133
134
135
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 133

def visit_TrueNode(o)
  "true"
end

#visit_TryNode(o) ⇒ Object



333
334
335
336
337
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 333

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



201
202
203
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 201

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

#visit_UnaryMinusNode(o) ⇒ Object



177
178
179
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 177

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

#visit_UnaryPlusNode(o) ⇒ Object



181
182
183
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 181

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

#visit_VarDeclNode(o) ⇒ Object



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

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

#visit_VarStatementNode(o) ⇒ Object



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

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

#visit_VoidNode(o) ⇒ Object



205
206
207
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 205

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

#visit_WhileNode(o) ⇒ Object



249
250
251
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 249

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

#visit_WithNode(o) ⇒ Object



279
280
281
# File 'lib/rbelly/visitors/ecma_visitor.rb', line 279

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