Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
Commit d1bcf610 authored by Ary Borenszweig's avatar Ary Borenszweig
Browse files

Some fixes to primitives

parent 99f17bc0
Branches
Tags
No related merge requests found
...@@ -24,7 +24,7 @@ describe "Code gen: next" do ...@@ -24,7 +24,7 @@ describe "Code gen: next" do
a = 0 a = 0
foo do |i| foo do |i|
next if i % 2 == 0 next if i.unsafe_mod(2) == 0
a += i a += i
end end
a a
...@@ -125,7 +125,7 @@ describe "Code gen: next" do ...@@ -125,7 +125,7 @@ describe "Code gen: next" do
b = 0 b = 0
while a < 4 while a < 4
a += 1 a += 1
next if a % 2 == 0 next if a.unsafe_mod(2) == 0
b += a b += a
end end
if b == i if b == i
......
...@@ -42,10 +42,6 @@ describe "Code gen: primitives" do ...@@ -42,10 +42,6 @@ describe "Code gen: primitives" do
run(%(2 * 3)).to_i.should eq(6) run(%(2 * 3)).to_i.should eq(6)
end end
it "codegens 8 / 3" do
run(%(8 / 3)).to_i.should eq(2)
end
it "codegens 8.unsafe_div 3" do it "codegens 8.unsafe_div 3" do
run(%(8.unsafe_div 3)).to_i.should eq(2) run(%(8.unsafe_div 3)).to_i.should eq(2)
end end
......
...@@ -75,7 +75,7 @@ describe "Codegen: while" do ...@@ -75,7 +75,7 @@ describe "Codegen: while" do
while i < 10 while i < 10
i += 1 i += 1
next if i % 2 == 0 next if i.unsafe_mod(2) == 0
x += i x += i
end end
x x
......
...@@ -92,4 +92,34 @@ describe "Type inference: primitives" do ...@@ -92,4 +92,34 @@ describe "Type inference: primitives" do
instantiate([string, fun_of(bool)] of TypeVar) instantiate([string, fun_of(bool)] of TypeVar)
end end
end end
it "extends from Number and doesn't find + method" do
assert_error %(
struct Foo < Number
end
Foo.new + 1
),
"undefined method"
end
it "extends from Number and doesn't find >= method" do
assert_error %(
struct Foo < Number
end
Foo.new >= 1
),
"undefined method"
end
it "extends from Number and doesn't find to_i method" do
assert_error %(
struct Foo < Number
end
Foo.new.to_i
),
"undefined method"
end
end end
...@@ -157,4 +157,9 @@ describe "Int" do ...@@ -157,4 +157,9 @@ describe "Int" do
expect_raises(DivisionByZero) { 1 / 0 } expect_raises(DivisionByZero) { 1 / 0 }
(4 / 2).should eq(2) (4 / 2).should eq(2)
end end
it "raises when mods by zero" do
expect_raises(DivisionByZero) { 1 % 0 }
(4 % 2).should eq(0)
end
end end
...@@ -27,20 +27,34 @@ module Crystal ...@@ -27,20 +27,34 @@ module Crystal
floats = [float32, float64] floats = [float32, float64]
nums = ints + floats nums = ints + floats
%w(+ - * /).each do |op| # The `/` operator is only defined for int vs float combinations.
nums.each do |another_number| # The `/` operator for int vs int is defined in Crystal on top
number.add_def Def.new(op, [Arg.new("other", type: another_number)], binary) # of unsafe div.
# The same goes with the `%` operator.
%w(+ - *).each do |op|
nums.product(nums) do |num1, num2|
num1.add_def Def.new(op, [Arg.new("other", type: num2)], binary)
end end
end end
floats.product(ints) do |num1, num2|
num1.add_def Def.new("/", [Arg.new("other", type: num2)], binary)
num2.add_def Def.new("/", [Arg.new("other", type: num1)], binary)
end
floats.product(floats) do |num1, num2|
num1.add_def Def.new("/", [Arg.new("other", type: num2)], binary)
end
%w(== < <= > >= !=).each do |op| %w(== < <= > >= !=).each do |op|
nums.each do |another_number| nums.product(nums) do |num1, num2|
number.add_def Def.new(op, [Arg.new("other", type: another_number)], binary) num1.add_def Def.new(op, [Arg.new("other", type: num2)], binary)
end end
char.add_def Def.new(op, [Arg.new("other", type: char)], binary) char.add_def Def.new(op, [Arg.new("other", type: char)], binary)
end end
%w(% << >> | & ^ unsafe_div unsafe_mod).each do |op| %w(<< >> | & ^ unsafe_div unsafe_mod).each do |op|
ints.each do |another_int| ints.each do |another_int|
int.add_def Def.new(op, [Arg.new("other", type: another_int)], binary) int.add_def Def.new(op, [Arg.new("other", type: another_int)], binary)
end end
...@@ -53,7 +67,9 @@ module Crystal ...@@ -53,7 +67,9 @@ module Crystal
end end
%w(to_i to_i8 to_i16 to_i32 to_i64 to_u to_u8 to_u16 to_u32 to_u64 to_f to_f32 to_f64).each do |op| %w(to_i to_i8 to_i16 to_i32 to_i64 to_u to_u8 to_u16 to_u32 to_u64 to_f to_f32 to_f64).each do |op|
number.add_def Def.new(op, body: cast) nums.each do |num|
num.add_def Def.new(op, body: cast)
end
end end
int.add_def Def.new("chr", body: cast) int.add_def Def.new("chr", body: cast)
......
...@@ -3,10 +3,6 @@ struct Float ...@@ -3,10 +3,6 @@ struct Float
cast(0) cast(0)
end end
def +
self
end
def nan? def nan?
!(self == self) !(self == self)
end end
......
...@@ -3,10 +3,6 @@ struct Int ...@@ -3,10 +3,6 @@ struct Int
cast(0) cast(0)
end end
def +
self
end
def ~ def ~
self ^ -1 self ^ -1
end end
...@@ -19,14 +15,13 @@ struct Int ...@@ -19,14 +15,13 @@ struct Int
unsafe_div x unsafe_div x
end end
# TODO: uncomment after 0.5.3 def %(x : Int)
# def %(x : Int) if x == 0
# if x == 0 raise DivisionByZero.new
# raise DivisionByZero.new end
# end
# unsafe_mod x unsafe_mod x
# end end
def abs def abs
self >= 0 ? self : -self self >= 0 ? self : -self
......
struct Number struct Number
def +
self
end
def step(limit = nil, by = 1) def step(limit = nil, by = 1)
x = self x = self
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment