diff --git a/lib/ast/if_expression.rb b/lib/ast/if_expression.rb index 84d6acac..f5df7ced 100644 --- a/lib/ast/if_expression.rb +++ b/lib/ast/if_expression.rb @@ -3,12 +3,27 @@ module Ast # attr_reader :cond, :if_true, :if_false def compile frame , method is = cond.compile(frame , method) -# is.is_false(frame,method) + # is.is_false(frame,method) + # TODO should/will use different branches for different conditions. + branch = Virtual::ImplicitBranch.new + method.add branch last = is if_true.each do |part| last = part.compile(frame,method ) raise part.inspect if last.nil? end + label = Virtual::Label.new + method.add label + branch.swap + method.current = branch + if_false.each do |part| + last = part.compile(frame,method ) + raise part.inspect if last.nil? + end + method.add label + branch.swap + method.current = label + #TODO should return the union of the true and false types last end def old diff --git a/lib/virtual/instruction.rb b/lib/virtual/instruction.rb index 491914a3..2a5efd82 100644 --- a/lib/virtual/instruction.rb +++ b/lib/virtual/instruction.rb @@ -24,14 +24,45 @@ module Virtual # an implementation (in a programm it would be a function). But in a virtual machine, not only do we need this instruction, # it is indeed the first instruction as just this instruction is the smallest possible programm for the machine. # As such it is the next instruction for any first instruction that we generate. - class Halt < Instruction - + class Halt < Instruction end # following classes are stubs. currently in brainstorming mode, so anything may change anytime - class MethodDefinitionEnter < Instruction + class MethodEnter < Instruction end + #resolves to nothing, but allows forward definition + class Label < Instruction + end + + # the next instruction represents the true branch and the other is the .... other + # could have been the false, but false is a keyword and is asymetric to next anyway + # this is an abstract base class (though no measures are taken to prevent instantiation) and derived + # class names indicate the actual test + class Branch < Instruction + def initialize nex = nil , other = nil + super(nex) + @other = other + end + attr_reader :other + def attributes + [:other] + super + end + # so code can be "poured in" in the same way as normal, we swap the braches around in after the true condition + # and swap them back after + def swap + tmp = @other + @other = @next + @next = tmp + end + end + + # 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 ImplicitBranch < Branch + end + + # A note: future branch conditions include OverflowBranch and other non-c class FrameGet < Instruction def initialize name , nex = nil super(nex)