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
|
2014-05-13 15:24:19 +02:00
|
|
|
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
|
2014-05-14 10:33:23 +02:00
|
|
|
last = nil
|
2014-05-13 15:24:19 +02:00
|
|
|
body.each do |part|
|
2014-05-14 10:33:23 +02:00
|
|
|
last = part.compile(context , into )
|
|
|
|
puts "compiled in while #{last.inspect}"
|
2014-05-13 15:24:19 +02:00
|
|
|
end
|
2014-05-14 21:04:03 +02:00
|
|
|
#jump back to test
|
2014-05-14 10:33:23 +02:00
|
|
|
return last
|
2014-05-13 15:24:19 +02:00
|
|
|
end
|
2014-05-10 18:02:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|