ridiculously easy to implement the if now

This commit is contained in:
Torsten Ruger
2014-05-24 10:41:57 +03:00
parent 00d85156da
commit 61a60abd08
5 changed files with 39 additions and 2 deletions

View File

@@ -14,6 +14,10 @@ module Arm
block << cmp( left , right )
Vm::BranchCondition.new :le
end
def integer_less_than block , left , right
block << cmp( left , right )
Vm::BranchCondition.new :lt
end
def integer_greater_than block , left , right
block << cmp( left , right )
Vm::BranchCondition.new :gt

View File

@@ -11,5 +11,31 @@ module Ast
def attributes
[:cond, :if_true, :if_false]
end
def compile context , into
false_block = into.new_block "#{into.name}_else"
merge_block = false_block.new_block "#{into.name}_if_merge"
cond_val = cond.compile(context , into)
puts "compiled if condition #{cond_val.inspect}"
into.b false_block , condition_code: cond_val.not_operator
last = nil
if_true.each do |part|
last = part.compile(context , into )
puts "compiled in if true #{last.inspect}"
end
into.b merge_block
if_false.each do |part|
last = part.compile(context , false_block )
puts "compiled in if false #{last.inspect}"
end
puts "compile if end"
into.insert_at merge_block
return last
end
end
end

View File

@@ -37,6 +37,8 @@ module Ast
case operator
when ">"
code = l_val.greater_than into , r_val
when "<"
code = l_val.less_than into , r_val
when "+"
res = context.function.new_local
into.res = l_val + r_val

View File

@@ -38,6 +38,8 @@ module Vm
:gt
when :gt
:le
when :lt
:ge
else
raise "no implemented #{@operator}"
end
@@ -96,6 +98,9 @@ module Vm
def greater_than block , right
RegisterMachine.instance.integer_greater_than block , self , right
end
def less_than block , right
RegisterMachine.instance.integer_less_than block , self , right
end
def == other
code = class_for(CompareInstruction).new(self , other , opcode: :cmp)
end