2014-06-25 01:47:59 +02:00
|
|
|
module Ast
|
|
|
|
class WhileExpression < Expression
|
|
|
|
# attr_reader :condition, :body
|
2014-07-25 09:49:34 +02:00
|
|
|
def compile method , message
|
2014-07-17 15:46:17 +02:00
|
|
|
start = Virtual::Label.new("while_start")
|
2014-08-13 10:59:51 +02:00
|
|
|
method.add_code start
|
2014-07-25 09:49:34 +02:00
|
|
|
is = condition.compile(method,message)
|
2014-07-17 15:46:17 +02:00
|
|
|
branch = Virtual::ImplicitBranch.new "while"
|
|
|
|
merge = Virtual::Label.new(branch.name)
|
|
|
|
branch.other = merge #false jumps to end of while
|
2014-08-13 10:59:51 +02:00
|
|
|
method.add_code branch
|
2014-07-17 15:46:17 +02:00
|
|
|
last = is
|
|
|
|
body.each do |part|
|
2014-07-25 09:49:34 +02:00
|
|
|
last = part.compile(method,message )
|
2014-07-17 15:46:17 +02:00
|
|
|
raise part.inspect if last.nil?
|
|
|
|
end
|
|
|
|
# unconditionally brnach to the start
|
2014-07-22 22:27:13 +02:00
|
|
|
merge.next = method.current.next
|
|
|
|
method.current.next = start
|
2014-07-17 15:46:17 +02:00
|
|
|
# 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
|
2014-07-14 15:19:47 +02:00
|
|
|
end
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|
|
|
|
end
|