class Ikra::Translator::ASTTranslator::StatementTranslator
Attributes
translator[R]
This class should not inherit from [AST::Visitor]. Otherwise, the dispatch mechanisum to [ExpressionTranslator] will not work properly anymore.
Public Class Methods
new(translator)
click to toggle source
# File lib/translator/ast_translator.rb, line 375 def initialize(translator) @translator = translator end
Public Instance Methods
expression_translator()
click to toggle source
# File lib/translator/ast_translator.rb, line 379 def expression_translator return translator.expression_translator end
method_missing(symbol, *args)
click to toggle source
Calls superclass method
# File lib/translator/ast_translator.rb, line 387 def method_missing(symbol, *args) if symbol.to_s.start_with?("visit_") if expression_translator.respond_to?(symbol) return expression_translator.send(symbol, *args) + ";\n" else super end else return translator.send(symbol, *args) end end
statement_translator()
click to toggle source
# File lib/translator/ast_translator.rb, line 383 def statement_translator return self end
visit_begin_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 443 def visit_begin_node(node) if node.body_stmts.size == 0 return wrap_in_c_block("") end body_translated = node.body_stmts.map do |stmt| stmt.accept(self) end.join("") return wrap_in_c_block(body_translated) end
visit_behavior_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 399 def visit_behavior_node(node) raise AssertionError.new("Methods/blocks cannot be translated as a statement") end
visit_break_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 432 def visit_break_node(node) return "break;\n" end
visit_for_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 407 def visit_for_node(node) loop_header = "for (#{node.iterator_identifier.to_s} = #{node.range_from.accept(expression_translator)}; #{node.iterator_identifier.to_s} <= #{node.range_to.accept(expression_translator)}; #{node.iterator_identifier.to_s}++)" return loop_header + "\n" + node.body_stmts.accept(self) + "#{node.iterator_identifier.to_s}--;\n" end
visit_if_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 436 def visit_if_node(node) return "if (#{node.condition.accept(expression_translator)})\n" + node.true_body_stmts.accept(self) + "else\n" + node.false_body_stmts.accept(self) end
visit_return_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 455 def visit_return_node(node) return "return #{node.value.accept(expression_translator)};\n" end
visit_root_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 403 def visit_root_node(node) return node.single_child.accept(self) end
visit_until_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 424 def visit_until_node(node) return "while (#{node.condition.accept(expression_translator)})\n#{node.body_stmts.accept(self)}" end
visit_until_post_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 428 def visit_until_post_node(node) return "do #{node.body_stmts.accept(self)}while (#{node.condition.accept(expression_translator)});\n" end
visit_while_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 416 def visit_while_node(node) return "while (#{node.condition.accept(expression_translator)})\n#{node.body_stmts.accept(self)}" end
visit_while_post_node(node)
click to toggle source
# File lib/translator/ast_translator.rb, line 420 def visit_while_post_node(node) return "do #{node.body_stmts.accept(self)}while (#{node.condition.accept(expression_translator)});\n" end