diff options
| author | iamcheeseman <[email protected]> | 2026-04-16 16:14:30 -0400 |
|---|---|---|
| committer | iamcheeseman <[email protected]> | 2026-04-16 16:14:30 -0400 |
| commit | 65737b5a37b9b3a2135012d05c8c37a9c4050705 (patch) | |
| tree | 35abfd3af775d11617ab23457ac0e6fd5c31d3e6 /uscript/parser.c | |
| parent | eb0e4255e9eb878b7a17830b5be06a71add3b34e (diff) | |
microscript: global functions
Diffstat (limited to 'uscript/parser.c')
| -rw-r--r-- | uscript/parser.c | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/uscript/parser.c b/uscript/parser.c index d064c6c..295f496 100644 --- a/uscript/parser.c +++ b/uscript/parser.c @@ -684,7 +684,7 @@ void var_def_stat(struct parser *p, bool is_global) } static -void fun_stat(struct parser *p) +void fun_stat(struct parser *p, bool is_global) { expect(p, TOKEN_IDENT, "expected function name"); struct token name_token = p->prev; @@ -692,8 +692,15 @@ void fun_stat(struct parser *p) struct us_str *name = copy_str(name_token.start, name_token.len); struct us_proto *proto = create_proto(name); + int global_idx = -1; + // parser_add_const(p, wrap_func(func)); - declare_variable(p, name_token); + if (is_global) { + struct us_str *ident = copy_str(name_token.start, name_token.len); + global_idx = declare_global(ident); + } else { + declare_variable(p, name_token); + } begin_function(p, proto); @@ -713,6 +720,13 @@ void fun_stat(struct parser *p) end_function(p); expect(p, TOKEN_END, "unterminated function"); + + if (global_idx != -1) { + parser_add_byte(p, BC_SET_GLOBAL); + parser_add_byte(p, (global_idx >> 8) & 0xFF); + parser_add_byte(p, global_idx & 0xFF); + parser_add_byte(p, BC_POP); // set global does not pop + } } static @@ -961,10 +975,14 @@ void stat(struct parser *p) "can only define globals in the outermost scope" ); } - var_def_stat(p, true); - consume(p, ';'); + if (consume(p, TOKEN_FUN)) { + fun_stat(p, true); + } else { + var_def_stat(p, true); + consume(p, ';'); + } } else if (consume(p, TOKEN_FUN)) { - fun_stat(p); + fun_stat(p, false); } else if (consume(p, TOKEN_IF)) { if_stat(p, false); } else if (consume(p, TOKEN_LOOP)) { |
