说-1
,将-
解析为文字的一部分作为符号,还是将值转换为负对应的运算符?
答案 0 :(得分:3)
它被解析为文字的一部分,并使文字成为否定文字。
解析器源代码中的 Here's the reference。如果您在文件中搜索tUMINUS_NUM
和tUMINUS
,则会看到-
令牌的定义位置。
答案 1 :(得分:3)
$ ruby --dump=parsetree -e "-1"
###########################################################
## Do NOT use this node dump for any purpose other than ##
## debug and research. Compatibility is not guaranteed. ##
###########################################################
# @ NODE_SCOPE (line: 1)
# +- nd_tbl: (empty)
# +- nd_args:
# | (null node)
# +- nd_body:
# @ NODE_LIT (line: 1)
# +- nd_lit: -1
而不是-(1)
,-+1
或- 1
(中间有空格)调用Fixnum#-@
:
$ ruby --dump=parsetree -e "-(1)"
###########################################################
## Do NOT use this node dump for any purpose other than ##
## debug and research. Compatibility is not guaranteed. ##
###########################################################
# @ NODE_SCOPE (line: 1)
# +- nd_tbl: (empty)
# +- nd_args:
# | (null node)
# +- nd_body:
# @ NODE_CALL (line: 1)
# +- nd_mid: :-@
# +- nd_recv:
# | @ NODE_LIT (line: 1)
# | +- nd_lit: 1
# +- nd_args:
# (null node)