rubyx/lib/melon/compiler.rb

47 lines
1.1 KiB
Ruby
Raw Normal View History

require_relative "compiler/total_processor"
require_relative "compiler/type_collector"
require_relative "compiler/method_collector"
2016-12-21 10:20:36 +01:00
require_relative "compiler/locals_collector"
require_relative "compiler/ruby_method"
module Melon
class Compiler < AST::Processor
2017-01-12 19:38:04 +01:00
def self.compile( input )
ast = Parser::Ruby22.parse( input )
self.new.process( ast )
2016-12-18 16:17:58 +01:00
end
def on_class statement
name , sup , body = *statement
2017-01-01 20:50:54 +01:00
class_name = get_name(name)
2017-01-12 19:38:04 +01:00
clazz = Parfait.object_space.get_class_by_name!(class_name , get_name(sup) )
ivar_hash = TypeCollector.new.collect(body)
clazz.set_instance_type( Parfait::Type.for_hash( clazz , ivar_hash ) )
2017-01-12 19:38:04 +01:00
create_methods(clazz , body)
end
2017-01-12 19:38:04 +01:00
def create_methods(clazz , body)
methods = MethodCollector.new.collect(body)
methods.each do |method|
clazz.add_method( method )
end
end
def handler_missing(node)
2017-01-12 19:38:04 +01:00
# raise "Oh"
node.children.each do |kid |
process(kid) if kid.is_a?(AST::Node)
end
end
2017-01-12 19:38:04 +01:00
private
def get_name( statement )
return nil unless statement
statement.children[1]
end
end
end