diff --git a/lib/bosl/compiler/if_expression.rb b/lib/bosl/compiler/if_expression.rb index 98da7875..b9d3d428 100644 --- a/lib/bosl/compiler/if_expression.rb +++ b/lib/bosl/compiler/if_expression.rb @@ -12,11 +12,10 @@ module Bosl true_block = @method.source.new_block "if_true" # second, linked in after current, before merge false_block = @method.source.new_block "if_false" # directly next in order, ie if we don't jump we land here - - is = process(condition ) + is = process(condition) # TODO should/will use different branches for different conditions. # just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? BranchCondition - @method.source.add_code Virtual::IsTrueBranch.new( true_block ) + @method.source.add_code Register::IsZeroBranch.new( condition , true_block ) # compile the true block (as we think of it first, even it is second in sequential order) @method.source.current true_block @@ -26,7 +25,7 @@ module Bosl # compile the false block @method.source.current false_block last = process_all(if_false).last if if_false - @method.source.add_code Virtual::UnconditionalBranch.new( merge_block ) + @method.source.add_code Register::AlwaysBranch.new(expression, merge_block ) #puts "compiled if: end" @method.source.current merge_block diff --git a/lib/bosl/compiler/while_expression.rb b/lib/bosl/compiler/while_expression.rb index b56deaa0..4b637fde 100644 --- a/lib/bosl/compiler/while_expression.rb +++ b/lib/bosl/compiler/while_expression.rb @@ -14,12 +14,12 @@ module Bosl cond = process(condition) - @method.source.add_code Virtual::IsTrueBranch.new(merge) + @method.source.add_code Register::IsZeroBranch.new(condition,merge) last = process_all(expressions).last # unconditionally branch to the start - @method.source.add_code Virtual::UnconditionalBranch.new(start) + @method.source.add_code Register::AlwaysBranch.new(expression,start) # continue execution / compiling at the merge block @method.source.current merge diff --git a/lib/register/instructions/branch.rb b/lib/register/instructions/branch.rb index 04fe3adf..80022493 100644 --- a/lib/register/instructions/branch.rb +++ b/lib/register/instructions/branch.rb @@ -13,7 +13,19 @@ module Register def to_s "Branch: #{block.name}" end + alias :inspect :to_s + end + class IsZeroBranch < Branch + end + + class IsNegativeBranch < Branch + end + + class IsPositiveBranch < Branch + end + + class AlwaysBranch < Branch end end diff --git a/lib/virtual/instruction.rb b/lib/virtual/instruction.rb index 3d4ac385..05da66b2 100644 --- a/lib/virtual/instruction.rb +++ b/lib/virtual/instruction.rb @@ -13,7 +13,6 @@ module Virtual end -require_relative "instructions/branch" require_relative "instructions/halt" require_relative "instructions/instance_get" require_relative "instructions/method_call" diff --git a/lib/virtual/instructions/branch.rb b/lib/virtual/instructions/branch.rb deleted file mode 100644 index e6368c3d..00000000 --- a/lib/virtual/instructions/branch.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Virtual - - - # a branch must branch to a block. This is an abstract class, names indicate the actual test - class Branch < Instruction - def initialize to - @to = to - end - attr_reader :to - end -end - -require_relative "is_true_branch" -require_relative "unconditional_branch" diff --git a/lib/virtual/instructions/is_true_branch.rb b/lib/virtual/instructions/is_true_branch.rb deleted file mode 100644 index 82f61bba..00000000 --- a/lib/virtual/instructions/is_true_branch.rb +++ /dev/null @@ -1,8 +0,0 @@ -module Virtual - - # implicit means there is no explcit test involved. - # normal ruby rules are false and nil are false, EVERYTHING else is true (and that includes 0) - class IsTrueBranch < Branch - end - -end diff --git a/lib/virtual/instructions/unconditional_branch.rb b/lib/virtual/instructions/unconditional_branch.rb deleted file mode 100644 index 1665932d..00000000 --- a/lib/virtual/instructions/unconditional_branch.rb +++ /dev/null @@ -1,6 +0,0 @@ -module Virtual - - class UnconditionalBranch < Branch - end - -end