rubyx/lib/ast/while_expression.rb

32 lines
876 B
Ruby
Raw Normal View History

2014-05-10 19:02:51 +03: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 10:49:26 +03:00
def to_s
"while(#{condition}) do\n" + body.join("\n") + "\nend\n"
end
2014-05-10 19:02:51 +03:00
def attributes
[:condition, :body]
end
def compile context , into
2014-05-22 14:56:31 +03:00
ret = into.new_block "#{into.name}_return_#{hash}"
while_block = into.new_block "#{into.name}_while_#{hash}"
cond_val = condition.compile(context , while_block)
while_block.beq ret
last = nil
body.each do |part|
2014-05-22 14:56:31 +03:00
last = part.compile(context , while_block )
puts "compiled in while #{last.inspect}"
end
2014-05-22 14:56:31 +03:00
while_block.b while_block
return last
end
2014-05-10 19:02:51 +03:00
end
end