Class: Oga::Ruby::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/oga/ruby/generator.rb

Overview

Class for converting a Ruby AST to a String.

This class takes a Node instance and converts it (and its child nodes) to a String that in turn can be passed to eval and the likes.

Instance Method Summary collapse

Instance Method Details

#on_and(ast) ⇒ String

Processes a boolean “and” node.

Parameters:

Returns:

  • (String)


78
79
80
81
82
83
84
85
# File 'lib/oga/ruby/generator.rb', line 78

def on_and(ast)
  left, right = *ast

  left_str  = process(left)
  right_str = process(right)

  "#{left_str} && #{right_str}"
end

#on_assign(ast) ⇒ String

Processes an assignment node.

Parameters:

Returns:

  • (String)


25
26
27
28
29
30
31
32
# File 'lib/oga/ruby/generator.rb', line 25

def on_assign(ast)
  var, val = *ast

  var_str = process(var)
  val_str = process(val)

  "#{var_str} = #{val_str}"
end

#on_begin(ast) ⇒ String

Processes a begin node.

Parameters:

Returns:

  • (String)


51
52
53
54
55
56
57
58
59
# File 'lib/oga/ruby/generator.rb', line 51

def on_begin(ast)
  body = process(ast.to_a[0])

  <<-EOF
begin
  #{body}
end
  EOF
end

#on_block(ast) ⇒ String

Processes a block node.

Parameters:

Returns:

  • (String)


173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/oga/ruby/generator.rb', line 173

def on_block(ast)
  receiver, args, body = *ast

  receiver_str = process(receiver)
  body_str     = body ? process(body) : nil
  arg_strs     = args.map { |arg| process(arg) }

  <<-EOF
#{receiver_str} do |#{arg_strs.join(', ')}|
  #{body_str}
end
  EOF
end

#on_eq(ast) ⇒ String

Processes an equality node.

Parameters:

Returns:

  • (String)


65
66
67
68
69
70
71
72
# File 'lib/oga/ruby/generator.rb', line 65

def on_eq(ast)
  left, right = *ast

  left_str  = process(left)
  right_str = process(right)

  "#{left_str} == #{right_str}"
end

#on_followed_by(ast) ⇒ String

Parameters:

Returns:

  • (String)


17
18
19
# File 'lib/oga/ruby/generator.rb', line 17

def on_followed_by(ast)
  ast.to_a.map { |child| process(child) }.join("\n\n")
end

#on_if(ast) ⇒ String

Processes an if statement node.

Parameters:

Returns:

  • (String)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/oga/ruby/generator.rb', line 104

def on_if(ast)
  cond, body, else_body = *ast

  cond_str = process(cond)
  body_str = process(body)

  if else_body
    else_str = process(else_body)

    <<-EOF
if #{cond_str}
  #{body_str}
else
  #{else_str}
end
    EOF
  else
    <<-EOF
if #{cond_str}
  #{body_str}
end
    EOF
  end
end

#on_lit(ast) ⇒ String

Processes a literal node.

Parameters:

Returns:

  • (String)


220
221
222
# File 'lib/oga/ruby/generator.rb', line 220

def on_lit(ast)
  ast.to_a[0]
end

#on_massign(ast) ⇒ String

Processes a mass assignment node.

Parameters:

Returns:

  • (String)


38
39
40
41
42
43
44
45
# File 'lib/oga/ruby/generator.rb', line 38

def on_massign(ast)
  vars, val = *ast

  var_names = vars.map { |var| process(var) }
  val_str   = process(val)

  "#{var_names.join(', ')} = #{val_str}"
end

#on_or(ast) ⇒ String

Processes a boolean “or” node.

Parameters:

Returns:

  • (String)


91
92
93
94
95
96
97
98
# File 'lib/oga/ruby/generator.rb', line 91

def on_or(ast)
  left, right = *ast

  left_str  = process(left)
  right_str = process(right)

  "(#{left_str} || #{right_str})"
end

#on_range(ast) ⇒ String

Processes a Range node.

Parameters:

Returns:

  • (String)


191
192
193
194
195
196
197
198
# File 'lib/oga/ruby/generator.rb', line 191

def on_range(ast)
  start, stop = *ast

  start_str = process(start)
  stop_str  = process(stop)

  "(#{start_str}..#{stop_str})"
end

#on_send(ast) ⇒ String

Processes a method call node.

Parameters:

Returns:

  • (String)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/oga/ruby/generator.rb', line 150

def on_send(ast)
  receiver, name, *args = *ast

  call     = name
  brackets = name == '[]'

  unless args.empty?
    arg_str = args.map { |arg| process(arg) }.join(', ')
    call    = brackets ? "[#{arg_str}]" : "#{call}(#{arg_str})"
  end

  if receiver
    rec_str = process(receiver)
    call    = brackets ? "#{rec_str}#{call}" : "#{rec_str}.#{call}"
  end

  call
end

#on_string(ast) ⇒ String

Processes a string node.

Parameters:

Returns:

  • (String)


204
205
206
# File 'lib/oga/ruby/generator.rb', line 204

def on_string(ast)
  ast.to_a[0].inspect
end

#on_symbol(ast) ⇒ String

Processes a Symbol node.

Parameters:

Returns:

  • (String)


212
213
214
# File 'lib/oga/ruby/generator.rb', line 212

def on_symbol(ast)
  ast.to_a[0].to_sym.inspect
end

#on_while(ast) ⇒ String

Processes a while statement node.

Parameters:

Returns:

  • (String)


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/oga/ruby/generator.rb', line 133

def on_while(ast)
  cond, body = *ast

  cond_str = process(cond)
  body_str = process(body)

  <<-EOF
while #{cond_str}
  #{body_str}
end
  EOF
end

#process(ast) ⇒ String

Parameters:

Returns:

  • (String)


11
12
13
# File 'lib/oga/ruby/generator.rb', line 11

def process(ast)
  send(:"on_#{ast.type}", ast)
end