awk: allow empty fuinctions with no arguments, disallow function redefinitions
function old new delta .rodata 103681 103700 +19 parse_program 303 307 +4 evaluate 3145 3141 -4 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/1 up/down: 23/-4) Total: 19 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
86fc2872b3
commit
d150710169
@ -139,6 +139,7 @@ typedef struct chain_s {
|
|||||||
/* Function */
|
/* Function */
|
||||||
typedef struct func_s {
|
typedef struct func_s {
|
||||||
unsigned nargs;
|
unsigned nargs;
|
||||||
|
smallint defined;
|
||||||
struct chain_s body;
|
struct chain_s body;
|
||||||
} func;
|
} func;
|
||||||
|
|
||||||
@ -1662,9 +1663,11 @@ static void parse_program(char *p)
|
|||||||
debug_printf_parse("%s: TC_FUNCDECL\n", __func__);
|
debug_printf_parse("%s: TC_FUNCDECL\n", __func__);
|
||||||
next_token(TC_FUNCTION);
|
next_token(TC_FUNCTION);
|
||||||
f = newfunc(t_string);
|
f = newfunc(t_string);
|
||||||
//FIXME: dup check: functions can't be redefined, this is not ok: awk 'func f(){}; func f(){}'
|
if (f->defined)
|
||||||
f->body.first = NULL;
|
syntax_error("Duplicate function");
|
||||||
f->nargs = 0;
|
f->defined = 1;
|
||||||
|
//f->body.first = NULL; - already is
|
||||||
|
//f->nargs = 0; - already is
|
||||||
/* func arg list: comma sep list of args, and a close paren */
|
/* func arg list: comma sep list of args, and a close paren */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (next_token(TC_VARIABLE | TC_RPAREN) == TC_RPAREN) {
|
if (next_token(TC_VARIABLE | TC_RPAREN) == TC_RPAREN) {
|
||||||
@ -2912,7 +2915,7 @@ static var *evaluate(node *op, var *res)
|
|||||||
|
|
||||||
debug_printf_eval("FUNC\n");
|
debug_printf_eval("FUNC\n");
|
||||||
|
|
||||||
if (op->r.f->nargs == 0 && !op->r.f->body.first)
|
if (!op->r.f->defined)
|
||||||
syntax_error(EMSG_UNDEF_FUNC);
|
syntax_error(EMSG_UNDEF_FUNC);
|
||||||
|
|
||||||
/* The body might be empty, still has to eval the args */
|
/* The body might be empty, still has to eval the args */
|
||||||
|
@ -44,6 +44,16 @@ testing "awk handles empty function f(arg){}" \
|
|||||||
"L1\n\nL2\n\n" \
|
"L1\n\nL2\n\n" \
|
||||||
"" ""
|
"" ""
|
||||||
|
|
||||||
|
prg='
|
||||||
|
function empty_fun(){}
|
||||||
|
END {empty_fun()
|
||||||
|
print "Ok"
|
||||||
|
}'
|
||||||
|
testing "awk handles empty function f(){}" \
|
||||||
|
"awk '$prg'" \
|
||||||
|
"Ok\n" \
|
||||||
|
"" ""
|
||||||
|
|
||||||
prg='
|
prg='
|
||||||
function outer_fun() {
|
function outer_fun() {
|
||||||
return 1
|
return 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user