Permalink
Browse files

add safe-navigation (aka lonely) operator `&.`

  • Loading branch information...
matz committed Mar 22, 2016
1 parent 7d06125 commit 21a3ae08f7dc252e4cf6d34e5ebead0124fb0117
Showing with 109 additions and 67 deletions.
  1. +20 −4 mrbgems/mruby-compiler/core/codegen.c
  2. +1 −0 mrbgems/mruby-compiler/core/node.h
  3. +88 −63 mrbgems/mruby-compiler/core/parse.y
@@ -826,13 +826,23 @@ gen_values(codegen_scope *s, node *t, int val)
#define CALL_MAXARGS 127
static void
gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val)
gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val, int safe)
{
mrb_sym sym = name ? name : sym(tree->cdr->car);
int idx;
int idx, skip;
int n = 0, noop = 0, sendv = 0, blk = 0;
codegen(s, tree->car, VAL); /* receiver */
if (safe) {
int recv = cursp()-1;
genop(s, MKOP_A(OP_LOADNIL, cursp()));
push();
genop(s, MKOP_AB(OP_MOVE, cursp(), recv));
pop();
idx = new_msym(s, mrb_intern_lit(s->mrb, "=="));
genop(s, MKOP_ABC(OP_EQ, cursp(), idx, 1));
skip = genop(s, MKOP_AsBx(OP_JMPIF, cursp(), 0));
}
idx = new_msym(s, sym);
tree = tree->cdr->cdr->car;
if (tree) {
@@ -905,6 +915,9 @@ gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val)
}
}
}
if (safe) {
dispatch(s, skip);
}
if (val) {
push();
}
@@ -968,7 +981,7 @@ gen_assignment(codegen_scope *s, node *tree, int sp, int val)
case NODE_CALL:
push();
gen_call(s, tree, attrsym(s, sym(tree->cdr->car)), sp, NOVAL);
gen_call(s, tree, attrsym(s, sym(tree->cdr->car)), sp, NOVAL, 0);
pop();
if (val) {
genop_peep(s, MKOP_AB(OP_MOVE, cursp(), sp), val);
@@ -1511,7 +1524,10 @@ codegen(codegen_scope *s, node *tree, int val)
case NODE_FCALL:
case NODE_CALL:
gen_call(s, tree, 0, 0, val);
gen_call(s, tree, 0, 0, val, 0);
break;
case NODE_SCALL:
gen_call(s, tree, 0, 0, val, 1);
break;
case NODE_DOT2:
@@ -38,6 +38,7 @@ enum node_type {
NODE_CVDECL,
NODE_OP_ASGN,
NODE_CALL,
NODE_SCALL,
NODE_FCALL,
NODE_VCALL,
NODE_SUPER,
Oops, something went wrong.

1 comment on commit 21a3ae0

@zzak

This comment has been minimized.

Show comment
Hide comment
@zzak

zzak Mar 23, 2016

Member

/cc #3140

Member

zzak commented on 21a3ae0 Mar 23, 2016

/cc #3140

Please sign in to comment.