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:
@ -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)
|
||||
|
Reference in New Issue
Block a user