diff options
-rw-r--r-- | editors/awk.c | 11 | ||||
-rwxr-xr-x | testsuite/awk.tests | 10 |
2 files changed, 17 insertions, 4 deletions
diff --git a/editors/awk.c b/editors/awk.c index 1115085..c05d5d6 100644 --- a/editors/awk.c +++ b/editors/awk.c @@ -139,6 +139,7 @@ typedef struct chain_s { /* Function */ typedef struct func_s { unsigned nargs; + smallint defined; struct chain_s body; } func; @@ -1662,9 +1663,11 @@ static void parse_program(char *p) debug_printf_parse("%s: TC_FUNCDECL\n", __func__); next_token(TC_FUNCTION); f = newfunc(t_string); -//FIXME: dup check: functions can't be redefined, this is not ok: awk 'func f(){}; func f(){}' - f->body.first = NULL; - f->nargs = 0; + if (f->defined) + syntax_error("Duplicate function"); + 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 */ for (;;) { 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"); - if (op->r.f->nargs == 0 && !op->r.f->body.first) + if (!op->r.f->defined) syntax_error(EMSG_UNDEF_FUNC); /* The body might be empty, still has to eval the args */ diff --git a/testsuite/awk.tests b/testsuite/awk.tests index 6e35d33..873cc36 100755 --- a/testsuite/awk.tests +++ b/testsuite/awk.tests @@ -45,6 +45,16 @@ testing "awk handles empty function f(arg){}" \ "" "" prg=' +function empty_fun(){} +END {empty_fun() + print "Ok" +}' +testing "awk handles empty function f(){}" \ + "awk '$prg'" \ + "Ok\n" \ + "" "" + +prg=' function outer_fun() { return 1 } |