Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2763,6 +2763,12 @@ module Crystal
end
end

describe TypeOf do
it "executes args" do
assert_macro %({{x.args}}), "[1, 'a', Foo]", {x: TypeOf.new([1.int32, CharLiteral.new('a'), "Foo".path])}
end
end

describe "case methods" do
describe "when" do
case_node = Case.new(1.int32, [When.new([2.int32, 3.int32] of ASTNode, 4.int32)], 5.int32, exhaustive: false)
Expand Down
14 changes: 12 additions & 2 deletions src/compiler/crystal/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2255,8 +2255,18 @@ module Crystal::Macros
end
end

# class TypeOf < ASTNode
# end
# A `typeof` expression.
#
# Every expression *node* is equivalent to:
#
# ```
# typeof({{ node.args.splat }})
# ```
class TypeOf < ASTNode
# Returns the arguments to this `typeof`.
def args : ArrayLiteral(ASTNode)
end
end

# A macro expression,
# surrounded by {{ ... }} (output = true)
Expand Down
11 changes: 11 additions & 0 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,17 @@ module Crystal
end
end

class TypeOf
def interpret(method : String, args : Array(ASTNode), named_args : Hash(String, ASTNode)?, block : Crystal::Block?, interpreter : Crystal::MacroInterpreter, name_loc : Location?)
case method
when "args"
interpret_check_args { ArrayLiteral.map(@expressions, &.itself) }
else
super
end
end
end

class Generic
def interpret(method : String, args : Array(ASTNode), named_args : Hash(String, ASTNode)?, block : Crystal::Block?, interpreter : Crystal::MacroInterpreter, name_loc : Location?)
case method
Expand Down