Skip to content

Commit

Permalink
Showing 3 changed files with 104 additions and 4 deletions.
58 changes: 58 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
@@ -202,6 +202,16 @@ describe "macro methods" do
end

describe "string methods" do
it "executes string == string" do
assert_macro "", %({{"foo" == "foo"}}), [] of ASTNode, %(true)
assert_macro "", %({{"foo" == "bar"}}), [] of ASTNode, %(false)
end

it "executes string != string" do
assert_macro "", %({{"foo" != "foo"}}), [] of ASTNode, %(false)
assert_macro "", %({{"foo" != "bar"}}), [] of ASTNode, %(true)
end

it "executes split without arguments" do
assert_macro "", %({{"1 2 3".split}}), [] of ASTNode, %(["1", "2", "3"])
end
@@ -310,6 +320,26 @@ describe "macro methods" do
assert_macro "", %({{"hello" =~ /ell/}}), [] of ASTNode, %(true)
end

it "executes string > string" do
assert_macro "", %({{"fooa" > "foo"}}), [] of ASTNode, %(true)
assert_macro "", %({{"foo" > "fooa"}}), [] of ASTNode, %(false)
end

it "executes string > macroid" do
assert_macro "", %({{"fooa" > "foo".id}}), [] of ASTNode, %(true)
assert_macro "", %({{"foo" > "fooa".id}}), [] of ASTNode, %(false)
end

it "executes string < string" do
assert_macro "", %({{"fooa" < "foo"}}), [] of ASTNode, %(false)
assert_macro "", %({{"foo" < "fooa"}}), [] of ASTNode, %(true)
end

it "executes string < macroid" do
assert_macro "", %({{"fooa" < "foo".id}}), [] of ASTNode, %(false)
assert_macro "", %({{"foo" < "fooa".id}}), [] of ASTNode, %(true)
end

it "executes tr" do
assert_macro "", %({{"hello".tr("e", "o")}}), [] of ASTNode, %("hollo")
end
@@ -347,11 +377,29 @@ describe "macro methods" do
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)

assert_macro "x", %({{x == "bar"}}), [MacroId.new("foo")] of ASTNode, %(false)
assert_macro "x", %({{"bar" == x}}), [MacroId.new("foo")] of ASTNode, %(false)

assert_macro "x", %({{x != "foo"}}), [MacroId.new("foo")] of ASTNode, %(false)
assert_macro "x", %({{"foo" != x}}), [MacroId.new("foo")] of ASTNode, %(false)

assert_macro "x", %({{x != "bar"}}), [MacroId.new("foo")] of ASTNode, %(true)
assert_macro "x", %({{"bar" != 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)

assert_macro "x", %({{x == :bar}}), [MacroId.new("foo")] of ASTNode, %(false)
assert_macro "x", %({{:bar == x}}), [MacroId.new("foo")] of ASTNode, %(false)

assert_macro "x", %({{x != :foo}}), [MacroId.new("foo")] of ASTNode, %(false)
assert_macro "x", %({{:foo != x}}), [MacroId.new("foo")] of ASTNode, %(false)

assert_macro "x", %({{x != :bar}}), [MacroId.new("foo")] of ASTNode, %(true)
assert_macro "x", %({{:bar != x}}), [MacroId.new("foo")] of ASTNode, %(true)
end
end

@@ -363,6 +411,16 @@ describe "macro methods" do
assert_macro "x", %({{x.chomp}}), [SymbolLiteral.new("hello\n")] of ASTNode, %(:hello)
assert_macro "x", %({{x.upcase}}), ["hello".symbol] of ASTNode, %(:HELLO)
end

it "executes symbol == symbol" do
assert_macro "", %({{:foo == :foo}}), [] of ASTNode, %(true)
assert_macro "", %({{:foo == :bar}}), [] of ASTNode, %(false)
end

it "executes symbol != symbol" do
assert_macro "", %({{:foo != :foo}}), [] of ASTNode, %(false)
assert_macro "", %({{:foo != :bar}}), [] of ASTNode, %(true)
end
end

describe "and methods" do
8 changes: 8 additions & 0 deletions src/compiler/crystal/macros.cr
Original file line number Diff line number Diff line change
@@ -251,6 +251,14 @@ module Crystal::Macros
def =~(range : RegexLiteral) : BoolLiteral
end

# Similar to `String#>`
def >(other : StringLiteral | MacroId) : BoolLiteral
end

# Similar to `String#<`
def <(other : StringLiteral | MacroId) : BoolLiteral
end

# Similar to `String#+`.
def +(other : StringLiteral | CharLiteral) : StringLiteral
end
42 changes: 38 additions & 4 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
@@ -407,7 +407,11 @@ module Crystal
when "==", "!="
case arg = args.first?
when MacroId
return BoolLiteral.new(@value == arg.value)
if method == "=="
return BoolLiteral.new(@value == arg.value)
else
return BoolLiteral.new(@value != arg.value)
end
else
return super
end
@@ -449,6 +453,24 @@ module Crystal
BoolLiteral.new(false)
end
end
when ">"
interpret_one_arg_method(method, args) do |arg|
case arg
when StringLiteral, MacroId
return BoolLiteral.new(interpret_compare(arg) > 0)
else
raise "Can't compare StringLiteral with #{arg.class_desc}"
end
end
when "<"
interpret_one_arg_method(method, args) do |arg|
case arg
when StringLiteral, MacroId
return BoolLiteral.new(interpret_compare(arg) < 0)
else
raise "Can't compare StringLiteral with #{arg.class_desc}"
end
end
when "+"
interpret_one_arg_method(method, args) do |arg|
case arg
@@ -920,9 +942,17 @@ module Crystal
when "==", "!="
case arg = args.first?
when StringLiteral
return BoolLiteral.new(@value == arg.value)
if method == "=="
return BoolLiteral.new(@value == arg.value)
else
return BoolLiteral.new(@value != arg.value)
end
when SymbolLiteral
return BoolLiteral.new(@value == arg.value)
if method == "=="
return BoolLiteral.new(@value == arg.value)
else
return BoolLiteral.new(@value != arg.value)
end
else
return super
end
@@ -948,7 +978,11 @@ module Crystal
when "==", "!="
case arg = args.first?
when MacroId
return BoolLiteral.new(@value == arg.value)
if method == "=="
return BoolLiteral.new(@value == arg.value)
else
return BoolLiteral.new(@value != arg.value)
end
else
return super
end

0 comments on commit a6cc655

Please sign in to comment.