rubyx/lib/melon/ruby_compiler.rb

65 lines
1.9 KiB
Ruby
Raw Normal View History

require "parser/ruby22"
2017-01-16 17:24:32 +02:00
require_relative "passes/total_processor"
require_relative "passes/type_collector"
require_relative "passes/method_collector"
require_relative "passes/method_compiler"
require_relative "passes/locals_collector"
require_relative "passes/normalizer"
require_relative "ruby_method"
module Melon
2017-01-16 17:24:32 +02:00
class RubyCompiler < Passes::TotalProcessor
2017-01-12 20:38:04 +02:00
def self.compile( input )
ast = Parser::Ruby22.parse( input )
self.new.process( ast )
2016-12-18 17:17:58 +02:00
end
def on_class statement
name , sup , body = *statement
2017-01-01 21:50:54 +02:00
class_name = get_name(name)
clazz = Parfait.object_space.get_class_by_name(class_name )
if(clazz)
#FIXME super class check with "sup"
else #existing class, don't overwrite type (parfait only?)
clazz = Parfait.object_space.create_class(class_name , get_name(sup) )
ivar_hash = Passes::TypeCollector.new.collect(body)
clazz.set_instance_type( Parfait::Type.for_hash( clazz , ivar_hash ) )
end
methods = create_methods(clazz , body)
compile_methods(clazz,methods)
2017-01-12 20:38:04 +02:00
end
2017-01-12 20:38:04 +02:00
def create_methods(clazz , body)
2017-01-16 17:24:32 +02:00
methods = Passes::MethodCollector.new.collect(body)
2017-01-12 20:38:04 +02:00
methods.each do |method|
clazz.add_method( method )
2017-01-16 17:24:32 +02:00
normalizer = Passes::Normalizer.new(method)
method.normalize_source { |sourc| normalizer.process( sourc ) }
end
methods
end
def compile_methods(clazz , methods)
methods.each do |method|
2017-01-16 17:24:32 +02:00
code = Passes::MethodCompiler.new(method).get_code
typed_method = method.create_vm_method(clazz.instance_type)
Vm::MethodCompiler.new( typed_method ).init_method.process( code )
2017-01-12 20:38:04 +02:00
end
end
2017-01-12 20:38:04 +02:00
private
2017-01-14 19:52:16 +02:00
2017-01-12 20:38:04 +02:00
def get_name( statement )
return nil unless statement
raise "Not const #{statement}" unless statement.type == :const
name = statement.children[1]
raise "Not symbol #{name}" unless name.is_a? Symbol
name
2017-01-12 20:38:04 +02:00
end
end
end