Fix ruby normalising to_vool
So that vool layer never has complex conditions or returns Start with while, next if, return and assign
This commit is contained in:
parent
84b9811e55
commit
c213cf874b
@ -1,21 +1,23 @@
|
|||||||
module Ruby
|
module Ruby
|
||||||
module Normalizer
|
module Normalizer
|
||||||
# given a something, determine if it is a Name
|
# Normalize ruby to vool by "flattening" structure
|
||||||
#
|
#
|
||||||
# Return a Name, and a possible rest that has a hoisted part of the statement
|
# This is a common issue for return, if and while , which all need to operate on the
|
||||||
|
# last value. In ruby the last value is always implicit, in vool not.
|
||||||
#
|
#
|
||||||
# eg if( @var % 5) is not normalized
|
# A "normalized" structure is first of all not recursive, a list not a tree,
|
||||||
# but if(tmp_123) is with tmp_123 = @var % 5 hoisted above the if
|
# The last expression of the list may be one of three things
|
||||||
|
# - a constant (unlikely, unless code is returning /testing a constant)
|
||||||
|
# - any variable (same)
|
||||||
|
# - a simple call (most common, but see call "normalisation" in SendStatement)
|
||||||
#
|
#
|
||||||
# also constants count, though they may not be so useful in ifs, but returns
|
# We return the last expression, the one that is returned or tested on, seperately
|
||||||
def normalize_name( condition )
|
#
|
||||||
if( condition.is_a?(ScopeStatement) and condition.single?)
|
def normalized_vool( condition )
|
||||||
condition = condition.first
|
vool_condition = condition.to_vool
|
||||||
end
|
return vool_condition unless( vool_condition.is_a?(Vool::Statements) )
|
||||||
return [condition] if condition.is_a?(Variable) or condition.is_a?(Constant)
|
return vool_condition.first if( vool_condition.single?)
|
||||||
local = "tmp_#{object_id}".to_sym
|
return [vool_condition.pop , vool_condition ]
|
||||||
assign = LocalAssignment.new( local , condition)
|
|
||||||
[LocalVariable.new(local) , assign]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,8 +4,8 @@ module Ruby
|
|||||||
# The SendStatement really only provides to_s, so see CallStatement
|
# The SendStatement really only provides to_s, so see CallStatement
|
||||||
#
|
#
|
||||||
class SendStatement < CallStatement
|
class SendStatement < CallStatement
|
||||||
def to_s
|
def to_s(depth = 0)
|
||||||
"#{receiver}.#{name}(#{arguments.join(', ')})"
|
at_depth( depth , "#{receiver}.#{name}(#{arguments.join(', ')})")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -23,8 +23,8 @@ module Ruby
|
|||||||
def add_ivar( array )
|
def add_ivar( array )
|
||||||
array << @name
|
array << @name
|
||||||
end
|
end
|
||||||
def to_s
|
def to_s(depth = 0)
|
||||||
"@#{name}"
|
at_depth(depth , "@#{name}" )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -12,8 +12,8 @@ module Ruby
|
|||||||
end
|
end
|
||||||
|
|
||||||
def to_vool
|
def to_vool
|
||||||
cond , rest = *normalize_name(@condition)
|
cond , hoisted = *normalized_vool(@condition)
|
||||||
Vool::WhileStatement.new(cond.to_vool , @body.to_vool , rest&.to_vool)
|
Vool::WhileStatement.new(cond , @body.to_vool , hoisted)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s(depth = 0)
|
def to_s(depth = 0)
|
||||||
|
@ -2,6 +2,10 @@ module Vool
|
|||||||
|
|
||||||
class IvarAssignment < Assignment
|
class IvarAssignment < Assignment
|
||||||
|
|
||||||
|
def to_s(depth = 0)
|
||||||
|
"@#{super(depth)}"
|
||||||
|
end
|
||||||
|
|
||||||
def to_mom( compiler )
|
def to_mom( compiler )
|
||||||
to = Mom::SlotDefinition.new(:message ,[ :receiver , @name])
|
to = Mom::SlotDefinition.new(:message ,[ :receiver , @name])
|
||||||
from = @value.slot_definition(compiler)
|
from = @value.slot_definition(compiler)
|
||||||
|
@ -36,7 +36,6 @@ module Vool
|
|||||||
# - Setting up the next message, with receiver, arguments, and (importantly) return address
|
# - Setting up the next message, with receiver, arguments, and (importantly) return address
|
||||||
# - a CachedCall , or a SimpleCall, depending on wether the receiver type can be determined
|
# - a CachedCall , or a SimpleCall, depending on wether the receiver type can be determined
|
||||||
def to_mom( compiler )
|
def to_mom( compiler )
|
||||||
puts "Compiling #{self.to_s}"
|
|
||||||
@receiver = SelfExpression.new(compiler.receiver_type) if @receiver.is_a?(SelfExpression)
|
@receiver = SelfExpression.new(compiler.receiver_type) if @receiver.is_a?(SelfExpression)
|
||||||
if(@receiver.ct_type)
|
if(@receiver.ct_type)
|
||||||
method = @receiver.ct_type.resolve_method(self.name)
|
method = @receiver.ct_type.resolve_method(self.name)
|
||||||
|
@ -33,6 +33,9 @@ module Vool
|
|||||||
def shift
|
def shift
|
||||||
@statements.shift
|
@statements.shift
|
||||||
end
|
end
|
||||||
|
def pop
|
||||||
|
@statements.pop
|
||||||
|
end
|
||||||
|
|
||||||
# to_mom all the statements. Append subsequent ones to the first, and return the
|
# to_mom all the statements. Append subsequent ones to the first, and return the
|
||||||
# first.
|
# first.
|
||||||
|
@ -14,6 +14,7 @@ module Vool
|
|||||||
cond_label = Mom::Label.new(self, "cond_label_#{object_id.to_s(16)}")
|
cond_label = Mom::Label.new(self, "cond_label_#{object_id.to_s(16)}")
|
||||||
codes = cond_label
|
codes = cond_label
|
||||||
codes << @hoisted.to_mom(compiler) if @hoisted
|
codes << @hoisted.to_mom(compiler) if @hoisted
|
||||||
|
codes << @condition.to_mom(compiler) if @condition.is_a?(SendStatement)
|
||||||
codes << Mom::TruthCheck.new(condition.slot_definition(compiler) , merge_label)
|
codes << Mom::TruthCheck.new(condition.slot_definition(compiler) , merge_label)
|
||||||
codes << @body.to_mom(compiler)
|
codes << @body.to_mom(compiler)
|
||||||
codes << Mom::Jump.new(cond_label)
|
codes << Mom::Jump.new(cond_label)
|
||||||
|
@ -22,20 +22,20 @@ module Ruby
|
|||||||
class TestWhileStatementHoist < MiniTest::Test
|
class TestWhileStatementHoist < MiniTest::Test
|
||||||
include RubyTests
|
include RubyTests
|
||||||
def setup
|
def setup
|
||||||
@lst = compile( "while(arg > 1) ; arg = 1 ; end" ).to_vool
|
@lst = compile( "while(call(arg > 1)) ; arg = 1 ; end" ).to_vool
|
||||||
end
|
end
|
||||||
def test_class
|
def test_class
|
||||||
assert_equal Vool::WhileStatement , @lst.class
|
assert_equal Vool::WhileStatement , @lst.class
|
||||||
assert_equal Vool::LocalAssignment , @lst.body.class
|
assert_equal Vool::LocalAssignment , @lst.body.class
|
||||||
end
|
end
|
||||||
def test_condition_class
|
def test_condition_class
|
||||||
assert_equal Vool::LocalVariable , @lst.condition.class
|
assert_equal Vool::SendStatement , @lst.condition.class
|
||||||
end
|
end
|
||||||
def test_hoist
|
def test_hoist
|
||||||
assert_equal Vool::LocalAssignment , @lst.hoisted.class
|
assert_equal Vool::Statements , @lst.hoisted.class
|
||||||
end
|
end
|
||||||
def test_hoist_is_cond
|
def test_hoist_is_assi
|
||||||
assert_equal @lst.hoisted.name , @lst.condition.name
|
assert_equal Vool::LocalAssignment , @lst.hoisted.first.class
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -12,13 +12,13 @@ module Vool
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_condition_compiles_to_check
|
def test_condition_compiles_to_check
|
||||||
assert_equal TruthCheck , @ins.next(5).class
|
assert_equal TruthCheck , @ins.next(4).class
|
||||||
end
|
end
|
||||||
def test_condition_is_slot
|
def test_condition_is_slot
|
||||||
assert_equal SlotDefinition , @ins.next(5).condition.class , @ins
|
assert_equal SlotDefinition , @ins.next(4).condition.class , @ins
|
||||||
end
|
end
|
||||||
def test_hoisetd
|
def test_hoisetd
|
||||||
jump = @ins.next(9)
|
jump = @ins.next(8)
|
||||||
assert_kind_of Jump , jump
|
assert_kind_of Jump , jump
|
||||||
assert jump.label.name.start_with?("cond_label") , jump.label.name
|
assert jump.label.name.start_with?("cond_label") , jump.label.name
|
||||||
end
|
end
|
||||||
@ -28,9 +28,9 @@ module Vool
|
|||||||
assert label.name.start_with?("cond_label") , label.name
|
assert label.name.start_with?("cond_label") , label.name
|
||||||
end
|
end
|
||||||
def test_array
|
def test_array
|
||||||
check_array [Label, MessageSetup, ArgumentTransfer, SimpleCall, SlotLoad ,
|
check_array [Label, MessageSetup, ArgumentTransfer, SimpleCall, TruthCheck ,
|
||||||
TruthCheck, MessageSetup, ArgumentTransfer, SimpleCall, Jump ,
|
MessageSetup, ArgumentTransfer, SimpleCall, Jump, Label ,
|
||||||
Label, Label, ReturnSequence, Label] , @ins
|
Label, ReturnSequence, Label] , @ins
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user