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:
2019-08-16 14:09:56 +03:00
parent 84b9811e55
commit c213cf874b
10 changed files with 40 additions and 31 deletions

View File

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

View File

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

View File

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

View File

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