mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
_0 is not numbered parameter
#### Before this patch:
```console
$ bin/mruby rb -e '_0=:l; p ->{_0}.()'
-e:1:13: _0 is not available
-e:1:13: syntax error, unexpected $end, expecting '}'
```
#### After this patch (same as Ruby):
```console
$ bin/mruby rb -e '_0=:l; p ->{_0}.()'
:l
```
This commit is contained in:
@@ -74,8 +74,6 @@ typedef unsigned int stack_type;
|
||||
#define NUM_SUFFIX_R (1<<0)
|
||||
#define NUM_SUFFIX_I (1<<1)
|
||||
|
||||
#define NUMPARAM_MAX 9
|
||||
|
||||
static inline mrb_sym
|
||||
intern_cstr_gen(parser_state *p, const char *s)
|
||||
{
|
||||
@@ -5954,30 +5952,25 @@ parser_yylex(parser_state *p)
|
||||
case '_':
|
||||
if (toklen(p) == 2 && ISDIGIT(tok(p)[1]) && p->nvars) {
|
||||
int n = tok(p)[1] - '0';
|
||||
node *nvars = p->nvars->car;
|
||||
|
||||
while (nvars) {
|
||||
if (intn(nvars->cdr) > 0) {
|
||||
yywarning(p, "numbered parameter in nested block");
|
||||
break;
|
||||
if (n > 0) {
|
||||
node *nvars = p->nvars->car;
|
||||
|
||||
while (nvars) {
|
||||
if (intn(nvars->cdr) > 0) {
|
||||
yywarning(p, "numbered parameter in nested block");
|
||||
break;
|
||||
}
|
||||
nvars->cdr = nint(-1);
|
||||
nvars = nvars->car;
|
||||
}
|
||||
nvars->cdr = nint(-1);
|
||||
nvars = nvars->car;
|
||||
if (intn(p->nvars->cdr) < 0) {
|
||||
yywarning(p, "numbered parameter in nested block");
|
||||
}
|
||||
pylval.num = n;
|
||||
p->lstate = EXPR_END;
|
||||
return tNUMPARAM;
|
||||
}
|
||||
if (intn(p->nvars->cdr) < 0) {
|
||||
yywarning(p, "numbered parameter in nested block");
|
||||
}
|
||||
if (n == 0) {
|
||||
yyerror(p, "_0 is not available");
|
||||
return 0;
|
||||
}
|
||||
if (n > NUMPARAM_MAX) {
|
||||
yyerror(p, "too large numbered parameter");
|
||||
return 0;
|
||||
}
|
||||
pylval.num = n;
|
||||
p->lstate = EXPR_END;
|
||||
return tNUMPARAM;
|
||||
}
|
||||
/* fall through */
|
||||
default:
|
||||
|
||||
+5
-1
@@ -672,7 +672,6 @@ assert 'keyword arguments' do
|
||||
assert_equal([:a, nil, :c], m(a: :a, c: :c))
|
||||
end
|
||||
|
||||
|
||||
assert('numbered parameters') do
|
||||
assert_equal(15, [1,2,3,4,5].reduce {_1+_2})
|
||||
assert_equal(3, ->{_1+_2}.call(1,2))
|
||||
@@ -680,3 +679,8 @@ assert('numbered parameters') do
|
||||
assert_equal(5, -> a: ->{_1} {a}.call.call(5))
|
||||
assert_equal(45, Proc.new do _1 + _2 + _3 + _4 + _5 + _6 + _7 + _8 + _9 end.call(*[1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||
end
|
||||
|
||||
assert('_0 is not numbered parameter') do
|
||||
_0 = :l
|
||||
assert_equal(:l, ->{_0}.call)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user