rubyx/lib/ast/while_expression.rb

31 lines
770 B
Ruby
Raw Normal View History

2014-05-10 18:02:51 +02:00
module Ast
class WhileExpression < Expression
attr_reader :condition, :body
def initialize condition, body
@condition , @body = condition , body
end
def inspect
self.class.name + ".new(" + condition.inspect + ", " + body.inspect + " )"
end
2014-05-13 09:49:26 +02:00
def to_s
"while(#{condition}) do\n" + body.join("\n") + "\nend\n"
end
2014-05-10 18:02:51 +02:00
def attributes
[:condition, :body]
end
def compile context , into
cond_val = condition.compile(context , into)
#set up branches for bodies
2014-05-14 21:04:03 +02:00
# jump to end if done
last = nil
body.each do |part|
last = part.compile(context , into )
puts "compiled in while #{last.inspect}"
end
2014-05-14 21:04:03 +02:00
#jump back to test
return last
end
2014-05-10 18:02:51 +02:00
end
end