[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [PROPOSAL] [PATCH] simple method call
- From: Rodrigo Azevedo <rodrigoams@...>
- Date: Thu, 11 Jun 2015 09:26:16 -0300
REASONING
There are uncountable examples of "methods" that do not need an argument, for example
led[1]:blink
arm[2].pos[3]:go
z:abs
z:Re
polynomial:diff
etc
PROPOSAL
Raise the "methods" syntactic sugar ":" to a function call without arguments, with the exception of the ubiquitous "self", namely, call "methods" without the common usage of '(' ')', or '{' '}' or ' " ' ' " '.
PATCH
This patch applies a minimalist update of lparser.c to implement the described behaviour.
For Lua 5.3.0
EXAMPLE USAGE
unpack, istable = table.unpack, function(t) return type(t) == 'table' end
t = {}
t.name = "testing ... "
function t:prt (str) print(
self.name .. ((istable(str) and str[1]) or str or "") .. " OK") end
t:prt -- NEW!
t:prt ()
t:prt "string"
t:prt {"table"}
t:prt
("ambiguity")
t:prt
(print("OI"))
print("c'est fini")
--
Rodrigo Azevedo Moreira da Silva
--- lparser.c 2015-06-11 08:34:36.553415456 -0300
+++ lparser.c 2015-06-11 08:18:07.138728628 -0300
@@ -808,7 +808,7 @@
}
-static void funcargs (LexState *ls, expdesc *f, int line) {
+static void funcargs (LexState *ls, expdesc *f, int line, int is_method) {
FuncState *fs = ls->fs;
expdesc args;
int base, nparams;
@@ -834,7 +834,11 @@
break;
}
default: {
- luaX_syntaxerror(ls, "function arguments expected");
+ if (is_method) { /* funcargs -> '(' ')' */
+ args.k = VVOID;
+ } else {
+ luaX_syntaxerror(ls, "function arguments expected");
+ }
}
}
lua_assert(f->k == VNONRELOC);
@@ -908,12 +912,12 @@
luaX_next(ls);
checkname(ls, &key);
luaK_self(fs, v, &key);
- funcargs(ls, v, line);
+ funcargs(ls, v, line, 1);
break;
}
case '(': case TK_STRING: case '{': { /* funcargs */
luaK_exp2nextreg(fs, v);
- funcargs(ls, v, line);
+ funcargs(ls, v, line, 0);
break;
}
default: return;