work on branches
the concept is different in bosl, move appropriate to virtual
This commit is contained in:
parent
4e26166dff
commit
f0611e52db
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
|
@ -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"
|
@ -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
|
@ -1,6 +0,0 @@
|
||||
module Virtual
|
||||
|
||||
class UnconditionalBranch < Branch
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue
Block a user