implement greater than

This commit is contained in:
Torsten Ruger 2018-04-19 22:57:31 +03:00
parent be3d125b82
commit 04359546b7
5 changed files with 38 additions and 16 deletions

View File

@ -184,7 +184,7 @@ module Risc
Risc.operators.each do |op| Risc.operators.each do |op|
obj.instance_type.add_method Builtin::Integer.operator_method(op) obj.instance_type.add_method Builtin::Integer.operator_method(op)
end end
[:putint, :div4, :div10 , :<].each do |f| #div4 is just a forward declaration [:putint, :div4, :div10 , :<, :>].each do |f| #div4 is just a forward declaration
obj.instance_type.add_method Builtin::Integer.send(f , nil) obj.instance_type.add_method Builtin::Integer.send(f , nil)
end end
end end

View File

@ -19,22 +19,28 @@ module Risc
compiler.add_mom( Mom::ReturnSequence.new) compiler.add_mom( Mom::ReturnSequence.new)
return compiler.method return compiler.method
end end
def >( context )
comparison( :> , Risc::IsMinus)
end
def <( context ) def <( context )
compiler = compiler_for(:Integer, :< ,{other: :Integer}) comparison( :< , Risc::IsPlus)
end
def comparison( operator , branch )
compiler = compiler_for(:Integer, operator ,{other: :Integer})
builder = compiler.builder(true, compiler.method) builder = compiler.builder(true, compiler.method)
me , other = builder.self_and_int_arg("< load receiver and arg") me , other = builder.self_and_int_arg("#{operator} load receiver and arg")
false_label = Risc.label(compiler.method , "false_label_#{builder.object_id}") false_label = Risc.label(compiler.method , "false_label_#{builder.object_id}")
merge_label = Risc.label(compiler.method , "merge_label_#{builder.object_id}") merge_label = Risc.label(compiler.method , "merge_label_#{builder.object_id}")
builder.reduce_int( "< fix me", me ) builder.reduce_int( "#{operator} fix me", me )
builder.reduce_int( "< fix arg", other ) builder.reduce_int( "#{operator} fix arg", other )
builder.add_code Risc.op( "< operator", :- , me , other) builder.add_code Risc.op( "#{operator} operator", :- , me , other)
builder.add_code Risc::IsPlus.new( "< if", false_label) builder.add_code branch.new( "#{operator} if", false_label)
builder.add_load_constant("< new int", Parfait.object_space.true_object , other) builder.add_load_constant("#{operator} new int", Parfait.object_space.true_object , other)
builder.add_code Risc::Branch.new("jump over false", merge_label) builder.add_code Risc::Branch.new("jump over false", merge_label)
builder.add_code false_label builder.add_code false_label
builder.add_load_constant("< new int", Parfait.object_space.false_object , other) builder.add_load_constant("#{operator} new int", Parfait.object_space.false_object , other)
builder.add_code merge_label builder.add_code merge_label
builder.add_reg_to_slot( "< save ret" , other , :message , :return_value) builder.add_reg_to_slot( "#{operator} save ret" , other , :message , :return_value)
compiler.add_mom( Mom::ReturnSequence.new) compiler.add_mom( Mom::ReturnSequence.new)
return compiler.method return compiler.method
end end

View File

@ -34,7 +34,7 @@ class TestSpace < MiniTest::Test
def test_integer def test_integer
int = Parfait.object_space.get_class_by_name :Integer int = Parfait.object_space.get_class_by_name :Integer
assert_equal 10, int.instance_type.method_names.get_length assert_equal 12, int.instance_type.method_names.get_length
end end
def test_classes_class def test_classes_class

View File

@ -29,10 +29,22 @@ module Risc
run_all "4 * 4" run_all "4 * 4"
assert_equal 16 , get_return.value assert_equal 16 , get_return.value
end end
def test_smaller def test_smaller_true
run_all "4 < 5" run_all "4 < 5"
assert_equal Parfait::TrueClass , get_return.class assert_equal Parfait::TrueClass , get_return.class
end end
def test_smaller_false
run_all "6 < 5"
assert_equal Parfait::FalseClass , get_return.class
end
def test_larger_true
run_all "5 > 4"
assert_equal Parfait::TrueClass , get_return.class
end
def test_larger_false
run_all "5 > 6"
assert_equal Parfait::FalseClass , get_return.class
end
end end
end end
end end

View File

@ -11,13 +11,17 @@ module Risc
def test_chain def test_chain
#show_main_ticks # get output of what is #show_main_ticks # get output of what is
check_main_chain [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, check_main_chain [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant,
SlotToReg, SlotToReg, SlotToReg, SlotToReg, OperatorInstruction, SlotToReg, SlotToReg, SlotToReg, SlotToReg, OperatorInstruction,
IsZero, SlotToReg, SlotToReg, SlotToReg, LoadConstant, IsZero, SlotToReg, SlotToReg, SlotToReg, LoadConstant,
RegToSlot, LoadConstant, LoadConstant, SlotToReg, SlotToReg, RegToSlot, LoadConstant, LoadConstant, SlotToReg, SlotToReg,
Label, LoadConstant, SlotToReg, OperatorInstruction, IsZero, Label, LoadConstant, SlotToReg, OperatorInstruction, IsZero,
SlotToReg, OperatorInstruction, IsZero, SlotToReg, Branch, SlotToReg, OperatorInstruction, IsZero, SlotToReg, Branch,
Label, LoadConstant, SlotToReg, OperatorInstruction, IsZero, Label, LoadConstant, SlotToReg, OperatorInstruction, IsZero,
SlotToReg, OperatorInstruction, IsZero, SlotToReg, Branch,
Label, LoadConstant, SlotToReg, OperatorInstruction, IsZero,
SlotToReg, OperatorInstruction, IsZero, SlotToReg, Branch,
Label, LoadConstant, SlotToReg, OperatorInstruction, IsZero,
SlotToReg, OperatorInstruction, IsZero, Label, RegToSlot, SlotToReg, OperatorInstruction, IsZero, Label, RegToSlot,
Label, LoadConstant, SlotToReg, LoadConstant, SlotToReg, Label, LoadConstant, SlotToReg, LoadConstant, SlotToReg,
RegToSlot, RegToSlot, SlotToReg, SlotToReg, RegToSlot, RegToSlot, RegToSlot, SlotToReg, SlotToReg, RegToSlot,
@ -47,16 +51,16 @@ module Risc
end end
def test_dyn def test_dyn
cal = main_ticks(68) cal = main_ticks(88)
assert_equal DynamicJump , cal.class assert_equal DynamicJump , cal.class
end end
#should end in exit, but doesn't, becasue resolve never returns #should end in exit, but doesn't, becasue resolve never returns
def test_sys def test_sys
sys = main_ticks(99) sys = main_ticks(119)
assert_equal Syscall , sys.class assert_equal Syscall , sys.class
end end
def test_return def test_return
ret = main_ticks(97) ret = main_ticks(117)
assert_equal FunctionReturn , ret.class assert_equal FunctionReturn , ret.class
link = @interpreter.get_register( ret.register ) link = @interpreter.get_register( ret.register )
assert_equal Label , link.class assert_equal Label , link.class