From 5a93c32a59775cd1be4b4f450e8230016b434366 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 15 Mar 2019 18:17:50 +0100 Subject: Resolve variable references --- src/parser.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/parser.c') 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; } -- cgit v1.2.3