rubyx/lib/compiler/while_expression.rb

26 lines
831 B
Ruby
Raw Normal View History

module Compiler
# while- attr_reader :condition, :body
2015-05-06 14:14:47 +02:00
def self.compile_while expression, method
start = Virtual::Label.new("while_start")
method.add_code start
2015-05-06 14:14:47 +02:00
is = expression.condition.compile(method )
2014-10-18 12:53:01 +02:00
branch = Virtual::IsTrueBranch.new "while"
merge = Virtual::Label.new(branch.name)
branch.other = merge #false jumps to end of while
method.add_code branch
last = is
expression.body.each do |part|
2015-05-06 14:14:47 +02:00
last = part.compile(method )
raise part.inspect if last.nil?
end
# unconditionally brnach to the start
merge.next = method.current.next
method.current.next = 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
2014-07-14 15:19:47 +02:00
end
end