codes the while, but the testing throws stack too deep because loop detection is missing

This commit is contained in:
Torsten Ruger 2014-07-17 16:46:17 +03:00
parent f00fa98818
commit e02c6ed082
3 changed files with 36 additions and 18 deletions

View File

@ -2,7 +2,24 @@ module Ast
class WhileExpression < Expression
# attr_reader :condition, :body
def compile frame , method
Virtual::Reference.new
start = Virtual::Label.new("while_start")
method.add start
is = condition.compile(frame , method)
branch = Virtual::ImplicitBranch.new "while"
merge = Virtual::Label.new(branch.name)
branch.other = merge #false jumps to end of while
method.add branch
last = is
body.each do |part|
last = part.compile(frame,method )
raise part.inspect if last.nil?
end
# unconditionally brnach to the start
method.add start
# here we add the end of while that the branch jumps to
#but don't link it in (not using add)
method.current = merge
last
end
def old
into = context.function

View File

@ -77,7 +77,8 @@ module Virtual
before_label.next = label
end
end
attr_reader :other , :name
attr_reader :name
attr_accessor :other
def attributes
[:name , :next , :other]
end

View File

@ -58,14 +58,28 @@ HERE
check
end
def ttest_function_return
def test_function_while
@string_input = <<HERE
def fibonaccit(n)
a = 0
while (n) do
some = 43
other = some * 4
end
end
HERE
@output = [Virtual::MethodDefinition.new(:ofthen,[Virtual::Argument.new(:n,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::Local.new(:maybenot,Virtual::IntegerConstant.new(667)),Virtual::MethodEnter.new(Virtual::ImplicitBranch.new(:if_merge_2,Virtual::FrameSet.new(:isit,Virtual::IntegerConstant.new(42),Virtual::Label.new(:if_merge_2,nil)),Virtual::FrameSet.new(:maybenot,Virtual::IntegerConstant.new(667),Virtual::Label.new(:if_merge_2,nil)))))]
check
end
def test_function_return
@string_input = <<HERE
def retvar(n)
i = 5
return i
end
HERE
@output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
@output = [Virtual::MethodDefinition.new(:retvar,[Virtual::Argument.new(:n,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::Reference.new(nil),Virtual::MethodEnter.new(Virtual::FrameSet.new(:i,Virtual::IntegerConstant.new(5),nil)))]
check
end
@ -96,20 +110,6 @@ HERE
check
end
def ttest_function_while
@string_input = <<HERE
def fibonaccit(n)
a = 0
while (n) do
some = 43
other = some * 4
end
end
HERE
@output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
check
end
def ttest_function_big_while
@string_input = <<HERE
def fibonaccit(n)