1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* eval.c (rb_eval): [EXPERIMENTAL] NODE_LAMBDA implemented.

[ruby-dev:25780]

* node.h (NODE_LAMBDA): for literal Proc object.

* parse.y (expr): interpret mere do...end block as proc object.

* parse.y (primary): ditto, for brace block.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8044 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2005-03-02 03:21:31 +00:00
parent eaf373d602
commit 6c0cc67bb2
4 changed files with 52 additions and 3 deletions

View file

@ -1,3 +1,14 @@
Wed Mar 2 12:21:18 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>
* eval.c (rb_eval): [EXPERIMENTAL] NODE_LAMBDA implemented.
[ruby-dev:25780]
* node.h (NODE_LAMBDA): for literal Proc object.
* parse.y (expr): interpret mere do...end block as proc object.
* parse.y (primary): ditto, for brace block.
Tue Mar 1 21:16:54 2005 K.Kosako <sndgk393 AT ybb.ne.jp> Tue Mar 1 21:16:54 2005 K.Kosako <sndgk393 AT ybb.ne.jp>
* regcomp.c (optimize_node_left): uninitialized member * regcomp.c (optimize_node_left): uninitialized member

5
eval.c
View file

@ -2941,6 +2941,7 @@ rb_eval(self, n)
case NODE_ITER: case NODE_ITER:
case NODE_FOR: case NODE_FOR:
case NODE_LAMBDA:
{ {
PUSH_TAG(PROT_LOOP); PUSH_TAG(PROT_LOOP);
PUSH_BLOCK(node->nd_var, node->nd_body); PUSH_BLOCK(node->nd_var, node->nd_body);
@ -2952,6 +2953,10 @@ rb_eval(self, n)
if (nd_type(node) == NODE_ITER) { if (nd_type(node) == NODE_ITER) {
result = rb_eval(self, node->nd_iter); result = rb_eval(self, node->nd_iter);
} }
else if (nd_type(node) == NODE_LAMBDA) {
ruby_iter->iter = ruby_frame->iter = ITER_CUR;
result = rb_block_proc();
}
else { else {
VALUE recv; VALUE recv;

2
node.h
View file

@ -125,6 +125,7 @@ enum node_type {
NODE_DSYM, NODE_DSYM,
NODE_ATTRASGN, NODE_ATTRASGN,
NODE_PRELUDE, NODE_PRELUDE,
NODE_LAMBDA,
NODE_LAST NODE_LAST
}; };
@ -340,6 +341,7 @@ typedef struct RNode {
#define NEW_BMETHOD(b) NEW_NODE(NODE_BMETHOD,0,0,b) #define NEW_BMETHOD(b) NEW_NODE(NODE_BMETHOD,0,0,b)
#define NEW_ATTRASGN(r,m,a) NEW_NODE(NODE_ATTRASGN,r,m,a) #define NEW_ATTRASGN(r,m,a) NEW_NODE(NODE_ATTRASGN,r,m,a)
#define NEW_PRELUDE(p,b) NEW_NODE(NODE_PRELUDE,p,b,0) #define NEW_PRELUDE(p,b) NEW_NODE(NODE_PRELUDE,p,b,0)
#define NEW_LAMBDA(v,b) NEW_NODE(NODE_LAMBDA,v,b,0)
#define NOEX_PUBLIC 0 #define NOEX_PUBLIC 0
#define NOEX_NOSUPER 1 #define NOEX_NOSUPER 1

37
parse.y
View file

@ -489,7 +489,7 @@ static void ripper_compile_error _((struct parser_params*, const char *fmt, ...)
%type <node> mrhs superclass block_call block_command %type <node> mrhs superclass block_call block_command
%type <node> f_arglist f_args f_optarg f_opt f_block_arg opt_f_block_arg %type <node> f_arglist f_args f_optarg f_opt f_block_arg opt_f_block_arg
%type <node> assoc_list assocs assoc undef_list backref string_dvar %type <node> assoc_list assocs assoc undef_list backref string_dvar
%type <node> for_var block_var opt_block_var block_par %type <node> for_var block_var opt_block_var block_var_def block_par
%type <node> brace_block cmd_brace_block do_block lhs none fitem %type <node> brace_block cmd_brace_block do_block lhs none fitem
%type <node> mlhs mlhs_head mlhs_basic mlhs_entry mlhs_item mlhs_node %type <node> mlhs mlhs_head mlhs_basic mlhs_entry mlhs_item mlhs_node
%type <id> fsym variable sym symbol operation operation2 operation3 %type <id> fsym variable sym symbol operation operation2 operation3
@ -1014,6 +1014,11 @@ expr : command_call
$$ = dispatch2(unary, ID2SYM('!'), $2); $$ = dispatch2(unary, ID2SYM('!'), $2);
%*/ %*/
} }
| do_block
{
$$ = $1;
nd_set_type($$, NODE_LAMBDA);
}
| arg | arg
; ;
@ -2504,6 +2509,26 @@ primary : literal
$$ = dispatch1(hash, escape_Qundef($2)); $$ = dispatch1(hash, escape_Qundef($2));
%*/ %*/
} }
| tLBRACE
{
/*%%%*/
$<vars>$ = dyna_push();
$<num>1 = ruby_sourceline;
/*%
%*/
}
block_var_def {$<vars>$ = ruby_dyna_vars;}
compstmt
'}'
{
/*%%%*/
$$ = NEW_LAMBDA($3, dyna_init($5, $<vars>4));
nd_set_line($$, $<num>1);
dyna_pop($<vars>2);
/*%
$$ = dispatch2(brace_block, escape_Qundef($3), $5);
%*/
}
| kRETURN | kRETURN
{ {
/*%%%*/ /*%%%*/
@ -3062,7 +3087,13 @@ block_var : block_par
; ;
opt_block_var : none opt_block_var : none
| '|' /* none */ '|' | block_var_def
{
$$ = $1;
}
;
block_var_def : '|' /* none */ '|'
{ {
/*%%%*/ /*%%%*/
$$ = (NODE*)1; $$ = (NODE*)1;
@ -6380,7 +6411,7 @@ parser_yylex(parser)
if (COND_P()) return kDO_COND; if (COND_P()) return kDO_COND;
if (CMDARG_P() && state != EXPR_CMDARG) if (CMDARG_P() && state != EXPR_CMDARG)
return kDO_BLOCK; return kDO_BLOCK;
if (state == EXPR_ENDARG) if (state == EXPR_ENDARG || state == EXPR_BEG)
return kDO_BLOCK; return kDO_BLOCK;
return kDO; return kDO;
} }