diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-07-20 09:15:59 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-07-30 00:41:56 +0200 |
commit | 78dfb08f7569904394249758b2d7a563366b60ff (patch) | |
tree | 9bc188074b77f1ea5cc03b846f9d1f4af7032de7 | |
parent | afd78c1f30ce5d32d0a8dbce72e76d7871903f2f (diff) |
compiler: require a name in function declarations
So far we allowed anonymous toplevel function expressions which makes
little sense since those can't be used for anything.
Require toplevel function declarations to be named and turn a missing
name into a compile time syntax error.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | compiler.c | 23 |
1 files changed, 19 insertions, 4 deletions
@@ -36,7 +36,7 @@ static void uc_compiler_compile_constant(uc_compiler_t *compiler); static void uc_compiler_compile_template(uc_compiler_t *compiler); static void uc_compiler_compile_comma(uc_compiler_t *compiler); static void uc_compiler_compile_labelexpr(uc_compiler_t *compiler); -static void uc_compiler_compile_function(uc_compiler_t *compiler); +static void uc_compiler_compile_funcexpr(uc_compiler_t *compiler); static void uc_compiler_compile_and(uc_compiler_t *compiler); static void uc_compiler_compile_or(uc_compiler_t *compiler); static void uc_compiler_compile_nullish(uc_compiler_t *compiler); @@ -76,7 +76,7 @@ uc_compiler_parse_rules[TK_ERROR + 1] = { [TK_TEMPLATE] = { uc_compiler_compile_template, NULL, P_NONE }, [TK_COMMA] = { NULL, uc_compiler_compile_comma, P_COMMA }, [TK_LABEL] = { uc_compiler_compile_labelexpr, NULL, P_NONE }, - [TK_FUNC] = { uc_compiler_compile_function, NULL, P_NONE }, + [TK_FUNC] = { uc_compiler_compile_funcexpr, NULL, P_NONE }, [TK_AND] = { NULL, uc_compiler_compile_and, P_AND }, [TK_OR] = { NULL, uc_compiler_compile_or, P_OR }, [TK_NULLISH] = { NULL, uc_compiler_compile_nullish, P_OR }, @@ -1537,7 +1537,7 @@ uc_compiler_compile_delimitted_block(uc_compiler_t *compiler, uc_tokentype_t end } static void -uc_compiler_compile_function(uc_compiler_t *compiler) +uc_compiler_compile_funcexpr_common(uc_compiler_t *compiler, bool require_name) { uc_compiler_t fncompiler = { 0 }; uc_value_t *name = NULL; @@ -1560,6 +1560,9 @@ uc_compiler_compile_function(uc_compiler_t *compiler) if (slot == -1) uc_compiler_initialize_local(compiler); } + else if (require_name) { + uc_compiler_syntax_error(compiler, compiler->parser->curr.pos, "Expecting function name"); + } uc_compiler_init(&fncompiler, name ? ucv_string_get(name) : NULL, @@ -1647,6 +1650,18 @@ uc_compiler_compile_function(uc_compiler_t *compiler) } static void +uc_compiler_compile_funcexpr(uc_compiler_t *compiler) +{ + return uc_compiler_compile_funcexpr_common(compiler, false); +} + +static void +uc_compiler_compile_funcdecl(uc_compiler_t *compiler) +{ + return uc_compiler_compile_funcexpr_common(compiler, true); +} + +static void uc_compiler_compile_and(uc_compiler_t *compiler) { uc_chunk_t *chunk = uc_compiler_current_chunk(compiler); @@ -2877,7 +2892,7 @@ uc_compiler_compile_statement(uc_compiler_t *compiler) else if (uc_compiler_parse_match(compiler, TK_TRY)) uc_compiler_compile_try(compiler); else if (uc_compiler_parse_match(compiler, TK_FUNC)) - uc_compiler_compile_function(compiler); + uc_compiler_compile_funcdecl(compiler); else if (uc_compiler_parse_match(compiler, TK_BREAK)) uc_compiler_compile_control(compiler); else if (uc_compiler_parse_match(compiler, TK_CONTINUE)) |