diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-03-15 18:17:50 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | 5a93c32a59775cd1be4b4f450e8230016b434366 (patch) | |
tree | b89f3f7c8b07176aa5ae783319d9e613a42db703 /src/parser.c | |
parent | d4ca9de33906972fa06bd2b7e38cbc2b4d3574c2 (diff) |
Resolve variable references
Diffstat (limited to 'src/parser.c')
-rw-r--r-- | src/parser.c | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/parser.c b/src/parser.c index 1e51af4..7c0280a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -63,7 +63,9 @@ int parser_init(Parser *self, amal_compiler *compiler, ScopedAllocator *allocato self->error.index = 0; self->error.str = NULL; self->error_context = ERROR_CONTEXT_NONE; - return scope_init(&self->scope, self->allocator); + /* TODO: When resolving ast uses mutex, add compiler->scope as the parent of the parser scope */ + self->current_scope = &self->scope; + return scope_init(&self->scope, NULL, self->allocator); } /* @@ -88,7 +90,7 @@ static THROWABLE parser_parse_body_loop(Parser *self, Scope *scope, Token end_to obj_name = ast_get_name(&body_obj); self->error = tokenizer_create_error(&self->tokenizer, tokenizer_get_code_reference_index(&self->tokenizer, obj_name.data), - "A variable with the name %.*s was declared twice in the same scope", obj_name.size, obj_name.data); + "Variable with the name %.*s was declared twice in the same scope", obj_name.size, obj_name.data); self->error_context = ERROR_CONTEXT_NONE; throw(result); } else { @@ -165,6 +167,7 @@ CLOSURE = 'fn' ('(' PARAM* ')')? '{' BODY_LOOP '}' */ static THROWABLE parser_parse_function_decl(Parser *self, FunctionDecl **func_decl) { bool match; + Scope *prev_scope; *func_decl = NULL; throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_FN, &match)); @@ -181,9 +184,13 @@ static THROWABLE parser_parse_function_decl(Parser *self, FunctionDecl **func_de } throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(FunctionDecl), (void**)func_decl)); - throw_if_error(funcdecl_init(*func_decl, self->allocator)); + throw_if_error(funcdecl_init(*func_decl, self->current_scope, self->allocator)); - try(parser_parse_body_loop(self, &(*func_decl)->body, TOK_CLOSING_BRACE)); + prev_scope = self->current_scope; + self->current_scope = &(*func_decl)->body; + try(parser_parse_body_loop(self, self->current_scope, TOK_CLOSING_BRACE)); + self->current_scope = (*func_decl)->body.parent; + assert(self->current_scope == prev_scope); return PARSER_OK; } @@ -472,6 +479,7 @@ int parser_parse_buffer(Parser *self, BufferView code_buffer, BufferView buffer_ break; } } + assert(self->current_scope == &self->scope); return result; } |