fix ruby send statement

remove ruby expression
This commit is contained in:
Torsten Ruger 2018-07-20 10:05:11 +03:00
parent 8cd9818f64
commit 7b02feae7a
6 changed files with 144 additions and 74 deletions

View File

@ -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

View File

@ -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

View File

@ -20,12 +20,4 @@ module Ruby
end
end
class Expression
def to_vool
raise "should not be normalized #{self}"
end
end
end

View File

@ -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

View File

@ -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

View File

@ -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