2017-01-14 19:05:58 +01:00
|
|
|
require "parser/ruby22"
|
|
|
|
|
|
|
|
require_relative "compilers/total_processor"
|
|
|
|
require_relative "compilers/type_collector"
|
|
|
|
require_relative "compilers/method_collector"
|
2017-01-15 11:10:37 +01:00
|
|
|
require_relative "compilers/method_compiler"
|
2017-01-14 19:05:58 +01:00
|
|
|
require_relative "compilers/locals_collector"
|
2017-01-14 19:30:19 +01:00
|
|
|
require_relative "compilers/normalizer"
|
2017-01-14 19:05:58 +01:00
|
|
|
require_relative "ruby_method"
|
2016-12-18 16:02:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
module Melon
|
2017-01-14 19:30:19 +01:00
|
|
|
class RubyCompiler < Compilers::TotalProcessor
|
2016-12-18 16:02:55 +01:00
|
|
|
|
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
|
|
|
|
|
2016-12-18 16:02:55 +01:00
|
|
|
def on_class statement
|
2016-12-18 19:05:11 +01:00
|
|
|
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) )
|
2017-01-14 19:05:58 +01:00
|
|
|
ivar_hash = Compilers::TypeCollector.new.collect(body)
|
2016-12-20 19:02:52 +01:00
|
|
|
clazz.set_instance_type( Parfait::Type.for_hash( clazz , ivar_hash ) )
|
2017-01-14 19:30:19 +01:00
|
|
|
methods = create_methods(clazz , body)
|
2017-01-15 11:10:37 +01:00
|
|
|
compile_methods(clazz,methods)
|
2017-01-12 19:38:04 +01:00
|
|
|
end
|
2016-12-20 19:02:52 +01:00
|
|
|
|
2017-01-12 19:38:04 +01:00
|
|
|
def create_methods(clazz , body)
|
2017-01-14 19:05:58 +01:00
|
|
|
methods = Compilers::MethodCollector.new.collect(body)
|
2017-01-12 19:38:04 +01:00
|
|
|
methods.each do |method|
|
|
|
|
clazz.add_method( method )
|
2017-01-14 19:30:19 +01:00
|
|
|
normalizer = Compilers::Normalizer.new(method)
|
|
|
|
method.normalize_source { |sourc| normalizer.process( sourc ) }
|
|
|
|
end
|
|
|
|
methods
|
|
|
|
end
|
|
|
|
|
2017-01-15 11:10:37 +01:00
|
|
|
def compile_methods(clazz , methods)
|
2017-01-14 19:30:19 +01:00
|
|
|
methods.each do |method|
|
2017-01-15 11:10:37 +01:00
|
|
|
typed_method = method.create_vm_method(clazz.instance_type)
|
|
|
|
code = Compilers::MethodCompiler.new(method).get_code
|
|
|
|
Vm::MethodCompiler.new( typed_method ).init_method.process( code)
|
2017-01-12 19:38:04 +01:00
|
|
|
end
|
2016-12-18 16:02:55 +01:00
|
|
|
end
|
2016-12-20 19:02:52 +01:00
|
|
|
|
2017-01-12 19:38:04 +01:00
|
|
|
private
|
2017-01-14 18:52:16 +01:00
|
|
|
|
2017-01-12 19:38:04 +01:00
|
|
|
def get_name( statement )
|
|
|
|
return nil unless statement
|
2017-01-15 15:59:29 +01:00
|
|
|
name = statement.children[1]
|
|
|
|
raise "Not symbol #{name}" unless name.is_a? Symbol
|
|
|
|
name
|
2017-01-12 19:38:04 +01:00
|
|
|
end
|
|
|
|
|
2016-12-18 16:02:55 +01:00
|
|
|
end
|
|
|
|
end
|