Skip to content

Commit d680a33

Browse files
committed
fixes #13. adding == & != on bools
1 parent 1a3da9f commit d680a33

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/mirah/jvm/types/boolean.rb

+33
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,38 @@ def box(builder)
2828
builder.invokestatic box_type, "valueOf", [box_type, self]
2929
end
3030

31+
def add_intrinsics
32+
args = [math_type]
33+
add_method('==', args, ComparisonIntrinsic.new(self, '==', :eq, args))
34+
add_method('!=', args, ComparisonIntrinsic.new(self, '!=', :ne, args))
35+
end
36+
37+
def math_type
38+
Boolean
39+
end
40+
41+
# same as NumberType's
42+
def compile_boolean_operator(compiler, op, negated, call, label)
43+
# Promote the target or the argument if necessary
44+
convert_args(compiler,
45+
[call.target, *call.parameters],
46+
[math_type, math_type])
47+
if negated
48+
op = invert_op(op)
49+
end
50+
if label
51+
jump_if(compiler.method, op, label)
52+
else
53+
compiler.method.op_to_bool do |label|
54+
jump_if(compiler.method, op, label)
55+
end
56+
end
57+
end
58+
59+
# Same as IntegerType's
60+
# bools are ints for comparison purposes
61+
def jump_if(builder, op, label)
62+
builder.send "if_icmp#{op}", label
63+
end
3164
end
3265
end

test/jvm/test_jvm_compiler.rb

+14
Original file line numberDiff line numberDiff line change
@@ -2262,4 +2262,18 @@ def test_missing_class_with_block_raises_inference_error
22622262
compile("Interface Implements_Go do; end")
22632263
end
22642264
end
2265+
2266+
def test_bool_equality
2267+
cls, = compile("puts true == false")
2268+
assert_output("false\n") do
2269+
cls.main(nil)
2270+
end
2271+
end
2272+
2273+
def test_bool_inequality
2274+
cls, = compile("puts true != false")
2275+
assert_output("true\n") do
2276+
cls.main(nil)
2277+
end
2278+
end
22652279
end

0 commit comments

Comments
 (0)