codes the while, but the testing throws stack too deep because loop detection is missing
This commit is contained in:
parent
f00fa98818
commit
e02c6ed082
@ -2,7 +2,24 @@ module Ast
|
|||||||
class WhileExpression < Expression
|
class WhileExpression < Expression
|
||||||
# attr_reader :condition, :body
|
# attr_reader :condition, :body
|
||||||
def compile frame , method
|
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
|
end
|
||||||
def old
|
def old
|
||||||
into = context.function
|
into = context.function
|
||||||
|
@ -77,7 +77,8 @@ module Virtual
|
|||||||
before_label.next = label
|
before_label.next = label
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
attr_reader :other , :name
|
attr_reader :name
|
||||||
|
attr_accessor :other
|
||||||
def attributes
|
def attributes
|
||||||
[:name , :next , :other]
|
[:name , :next , :other]
|
||||||
end
|
end
|
||||||
|
@ -58,14 +58,28 @@ HERE
|
|||||||
check
|
check
|
||||||
end
|
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
|
@string_input = <<HERE
|
||||||
def retvar(n)
|
def retvar(n)
|
||||||
i = 5
|
i = 5
|
||||||
return i
|
return i
|
||||||
end
|
end
|
||||||
HERE
|
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
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -96,20 +110,6 @@ HERE
|
|||||||
check
|
check
|
||||||
end
|
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
|
def ttest_function_big_while
|
||||||
@string_input = <<HERE
|
@string_input = <<HERE
|
||||||
def fibonaccit(n)
|
def fibonaccit(n)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user