Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 30e16eb7e048
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f25f2700e174
Choose a head ref
  • 2 commits
  • 9 files changed
  • 1 contributor

Commits on May 6, 2016

  1. Number: added Number::Primitive alias

    Ary Borenszweig committed May 6, 2016
    Copy the full SHA
    31f60d8 View commit details
  2. Removed Char#ord primitive from compiler's code, and prepared the t…

    …errain to move more primtiives to Crystal code.
    Ary Borenszweig committed May 6, 2016
    Copy the full SHA
    f25f270 View commit details
12 changes: 6 additions & 6 deletions spec/compiler/codegen/magic_constants_spec.cr
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ describe "Code gen: magic constants" do
end
foo
)).to_i.should eq(6)
), inject_primitives: false).to_i.should eq(6)
end

it "does __FILE__" do
@@ -43,7 +43,7 @@ describe "Code gen: magic constants" do
a = 1 || "hello"
foo(a)
)).to_i.should eq(11)
), inject_primitives: false).to_i.should eq(11)
end

it "does __LINE__ when specifying one default arg with __FILE__" do
@@ -53,7 +53,7 @@ describe "Code gen: magic constants" do
end
foo 1, "hello"
)).to_i.should eq(6)
), inject_primitives: false).to_i.should eq(6)
end

it "does __LINE__ when specifying one normal default arg" do
@@ -63,7 +63,7 @@ describe "Code gen: magic constants" do
end
foo 1, 20
)).to_i.should eq(26)
), inject_primitives: false).to_i.should eq(26)
end

it "does __LINE__ when specifying one middle argument" do
@@ -73,7 +73,7 @@ describe "Code gen: magic constants" do
end
foo 1, z: 20
)).to_i.should eq(26)
), inject_primitives: false).to_i.should eq(26)
end

it "does __LINE__ in macro" do
@@ -83,7 +83,7 @@ describe "Code gen: magic constants" do
end
foo
)).to_i.should eq(6)
), inject_primitives: false).to_i.should eq(6)
end

it "does __FILE__ in macro" do
6 changes: 5 additions & 1 deletion spec/compiler/codegen/primitives_spec.cr
Original file line number Diff line number Diff line change
@@ -18,6 +18,10 @@ describe "Code gen: primitives" do
run("'a'").to_i.should eq('a'.ord)
end

it "codegens char ord" do
run("'a'.ord").to_i.should eq('a'.ord)
end

it "codegens f32" do
run("2.5_f32").to_f32.should eq(2.5_f32)
end
@@ -75,7 +79,7 @@ describe "Code gen: primitives" do
run("
__LINE__
").to_i.should eq(3)
", inject_primitives: false).to_i.should eq(3)
end

it "codeges crystal_type_id with union type" do
4 changes: 2 additions & 2 deletions spec/compiler/type_inference/closure_spec.cr
Original file line number Diff line number Diff line change
@@ -107,7 +107,7 @@ describe "Type inference: closure" do
x
end
end
") { int32 }
", inject_primitives: false) { int32 }
node = result.node as Expressions
call = node[1] as Call
block = call.block.not_nil!
@@ -442,7 +442,7 @@ describe "Type inference: closure" do
it "passes #227" do
result = assert_type(%(
->{ a = 1; ->{ a } }
)) { fun_of(fun_of(int32)) }
), inject_primitives: false) { fun_of(fun_of(int32)) }
fn = result.node as FunLiteral
fn.def.closure.should be_false
end
2 changes: 1 addition & 1 deletion spec/compiler/type_inference/exception_spec.cr
Original file line number Diff line number Diff line change
@@ -266,7 +266,7 @@ describe "Type inference: exception" do
end

it "marks fun literal as raises" do
result = assert_type("->{ 1 }.call") { int32 }
result = assert_type("->{ 1 }.call", inject_primitives: false) { int32 }
call = result.node as Call
call.target_def.raises.should be_true
end
9 changes: 6 additions & 3 deletions spec/compiler/type_inference/macro_spec.cr
Original file line number Diff line number Diff line change
@@ -111,7 +111,8 @@ describe "Type inference: macro" do
end
bar
}, "Error in line 7: expected 'bar' to return Foo(String), not Foo(Int32)"
}, "Error in line 7: expected 'bar' to return Foo(String), not Foo(Int32)",
inject_primitives: false
end

