implement larger/smaller or equal

This commit is contained in:
Torsten Ruger 2018-04-24 19:45:58 +03:00
parent 7a2160e9b4
commit ec5a7f8a02
4 changed files with 70 additions and 28 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

@ -25,6 +25,12 @@ module Risc
def <( context ) def <( context )
comparison( :< ) comparison( :< )
end end
def <=( context )
comparison( :<= )
end
def >=( context )
comparison( :>= )
end
def comparison( operator ) def comparison( operator )
compiler = compiler_for(:Integer, operator ,{other: :Integer}) compiler = compiler_for(:Integer, operator ,{other: :Integer})
builder = compiler.builder(true, compiler.method) builder = compiler.builder(true, compiler.method)
@ -33,12 +39,14 @@ module Risc
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( "#{operator} fix me", me ) builder.reduce_int( "#{operator} fix me", me )
builder.reduce_int( "#{operator} fix arg", other ) builder.reduce_int( "#{operator} fix arg", other )
if(operator == :<) if(operator.to_s.start_with?('<') )
me , other = other , me me , other = other , me
end end
builder.add_code Risc.op( "#{operator} operator", :- , me , other) builder.add_code Risc.op( "#{operator} operator", :- , me , other)
builder.add_code IsMinus.new( "#{operator} if", false_label) builder.add_code IsMinus.new( "#{operator} if", false_label)
builder.add_code IsZero.new( "#{operator} if", false_label) if(operator.to_s.length == 1)
builder.add_code IsZero.new( "#{operator} if", false_label)
end
builder.add_load_constant("#{operator} 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

View File

@ -0,0 +1,58 @@
require_relative "helper"
module Risc
module Builtin
class IntCmp < BuiltinTest
def test_smaller_true
run_input "4 < 5"
assert_equal Parfait::TrueClass , get_return.class
end
def test_smaller_false
run_input "6 < 5"
assert_equal Parfait::FalseClass , get_return.class
end
def test_smaller_false_same
run_input "5 < 5"
assert_equal Parfait::FalseClass , get_return.class
end
def test_larger_true
run_input "5 > 4"
assert_equal Parfait::TrueClass , get_return.class
end
def test_larger_false
run_input "5 > 6"
assert_equal Parfait::FalseClass , get_return.class
end
def test_larger_false_same
run_input "5 > 5"
assert_equal Parfait::FalseClass , get_return.class
end
def test_smaller_or_true
run_input "4 <= 5"
assert_equal Parfait::TrueClass , get_return.class
end
def test_smaller_or_false
run_input "6 <= 5"
assert_equal Parfait::FalseClass , get_return.class
end
def test_smaller_or_same
run_input "5 <= 5"
assert_equal Parfait::TrueClass , get_return.class
end
def test_larger_or_true
run_input "5 >= 4"
assert_equal Parfait::TrueClass , get_return.class
end
def test_larger_or_false
run_input "5 >= 6"
assert_equal Parfait::FalseClass , get_return.class
end
def test_larger_or_same
run_input "5 >= 5"
assert_equal Parfait::TrueClass , get_return.class
end
end
end
end

View File

@ -2,7 +2,7 @@ require_relative "helper"
module Risc module Risc
module Builtin module Builtin
class SimpleInt < BuiltinTest class IntMath < BuiltinTest
def test_add def test_add
run_input "5 + 5" run_input "5 + 5"
@ -37,30 +37,6 @@ module Risc
run_input "4 * 4" run_input "4 * 4"
assert_equal 16 , get_return.value assert_equal 16 , get_return.value
end end
def test_smaller_true
run_input "4 < 5"
assert_equal Parfait::TrueClass , get_return.class
end
def test_smaller_false
run_input "6 < 5"
assert_equal Parfait::FalseClass , get_return.class
end
def test_smaller_false_same
run_input "5 < 5"
assert_equal Parfait::FalseClass , get_return.class
end
def test_larger_true
run_input "5 > 4"
assert_equal Parfait::TrueClass , get_return.class
end
def test_larger_false
run_input "5 > 6"
assert_equal Parfait::FalseClass , get_return.class
end
def test_larger_false_same
run_input "5 > 5"
assert_equal Parfait::FalseClass , get_return.class
end
end end
end end
end end