Skip to content

Commit

Permalink
Remove '$0' special syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin authored and ysbaddaden committed Aug 18, 2017
1 parent a4a5ab8 commit a0f31a5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
4 changes: 1 addition & 3 deletions spec/compiler/parser/parser_spec.cr
Expand Up @@ -858,6 +858,7 @@ describe "Parser" do

it_parses "$~", Global.new("$~")
it_parses "$~.foo", Call.new(Global.new("$~"), "foo")
it_parses "$0", Call.new(Global.new("$~"), "[]", 0.int32)
it_parses "$1", Call.new(Global.new("$~"), "[]", 1.int32)
it_parses "$1?", Call.new(Global.new("$~"), "[]?", 1.int32)
it_parses "foo $1", Call.new(nil, "foo", Call.new(Global.new("$~"), "[]", 1.int32))
Expand All @@ -874,9 +875,6 @@ describe "Parser" do
it_parses "foo $?", Call.new(nil, "foo", Global.new("$?"))
it_parses "$? = 1", Assign.new("$?".var, 1.int32)

it_parses "$0", Path.global("PROGRAM_NAME")
it_parses "foo $0", Call.new(nil, "foo", Path.global("PROGRAM_NAME"))

it_parses "foo out x; x", [Call.new(nil, "foo", Out.new("x".var)), "x".var]
it_parses "foo(out x); x", [Call.new(nil, "foo", Out.new("x".var)), "x".var]
it_parses "foo out @x; @x", [Call.new(nil, "foo", Out.new("@x".instance_var)), "@x".instance_var]
Expand Down
16 changes: 6 additions & 10 deletions src/compiler/crystal/syntax/parser.cr
Expand Up @@ -894,18 +894,14 @@ module Crystal
end
when :GLOBAL_MATCH_DATA_INDEX
value = @token.value.to_s
if value == "0"
node_and_next_token Path.global("PROGRAM_NAME")
if value.ends_with? '?'
method = "[]?"
value = value.rchop
else
if value.ends_with? '?'
method = "[]?"
value = value.rchop
else
method = "[]"
end
location = @token.location
node_and_next_token Call.new(Global.new("$~").at(location), method, NumberLiteral.new(value.to_i))
method = "[]"
end
location = @token.location
node_and_next_token Call.new(Global.new("$~").at(location), method, NumberLiteral.new(value.to_i))
when :__LINE__
node_and_next_token MagicConstant.expand_line_node(@token.location)
when :__END_LINE__
Expand Down
8 changes: 0 additions & 8 deletions src/compiler/crystal/tools/formatter.cr
Expand Up @@ -970,14 +970,6 @@ module Crystal
end

def visit(node : Path)
# This is the case of $0, which generates `PROGRAM_NAME` by the parser
if @token.type == :GLOBAL_MATCH_DATA_INDEX
write "$"
write @token.value
next_token
return false
end

check_open_paren

# Sometimes the :: is not present because the parser generates ::Nil, for example
Expand Down

0 comments on commit a0f31a5

Please sign in to comment.