rubyx/lib/ast/while_expression.rb

35 lines
1015 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
while_block = into.new_block "#{into.name}_while"
ret = while_block.new_block "#{into.name}_return"
2014-05-22 13:56:31 +02:00
cond_val = condition.compile(context , while_block)
2014-05-22 20:38:57 +02:00
puts "compiled while condition #{cond_val.inspect}"
2014-05-28 13:26:24 +02:00
while_block.b ret , condition_code: cond_val.operator
last = nil
body.each do |part|
2014-05-22 13:56:31 +02:00
last = part.compile(context , while_block )
puts "compiled in while #{last.inspect}"
end
2014-05-22 13:56:31 +02:00
while_block.b while_block
2014-05-22 20:38:57 +02:00
puts "compile while end"
into.insert_at ret
return last
end
2014-05-10 18:02:51 +02:00
end
end