get conditions to work for while
This commit is contained in:
parent
c196817568
commit
4f0b769e82
@ -12,7 +12,11 @@ module Arm
|
|||||||
|
|
||||||
def integer_less_or_equal block , left , right
|
def integer_less_or_equal block , left , right
|
||||||
block << cmp( left , right )
|
block << cmp( left , right )
|
||||||
Vm::Bool.new
|
Vm::BranchCondition.new :le
|
||||||
|
end
|
||||||
|
def integer_greater_than block , left , right
|
||||||
|
block << cmp( left , right )
|
||||||
|
Vm::BranchCondition.new :gt
|
||||||
end
|
end
|
||||||
|
|
||||||
def integer_plus block , result , left , right
|
def integer_plus block , result , left , right
|
||||||
|
@ -20,13 +20,13 @@ module Ast
|
|||||||
|
|
||||||
def save_locals context , into
|
def save_locals context , into
|
||||||
into.instance_eval do
|
into.instance_eval do
|
||||||
push [:r1 , :r2 , :r3]
|
push [:r0,:r1 , :r2 , :r3]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def restore_locals context , into
|
def restore_locals context , into
|
||||||
into.instance_eval do
|
into.instance_eval do
|
||||||
pop [:r1, :r2 , :r3]
|
pop [:r0,:r1, :r2 , :r3]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ module Ast
|
|||||||
body.each do |b|
|
body.each do |b|
|
||||||
compiled = b.compile(context , into)
|
compiled = b.compile(context , into)
|
||||||
function.return_type = compiled
|
function.return_type = compiled
|
||||||
puts "compiled in function #{compiled.class}"
|
puts "compiled in function #{compiled.inspect}"
|
||||||
raise "alarm #{compiled} \n #{b}" unless compiled.is_a? Vm::Word
|
raise "alarm #{compiled} \n #{b}" unless compiled.is_a? Vm::Word
|
||||||
end
|
end
|
||||||
context.locals = parent_locals
|
context.locals = parent_locals
|
||||||
|
@ -36,7 +36,7 @@ module Ast
|
|||||||
|
|
||||||
case operator
|
case operator
|
||||||
when ">"
|
when ">"
|
||||||
code = l_val.less_or_equal into , r_val
|
code = l_val.greater_than into , r_val
|
||||||
when "+"
|
when "+"
|
||||||
res = context.function.new_local
|
res = context.function.new_local
|
||||||
into.res = l_val + r_val
|
into.res = l_val + r_val
|
||||||
|
@ -14,16 +14,18 @@ module Ast
|
|||||||
[:condition, :body]
|
[:condition, :body]
|
||||||
end
|
end
|
||||||
def compile context , into
|
def compile context , into
|
||||||
ret = into.new_block "#{into.name}_return_#{hash}"
|
|
||||||
while_block = into.new_block "#{into.name}_while_#{hash}"
|
while_block = into.new_block "#{into.name}_while_#{hash}"
|
||||||
|
ret = while_block.new_block "#{into.name}_return_#{hash}"
|
||||||
cond_val = condition.compile(context , while_block)
|
cond_val = condition.compile(context , while_block)
|
||||||
while_block.bne ret
|
puts "compiled while condition #{cond_val.inspect}"
|
||||||
|
while_block.b ret , condition_code: cond_val.not_operator
|
||||||
last = nil
|
last = nil
|
||||||
body.each do |part|
|
body.each do |part|
|
||||||
last = part.compile(context , while_block )
|
last = part.compile(context , while_block )
|
||||||
puts "compiled in while #{last.inspect}"
|
puts "compiled in while #{last.inspect}"
|
||||||
end
|
end
|
||||||
while_block.b while_block
|
while_block.b while_block
|
||||||
|
puts "compile while end"
|
||||||
into.insert_at_end
|
into.insert_at_end
|
||||||
return last
|
return last
|
||||||
end
|
end
|
||||||
|
@ -25,8 +25,23 @@ module Vm
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Just a nice way to write branches
|
# Just a nice way to write branches
|
||||||
class Bool < Value
|
class BranchCondition < Value
|
||||||
|
|
||||||
|
def initialize operator
|
||||||
|
@operator = operator
|
||||||
|
end
|
||||||
|
attr_accessor :operator
|
||||||
|
#needed to check the opposite, ie not true
|
||||||
|
def not_operator
|
||||||
|
case @operator
|
||||||
|
when :le
|
||||||
|
:gt
|
||||||
|
when :gt
|
||||||
|
:le
|
||||||
|
else
|
||||||
|
raise "no implemented #{@operator}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# This is what it is when we don't know what it is.
|
# This is what it is when we don't know what it is.
|
||||||
@ -77,6 +92,9 @@ module Vm
|
|||||||
def less_or_equal block , right
|
def less_or_equal block , right
|
||||||
RegisterMachine.instance.integer_less_or_equal block , self , right
|
RegisterMachine.instance.integer_less_or_equal block , self , right
|
||||||
end
|
end
|
||||||
|
def greater_than block , right
|
||||||
|
RegisterMachine.instance.integer_greater_than block , self , right
|
||||||
|
end
|
||||||
def == other
|
def == other
|
||||||
code = class_for(CompareInstruction).new(self , other , opcode: :cmp)
|
code = class_for(CompareInstruction).new(self , other , opcode: :cmp)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user