From 5a93c32a59775cd1be4b4f450e8230016b434366 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 15 Mar 2019 18:17:50 +0100 Subject: Resolve variable references --- build.sh | 1 - include/ast.h | 18 +++++---- include/compiler.h | 2 +- include/parser.h | 1 + src/ast.c | 114 +++++++++++++++++++++++++++++++++++++---------------- src/compiler.c | 22 ++++++----- src/parser.c | 16 ++++++-- src/ssa/ssa.c | 3 +- 8 files changed, 120 insertions(+), 57 deletions(-) diff --git a/build.sh b/build.sh index 5cdade0..ab43d20 100755 --- a/build.sh +++ b/build.sh @@ -14,7 +14,6 @@ CFLAGS="" LIBS="" if [ ! -z "$SANITIZE_ADDRESS" ]; then CFLAGS+="-fsanitize=address " - LIBS+="-lasan " fi CFLAGS+="-Wall -Wextra -Werror -g -O0 -DDEBUG -std=c89 -pedantic -D_GNU_SOURCE" diff --git a/include/ast.h b/include/ast.h index ae9b6b3..6077d5d 100644 --- a/include/ast.h +++ b/include/ast.h @@ -64,6 +64,12 @@ typedef struct { struct Scope { Buffer ast_objects; HashMap/*(key=BufferView, value=Ast)*/ named_objects; + Scope *parent; +}; + +struct Variable { + BufferView name; + Ast resolved_variable; }; struct FunctionDecl { @@ -72,7 +78,7 @@ struct FunctionDecl { }; struct FunctionCall { - BufferView name; + Variable func; Buffer/*Ast*/ args; }; @@ -99,11 +105,6 @@ struct Number { bool is_integer; }; -struct Variable { - BufferView name; - Ast resolved_variable; -}; - struct Binop { Ast lhs; Ast rhs; @@ -115,13 +116,14 @@ struct Binop { typedef struct { jmp_buf env; Parser *parser; + Scope *scope; } AstCompilerContext; Ast ast_none(); void ast_init(Ast *self, void *value, AstType type); BufferView ast_get_name(Ast *self); -CHECK_RESULT int funcdecl_init(FunctionDecl *self, ScopedAllocator *allocator); +CHECK_RESULT int funcdecl_init(FunctionDecl *self, Scope *parent, ScopedAllocator *allocator); CHECK_RESULT int funccall_init(FunctionCall *self, BufferView name, ScopedAllocator *allocator); void lhsexpr_init(LhsExpr *self, int isConst, BufferView var_name); void import_init(Import *self, BufferView path); @@ -130,7 +132,7 @@ void number_init(Number *self, i64 value, bool is_integer); void variable_init(Variable *self, BufferView name); void binop_init(Binop *self); -CHECK_RESULT int scope_init(Scope *self, ScopedAllocator *allocator); +CHECK_RESULT int scope_init(Scope *self, Scope *parent, ScopedAllocator *allocator); CHECK_RESULT int scope_add_child(Scope *self, Ast *child); /* longjump to compiler env on failure */ void scope_resolve(Scope *self, AstCompilerContext *context); diff --git a/include/compiler.h b/include/compiler.h index 69d0254..32a5eab 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -15,7 +15,7 @@ struct amal_compiler { ScopedAllocator allocator; ScopedAllocator main_thread_allocator; - Buffer parsers; + Buffer/**/ parsers; Buffer queued_files; ParserThreadData *threads; int usable_thread_count; diff --git a/include/parser.h b/include/parser.h index 20f02fe..84d46bd 100644 --- a/include/parser.h +++ b/include/parser.h @@ -35,6 +35,7 @@ typedef enum { struct Parser { Tokenizer tokenizer; Scope scope; + Scope *current_scope; ScopedAllocator *allocator; /* borrowed. Copied from @compiler for faster access to allocator */ amal_compiler *compiler; bool started; diff --git a/src/ast.c b/src/ast.c index cfc1369..9605b57 100644 --- a/src/ast.c +++ b/src/ast.c @@ -44,7 +44,7 @@ BufferView ast_get_name(Ast *self) { name = self->value.lhs_expr->var_name; break; case AST_FUNCTION_CALL: - name = self->value.func_call->name; + name = self->value.func_call->func.name; break; case AST_VARIABLE: name = self->value.variable->name; @@ -53,13 +53,17 @@ BufferView ast_get_name(Ast *self) { return name; } -int funcdecl_init(FunctionDecl *self, ScopedAllocator *allocator) { +static BufferView ast_get_code_reference(Ast *self) { + return ast_get_name(self); +} + +int funcdecl_init(FunctionDecl *self, Scope *parent, ScopedAllocator *allocator) { self->ssa_func_index = 0; - return scope_init(&self->body, allocator); + return scope_init(&self->body, parent, allocator); } int funccall_init(FunctionCall *self, BufferView name, ScopedAllocator *allocator) { - self->name = name; + variable_init(&self->func, name); return buffer_init(&self->args, allocator); } @@ -97,24 +101,26 @@ void binop_init(Binop *self) { self->grouped = bool_false; } -int scope_init(Scope *self, ScopedAllocator *allocator) { +int scope_init(Scope *self, Scope *parent, ScopedAllocator *allocator) { return_if_error(buffer_init(&self->ast_objects, allocator)); return_if_error(hash_map_init(&self->named_objects, allocator, sizeof(Ast), hash_compare_string, amal_hash_string)); + self->parent = parent; return 0; } int scope_add_child(Scope *self, Ast *child) { - BufferView child_name; Ast existing_child; bool child_already_exists; - child_name = ast_get_name(child); - if(child_name.data) { - child_already_exists = hash_map_get(&self->named_objects, child_name, &existing_child); + /* TODO: Implement for parameter */ + if(child->type == AST_LHS) { + BufferView var_name; + var_name = child->value.lhs_expr->var_name; + child_already_exists = hash_map_get(&self->named_objects, var_name, &existing_child); if(child_already_exists) return AST_ERR_DEF_DUP; - cleanup_if_error(hash_map_insert(&self->named_objects, child_name, child)); + cleanup_if_error(hash_map_insert(&self->named_objects, var_name, child)); } cleanup_if_error(buffer_append(&self->ast_objects, child, sizeof(Ast))); return 0; @@ -126,56 +132,99 @@ int scope_add_child(Scope *self, Ast *child) { void scope_resolve(Scope *self, AstCompilerContext *context) { Ast *ast; Ast *ast_end; + Scope *prev_scope = context->scope; ast = buffer_start(&self->ast_objects); ast_end = buffer_end(&self->ast_objects); + assert(self->parent == context->scope); + context->scope = self; for(; ast != ast_end; ++ast) { ast_resolve(ast, context); } + context->scope = self->parent; + assert(context->scope == prev_scope); +} + +static Ast scope_get_resolved_variable(Scope *self, AstCompilerContext *context, BufferView name) { + Ast result; + bool exists; + Scope *prev_scope; + + assert(self); + exists = hash_map_get(&self->named_objects, name, &result); + if(!exists) { + if(self->parent) + return scope_get_resolved_variable(self->parent, context, name); + + tokenizer_get_code_reference_index(&context->parser->tokenizer, name.data); + tokenizer_print_error(&context->parser->tokenizer, + tokenizer_get_code_reference_index(&context->parser->tokenizer, name.data), + "Undefined reference to variable \"%.*s\"", name.size, name.data); + throw(AST_ERR); + } + + /* + Need to change scope here because we are changing the visible scope + and the ast object may be in another scope than the current + resolving ast. + */ + prev_scope = context->scope; + context->scope = self; + ast_resolve(&result, context); + context->scope = prev_scope; + + assert(result.type == AST_LHS); + return result.value.lhs_expr->rhs_expr; } -static void lhs_resolve(LhsExpr *self, AstCompilerContext *context) { +static void variable_resolve(Variable *self, AstCompilerContext *context) { /* TODO: Implement */ + amal_log_debug("variable resolve, var name: %.*s", self->name.size, self->name.data); + self->resolved_variable = scope_get_resolved_variable(context->scope, context, self->name); +} + +static void lhs_resolve(Ast *self, AstCompilerContext *context) { + /* TODO: Implement */ + LhsExpr *lhs_expr; + assert(self->type == AST_LHS); + lhs_expr = self->value.lhs_expr; amal_log_debug("Lhs resolve %s name: %.*s, type: %.*s", - self->is_const ? "const" : "var", - self->var_name.size, self->var_name.data, - self->type_name.size, self->type_name.data); - ast_resolve(&self->rhs_expr, context); + lhs_expr->is_const ? "const" : "var", + lhs_expr->var_name.size, lhs_expr->var_name.data, + lhs_expr->type_name.size, lhs_expr->type_name.data); + self->resolve_status = AST_RESOLVED; + ast_resolve(&lhs_expr->rhs_expr, context); } -static void funcdecl_resolve(FunctionDecl *self, AstCompilerContext *context) { +static void funcdecl_resolve(Ast *self, AstCompilerContext *context) { /* TODO: Implement */ + FunctionDecl *func_decl; + assert(self->type == AST_FUNCTION_DECL); + func_decl = self->value.func_decl; amal_log_debug("funcdecl resolve"); - scope_resolve(&self->body, context); + self->resolve_status = AST_RESOLVED; + scope_resolve(&func_decl->body, context); } static void funccall_resolve(FunctionCall *self, AstCompilerContext *context) { /* TODO: Implement */ Ast *ast; Ast *ast_end; + + variable_resolve(&self->func, context); + ast = buffer_start(&self->args); ast_end = buffer_end(&self->args); - amal_log_debug("funccall resolve, func name: %.*s", self->name.size, self->name.data); + amal_log_debug("funccall resolve, func name: %.*s", self->func.name.size, self->func.name.data); for(; ast != ast_end; ++ast) { ast_resolve(ast, context); } } -static void variable_resolve(Variable *self, AstCompilerContext *context) { - /* TODO: Implement */ - amal_log_debug("variable resolve, var name: %.*s", self->name.size, self->name.data); - (void)context; -} - static void binop_resolve(Binop *self, AstCompilerContext *context) { /* TODO: Implement */ ast_resolve(&self->lhs, context); ast_resolve(&self->rhs, context); } -/* -static BufferView ast_get_code_reference(Ast *self) { - return ast_get_name(self); -} -*/ void ast_resolve(Ast *self, AstCompilerContext *context) { /* @@ -184,7 +233,7 @@ void ast_resolve(Ast *self, AstCompilerContext *context) { instead of the whole function declaration including the body because the body can have function call that calls functions that are resolving or even recursive function call, which should be allowed. - + */ if(self->resolve_status == AST_RESOLVED) { return; } else if(self->resolve_status == AST_RESOLVING) { @@ -194,7 +243,6 @@ void ast_resolve(Ast *self, AstCompilerContext *context) { "Found recursive dependency"); throw(AST_ERR); } - */ self->resolve_status = AST_RESOLVING; switch(self->type) { @@ -203,13 +251,13 @@ void ast_resolve(Ast *self, AstCompilerContext *context) { /* Nothing to resolve for numbers */ break; case AST_FUNCTION_DECL: - funcdecl_resolve(self->value.func_decl, context); + funcdecl_resolve(self, context); break; case AST_FUNCTION_CALL: funccall_resolve(self->value.func_call, context); break; case AST_LHS: - lhs_resolve(self->value.lhs_expr, context); + lhs_resolve(self, context); break; case AST_IMPORT: /* TODO: When @import(...).data syntax is added, implement the resolve for it */ diff --git a/src/compiler.c b/src/compiler.c index 4e097ce..c56221b 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -104,13 +104,13 @@ typedef struct { } ThreadWorkData; static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *self, BufferView filepath, ScopedAllocator *allocator) { - Parser parser; + Parser *parser; int result; result = AMAL_COMPILER_ERR; - am_memset(&parser, 0, sizeof(parser)); - return_if_error(parser_init(&parser, self, allocator)); - cleanup_if_error(parser_parse_file(&parser, filepath)); + return_if_error(scoped_allocator_alloc(&self->allocator, sizeof(Parser), (void**)&parser)); + return_if_error(parser_init(parser, self, allocator)); + return_if_error(parser_parse_file(parser, filepath)); cleanup_if_error(amal_mutex_lock(&self->mutex, "amal_compiler_load_in_this_thread")); cleanup_if_error(buffer_append(&self->parsers, &parser, sizeof(parser))); result = AMAL_COMPILER_OK; @@ -154,11 +154,15 @@ static CHECK_RESULT int thread_resolve_ast(Parser *parser) { AstCompilerContext compiler_context; int result; compiler_context.parser = parser; + compiler_context.scope = NULL; result = setjmp(compiler_context.env); + assert(!parser->scope.parent); if(result == 0) { amal_log_debug("Resolving AST for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data); scope_resolve(&parser->scope, &compiler_context); } + if(result == 0) + assert(!compiler_context.scope); return result; } @@ -201,10 +205,10 @@ static void* thread_callback_generic(void *userdata) { break; } cleanup_if_error(amal_mutex_lock(&compiler_userdata.compiler->mutex, "thread_callback_generic")); - if(compiler_userdata.compiler->generic_work_object_index + 1 >= (int)buffer_get_size(&compiler_userdata.compiler->parsers, Parser)) + if(compiler_userdata.compiler->generic_work_object_index + 1 >= (int)buffer_get_size(&compiler_userdata.compiler->parsers, Parser*)) break; ++compiler_userdata.compiler->generic_work_object_index; - parser = buffer_get(&compiler_userdata.compiler->parsers, compiler_userdata.compiler->generic_work_object_index, sizeof(Parser)); + parser = *(Parser**)buffer_get(&compiler_userdata.compiler->parsers, compiler_userdata.compiler->generic_work_object_index, sizeof(parser)); amal_mutex_tryunlock(&compiler_userdata.compiler->mutex); } result = NULL; @@ -333,8 +337,8 @@ static CHECK_RESULT int amal_compiler_load_file_join_threads(amal_compiler *self } static CHECK_RESULT int amal_compiler_dispatch_generic(amal_compiler *self, ThreadWorkType work_type) { - Parser *parser; - Parser *parser_end; + Parser **parser; + Parser **parser_end; parser = buffer_start(&self->parsers); parser_end = buffer_end(&self->parsers); self->generic_work_object_index = 0; @@ -342,7 +346,7 @@ static CHECK_RESULT int amal_compiler_dispatch_generic(amal_compiler *self, Thre ParserThreadData *thread_selected; ThreadWorkData thread_work_data; thread_work_data.type = work_type; - thread_work_data.value.parser = parser; + thread_work_data.value.parser = *parser; return_if_error(amal_compiler_select_thread_for_work(self, thread_work_data, &thread_selected)); /* After all threads have been used, they will handle using the remaining parsers or stop if there is an error */ if(!thread_selected) 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; } diff --git a/src/ssa/ssa.c b/src/ssa/ssa.c index b8aa8b0..f892531 100644 --- a/src/ssa/ssa.c +++ b/src/ssa/ssa.c @@ -184,8 +184,9 @@ int ssa_ins_func_start(Ssa *self, u8 num_args, SsaFuncIndex *result) { } int ssa_ins_func_end(Ssa *self) { - SsaInstructionType ins; + u8 ins; ins = SSA_FUNC_END; + amal_log_debug("FUNC_END"); return buffer_append(&self->instructions, &ins, 1); } -- cgit v1.2.3