Class: Riml::Compiler::LiteralNodeVisitor

Inherits:
Visitor
  • Object
show all
Defined in:
lib/riml/compiler.rb

Instance Method Summary collapse

Methods inherited from Visitor

#initialize, #visit

Constructor Details

This class inherits a constructor from Riml::Compiler::Visitor

Instance Method Details

#compile(node) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/riml/compiler.rb', line 167

def compile(node)
  value = case node.value
  when TrueClass
    1
  when FalseClass
    0
  when Numeric
    node.value
  when String
    StringNode === node ? string_surround(node) : node.value
  when Array
    if ListNode === node
      node.value.each {|n| n.parent_node = node}
      '[' <<
      node.value.map do |n|
        n.accept(visitor_for_node(n))
        n.compiled_output
      end.join(', ') << ']'
    elsif DictionaryNode === node
      '{' <<
      node.value.map do |(k, v)|
        k.accept(visitor_for_node(k))
        v.accept(visitor_for_node(v))
        k.compiled_output << ': ' << v.compiled_output
      end.join(', ') << '}'
    end
  end.to_s

  node.compiled_output = value
end