From 7b02feae7a0692626842e742f4b74d9ed6dba9a7 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 20 Jul 2018 10:05:11 +0300 Subject: [PATCH] fix ruby send statement remove ruby expression --- lib/ruby/basic_values.rb | 49 +++++----------- lib/ruby/send_statement.rb | 12 ++-- lib/ruby/statement.rb | 8 --- lib/ruby/variables.rb | 8 +-- test/ruby/test_send_statement.rb | 43 +++++++------- test/ruby/test_send_statement1.rb | 98 +++++++++++++++++++++++++++++++ 6 files changed, 144 insertions(+), 74 deletions(-) create mode 100644 test/ruby/test_send_statement1.rb diff --git a/lib/ruby/basic_values.rb b/lib/ruby/basic_values.rb index f4a73a66..d0a3c332 100644 --- a/lib/ruby/basic_values.rb +++ b/lib/ruby/basic_values.rb @@ -1,28 +1,27 @@ module Ruby - class Constant < Expression - #gobble it up - def each(&block) + class Constant < Statement + def to_vool + vool_brother.new end end - - class IntegerConstant < Constant + class ValueConstant < Constant attr_reader :value def initialize(value) @value = value end - def slot_definition(compiler) - return Mom::SlotDefinition.new(Mom::IntegerConstant.new(@value) , []) + def to_vool + vool_brother.new(@value) end + end + class IntegerConstant < ValueConstant def ct_type Parfait.object_space.get_type_by_class_name(:Integer) end def to_s value.to_s end - def each(&block) - end end - class FloatConstant < Constant + class FloatConstant < ValueConstant attr_reader :value def initialize(value) @value = value @@ -35,9 +34,6 @@ module Ruby def ct_type Parfait.object_space.get_type_by_class_name(:True) end - def slot_definition(compiler) - return Mom::SlotDefinition.new(Parfait.object_space.true_object , []) - end def to_s(depth = 0) "true" end @@ -46,9 +42,6 @@ module Ruby def ct_type Parfait.object_space.get_type_by_class_name(:False) end - def slot_definition(compiler) - return Mom::SlotDefinition.new(Parfait.object_space.false_object , []) - end def to_s(depth = 0) "false" end @@ -57,25 +50,11 @@ module Ruby def ct_type Parfait.object_space.get_type_by_class_name(:Nil) end - def slot_definition(compiler) - return Mom::SlotDefinition.new(Parfait.object_space.nil_object , []) - end def to_s(depth = 0) "nil" end end - class SelfExpression < Expression - attr_reader :my_type - def initialize(type = nil) - @my_type = type - end - def slot_definition(compiler) - @my_type = compiler.receiver_type - Mom::SlotDefinition.new(:message , [:receiver]) - end - def ct_type - @my_type - end + class SelfExpression < Constant def to_s(depth = 0) "self" end @@ -84,15 +63,15 @@ module Ruby def to_s(depth = 0) "super" end + def to_vool + vool_brother.new + end end - class StringConstant < Constant + class StringConstant < ValueConstant attr_reader :value def initialize(value) @value = value end - def slot_definition(compiler) - return Mom::SlotDefinition.new(Mom::StringConstant.new(@value),[]) - end def ct_type Parfait.object_space.get_type_by_class_name(:Word) end diff --git a/lib/ruby/send_statement.rb b/lib/ruby/send_statement.rb index 3b851e1c..9a8f2a05 100644 --- a/lib/ruby/send_statement.rb +++ b/lib/ruby/send_statement.rb @@ -9,27 +9,27 @@ module Ruby end def to_vool - statements = Statements.new([]) + statements = Vool::Statements.new([]) arguments = [] @arguments.each_with_index do |arg , index | normalize_arg(arg , arguments , statements) end if statements.empty? - return SendStatement.new(@name, @receiver , @arguments) + return Vool::SendStatement.new(@name, @receiver.to_vool , @arguments) else - statements << SendStatement.new(@name, @receiver , arguments) + statements << Vool::SendStatement.new(@name, @receiver.to_vool , arguments) return statements end end - def to_vool_arg(arg , arguments , statements) + def normalize_arg(arg , arguments , statements) if arg.respond_to?(:slot_definition) and !arg.is_a?(SendStatement) arguments << arg return end - assign = LocalAssignment.new( "tmp_#{arg.object_id}".to_sym, arg) + assign = Vool::LocalAssignment.new( "tmp_#{arg.object_id}".to_sym, arg) statements << assign - arguments << LocalVariable.new(assign.name) + arguments << Vool::LocalVariable.new(assign.name) end def to_s diff --git a/lib/ruby/statement.rb b/lib/ruby/statement.rb index 7993498e..c34261eb 100644 --- a/lib/ruby/statement.rb +++ b/lib/ruby/statement.rb @@ -20,12 +20,4 @@ module Ruby end end - class Expression - - def to_vool - raise "should not be normalized #{self}" - end - - end - end diff --git a/lib/ruby/variables.rb b/lib/ruby/variables.rb index c2145985..551ec994 100644 --- a/lib/ruby/variables.rb +++ b/lib/ruby/variables.rb @@ -8,14 +8,14 @@ module Ruby end end - class LocalVariable < Expression + class LocalVariable < Statement include Named def to_s name.to_s end end - class InstanceVariable < Expression + class InstanceVariable < Statement include Named # used to collect type information def add_ivar( array ) @@ -26,11 +26,11 @@ module Ruby end end - class ClassVariable < Expression + class ClassVariable < Statement include Named end - class ModuleName < Expression + class ModuleName < Statement include Named end end diff --git a/test/ruby/test_send_statement.rb b/test/ruby/test_send_statement.rb index 25b07169..4ab19949 100644 --- a/test/ruby/test_send_statement.rb +++ b/test/ruby/test_send_statement.rb @@ -1,43 +1,44 @@ require_relative "helper" module Ruby - class TestSend < MiniTest::Test + class TestSendFoo < MiniTest::Test include RubyTests - - def test_simple - lst = compile( "foo") - assert_equal SendStatement , lst.class + def setup + @lst = compile( "foo") + end + def test_simple_class + assert_equal SendStatement , @lst.class end def test_simple_name - lst = compile( "foo") - assert_equal :foo , lst.name + assert_equal :foo , @lst.name end def test_simple_receiver - lst = compile( "foo") - assert_equal SelfExpression , lst.receiver.class + assert_equal SelfExpression , @lst.receiver.class end def test_simple_args - lst = compile( "foo") - assert_equal [] , lst.arguments + assert_equal [] , @lst.arguments + end + end + class TestSendBar < MiniTest::Test + include RubyTests + def setup + @lst = compile( "bar(1)") end - def test_one_arg - lst = compile( "bar(1)") - assert_equal SendStatement , lst.class + assert_equal SendStatement , @lst.class end def test_one_arg_name - lst = compile( "bar(1)") - assert_equal :bar , lst.name + assert_equal :bar , @lst.name end def test_one_arg_receiver - lst = compile( "bar(1)") - assert_equal SelfExpression , lst.receiver.class + assert_equal SelfExpression , @lst.receiver.class end def test_one_arg_args - lst = compile( "bar(1)") - assert_equal 1 , lst.arguments.first.value + assert_equal 1 , @lst.arguments.first.value end - + end + class TestSendSuper < MiniTest::Test + include RubyTests def test_super0_receiver lst = compile( "super") assert_equal SuperExpression , lst.receiver.class diff --git a/test/ruby/test_send_statement1.rb b/test/ruby/test_send_statement1.rb new file mode 100644 index 00000000..28e971bc --- /dev/null +++ b/test/ruby/test_send_statement1.rb @@ -0,0 +1,98 @@ +require_relative "helper" + +module Ruby + class TestSendFooVool < MiniTest::Test + include RubyTests + def setup + @lst = compile( "foo").to_vool + end + def test_simple_class + assert_equal Vool::SendStatement , @lst.class + end + def test_simple_name + assert_equal :foo , @lst.name + end + def test_simple_receiver + assert_equal Vool::SelfExpression , @lst.receiver.class + end + def test_simple_args + assert_equal [] , @lst.arguments + end + end + class TestSendBarVool < MiniTest::Test + include RubyTests + def setup + @lst = compile( "bar(1)").to_vool + end + def test_class + assert_equal Vool::Statements , @lst.class + assert_equal 2 , @lst.length + end + def test_first + assert_equal Vool::LocalAssignment , @lst.first.class + end + def test_last + assert_equal Vool::SendStatement , @lst.last.class + end + def test_name + assert_equal :bar , @lst.last.name + end + def test_receiver + assert_equal Vool::SelfExpression , @lst.last.receiver.class + end + def test_args + assert @lst.last.arguments.first.name.to_s.start_with?("tmp") + end + end + class TestSendSuperVool < MiniTest::Test + include RubyTests + def test_super0_receiver + lst = compile( "super").to_vool + assert_equal Vool::SuperExpression , lst.receiver.class + end + def test_super0 + lst = compile( "super").to_vool + assert_equal Vool::SendStatement , lst.class + end + end + class TestSendSuperArgsVool < MiniTest::Test + include RubyTests + def setup + @lst = compile( "super(1)").to_vool + end + def test_super_args_class + assert_equal Vool::Statements , @lst.class + assert_equal 2 , @lst.length + end + def test_super_args_first + assert_equal Vool::LocalAssignment , @lst.first.class + end + def test_super_args_last + assert_equal Vool::SendStatement , @lst.last.class + end + def test_super_receiver + assert_equal Vool::SuperExpression , @lst.last.receiver.class + end + def test_super_name #is nil + assert_nil @lst.last.name + end + end + class TestSendReceiverTypeVool < MiniTest::Test + include RubyTests + + def setup + Parfait.boot! + end + + def test_int_receiver + sent = compile( "5.div4").to_vool + assert_equal Parfait::Type , sent.receiver.ct_type.class + assert_equal "Integer_Type" , sent.receiver.ct_type.name + end + def test_string_receiver + sent = compile( "'5'.putstring").to_vool + assert_equal Parfait::Type , sent.receiver.ct_type.class + assert_equal "Word_Type" , sent.receiver.ct_type.name + end + end +end