Skip to content

Commit

Permalink
Add macro methods for Include and Extend (crystal-lang#14064)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Dec 8, 2023
1 parent 3a95615 commit 4925cad
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
20 changes: 20 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,26 @@ module Crystal
end
end

describe Include do
foo = Include.new("Foo".path)
bar = Include.new(Generic.new("Bar".path, ["Int32".path] of ASTNode))

it "executes name" do
assert_macro %({{x.name}}), "Foo", {x: foo}
assert_macro %({{x.name}}), "Bar(Int32)", {x: bar}
end
end

describe Extend do
foo = Extend.new("Foo".path)
bar = Extend.new(Generic.new("Bar".path, ["Int32".path] of ASTNode))

it "executes name" do
assert_macro %({{x.name}}), "Foo", {x: foo}
assert_macro %({{x.name}}), "Bar(Int32)", {x: bar}
end
end

describe "visibility modifier methods" do
node = VisibilityModifier.new(Visibility::Protected, Def.new("some_def"))

Expand Down
28 changes: 24 additions & 4 deletions src/compiler/crystal/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1851,11 +1851,31 @@ module Crystal::Macros
end
end

# class Include < ASTNode
# end
# An `include` statement.
#
# Every statement `node` is equivalent to:
#
# ```
# include {{ node.name }}
# ```
class Include < ASTNode
# Returns the name of the type being included.
def name : ASTNode
end
end

# class Extend < ASTNode
# end
# An `extend` statement.
#
# Every statement `node` is equivalent to:
#
# ```
# extend {{ node.name }}
# ```
class Extend < ASTNode
# Returns the name of the type being extended.
def name : ASTNode
end
end

# class LibDef < ASTNode
# end
Expand Down
22 changes: 22 additions & 0 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,28 @@ module Crystal
end
end

class Include
def interpret(method : String, args : Array(ASTNode), named_args : Hash(String, ASTNode)?, block : Crystal::Block?, interpreter : Crystal::MacroInterpreter, name_loc : Location?)
case method
when "name"
interpret_check_args { @name }
else
super
end
end
end

class Extend
def interpret(method : String, args : Array(ASTNode), named_args : Hash(String, ASTNode)?, block : Crystal::Block?, interpreter : Crystal::MacroInterpreter, name_loc : Location?)
case method
when "name"
interpret_check_args { @name }
else
super
end
end
end

class OffsetOf
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

0 comments on commit 4925cad

Please sign in to comment.