Skip to content

Commit

Permalink
Compiler: remove macro def (#5040)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and RX14 committed Sep 26, 2017
1 parent 1c859ca commit 2c1bb2d
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 82 deletions.
42 changes: 28 additions & 14 deletions spec/compiler/codegen/macro_spec.cr
Expand Up @@ -139,11 +139,14 @@ describe "Code gen: macro" do

it "expands def macro with var" do
run(%(
macro def foo : Int32
a = {{ 1 }}
class Foo
def foo : Int32
{{ @type }}
a = {{ 1 }}
end
end
foo
Foo.new.foo
)).to_i.should eq(1)
end

Expand Down Expand Up @@ -226,7 +229,8 @@ describe "Code gen: macro" do
it "expands macro and resolves type correctly" do
run(%(
class Foo
macro def foo : Int32
def foo : Int32
{{ @type }}
1
end
end
Expand Down Expand Up @@ -337,7 +341,8 @@ describe "Code gen: macro" do
class Foo
@name : Int32?
macro def foo : Int32
def foo : Int32
{{ @type }}
name = 1
@name = name
end
Expand Down Expand Up @@ -392,7 +397,8 @@ describe "Code gen: macro" do
it "doesn't skip abstract classes when defining macro methods" do
run(%(
class Object
macro def foo : Int32
def foo : Int32
{{ @type }}
1
end
end
Expand Down Expand Up @@ -443,7 +449,8 @@ describe "Code gen: macro" do
def initialize(@x : Int32, @y : Int32)
end
macro def foo : String
def foo : String
{{ @type }}
{{ Foo.instance_vars.last.name.stringify }}
end
Expand Down Expand Up @@ -974,7 +981,8 @@ describe "Code gen: macro" do
it "codegens macro def with splat (#496)" do
run(%(
class Foo
macro def bar(*args) : Int32
def bar(*args) : Int32
{{ @type }}
args[0] + args[1] + args[2]
end
end
Expand All @@ -986,7 +994,8 @@ describe "Code gen: macro" do
it "codegens macro def with default arg (similar to #496)" do
run(%(
class Foo
macro def bar(foo = 1) : Int32
def bar(foo = 1) : Int32
{{ @type }}
foo + 2
end
end
Expand Down Expand Up @@ -1118,11 +1127,14 @@ describe "Code gen: macro" do

it "expands macro def with return (#1040)" do
run(%(
macro def a : Int32
return 123
class Foo
def a : Int32
{{ @type }}
return 123
end
end
a
Foo.new.a
)).to_i.should eq(123)
end

Expand Down Expand Up @@ -1190,7 +1202,8 @@ describe "Code gen: macro" do
class Foo
Const = 123
macro def self.bar : Int32
def self.bar : Int32
{{ @type }}
foo { Const }
end
end
Expand All @@ -1212,7 +1225,8 @@ describe "Code gen: macro" do
it "types macro expansion bug (#1734)" do
run(%(
class Foo
macro def foo : Int32
def foo : Int32
{{ @type }}
1 || 2
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/compiler/formatter/formatter_spec.cr
Expand Up @@ -537,7 +537,7 @@ describe Crystal::Formatter do
assert_format "macro foo\n %foo\nend"
assert_format "macro foo\n %foo{x.id+2} \nend", "macro foo\n %foo{x.id + 2}\nend"
assert_format "macro foo\n %foo{x,y}\nend", "macro foo\n %foo{x, y}\nend"
assert_format "macro def foo : Int32\n 1\nend"
assert_format "def foo : Int32\n 1\nend"
assert_format "class Foo\n macro foo\n 1\n end\nend"
assert_format " {{ 1 + 2 }}", "{{ 1 + 2 }}"
assert_format " {% for x in y %} 2 {% end %}", "{% for x in y %} 2 {% end %}"
Expand All @@ -546,7 +546,7 @@ describe Crystal::Formatter do
assert_format "if 1\n {% if 2 %} {% end %}\nend"
assert_format "if 1\n {% for x in y %} {% end %}\nend"
assert_format "if 1\n {{1 + 2}}\nend"
assert_format "macro def foo : self | Nil\n nil\nend"
assert_format "def foo : self | Nil\n nil\nend"
assert_format "macro foo(x)\n {% if 1 %} 2 {% end %}\nend"
assert_format "macro foo()\n {% if 1 %} 2 {% end %} \nend", "macro foo\n {% if 1 %} 2 {% end %}\nend"
assert_format "macro flags\n {% if 1 %}\\\n {% end %}\\\nend"
Expand Down Expand Up @@ -915,10 +915,10 @@ describe Crystal::Formatter do
assert_format "foo : (A -> B)\nbar : C"
assert_format "def foo(x : A(B), y)\nend"
assert_format "alias X = (A, B) ->\nbar : C"
assert_format "macro def foo : A(B)\n nil\nend"
assert_format "macro def foo : (A, B) ->\n nil\nend"
assert_format "macro def foo : (A | B(C))\n nil\nend"
assert_format "macro def foo : A | B(C)\n nil\nend"
assert_format "def foo : A(B)\n nil\nend"
assert_format "def foo : (A, B) ->\n nil\nend"
assert_format "def foo : (A | B(C))\n nil\nend"
assert_format "def foo : A | B(C)\n nil\nend"

assert_format "foo &.bar.is_a?(Baz)"
assert_format "foo &.bar.responds_to?(:baz)"
Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/lexer/lexer_macro_spec.cr
Expand Up @@ -39,7 +39,7 @@ describe "Lexer macro" do
token.type.should eq(:MACRO_END)
end

["begin", "do", "if", "unless", "class", "struct", "module", "def", "while", "until", "case", "macro", "fun", "lib", "union", "macro def"].each do |keyword|
["begin", "do", "if", "unless", "class", "struct", "module", "def", "while", "until", "case", "macro", "fun", "lib", "union"].each do |keyword|
it "lexes macro with nested #{keyword}" do
lexer = Lexer.new(%(hello\n #{keyword} {{world}} end end))

Expand Down
1 change: 0 additions & 1 deletion spec/compiler/parser/parser_doc_spec.cr
Expand Up @@ -9,7 +9,6 @@ describe "Parser doc" do
{"def", "def foo\nend"},
{"abstract def", "abstract def foo"},
{"macro", "macro foo\nend"},
{"macro def", "macro def foo : Int32\nend"},
{"call without obj", "foo"},
{"fun def", "fun foo : Int32\nend"},
{"enum def", "enum Foo\nend"},
Expand Down
5 changes: 2 additions & 3 deletions spec/compiler/parser/parser_spec.cr
Expand Up @@ -769,9 +769,8 @@ describe "Parser" do
it_parses "macro foo;bar{% for x in y %}\\ \n body{% end %}\\ baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroFor.new(["x".var], "y".var, "body".macro_literal), "baz;".macro_literal] of ASTNode))
it_parses "macro foo; 1 + 2 {{foo}}\\ 3 + 4; end", Macro.new("foo", [] of Arg, Expressions.from([" 1 + 2 ".macro_literal, MacroExpression.new("foo".var), "3 + 4; ".macro_literal] of ASTNode))

it_parses "macro def foo : String; 1; end", Def.new("foo", body: 1.int32, return_type: "String".path, macro_def: true)
it_parses "macro def foo(x) : String; 1; end", Def.new("foo", ["x".arg], 1.int32, return_type: "String".path, macro_def: true)
it_parses "macro def foo; 1; end", Def.new("foo", body: 1.int32, macro_def: true)
assert_syntax_error "macro def foo : String; 1; end"

it_parses "def foo;{{@type}};end", Def.new("foo", body: Expressions.from([MacroExpression.new("@type".instance_var)] of ASTNode), macro_def: true)

it_parses "macro foo;bar{% begin %}body{% end %}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroIf.new(true.bool, "body".macro_literal), "baz;".macro_literal] of ASTNode))
Expand Down

0 comments on commit 2c1bb2d

Please sign in to comment.