From fa14e35a50edd28c59f74fc1365b8947dfe5b751 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 9 Dec 2016 13:38:07 +0200 Subject: [PATCH] refactor if_statement --- lib/typed/ast/if_statement.rb | 6 +++-- lib/typed/ast/to_code.rb | 2 +- lib/typed/compiler.rb | 3 +-- lib/typed/compiler/if_statement.rb | 36 +++++++++++++++++------------- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/typed/ast/if_statement.rb b/lib/typed/ast/if_statement.rb index 63cc6917..a07a8827 100644 --- a/lib/typed/ast/if_statement.rb +++ b/lib/typed/ast/if_statement.rb @@ -1,5 +1,7 @@ module Typed - class IfStatement < Statement - attr_accessor :branch_type , :condition , :if_true , :if_false + module Tree + class IfStatement < Statement + attr_accessor :branch_type , :condition , :if_true , :if_false + end end end diff --git a/lib/typed/ast/to_code.rb b/lib/typed/ast/to_code.rb index ae851e61..54accd53 100644 --- a/lib/typed/ast/to_code.rb +++ b/lib/typed/ast/to_code.rb @@ -62,7 +62,7 @@ module Typed def on_if_statement statement branch_type , condition , if_true , if_false = *statement - w = IfStatement.new() + w = Tree::IfStatement.new() w.branch_type = branch_type w.condition = process(condition) w.if_true = process(if_true) diff --git a/lib/typed/compiler.rb b/lib/typed/compiler.rb index 69a6651a..c4004ec1 100644 --- a/lib/typed/compiler.rb +++ b/lib/typed/compiler.rb @@ -1,6 +1,6 @@ module Typed - CompilerModules = ["assignment" , "call_site", "name_expression"] + CompilerModules = ["assignment" , "call_site", "if_statement" ,"name_expression"] CompilerModules.each do |mod| require_relative "compiler/" + mod end @@ -194,7 +194,6 @@ require_relative "compiler/collections" require_relative "compiler/field_def" require_relative "compiler/field_access" require_relative "compiler/function_definition" -require_relative "compiler/if_statement" require_relative "compiler/operator_value" require_relative "compiler/return_statement" require_relative "compiler/statement_list" diff --git a/lib/typed/compiler/if_statement.rb b/lib/typed/compiler/if_statement.rb index c4f57ea7..3c128f93 100644 --- a/lib/typed/compiler/if_statement.rb +++ b/lib/typed/compiler/if_statement.rb @@ -1,35 +1,41 @@ module Typed - Compiler.class_eval do + module IfStatement # an if evaluates the condition and jumps to the true block if true # so the else block is automatically after that. # But then the else needs to jump over the true block unconditionally. - def on_IfStatement statement + def on_IfStatement( statement ) # branch_type , condition , if_true , if_false = *statement -# condition = condition.first + true_block = compile_condition( statement ) + merge = compile_false( statement ) + add_code true_block + compile_true(statement) + add_code merge + nil # statements don't return anything + end + + private + + def compile_condition( statement ) reset_regs process(statement.condition) - branch_class = Object.const_get "Register::Is#{statement.branch_type.capitalize}" true_block = Register::Label.new(statement, "if_true") add_code branch_class.new( statement.condition , true_block ) + return true_block + end + def compile_true( statement ) + reset_regs + process(statement.if_true) + end - # compile the false block + def compile_false( statement ) reset_regs process(statement.if_false) if statement.if_false.statements merge = Register::Label.new(statement , "if_merge") add_code Register::Branch.new(statement.if_false, merge ) - - # compile the true block - add_code true_block - reset_regs - process(statement.if_true) - - #puts "compiled if: end" - add_code merge - - nil # statements don't return anything + merge end end end