diff options
Diffstat (limited to 'src/ast.c')
-rw-r--r-- | src/ast.c | 39 |
1 files changed, 35 insertions, 4 deletions
@@ -82,12 +82,17 @@ void structfield_init(StructField *self, BufferView name, BufferView type_name) variable_init(&self->type, type_name); } -void lhsexpr_init(LhsExpr *self, bool is_pub, bool is_const, BufferView var_name) { +int lhsexpr_init(LhsExpr *self, bool is_pub, bool is_const, BufferView var_name, ScopedAllocator *allocator) { self->is_pub = is_pub; self->is_const = is_const; variable_init(&self->type, create_buffer_view_null()); self->var_name = var_name; self->rhs_expr = ast_none(); + if(is_pub && allocator) + return_if_error(scoped_allocator_create_mutex(allocator, &self->mutex)); + else + self->mutex = NULL; + return 0; } void import_init(Import *self, BufferView path) { @@ -208,7 +213,7 @@ static void variable_resolve(Variable *self, AstCompilerContext *context, LhsExp *resolved_type = scope_get_resolved_variable(context->scope, context, self->name); } -static void lhs_resolve(Ast *self, AstCompilerContext *context) { +static void lhsexpr_resolve(Ast *self, AstCompilerContext *context) { LhsExpr *lhs_expr; lhs_expr = self->value.lhs_expr; @@ -228,6 +233,22 @@ static void lhs_resolve(Ast *self, AstCompilerContext *context) { self->resolved_type = lhs_expr->rhs_expr.resolved_type; } +/* LhsExpr has to be resolved before this is called */ +static Scope* lhsexpr_get_scope(LhsExpr *self) { + switch(self->rhs_expr.type) { + case AST_FUNCTION_DECL: + return &self->rhs_expr.value.func_decl->body; + case AST_STRUCT_DECL: + return &self->rhs_expr.value.struct_decl->body; + case AST_IMPORT: + return self->rhs_expr.value.import->file_scope->scope; + default: + break; + } + assert(bool_false && "Expected lhsexpr_get_scope to only be called for function decl and struct decl"); + return NULL; +} + static void import_resolve(Ast *self, AstCompilerContext *context) { Import *import; import = self->value.import; @@ -278,7 +299,17 @@ static void structfield_resolve(Ast *self, AstCompilerContext *context) { static void binop_resolve(Binop *self, AstCompilerContext *context) { /* TODO: Implement */ ast_resolve(&self->lhs, context); - ast_resolve(&self->rhs, context); + if(self->rhs.type == AST_VARIABLE) { + Scope *prev_scope; + assert(self->lhs.resolved_type); + prev_scope = context->scope; + context->scope = lhsexpr_get_scope(self->lhs.resolved_type); + assert(context->scope); + ast_resolve(&self->rhs, context); + context->scope = prev_scope; + } else { + ast_resolve(&self->rhs, context); + } } void ast_resolve(Ast *self, AstCompilerContext *context) { @@ -317,7 +348,7 @@ void ast_resolve(Ast *self, AstCompilerContext *context) { structfield_resolve(self, context); break; case AST_LHS: - lhs_resolve(self, context); + lhsexpr_resolve(self, context); break; case AST_IMPORT: /* TODO: When @import(...).data syntax is added, implement the resolve for it */ |