From c3939ef622a1bdf9e347a7336688ecd4f956f998 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 30 Aug 2017 22:27:12 +0300 Subject: [PATCH] start better if mom test --- lib/rubyx/ruby_method.rb | 6 +++++- lib/vm/tree/to_code.rb | 4 ++-- lib/vool/ruby_compiler.rb | 4 ++++ lib/vool/statements/if_statement.rb | 18 ++++++++--------- test/rubyx/test_ruby_method.rb | 22 +++++++++++++++++++++ test/vool/to_mom/test_condition_if.rb | 28 +++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 test/vool/to_mom/test_condition_if.rb diff --git a/lib/rubyx/ruby_method.rb b/lib/rubyx/ruby_method.rb index 8557fdc3..26c84986 100644 --- a/lib/rubyx/ruby_method.rb +++ b/lib/rubyx/ruby_method.rb @@ -20,6 +20,10 @@ module Rubyx type.create_method( @name , @args_type )#FIXME, @locals_type) end - + def create_tmp + tmp_name = "tmp_#{@locals_type.instance_length}" + @locals_type = @locals_type.add_instance_variable( tmp_name , :Object ) + tmp_name + end end end diff --git a/lib/vm/tree/to_code.rb b/lib/vm/tree/to_code.rb index 575bedbc..1e5e2adf 100644 --- a/lib/vm/tree/to_code.rb +++ b/lib/vm/tree/to_code.rb @@ -131,8 +131,8 @@ module Vm Tree::KnownName.new(statement.children.first) end - def on_string expression - Tree::StringExpression.new(expression.children.first) + def on_string expressions + Tree::StringExpression.new(expressions.children.first) end def on_class_name expression diff --git a/lib/vool/ruby_compiler.rb b/lib/vool/ruby_compiler.rb index d17c727e..9f8c2f52 100644 --- a/lib/vool/ruby_compiler.rb +++ b/lib/vool/ruby_compiler.rb @@ -185,6 +185,10 @@ module Vool w end + def handler_missing(node) + raise "Handler missing #{node}" + end + private def instance_name sym diff --git a/lib/vool/statements/if_statement.rb b/lib/vool/statements/if_statement.rb index bbe73d90..ab485031 100644 --- a/lib/vool/statements/if_statement.rb +++ b/lib/vool/statements/if_statement.rb @@ -10,18 +10,18 @@ module Vool end def to_mom( method ) - if_true = @if_true.to_mom( method ) - if_false = @if_false.to_mom( method ) merge = Mom::Noop.new(:merge) - make_condition( add_jump(if_true,merge) , add_jump(if_false,merge) , merge) + if_true = add_jump(@if_true.to_mom( method ) , merge) + if_false = add_jump(@if_false.to_mom( method ) , merge) + cond = hoist_condition( method ) + check = Mom::TruthCheck.new( cond.pop , if_true , if_false , merge) + [ *check , if_true , if_false , merge ] end - # conditions in ruby are almost always method sends (as even comparisons are) - # currently we just deal with straight up values which get tested - # for the funny ruby logic (everything but false and nil is true) - def make_condition( if_true , if_false , merge) - check = Mom::TruthCheck.new( @condition , if_true , if_false , merge) - [ check , if_true , if_false , merge ] + def hoist_condition( method ) + return [@condition] if @condition.is_a?(Vool::Named) + local = method.create_tmp + puts local end def collect(arr) diff --git a/test/rubyx/test_ruby_method.rb b/test/rubyx/test_ruby_method.rb index 98d7036f..f4e35d69 100644 --- a/test/rubyx/test_ruby_method.rb +++ b/test/rubyx/test_ruby_method.rb @@ -14,6 +14,18 @@ module Rubyx test.get_method(:meth) end + def create_method_arg + RubyCompiler.compile in_Test("def meth_arg(arg); arg ;end") + test = Parfait.object_space.get_class_by_name(:Test) + test.get_method(:meth_arg) + end + + def create_method_local + RubyCompiler.compile in_Test("def meth_local(arg); local = 5 ;end") + test = Parfait.object_space.get_class_by_name(:Test) + test.get_method(:meth_arg) + end + def test_creates_method_in_class method = create_method assert method , "No method created" @@ -34,5 +46,15 @@ module Rubyx assert_equal 1 , method.locals_type.instance_length end + def test_method_has_args + method = create_method_arg + assert_equal 2 , method.args_type.instance_length + end + + def Test_method_has_locals + method = create_method_local + assert_equal 2 , method.locals_type.instance_length + end + end end diff --git a/test/vool/to_mom/test_condition_if.rb b/test/vool/to_mom/test_condition_if.rb new file mode 100644 index 00000000..d541d818 --- /dev/null +++ b/test/vool/to_mom/test_condition_if.rb @@ -0,0 +1,28 @@ + +require_relative "helper" + +module Vool + class TestIfMom #< MiniTest::Test + include MomCompile + + def setup + Risc.machine.boot + @stats = compile_first_method( "if(@a == 5) ; 5.mod4 ; else; 4.mod4 ; end") + @first = @stats.first + end + + def test_if_compiles_as_array + assert_equal Array , @first.class , @stats + end + def test_condition_compiles_to_check + assert_equal Mom::TruthCheck , @first.first.class , @stats + end + def test_condition_is_instance + assert_equal Vool::LocalVariable , @first.first.condition.class , @stats + end + def est_true_block_is_second + assert_equal @first[1] , @first.first.true_block , @stats + end + + end +end