summaryrefslogtreecommitdiff
path: root/uscript/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'uscript/parser.c')
-rw-r--r--uscript/parser.c28
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)) {