it "allows union return types for macro def" do
@@ -642,7 +643,8 @@ describe "Type inference: macro" do
foo
),
"Error in line 6: expanding macro"
"Error in line 6: expanding macro",
inject_primitives: false
end

it "show macro trace in errors (2)" do
@@ -651,7 +653,8 @@ describe "Type inference: macro" do
Bar
{% end %}
),
"Error in line 2: expanding macro"
"Error in line 2: expanding macro",
inject_primitives: false
end

it "errors if using macro that is defined later" do
4 changes: 4 additions & 0 deletions spec/compiler/type_inference/primitives_spec.cr
Original file line number Diff line number Diff line change
@@ -25,6 +25,10 @@ describe "Type inference: primitives" do
assert_type("'a'") { char }
end

it "types char ord" do
assert_type("'a'.ord") { int32 }
end

it "types a symbol" do
assert_type(":foo") { symbol }
end
21 changes: 16 additions & 5 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -25,15 +25,16 @@ record InferTypeResult,
node : ASTNode,
type : Type

def assert_type(str, flags = nil)
result = infer_type_result(str, flags)
def assert_type(str, flags = nil, inject_primitives = true)
result = infer_type_result(str, flags, inject_primitives: inject_primitives)
program = result.program
expected_type = with program yield program
result.type.should eq(expected_type)
result
end

def infer_type(code : String, wants_doc = false)
code = inject_primitives(code)
infer_type parse(code, wants_doc: wants_doc), wants_doc: wants_doc
end

@@ -45,7 +46,8 @@ def infer_type(node : ASTNode, wants_doc = false)
InferTypeResult.new(program, node, node.type)
end

def infer_type_result(str, flags = nil)
def infer_type_result(str, flags = nil, inject_primitives = true)
str = inject_primitives(str) if inject_primitives
program = Program.new
program.flags = flags if flags
input = parse str
@@ -84,6 +86,7 @@ def assert_expand_third(from : String, to)
end

def assert_after_cleanup(before, after)
# before = inject_primitives(before)
node = Parser.parse(before)
result = infer_type node
result.node.to_s.strip.should eq(after.strip)
@@ -102,7 +105,8 @@ def assert_syntax_error(str, message = nil, line = nil, column = nil, metafile =
end
end

def assert_error(str, message)
def assert_error(str, message, inject_primitives = true)
str = inject_primitives(str) if inject_primitives
nodes = parse str
expect_raises TypeException, message do
infer_type nodes
@@ -137,6 +141,7 @@ def parse(string, wants_doc = false)
end

def codegen(code)
code = inject_primitives(code)
node = parse code
result = infer_type node
result.program.codegen result.node, single_module: false
@@ -163,7 +168,9 @@ class Crystal::SpecRunOutput
end
end

def run(code, filename = nil)
def run(code, filename = nil, inject_primitives = true)
code = inject_primitives(code) if inject_primitives

# Code that requires the prelude doesn't run in LLVM's MCJIT
# because of missing linked functions (which are available
# in the current executable!), so instead we compile
@@ -211,3 +218,7 @@ def test_c(c_code, crystal_code)
File.delete(o_filename)
end
end

private def inject_primitives(code)
%(require "primitives"\n) + code
end
1 change: 0 additions & 1 deletion src/compiler/crystal/primitives.cr
Original file line number Diff line number Diff line change
@@ -73,7 +73,6 @@ module Crystal
end

int.add_def Def.new("chr", body: cast)
char.add_def Def.new("ord", body: cast)
symbol.add_def Def.new("to_i", body: cast)
end

2 changes: 2 additions & 0 deletions src/number.cr
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@
struct Number
include Comparable(Number)

alias Primitive = Int::Primitive | Float::Primitive

def self.zero : self
new(0)
end