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



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

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



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

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



60
61
62
63
# File 'lib/rkelly/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



310
311
312
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 310

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



284
285
286
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 284

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

#visit_ConditionalNode(o) ⇒ Object



293
294
295
296
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 293

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/rkelly/visitors/ecma_visitor.rb', line 20

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



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

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



298
299
300
301
302
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 298

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



36
37
38
39
40
41
42
# File 'lib/rkelly/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



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



73
74
75
# File 'lib/rkelly/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/rkelly/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



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

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

#visit_GetterPropertyNode(o) ⇒ Object



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

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

#visit_IfNode(o) ⇒ Object



288
289
290
291
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 288

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



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

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



314
315
316
# File 'lib/rkelly/visitors/ecma_visitor.rb', line 314

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

#visit_NullNode(o) ⇒ Object



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

def visit_NullNode(o)
  "null"
end

#visit_NumberNode(o) ⇒ Object



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

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



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

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



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

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

#visit_PostfixNode(o) ⇒ Object



52
53
54
# File 'lib/rkelly/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/rkelly/visitors/ecma_visitor.rb', line 56

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



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

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}#{o.value.accept(self)}"
end

#visit_SourceElementsNode(o) ⇒ Object



12
13
14
# File 'lib/rkelly/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/rkelly/visitors/ecma_visitor.rb', line 81

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



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

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



24
25
26
# File 'lib/rkelly/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/rkelly/visitors/ecma_visitor.rb', line 16

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