Class: Ionize::Php::Translate::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/ionize/translate/translator.rb

Direct Known Subclasses

FunctionArgs, Rewrites, Statements

Constant Summary collapse

SimpleTransforms =
["single_string", "double_string", "fun_call", "a_string", "positive_number", "number"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Translator

Returns a new instance of Translator.



8
9
10
# File 'lib/ionize/translate/translator.rb', line 8

def initialize(parent)
  @parent = parent
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/ionize/translate/translator.rb', line 6

def parent
  @parent
end

Class Method Details

.handle_node(*names, &block) ⇒ Object



38
39
40
41
42
# File 'lib/ionize/translate/translator.rb', line 38

def self.handle_node(*names, &block)
  names.each do |name|
    define_method("handle_#{name}", &block)
  end
end

Instance Method Details

#handle_num(node) ⇒ Object



34
35
36
# File 'lib/ionize/translate/translator.rb', line 34

def handle_num(node)
  [:lit, node.to_i]
end

#send(method, arg) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ionize/translate/translator.rb', line 26

def send(method, arg)
  if methods.include? "handle_#{method}"
    __send__("handle_#{method}", arg)
  else
    raise "Couldn't find handler for #{method.inspect} in class #{self.class.name} for argument #{arg.inspect}"
  end
end

#transform(ast) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ionize/translate/translator.rb', line 12

def transform(ast)
  if ast.is_a? Array 
    if ast.length == 1
      transform(ast.first)
    else
      ast.map {|node| transform(node) }.compact
    end
  elsif ast.is_a? Hash and ast.length == 1
    debug "Ast is hash #{ast.inspect}"
    first, second = *(ast.to_a).first
    send(first, second)
  end          
end