Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support named arguments in macro methods #8429

Merged
merged 5 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ module Crystal
assert_macro "", %({{"foo_bar".camelcase}}), [] of ASTNode, %("FooBar")
end

it "executes camelcase with lower" do
assert_macro "", %({{"foo_bar".camelcase(lower: true)}}), [] of ASTNode, %("fooBar")
end

it "executes camelcase with invalid lower arg type" do
expect_raises(Crystal::TypeException, "named argument 'lower' to StringLiteral#camelcase must be a bool, not NumberLiteral") do
assert_macro "", %({{"foo_bar".camelcase(lower: 99)}}), [] of ASTNode, ""
end
end

it "executes underscore" do
assert_macro "", %({{"FooBar".underscore}}), [] of ASTNode, %("foo_bar")
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ module Crystal::Macros
end

# Similar to `String#camelcase`.
def camelcase : StringLiteral
def camelcase(*, lower : BoolLiteral = false) : StringLiteral
end

# Similar to `String#capitalize`.
Expand Down
13 changes: 10 additions & 3 deletions src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,14 @@ module Crystal
end

args = node.args.map { |arg| accept arg }
named_args = Hash(String, ASTNode).new
Blacksmoke16 marked this conversation as resolved.
Show resolved Hide resolved

if (nargs = node.named_args)
Blacksmoke16 marked this conversation as resolved.
Show resolved Hide resolved
nargs.each_with_object(named_args) { |arg, named_arg_hash| named_arg_hash[arg.name] = accept arg.value }
end

begin
@last = receiver.interpret(node.name, args, node.block, self)
@last = receiver.interpret(node.name, args, named_args, node.block, self)
rescue ex : MacroRaiseException
raise ex
rescue ex : Crystal::Exception
Expand Down Expand Up @@ -511,13 +516,15 @@ module Crystal

def visit(node : Splat)
node.exp.accept self
@last = @last.interpret("splat", [] of ASTNode, nil, self)
named_args = Hash(String, ASTNode).new
@last = @last.interpret("splat", [] of ASTNode, named_args, nil, self)
false
end

def visit(node : DoubleSplat)
node.exp.accept self
@last = @last.interpret("double_splat", [] of ASTNode, nil, self)
named_args = Hash(String, ASTNode).new
@last = @last.interpret("double_splat", [] of ASTNode, named_args, nil, self)
false
end

Expand Down
Loading