rename melon to rubyx

melon was still from the salama days and rubyx describes it much better
This commit is contained in:
Torsten Ruger
2017-01-18 00:05:36 +02:00
parent 48d32a2b8e
commit 7abd777d05
32 changed files with 37 additions and 37 deletions

View File

@ -0,0 +1,25 @@
module Rubyx
module Passes
class LocalsCollector < TotalProcessor
def initialize
@locals = {}
end
def collect(statement)
process statement
@locals
end
def on_lvasgn statement
add_local( statement )
end
def add_local(statement)
var = statement.children[0]
@locals[var] = :Object #not really used right now
end
end
end
end

View File

@ -0,0 +1,38 @@
module Rubyx
module Passes
class MethodCollector < TotalProcessor
def initialize
@methods = []
end
def collect(statement)
process statement
@methods
end
def on_def(statement)
name , args , body = *statement
args_type = make_type(args)
locals_type = make_locals(body)
@methods << RubyMethod.new(name , args_type , locals_type , body )
end
private
def make_type( statement )
type_hash = {}
statement.children.each do |arg|
type_hash[arg.children[0]] = :Object
end
Parfait::NamedList.type_for( type_hash )
end
def make_locals(body)
type_hash = LocalsCollector.new.collect(body)
Parfait::NamedList.type_for( type_hash )
end
end
end
end

View File

@ -0,0 +1,61 @@
module Rubyx
module Passes
class MethodCompiler < AST::Processor
def initialize( ruby_method )
@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()
w.name = Vm::Tree::InstanceName.new( name[1..-1].to_sym)
w.value = process(value)
w
end
def on_ivar( var )
name = var.children.first
w = Vm::Tree::FieldAccess.new()
w.receiver = Vm::Tree::KnownName.new(:self)
w.field = Vm::Tree::InstanceName.new( name[1..-1].to_sym)
w
end
def on_send( statement )
receiver , name , args = *statement
w = Vm::Tree::CallSite.new()
w.name = name
w.arguments = process(args) || []
w.receiver = process(receiver)
w
end
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
def on_str( string )
Vm::Tree::StringExpression.new(string.children.first)
end
def on_int( expression)
Vm::Tree::IntegerExpression.new(expression.children.first)
end
def handler_missing(node)
raise "No handler for #{node}"
end
end
end
end

View File

@ -0,0 +1,12 @@
module Rubyx
module Passes
class Normalizer < AST::Processor
def initialize( ruby_method )
@ruby_method
end
end
end
end

View File

@ -0,0 +1,14 @@
module Rubyx
module Passes
class TotalProcessor < AST::Processor
def handler_missing(node)
node.children.each do |kid |
process(kid) if kid.is_a?(AST::Node)
end
end
end
end
end

View File

@ -0,0 +1,29 @@
module Rubyx
module Passes
class TypeCollector < TotalProcessor
def initialize
@ivars = {}
end
def collect(statement)
process statement
@ivars
end
def on_ivar(statement)
add_ivar(statement)
end
def on_ivasgn( statement )
add_ivar(statement)
end
def add_ivar(statement)
var = statement.children[0].to_s[1..-1].to_sym
@ivars[var] = :Object #guess, can maybe guess better
end
end
end
end

View File

@ -0,0 +1,64 @@
require "parser/ruby22"
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 Rubyx
class RubyCompiler < Passes::TotalProcessor
def self.compile( input )
ast = Parser::Ruby22.parse( input )
self.new.process( ast )
end
def on_class statement
name , sup , body = *statement
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)
end
def create_methods(clazz , body)
methods = Passes::MethodCollector.new.collect(body)
methods.each do |method|
clazz.add_method( method )
normalizer = Passes::Normalizer.new(method)
method.normalize_source { |sourc| normalizer.process( sourc ) }
end
methods
end
def compile_methods(clazz , methods)
methods.each do |method|
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 )
end
end
private
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
end
end
end

25
lib/rubyx/ruby_method.rb Normal file
View File

@ -0,0 +1,25 @@
module Rubyx
class RubyMethod
attr_reader :name , :args_type , :locals_type , :source
def initialize(name , args_type , locals_type , source )
@name , @args_type , @locals_type , @source = name , args_type, locals_type , source
raise "Name must be symbol" unless name.is_a?(Symbol)
raise "args_type must be type" unless args_type.is_a?(Parfait::Type)
raise "locals_type must be type" unless locals_type.is_a?(Parfait::Type)
end
def normalize_source
@source = yield @source
end
def create_vm_method( type )
raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type
type.create_method( @name , @args_type )#FIXME, @locals_type)
end
end
end