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: 2e01a895f054
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: 6dfadb5a5d58
Choose a head ref
  • 4 commits
  • 9 files changed
  • 1 contributor

Commits on Jun 9, 2016

  1. Added RangeLiteral#to_a macro method

    Ary Borenszweig committed Jun 9, 2016
    Copy the full SHA
    7088781 View commit details
  2. Added ArrayLiteral#[range] and ArrayLiteral#[from, to] in macros (for…

    … TupleLiteral too)
    Ary Borenszweig committed Jun 9, 2016
    Copy the full SHA
    f3cbdff View commit details
  3. method_missing: removed the form that receives 3 arguments

    Ary Borenszweig committed Jun 9, 2016
    Copy the full SHA
    0f77107 View commit details
  4. Copy the full SHA
    6dfadb5 View commit details
56 changes: 28 additions & 28 deletions spec/compiler/codegen/method_missing_spec.cr
Original file line number Diff line number Diff line change
@@ -8,8 +8,8 @@ describe "Code gen: method_missing" do
1
end
macro method_missing(name, args, block)
{{name.id}}_something
macro method_missing(call)
{{call.name.id}}_something
end
end
@@ -20,8 +20,8 @@ describe "Code gen: method_missing" do
it "does method_missing macro with args" do
run(%(
class Foo
macro method_missing(name, args, block)
{{args.join(" + ").id}}
macro method_missing(call)
{{call.args.join(" + ").id}}
end
end
@@ -38,8 +38,8 @@ describe "Code gen: method_missing" do
yield 3
end
macro method_missing(name, args, block)
{{name.id}}_something {{block}}
macro method_missing(call)
{{call.name.id}}_something {{call.block}}
end
end
@@ -58,8 +58,8 @@ describe "Code gen: method_missing" do
1 + 2
end
macro method_missing(name, args, block)
{{name.id}}_something {{block}}
macro method_missing(call)
{{call.name.id}}_something {{call.block}}
end
end
@@ -70,8 +70,8 @@ describe "Code gen: method_missing" do
it "does method_missing macro with virtual type (1)" do
run(%(
class Foo
macro method_missing(name, args, block)
"{{@type.name.id}}{{name.id}}"
macro method_missing(call)
"{{@type.name.id}}{{call.name.id}}"
end
end
@@ -86,8 +86,8 @@ describe "Code gen: method_missing" do
it "does method_missing macro with virtual type (2)" do
run(%(
class Foo
macro method_missing(name, args, block)
"{{@type.name.id}}{{name.id}}"
macro method_missing(call)
"{{@type.name.id}}{{call.name.id}}"
end
end
@@ -106,7 +106,7 @@ describe "Code gen: method_missing" do
1
end
macro method_missing(name, args, block)
macro method_missing(call)
2
end
end
@@ -122,13 +122,13 @@ describe "Code gen: method_missing" do
it "does method_missing macro with virtual type (4)" do
run(%(
class Foo
macro method_missing(name, args, block)
macro method_missing(call)
1
end
end
class Bar < Foo
macro method_missing(name, args, block)
macro method_missing(call)
2
end
end
@@ -141,19 +141,19 @@ describe "Code gen: method_missing" do
it "does method_missing macro with virtual type (5)" do
run(%(
class Foo
macro method_missing(name, args, block)
macro method_missing(call)
1
end
end
class Bar < Foo
macro method_missing(name, args, block)
macro method_missing(call)
2
end
end
class Baz < Bar
macro method_missing(name, args, block)
macro method_missing(call)
3
end
end
@@ -169,7 +169,7 @@ describe "Code gen: method_missing" do
end
class Bar < Foo
macro method_missing(name, args, block)
macro method_missing(call)
2
end
end
@@ -191,7 +191,7 @@ describe "Code gen: method_missing" do
end
class Bar < Foo
macro method_missing(name, args, block)
macro method_missing(call)
2
end
end
@@ -210,7 +210,7 @@ describe "Code gen: method_missing" do
it "does method_missing macro with virtual type (8)" do
run(%(
class Foo
macro method_missing(name, args, block)
macro method_missing(call)
{{@type.name.stringify}}
end
end
@@ -237,7 +237,7 @@ describe "Code gen: method_missing" do
class Foo
include Moo
macro method_missing(name, args, block)
macro method_missing(call)
2
end
end
@@ -253,7 +253,7 @@ describe "Code gen: method_missing" do
end
class Foo
macro method_missing(name, args, block)
macro method_missing(call)
2
end
@@ -270,7 +270,7 @@ describe "Code gen: method_missing" do
it "does method_missing macro with included module" do
run("
module Moo
macro method_missing(name, args, block)
macro method_missing(call)
{{@type.name.stringify}}
end
end
@@ -286,8 +286,8 @@ describe "Code gen: method_missing" do
it "does method_missing with assignment (bug)" do
run(%(
class Foo
macro method_missing(name, args, block)
x = {{args[0]}}
macro method_missing(call)
x = {{call.args[0]}}
x
end
end
@@ -308,8 +308,8 @@ describe "Code gen: method_missing" do
class Foo
@x : Int32?
macro method_missing(name, args, block)
@x = {{args[0]}}
macro method_missing(call)
@x = {{call.args[0]}}
@x
end
end
13 changes: 13 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
@@ -524,6 +524,14 @@ describe "macro methods" do
it "executes +" do
assert_macro "", %({{ [1, 2] + [3, 4, 5] }}), [] of ASTNode, %([1, 2, 3, 4, 5])
end

it "executes [] with range" do
assert_macro "", %({{ [1, 2, 3, 4][1...-1] }}), [] of ASTNode, %([2, 3])
end

it "executes [] with two numbers" do
assert_macro "", %({{ [1, 2, 3, 4, 5][1, 3] }}), [] of ASTNode, %([2, 3, 4])
end
end

describe "hash methods" do
@@ -994,6 +1002,11 @@ describe "macro methods" do
assert_macro "x", %({{x.map(&.stringify)}}), [RangeLiteral.new(1.int32, 3.int32, false)] of ASTNode, %(["1", "2", "3"])
assert_macro "x", %({{x.map(&.stringify)}}), [RangeLiteral.new(1.int32, 3.int32, true)] of ASTNode, %(["1", "2"])
end

it "executes to_a" do
assert_macro "x", %({{x.to_a}}), [RangeLiteral.new(1.int32, 3.int32, false)] of ASTNode, %([1, 2, 3])
assert_macro "x", %({{x.to_a}}), [RangeLiteral.new(1.int32, 3.int32, true)] of ASTNode, %([1, 2])
end
end

describe "env" do
8 changes: 4 additions & 4 deletions spec/compiler/type_inference/method_missing_spec.cr
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ describe "Type inference: method_missing" do
end
class Bar < Foo
macro method_missing(name, args, block)
macro method_missing(call)
2
end
end
@@ -23,16 +23,16 @@ describe "Type inference: method_missing" do
it "does error in method_missing if wrong number of args" do
assert_error %(
class Foo
macro method_missing(name, args)
macro method_missing(call, foo)
end
end
), "macro 'method_missing' expects 1 or 3 arguments: (call) or (name, args, block)"
), "macro 'method_missing' expects 1 argument (call)"
end

it "does method missing for generic type" do
assert_type(%(
class Foo(T)
macro method_missing(name, args, block)
macro method_missing(call)
1
end
end
4 changes: 2 additions & 2 deletions src/compiler/crystal/codegen/crystal_llvm_builder.cr
Original file line number Diff line number Diff line change
@@ -58,10 +58,10 @@ module Crystal
@builder.to_unsafe
end

macro method_missing(name, args, block)
macro method_missing(call)
return llvm_nil if @end

@builder.{{name.id}}({{*args}}) {{block}}
@builder.{{call}}
end
end
end
10 changes: 9 additions & 1 deletion src/compiler/crystal/macros.cr
Original file line number Diff line number Diff line change
@@ -8,7 +8,10 @@ module Crystal::Macros
# Outputs the current macro's buffer to the standard output. Useful for debugging
# a macro to see what's being generated. Use it like `{{debug()}}`, the parenthesis
# are mandatory.
def debug : Nop
#
# By default, the output is tried to be formatted using Crystal's
# formatter, but you can disable this by passing `false` to this method.
def debug(format = true) : Nop
end

# Gets the value of an environment variable at compile-time, or `nil` if it doesn't exist.
@@ -571,6 +574,11 @@ module Crystal::Macros
# Only works on ranges of `NumberLiteral`s considered as integers.
def map : ArrayLiteral
end

# Similar to `Enumerable#to_a` for a `Range`.
# Only works on ranges of `NumberLiteral`s considered as integers.
def to_a : ArrayLiteral
end
end

# A regex literal.
Loading