create compilers directory, found and renamed salama.rb
This commit is contained in:
parent
01fe3b4b04
commit
2e62c5bbb6
@ -1,24 +0,0 @@
|
||||
module Melon
|
||||
|
||||
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
|
@ -1,36 +0,0 @@
|
||||
module Melon
|
||||
|
||||
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
|
@ -1,12 +0,0 @@
|
||||
module Melon
|
||||
|
||||
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
|
@ -1,28 +0,0 @@
|
||||
module Melon
|
||||
|
||||
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
|
25
lib/melon/compilers/locals_collector.rb
Normal file
25
lib/melon/compilers/locals_collector.rb
Normal file
@ -0,0 +1,25 @@
|
||||
module Melon
|
||||
module Compilers
|
||||
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
|
38
lib/melon/compilers/method_collector.rb
Normal file
38
lib/melon/compilers/method_collector.rb
Normal file
@ -0,0 +1,38 @@
|
||||
module Melon
|
||||
module Compilers
|
||||
|
||||
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
|
14
lib/melon/compilers/total_processor.rb
Normal file
14
lib/melon/compilers/total_processor.rb
Normal file
@ -0,0 +1,14 @@
|
||||
module Melon
|
||||
|
||||
module Compilers
|
||||
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
|
29
lib/melon/compilers/type_collector.rb
Normal file
29
lib/melon/compilers/type_collector.rb
Normal file
@ -0,0 +1,29 @@
|
||||
module Melon
|
||||
module Compilers
|
||||
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
|
@ -1,12 +1,14 @@
|
||||
require_relative "compiler/total_processor"
|
||||
require_relative "compiler/type_collector"
|
||||
require_relative "compiler/method_collector"
|
||||
require_relative "compiler/locals_collector"
|
||||
require_relative "compiler/ruby_method"
|
||||
require "parser/ruby22"
|
||||
|
||||
require_relative "compilers/total_processor"
|
||||
require_relative "compilers/type_collector"
|
||||
require_relative "compilers/method_collector"
|
||||
require_relative "compilers/locals_collector"
|
||||
require_relative "ruby_method"
|
||||
|
||||
|
||||
module Melon
|
||||
class Compiler < TotalProcessor
|
||||
class Compiler < Compilers::TotalProcessor
|
||||
|
||||
def self.compile( input )
|
||||
ast = Parser::Ruby22.parse( input )
|
||||
@ -17,13 +19,13 @@ module Melon
|
||||
name , sup , body = *statement
|
||||
class_name = get_name(name)
|
||||
clazz = Parfait.object_space.get_class_by_name!(class_name , get_name(sup) )
|
||||
ivar_hash = TypeCollector.new.collect(body)
|
||||
ivar_hash = Compilers::TypeCollector.new.collect(body)
|
||||
clazz.set_instance_type( Parfait::Type.for_hash( clazz , ivar_hash ) )
|
||||
create_methods(clazz , body)
|
||||
end
|
||||
|
||||
def create_methods(clazz , body)
|
||||
methods = MethodCollector.new.collect(body)
|
||||
methods = Compilers::MethodCollector.new.collect(body)
|
||||
methods.each do |method|
|
||||
clazz.add_method( method )
|
||||
end
|
@ -14,5 +14,4 @@ require "register/builtin/space"
|
||||
require "arm/arm_machine"
|
||||
require "arm/translator"
|
||||
|
||||
require "parser/ruby22"
|
||||
require "melon/compiler"
|
||||
require "melon/ruby_compiler"
|
@ -18,7 +18,7 @@ require "minitest/autorun"
|
||||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
||||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'test'))
|
||||
|
||||
require 'salama'
|
||||
require 'rubyx'
|
||||
|
||||
module Compiling
|
||||
def clean_compile(clazz_name , method_name , args , statements)
|
||||
|
@ -1,35 +0,0 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
class TestLocalsCollector < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot unless Register.machine.booted
|
||||
end
|
||||
|
||||
def parse_collect( input )
|
||||
ast = Parser::Ruby22.parse input
|
||||
LocalsCollector.new.collect(ast)
|
||||
end
|
||||
|
||||
def test_no_locals
|
||||
locals = parse_collect "def meth; end"
|
||||
assert locals.empty?
|
||||
end
|
||||
|
||||
def test_method_is_not_local
|
||||
locals = parse_collect("def meth2(arg1); foo ;end")
|
||||
assert locals.empty?
|
||||
end
|
||||
|
||||
def test_local_assign_one
|
||||
locals = parse_collect("def meth2(arg1); foo = bar ;end")
|
||||
assert locals[:foo] , locals.inspect
|
||||
end
|
||||
def test_local_assign_two
|
||||
locals = parse_collect("def meth2(arg1); foo = bar ; groove = 1 + 2 ;end")
|
||||
assert locals.length == 2 , locals.inspect
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -1,37 +0,0 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
class TestMethodCollector < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot unless Register.machine.booted
|
||||
end
|
||||
|
||||
def parse_collect( input )
|
||||
ast = Parser::Ruby22.parse input
|
||||
MethodCollector.new.collect(ast)
|
||||
end
|
||||
|
||||
def test_no_args
|
||||
methods = parse_collect "def meth; @ivar;end"
|
||||
assert methods.find{|m| m.name == :meth }
|
||||
end
|
||||
|
||||
def test_one_arg
|
||||
method = parse_collect("def meth2(arg1); 1;end").first
|
||||
assert method.name == :meth2
|
||||
assert_equal 2 , method.args_type.variable_index(:arg1) , method.args_type.inspect
|
||||
end
|
||||
|
||||
def test_three_args
|
||||
method = parse_collect("def meth3(yksi,kaksi,kolme); 1;end").first
|
||||
assert method.args_type.variable_index(:kolme) , method.args_type.inspect
|
||||
end
|
||||
|
||||
def test_one_local
|
||||
method = parse_collect("def meth2(arg1); foo = 2 ;end").first
|
||||
assert method.locals_type.variable_index(:foo) , method.locals_type.inspect
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -1,38 +0,0 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
class TestTypeCollector < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
end
|
||||
|
||||
def parse_collect( input )
|
||||
ast = Parser::Ruby22.parse input
|
||||
TypeCollector.new.collect(ast)
|
||||
end
|
||||
|
||||
def test_ivar_name
|
||||
hash = parse_collect "def meth; @ivar;end"
|
||||
assert hash[:ivar] , hash
|
||||
end
|
||||
|
||||
def test_ivar_assign
|
||||
hash = parse_collect "def meth; @ivar = 5;end"
|
||||
assert hash[:ivar] , hash
|
||||
end
|
||||
|
||||
def test_ivar_operator_assign
|
||||
hash = parse_collect "def meth; @ivar += 5;end"
|
||||
assert hash[:ivar] , hash
|
||||
end
|
||||
|
||||
def test_compile_class
|
||||
Compiler.compile "class TestIvar < Object ; def meth; @ivar;end; end"
|
||||
itest = Parfait.object_space.get_class_by_name(:TestIvar)
|
||||
assert itest
|
||||
assert itest.instance_type.names.include?(:ivar) , itest.instance_type.names.inspect
|
||||
end
|
||||
|
||||
end
|
||||
end
|
37
test/melon/compilers/test_locals_collector.rb
Normal file
37
test/melon/compilers/test_locals_collector.rb
Normal file
@ -0,0 +1,37 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
module Compilers
|
||||
class TestLocalsCollector < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot unless Register.machine.booted
|
||||
end
|
||||
|
||||
def parse_collect( input )
|
||||
ast = Parser::Ruby22.parse input
|
||||
LocalsCollector.new.collect(ast)
|
||||
end
|
||||
|
||||
def test_no_locals
|
||||
locals = parse_collect "def meth; end"
|
||||
assert locals.empty?
|
||||
end
|
||||
|
||||
def test_method_is_not_local
|
||||
locals = parse_collect("def meth2(arg1); foo ;end")
|
||||
assert locals.empty?
|
||||
end
|
||||
|
||||
def test_local_assign_one
|
||||
locals = parse_collect("def meth2(arg1); foo = bar ;end")
|
||||
assert locals[:foo] , locals.inspect
|
||||
end
|
||||
def test_local_assign_two
|
||||
locals = parse_collect("def meth2(arg1); foo = bar ; groove = 1 + 2 ;end")
|
||||
assert locals.length == 2 , locals.inspect
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
39
test/melon/compilers/test_method_collector.rb
Normal file
39
test/melon/compilers/test_method_collector.rb
Normal file
@ -0,0 +1,39 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
module Compilers
|
||||
class TestMethodCollector < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot unless Register.machine.booted
|
||||
end
|
||||
|
||||
def parse_collect( input )
|
||||
ast = Parser::Ruby22.parse input
|
||||
MethodCollector.new.collect(ast)
|
||||
end
|
||||
|
||||
def test_no_args
|
||||
methods = parse_collect "def meth; @ivar;end"
|
||||
assert methods.find{|m| m.name == :meth }
|
||||
end
|
||||
|
||||
def test_one_arg
|
||||
method = parse_collect("def meth2(arg1); 1;end").first
|
||||
assert method.name == :meth2
|
||||
assert_equal 2 , method.args_type.variable_index(:arg1) , method.args_type.inspect
|
||||
end
|
||||
|
||||
def test_three_args
|
||||
method = parse_collect("def meth3(yksi,kaksi,kolme); 1;end").first
|
||||
assert method.args_type.variable_index(:kolme) , method.args_type.inspect
|
||||
end
|
||||
|
||||
def test_one_local
|
||||
method = parse_collect("def meth2(arg1); foo = 2 ;end").first
|
||||
assert method.locals_type.variable_index(:foo) , method.locals_type.inspect
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
40
test/melon/compilers/test_type_collector.rb
Normal file
40
test/melon/compilers/test_type_collector.rb
Normal file
@ -0,0 +1,40 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
module Compilers
|
||||
class TestTypeCollector < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
end
|
||||
|
||||
def parse_collect( input )
|
||||
ast = Parser::Ruby22.parse input
|
||||
TypeCollector.new.collect(ast)
|
||||
end
|
||||
|
||||
def test_ivar_name
|
||||
hash = parse_collect "def meth; @ivar;end"
|
||||
assert hash[:ivar] , hash
|
||||
end
|
||||
|
||||
def test_ivar_assign
|
||||
hash = parse_collect "def meth; @ivar = 5;end"
|
||||
assert hash[:ivar] , hash
|
||||
end
|
||||
|
||||
def test_ivar_operator_assign
|
||||
hash = parse_collect "def meth; @ivar += 5;end"
|
||||
assert hash[:ivar] , hash
|
||||
end
|
||||
|
||||
def test_compile_class
|
||||
Compiler.compile "class TestIvar < Object ; def meth; @ivar;end; end"
|
||||
itest = Parfait.object_space.get_class_by_name(:TestIvar)
|
||||
assert itest
|
||||
assert itest.instance_type.names.include?(:ivar) , itest.instance_type.names.inspect
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
@ -1,2 +1,3 @@
|
||||
require_relative "compiler/test_all"
|
||||
require_relative "compilers/test_all"
|
||||
require_relative "fragments/test_all"
|
||||
require_relative "test_ruby_method"
|
||||
|
@ -1,7 +1,7 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
class TestMethod < MiniTest::Test
|
||||
class TestRubyMethod < MiniTest::Test
|
||||
include CompilerHelper
|
||||
|
||||
def setup
|
Loading…
Reference in New Issue
Block a user