Skip to content

Commit

Permalink
Showing 2 changed files with 41 additions and 4 deletions.
10 changes: 10 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
@@ -339,6 +339,16 @@ describe "macro methods" do
assert_macro "x", %({{x.chomp}}), [MacroId.new("hello\n")] of ASTNode, %(hello)
assert_macro "x", %({{x.upcase}}), [MacroId.new("hello")] of ASTNode, %(HELLO)
end

it "compares with string" do
assert_macro "x", %({{x == "foo"}}), [MacroId.new("foo")] of ASTNode, %(true)
assert_macro "x", %({{"foo" == x}}), [MacroId.new("foo")] of ASTNode, %(true)
end

it "compares with symbol" do
assert_macro "x", %({{x == :foo}}), [MacroId.new("foo")] of ASTNode, %(true)
assert_macro "x", %({{:foo == x}}), [MacroId.new("foo")] of ASTNode, %(true)
end
end

describe "symbol methods" do
35 changes: 31 additions & 4 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
@@ -25,9 +25,13 @@ module Crystal
raise arg.to_s
end
when "=="
BoolLiteral.new(self == args.first)
interpret_one_arg_method(method, args) do |arg|
BoolLiteral.new(self == arg)
end
when "!="
BoolLiteral.new(self != args.first)
interpret_one_arg_method(method, args) do |arg|
BoolLiteral.new(self != arg)
end
when "!"
BoolLiteral.new(!truthy?)
else
@@ -224,6 +228,13 @@ module Crystal
class StringLiteral
def interpret(method, args, block, interpreter)
case method
when "==", "!="
case arg = args.first?
when MacroId
return BoolLiteral.new(@value == arg.value)
else
return super
end
when "[]"
interpret_one_arg_method(method, args) do |arg|
case arg
@@ -670,7 +681,16 @@ module Crystal
class MacroId
def interpret(method, args, block, interpreter)
case method
when "==", "!=", "stringify", "class_name", "symbolize"
when "==", "!="
case arg = args.first?
when StringLiteral
return BoolLiteral.new(@value == arg.value)
when SymbolLiteral
return BoolLiteral.new(@value == arg.value)
else
return super
end
when "stringify", "class_name", "symbolize"
return super
end

@@ -689,7 +709,14 @@ module Crystal
class SymbolLiteral
def interpret(method, args, block, interpreter)
case method
when "==", "!=", "stringify", "class_name", "symbolize"
when "==", "!="
case arg = args.first?
when MacroId
return BoolLiteral.new(@value == arg.value)
else
return super
end
when "stringify", "class_name", "symbolize"
return super
end

0 comments on commit 97a7216

Please sign in to comment.