Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operators and methods in Ruby

Most things that look like operators are methods in Ruby; 1 + 2 is syntactic sugar for 1.+(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).

I wonder where this special magic lives in Ruby--if it is hard-wired into the interpreter.

Ari.

like image 554
iter Avatar asked Feb 13 '10 19:02

iter


2 Answers

In all Ruby Implementations, Operator Precedence is handled by the parser. 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. (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.)

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.

Cardinal is a Ruby implementation for the Parrot virtual machine, and since Parrot includes the Parrot Grammar Engine, Cardinal naturally uses that. The interesting file is 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.

Tinyrb uses a PEG parser utilizing Ian Piumarta's leg library. As is typical for PEG parsers, there is no operator precedence table, rather the precedence is implicit in the hierarchical structure of the grammar. See vm/grammar.leg for details. RubyGoLightly is derived from tinyrb, except it uses Go instead of C as the implementation language, but it uses the same PEG grammar.

XRuby uses ANTLR for its parser. Here, the interesting file is src/com/xruby/compiler/parser/ruby.g.

Rubinius uses the Melbourne parser, which is essentially YARV's parser packaged as a C extension. MagLev uses ruby_parser (see below).

Apart from the Ruby Implementations, there are also other Ruby parsers available.

Ryan Davis's ruby_parser is derived from the YARV YACC grammar. It uses racc as the parser generator. See lib/ruby_parser.y.

Caleb Clausen's RedParse uses Caleb's own hand-written compiler-interpreter. The interesting file is 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. They don't deal with operator precedence.

like image 162
Jörg W Mittag Avatar answered Oct 06 '22 20:10

Jörg W Mittag


"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.

like image 35
Josh Lee Avatar answered Oct 06 '22 20:10

Josh Lee