开发者

In the C source code of Ruby, where is the Ruby operator `=` defined?

开发者 https://www.devze.com 2023-01-14 14:58 出处:网络
I\'d like to read the logic code of =, but can\'t find it. UPDATE: I found the test_multi method text/ruby/test_assignment.rb. It\'s Ruby code, but seems will let me to the destination.

I'd like to read the logic code of =, but can't find it.

UPDATE:

I found the test_multi method text/ruby/test_assignment.rb. It's Ruby code, but seems will let me to the destination.

The reason I want to check the code is find how it handles multi-assignment. Like a,b,c = [1,2,3].

UPDATE:

I found keywords "MASGN" and led me to

compile_massign(rb_iseq_t *iseq, LINK_ANCH开发者_开发问答OR *ret, NODE *node, int poped) in compile.c

http://github.com/ruby/ruby/commit/e39eb9dab50eaa681467e51145b37cdc11667830#diff-2


I don't think you'll find any code for =.

The = will be part of the grammar rules which define the Ruby language and the parser(written in C) will make use of the grammar.


http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_9_2/parse.y line 1088-1116

| var_lhs tOP_ASGN command_call
        {
        /*%%%*/
        value_expr($3);
        if ($1) {
            ID vid = $1->nd_vid;
            if ($2 == tOROP) {
            $1->nd_value = $3;
            $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
            if (is_asgn_or_id(vid)) {
                $$->nd_aid = vid;
            }
            }
            else if ($2 == tANDOP) {
            $1->nd_value = $3;
            $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
            }
            else {
            $$ = $1;
            $$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3));
            }
        }
        else {
            $$ = NEW_BEGIN(0);
        }
        /*%
        $$ = dispatch3(opassign, $1, $2, $3);
        %*/
        }


I don't know what you mean by "the C source code of Ruby". Ruby is a programming language. Programming languages don't have source code (only compilers and interpreters do), they have specifications.

The specification of multiple assignment is in section 11.3.1.3 Multiple Assignments on pages 59–62 of the current (2009-12-01) ISO Ruby Draft Specification and in language/variables_spec.rb (search for "multiple", unfortunately the tests are a bit scattered around the file) in the executable RubySpec.

A nice overview of a possible implementation can be found in the Rubinius compiler (sorry, no C source code here, either) in lines 482–607 of lib/compiler/ast/variables.rb.

0

精彩评论

暂无评论...
验证码 换一张
取 消