Skip to content

Commit a0f31a5

Browse files
oprypinysbaddaden
authored andcommittedAug 18, 2017
Remove '$0' special syntax
1 parent a4a5ab8 commit a0f31a5

File tree

3 files changed

+7
-21
lines changed

3 files changed

+7
-21
lines changed
 

‎spec/compiler/parser/parser_spec.cr

+1-3
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ describe "Parser" do
858858

859859
it_parses "$~", Global.new("$~")
860860
it_parses "$~.foo", Call.new(Global.new("$~"), "foo")
861+
it_parses "$0", Call.new(Global.new("$~"), "[]", 0.int32)
861862
it_parses "$1", Call.new(Global.new("$~"), "[]", 1.int32)
862863
it_parses "$1?", Call.new(Global.new("$~"), "[]?", 1.int32)
863864
it_parses "foo $1", Call.new(nil, "foo", Call.new(Global.new("$~"), "[]", 1.int32))
@@ -874,9 +875,6 @@ describe "Parser" do
874875
it_parses "foo $?", Call.new(nil, "foo", Global.new("$?"))
875876
it_parses "$? = 1", Assign.new("$?".var, 1.int32)
876877

877-
it_parses "$0", Path.global("PROGRAM_NAME")
878-
it_parses "foo $0", Call.new(nil, "foo", Path.global("PROGRAM_NAME"))
879-
880878
it_parses "foo out x; x", [Call.new(nil, "foo", Out.new("x".var)), "x".var]
881879
it_parses "foo(out x); x", [Call.new(nil, "foo", Out.new("x".var)), "x".var]
882880
it_parses "foo out @x; @x", [Call.new(nil, "foo", Out.new("@x".instance_var)), "@x".instance_var]

‎src/compiler/crystal/syntax/parser.cr

+6-10
Original file line numberDiff line numberDiff line change
@@ -894,18 +894,14 @@ module Crystal
894894
end
895895
when :GLOBAL_MATCH_DATA_INDEX
896896
value = @token.value.to_s
897-
if value == "0"
898-
node_and_next_token Path.global("PROGRAM_NAME")
897+
if value.ends_with? '?'
898+
method = "[]?"
899+
value = value.rchop
899900
else
900-
if value.ends_with? '?'
901-
method = "[]?"
902-
value = value.rchop
903-
else
904-
method = "[]"
905-
end
906-
location = @token.location
907-
node_and_next_token Call.new(Global.new("$~").at(location), method, NumberLiteral.new(value.to_i))
901+
method = "[]"
908902
end
903+
location = @token.location
904+
node_and_next_token Call.new(Global.new("$~").at(location), method, NumberLiteral.new(value.to_i))
909905
when :__LINE__
910906
node_and_next_token MagicConstant.expand_line_node(@token.location)
911907
when :__END_LINE__

‎src/compiler/crystal/tools/formatter.cr

-8
Original file line numberDiff line numberDiff line change
@@ -970,14 +970,6 @@ module Crystal
970970
end
971971

972972
def visit(node : Path)
973-
# This is the case of $0, which generates `PROGRAM_NAME` by the parser
974-
if @token.type == :GLOBAL_MATCH_DATA_INDEX
975-
write "$"
976-
write @token.value
977-
next_token
978-
return false
979-
end
980-
981973
check_open_paren
982974

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

0 commit comments

Comments
 (0)
Please sign in to comment.