add branches to builder

This commit is contained in:
Torsten Ruger
2018-04-08 00:50:51 +03:00
parent 695ae5ad99
commit 5d4b9d4834
3 changed files with 29 additions and 3 deletions

View File

@ -32,6 +32,16 @@ module Risc
reg
end
def if_zero( label )
add Risc::IsZero.new("jump if zero" , label)
end
def if_not_zero( label )
add Risc::IsNotZero.new("jump if not zero" , label)
end
def branch( label )
add Risc::Branch.new("jump to" , label)
end
# build code using dsl (see __init__ or MessageSetup for examples)
# names (that ruby would resolve to a variable/method) are converted
# to registers. << means assignment and [] is supported both on

View File

@ -70,11 +70,11 @@ module Risc
space << space[:nil_object]
add Risc.op(source + "if method is nil", :- , space , typed_method )
add Risc::IsZero.new(source + "jump if nil" , exit_label)
if_zero exit_label
name << typed_method[:name]
add Risc.op(source + " compare name with me", :- , name , word )
add Risc::IsNotZero.new(source + "jump if not same" , false_label)
if_not_zero false_label
typed_method << typed_method[:binary]
message[:return_value] << typed_method
@ -83,7 +83,7 @@ module Risc
add false_label
typed_method << typed_method[:next_method]
add Risc::Branch.new(source + "back to while", while_start_label)
branch while_start_label
add exit_label
end