Skip to content

Commit

Permalink
Showing 3 changed files with 26 additions and 3 deletions.
10 changes: 10 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
@@ -989,4 +989,14 @@ describe "macro methods" do
assert_macro "", %({{env("FOO")}}), [] of ASTNode, %(nil)
end
end

describe "flag?" do
it "has flag" do
assert_macro "", %({{flag?(:foo)}}), [] of ASTNode, %(true), flags: "foo"
end

it "doesn't have flag" do
assert_macro "", %({{flag?(:foo)}}), [] of ASTNode, %(false)
end
end
end
7 changes: 4 additions & 3 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -231,12 +231,13 @@ def assert_error(str, message, inject_primitives = true)
end
end

def assert_macro(macro_args, macro_body, call_args, expected)
assert_macro(macro_args, macro_body, expected) { call_args }
def assert_macro(macro_args, macro_body, call_args, expected, flags = nil)
assert_macro(macro_args, macro_body, expected, flags) { call_args }
end

def assert_macro(macro_args, macro_body, expected)
def assert_macro(macro_args, macro_body, expected, flags = nil)
program = Program.new
program.flags = flags if flags
sub_node = yield program
assert_macro_internal program, sub_node, macro_args, macro_body, expected
end
12 changes: 12 additions & 0 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@ module Crystal
interpret_debug
when "env"
interpret_env(node)
when "flag?"
interpret_flag?(node)
when "puts", "p"
interpret_puts(node)
when "pp"
@@ -40,6 +42,16 @@ module Crystal
end
end

def interpret_flag?(node)
if node.args.size == 1
node.args[0].accept self
flag = @last.to_macro_id
@last = BoolLiteral.new(@mod.has_flag?(flag))
else
node.wrong_number_of_arguments "macro call 'flag?'", node.args.size, 1
end
end

def interpret_puts(node)
node.args.each do |arg|
arg.accept self

0 comments on commit 5fcfdd7

Please sign in to comment.