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 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
|
||||
# but if(tmp_123) is with tmp_123 = @var % 5 hoisted above the if
|
||||
# A "normalized" structure is first of all not recursive, a list not a tree,
|
||||
# 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
|
||||
def normalize_name( condition )
|
||||
if( condition.is_a?(ScopeStatement) and condition.single?)
|
||||
condition = condition.first
|
||||
end
|
||||
return [condition] if condition.is_a?(Variable) or condition.is_a?(Constant)
|
||||
local = "tmp_#{object_id}".to_sym
|
||||
assign = LocalAssignment.new( local , condition)
|
||||
[LocalVariable.new(local) , assign]
|
||||
# We return the last expression, the one that is returned or tested on, seperately
|
||||
#
|
||||
def normalized_vool( condition )
|
||||
vool_condition = condition.to_vool
|
||||
return vool_condition unless( vool_condition.is_a?(Vool::Statements) )
|
||||
return vool_condition.first if( vool_condition.single?)
|
||||
return [vool_condition.pop , vool_condition ]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -4,8 +4,8 @@ module Ruby
|
||||
# The SendStatement really only provides to_s, so see CallStatement
|
||||
#
|
||||
class SendStatement < CallStatement
|
||||
def to_s
|
||||
"#{receiver}.#{name}(#{arguments.join(', ')})"
|
||||
def to_s(depth = 0)
|
||||
at_depth( depth , "#{receiver}.#{name}(#{arguments.join(', ')})")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -23,8 +23,8 @@ module Ruby
|
||||
def add_ivar( array )
|
||||
array << @name
|
||||
end
|
||||
def to_s
|
||||
"@#{name}"
|
||||
def to_s(depth = 0)
|
||||
at_depth(depth , "@#{name}" )
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -12,8 +12,8 @@ module Ruby
|
||||
end
|
||||
|
||||
def to_vool
|
||||
cond , rest = *normalize_name(@condition)
|
||||
Vool::WhileStatement.new(cond.to_vool , @body.to_vool , rest&.to_vool)
|
||||
cond , hoisted = *normalized_vool(@condition)
|
||||
Vool::WhileStatement.new(cond , @body.to_vool , hoisted)
|
||||
end
|
||||
|
||||
def to_s(depth = 0)
|
||||
|
@ -2,6 +2,10 @@ module Vool
|
||||
|
||||
class IvarAssignment < Assignment
|
||||
|
||||
def to_s(depth = 0)
|
||||
"@#{super(depth)}"
|
||||
end
|
||||
|
||||
def to_mom( compiler )
|
||||
to = Mom::SlotDefinition.new(:message ,[ :receiver , @name])
|
||||
from = @value.slot_definition(compiler)
|
||||
|
@ -36,7 +36,6 @@ module Vool
|
||||
# - 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
|
||||
def to_mom( compiler )
|
||||
puts "Compiling #{self.to_s}"
|
||||
@receiver = SelfExpression.new(compiler.receiver_type) if @receiver.is_a?(SelfExpression)
|
||||
if(@receiver.ct_type)
|
||||
method = @receiver.ct_type.resolve_method(self.name)
|
||||
|
@ -33,6 +33,9 @@ module Vool
|
||||
def shift
|
||||
@statements.shift
|
||||
end
|
||||
def pop
|
||||
@statements.pop
|
||||
end
|
||||
|
||||
# to_mom all the statements. Append subsequent ones to the first, and return the
|
||||
# first.
|
||||
|
@ -14,6 +14,7 @@ module Vool
|
||||
cond_label = Mom::Label.new(self, "cond_label_#{object_id.to_s(16)}")
|
||||
codes = cond_label
|
||||
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 << @body.to_mom(compiler)
|
||||
codes << Mom::Jump.new(cond_label)
|
||||
|
@ -22,20 +22,20 @@ module Ruby
|
||||
class TestWhileStatementHoist < MiniTest::Test
|
||||
include RubyTests
|
||||
def setup
|
||||
@lst = compile( "while(arg > 1) ; arg = 1 ; end" ).to_vool
|
||||
@lst = compile( "while(call(arg > 1)) ; arg = 1 ; end" ).to_vool
|
||||
end
|
||||
def test_class
|
||||
assert_equal Vool::WhileStatement , @lst.class
|
||||
assert_equal Vool::LocalAssignment , @lst.body.class
|
||||
end
|
||||
def test_condition_class
|
||||
assert_equal Vool::LocalVariable , @lst.condition.class
|
||||
assert_equal Vool::SendStatement , @lst.condition.class
|
||||
end
|
||||
def test_hoist
|
||||
assert_equal Vool::LocalAssignment , @lst.hoisted.class
|
||||
assert_equal Vool::Statements , @lst.hoisted.class
|
||||
end
|
||||
def test_hoist_is_cond
|
||||
assert_equal @lst.hoisted.name , @lst.condition.name
|
||||
def test_hoist_is_assi
|
||||
assert_equal Vool::LocalAssignment , @lst.hoisted.first.class
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -12,13 +12,13 @@ module Vool
|
||||
end
|
||||
|
||||
def test_condition_compiles_to_check
|
||||
assert_equal TruthCheck , @ins.next(5).class
|
||||
assert_equal TruthCheck , @ins.next(4).class
|
||||
end
|
||||
def test_condition_is_slot
|
||||
assert_equal SlotDefinition , @ins.next(5).condition.class , @ins
|
||||
assert_equal SlotDefinition , @ins.next(4).condition.class , @ins
|
||||
end
|
||||
def test_hoisetd
|
||||
jump = @ins.next(9)
|
||||
jump = @ins.next(8)
|
||||
assert_kind_of Jump , jump
|
||||
assert jump.label.name.start_with?("cond_label") , jump.label.name
|
||||
end
|
||||
@ -28,9 +28,9 @@ module Vool
|
||||
assert label.name.start_with?("cond_label") , label.name
|
||||
end
|
||||
def test_array
|
||||
check_array [Label, MessageSetup, ArgumentTransfer, SimpleCall, SlotLoad ,
|
||||
TruthCheck, MessageSetup, ArgumentTransfer, SimpleCall, Jump ,
|
||||
Label, Label, ReturnSequence, Label] , @ins
|
||||
check_array [Label, MessageSetup, ArgumentTransfer, SimpleCall, TruthCheck ,
|
||||
MessageSetup, ArgumentTransfer, SimpleCall, Jump, Label ,
|
||||
Label, ReturnSequence, Label] , @ins
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user