2017-01-14 19:30:19 +01:00
|
|
|
module Melon
|
|
|
|
module Compilers
|
|
|
|
|
|
|
|
class MethodCompiler < AST::Processor
|
|
|
|
|
|
|
|
def initialize( ruby_method )
|
2017-01-15 11:10:37 +01:00
|
|
|
@ruby_method = ruby_method
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_code
|
|
|
|
process(@ruby_method.source)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_ivasgn(statement)
|
|
|
|
name , value = *statement
|
|
|
|
w = Vm::Tree::Assignment.new()
|
2017-01-16 08:33:49 +01:00
|
|
|
w.name = Vm::Tree::InstanceName.new( name[1..-1].to_sym)
|
2017-01-15 11:10:37 +01:00
|
|
|
w.value = process(value)
|
|
|
|
w
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_ivar( var )
|
|
|
|
name = var.children.first
|
|
|
|
w = Vm::Tree::FieldAccess.new()
|
2017-01-16 08:33:49 +01:00
|
|
|
w.receiver = Vm::Tree::KnownName.new(:self)
|
|
|
|
w.field = Vm::Tree::InstanceName.new( name[1..-1].to_sym)
|
2017-01-15 11:10:37 +01:00
|
|
|
w
|
|
|
|
end
|
|
|
|
|
2017-01-15 19:30:47 +01:00
|
|
|
def on_send( statement )
|
|
|
|
receiver , name , args = *statement
|
|
|
|
w = Vm::Tree::CallSite.new()
|
|
|
|
puts "receiver #{statement}"
|
2017-01-16 08:33:49 +01:00
|
|
|
w.name = name
|
2017-01-15 19:30:47 +01:00
|
|
|
w.arguments = process(args)
|
2017-01-16 08:33:49 +01:00
|
|
|
w.receiver = process(receiver)
|
2017-01-15 19:30:47 +01:00
|
|
|
w
|
|
|
|
end
|
|
|
|
|
2017-01-16 08:33:49 +01:00
|
|
|
def on_lvar(statement)
|
|
|
|
name = statement.children.first.to_sym
|
|
|
|
if(@ruby_method.args_type.variable_index(name))
|
|
|
|
return Vm::Tree::ArgumentName.new(name)
|
|
|
|
end
|
|
|
|
raise "Not found #{name}"
|
|
|
|
end
|
|
|
|
|
2017-01-15 19:30:47 +01:00
|
|
|
def on_str( string )
|
|
|
|
Vm::Tree::StringExpression.new(string.children.first)
|
|
|
|
end
|
|
|
|
|
2017-01-15 11:10:37 +01:00
|
|
|
def on_int( expression)
|
|
|
|
Vm::Tree::IntegerExpression.new(expression.children.first)
|
2017-01-14 19:30:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def handler_missing(node)
|
|
|
|
raise "No handler for #{node}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|