diff --git a/lib/arm/arm_machine.rb b/lib/arm/arm_machine.rb index 0eba5447..d5bcb561 100644 --- a/lib/arm/arm_machine.rb +++ b/lib/arm/arm_machine.rb @@ -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 diff --git a/lib/ast/if_expression.rb b/lib/ast/if_expression.rb index 29b30854..d67c2c87 100644 --- a/lib/ast/if_expression.rb +++ b/lib/ast/if_expression.rb @@ -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 \ No newline at end of file diff --git a/lib/ast/operator_expressions.rb b/lib/ast/operator_expressions.rb index b235e035..223c7364 100644 --- a/lib/ast/operator_expressions.rb +++ b/lib/ast/operator_expressions.rb @@ -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 diff --git a/lib/vm/values.rb b/lib/vm/values.rb index 72a95518..277ebe95 100644 --- a/lib/vm/values.rb +++ b/lib/vm/values.rb @@ -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 diff --git a/test/fragments/test_if.rb b/test/fragments/test_if.rb index 34867e09..dc16aa56 100644 --- a/test/fragments/test_if.rb +++ b/test/fragments/test_if.rb @@ -12,9 +12,9 @@ class TestIf < MiniTest::Test putstring("else") end end - itest(10) + itest(20) HERE - @should = [0x0,0xb0,0xa0,0xe3,0xa,0x0,0xa0,0xe3,0x1,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x40,0x2d,0xe9,0x0,0x10,0xa0,0xe3,0x1,0x20,0xa0,0xe3,0x1,0x0,0x50,0xe3,0x6,0x0,0x0,0xda,0x1,0x30,0xa0,0xe1,0x2,0x10,0xa0,0xe1,0x2,0x40,0x83,0xe0,0x4,0x20,0xa0,0xe1,0x1,0x50,0x40,0xe2,0x5,0x0,0xa0,0xe1,0xf6,0xff,0xff,0xea,0x3f,0x0,0x2d,0xe9,0x2,0x0,0xa0,0xe1,0x12,0x0,0x0,0xeb,0x3f,0x0,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0xa,0x20,0x41,0xe2,0x21,0x11,0x41,0xe0,0x21,0x12,0x81,0xe0,0x21,0x14,0x81,0xe0,0x21,0x18,0x81,0xe0,0xa1,0x11,0xa0,0xe1,0x1,0x31,0x81,0xe0,0x83,0x20,0x52,0xe0,0x1,0x10,0x81,0x52,0xa,0x20,0x82,0x42,0x30,0x20,0x82,0xe2,0x0,0x20,0xc0,0xe5,0x1,0x0,0x40,0xe2,0x0,0x0,0x51,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x0,0x10,0xa0,0xe1,0x24,0x0,0x8f,0xe2,0x9,0x0,0x80,0xe2,0xe9,0xff,0xff,0xeb,0x18,0x0,0x8f,0xe2,0xc,0x10,0xa0,0xe3,0x1,0x20,0xa0,0xe1,0x0,0x10,0xa0,0xe1,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0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