简体   繁体   English

Ruby中的运算符和方法

[英]Operators and methods in Ruby

Most things that look like operators are methods in Ruby; 大多数看起来像运算符的东西都是Ruby中的方法; 1 + 2 is syntactic sugar for 1.+(2) . 1 + 21.+(2)语法糖。

Even though + and * are methods that a program can redefine, Ruby has special magic to evaluate 1 + 2 * 3 as 1.+(2.*(3)) instead of 1.+(2).*(3) . 尽管+*是程序可以重新定义的方法,但Ruby具有特殊的魔力来将1 + 2 * 3评估为1.+(2.*(3))而不是1.+(2).*(3)

I wonder where this special magic lives in Ruby--if it is hard-wired into the interpreter. 我想知道这个特殊魔法在Ruby中的位置 - 如果它与解释器连接起来很困难。

Ari. 阿里。

In all Ruby Implementations, Operator Precedence is handled by the parser. 在所有Ruby实现中,运算符优先级由解析器处理。 Since pretty much all existing Ruby Implementations use the same parser, or a parser generated from the same YACC grammar, parse.y in YARV is the file you want to look at . 由于几乎所有现有的Ruby实现使用相同的解析器,或者使用相同的YACC语法生成的解析器, parse.y YARV中的parse.y是您要查看的文件 (In JRuby, for example, that file is essentially the same: src/org/jruby/parser/Ruby19Parser.y . Same for IronRuby: Merlin/Main/Languages/Ruby/Ruby/Compiler/Parser/Parser.y .) (例如,在JRuby中,该文件基本相同: src/org/jruby/parser/Ruby19Parser.y 。与IronRuby相同: Merlin/Main/Languages/Ruby/Ruby/Compiler/Parser/Parser.y 。)

The only four Ruby Implementations that do not either use the YARV parser directly or use a YACC clone generated parser from YARV's parse.y , are Cardinal, tinyrb, RubyGoLightly and XRuby. 没有直接使用YARV解析器或使用YARV的parse.y的YACC克隆生成解析器的唯一四个Ruby实现是Cardinal,tinyrb,RubyGoLightly和XRuby。

Cardinal is a Ruby implementation for the Parrot virtual machine, and since Parrot includes the Parrot Grammar Engine, Cardinal naturally uses that. Cardinal是Parrot虚拟机的Ruby实现,由于Parrot包含Parrot Grammar Engine,Cardinal自然会使用它。 The interesting file is src/parser/grammar.pg . 有趣的文件是src/parser/grammar.pg PGE is a hybrid recursive-descent parser/operator precedence parser, which means that operator precedence shows up pretty nicely in the grammar file. PGE是一个混合递归下降解析器/运算符优先级解析器,这意味着运算符优先级在语法文件中显示得非常好。

Tinyrb uses a PEG parser utilizing Ian Piumarta's leg library. Tinyrb使用了Ian Piumarta腿部库的PEG解析器。 As is typical for PEG parsers, there is no operator precedence table, rather the precedence is implicit in the hierarchical structure of the grammar. 正如PEG解析器的典型情况一样,没有运算符优先级表,而优先级隐含在语法的层次结构中。 See vm/grammar.leg for details. 有关详细信息,请参阅vm/grammar.leg RubyGoLightly is derived from tinyrb, except it uses Go instead of C as the implementation language, but it uses the same PEG grammar. RubyGoLightly源自tinyrb,除了它使用Go而不是C作为实现语言,但它使用相同的PEG语法。

XRuby uses ANTLR for its parser. XRuby使用ANTLR作为其解析器。 Here, the interesting file is src/com/xruby/compiler/parser/ruby.g . 这里,有趣的文件是src/com/xruby/compiler/parser/ruby.g

Rubinius uses the Melbourne parser, which is essentially YARV's parser packaged as a C extension. Rubinius使用墨尔本解析器,它本质上是YARV的解析器,打包为C扩展。 MagLev uses ruby_parser (see below). MagLev使用ruby_parser (见下文)。

Apart from the Ruby Implementations, there are also other Ruby parsers available. 除了Ruby实现之外,还有其他Ruby解析器可用。

Ryan Davis's ruby_parser is derived from the YARV YACC grammar. Ryan Davis的ruby_parser源自YARV YACC语法。 It uses racc as the parser generator. 它使用racc作为解析器生成器。 See lib/ruby_parser.y . 请参阅lib/ruby_parser.y

Caleb Clausen's RedParse uses Caleb's own hand-written compiler-interpreter. Caleb Clausen的RedParse使用Caleb自己的手写编译器解释器。 The interesting file is lib/redparse/babyparser.rb . 有趣的文件是lib/redparse/babyparser.rb

That's all the parsers I know, that actually handle operator precedence. 这就是我所知道的所有解析器,它实际上处理运算符优先级。 There is another parser built into RDoc, and there used to be one in YARD (it now uses RedParse), but those only handle just enough of Ruby's syntax to find modules, classes and methods, comments and extract method parameter lists. 在RDoc中内置了另一个解析器,在YARD中曾经有一个解析器(它现在使用RedParse),但那些只处理Ruby的语法足以找到模块,类和方法,注释和提取方法参数列表。 They don't deal with operator precedence. 它们不处理运算符优先级。

" Operator Expressions " in the language documentation gives a table with the operators that can be overridden as methods. 语言文档中的“ 运算符表达式 ”提供了一个表,其中包含可以作为方法重写的运算符。 You can't make up your own operators — the mapping of operators to their symbol names lives inside the parser. 您无法构建自己的运算符 - 运算符到其符号名称的映射存在于解析器中。

是的,它是硬连线的,因此您无法添加新运算符或更改现有运算符的优先级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM