From 12e05d464eb05202b00e65553d920984cb86123d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 9 Mar 2019 18:10:09 +0100 Subject: Use ast_init --- include/ast.h | 3 ++- src/ast.c | 8 +++++--- src/parser.c | 37 +++++++++++++++---------------------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/include/ast.h b/include/ast.h index f0020d7..3eb7bb6 100644 --- a/include/ast.h +++ b/include/ast.h @@ -26,6 +26,7 @@ typedef struct Binop Binop; typedef struct Scope Scope; typedef union { + void *data; FunctionDecl *func_decl; FunctionCall *func_call; LhsExpr *lhs_expr; @@ -116,7 +117,7 @@ typedef struct { } AstCompilerContext; Ast ast_none(); -void ast_init(Ast *self, AstValue value, AstType type); +void ast_init(Ast *self, void *value, AstType type); BufferView ast_get_name(Ast *self); CHECK_RESULT int funcdecl_init(FunctionDecl *self, ScopedAllocator *allocator); diff --git a/src/ast.c b/src/ast.c index 5f56419..079d45a 100644 --- a/src/ast.c +++ b/src/ast.c @@ -19,11 +19,12 @@ Ast ast_none() { Ast ast; ast.value.func_decl = NULL; ast.type = AST_NONE; + ast.resolve_status = AST_NOT_RESOLVED; return ast; } -void ast_init(Ast *self, AstValue value, AstType type) { - self->value = value; +void ast_init(Ast *self, void *value, AstType type) { + self->value.data = value; self->type = type; self->resolve_status = AST_NOT_RESOLVED; } @@ -169,10 +170,11 @@ static void binop_resolve(Binop *self, AstCompilerContext *context) { 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) { /* diff --git a/src/parser.c b/src/parser.c index b91e15a..22c8b77 100644 --- a/src/parser.c +++ b/src/parser.c @@ -232,15 +232,13 @@ static THROWABLE parser_parse_function_call_or_variable(Parser *self, Ast *expr) Variable *variable; throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(Variable), (void**)&variable)); variable_init(variable, identifier); - expr->type = AST_VARIABLE; - expr->value.variable = variable; + ast_init(expr, variable, AST_VARIABLE); return PARSER_OK; } throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(FunctionCall), (void**)&func_call)); throw_if_error(funccall_init(func_call, self->tokenizer.value.identifier, self->allocator)); - expr->type = AST_FUNCTION_CALL; - expr->value.func_call = func_call; + ast_init(expr, func_call, AST_FUNCTION_CALL); try(parser_parse_function_args(self, func_call)); return PARSER_OK; } @@ -271,8 +269,7 @@ static THROWABLE parser_parse_number(Parser *self, Ast *rhs_expr) { throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(Number), (void**)&number)); number_init(number, self->tokenizer.value.integer, self->tokenizer.number_is_integer); - rhs_expr->type = AST_NUMBER; - rhs_expr->value.number = number; + ast_init(rhs_expr, number, AST_NUMBER); return PARSER_OK; } @@ -288,8 +285,7 @@ static THROWABLE parser_parse_rhs_single_expr(Parser *self, Ast *rhs_expr) { String *string; throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(String), (void**)&string)); throw_if_error(string_init(string, self->tokenizer.value.string)); - rhs_expr->type = AST_STRING; - rhs_expr->value.string = string; + ast_init(rhs_expr, string, AST_STRING); return PARSER_OK; } @@ -338,6 +334,7 @@ int parser_parse_rhs_binop(Parser *self, Ast *expr) { Ast lhs; Ast rhs; BinopType binop_type; + Binop *binop; try(parser_parse_rhs_binop_opt_paren(self, &lhs)); throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_BINOP, &match)); @@ -348,12 +345,12 @@ int parser_parse_rhs_binop(Parser *self, Ast *expr) { binop_type = self->tokenizer.value.binop_type; try(parser_parse_rhs_binop(self, &rhs)); - throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(Binop), (void**)&expr->value.binop)); - binop_init(expr->value.binop); - expr->value.binop->type = binop_type; - expr->value.binop->lhs = lhs; - expr->value.binop->rhs = rhs; - expr->type = AST_BINOP; + throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(Binop), (void**)&binop)); + binop_init(binop); + binop->type = binop_type; + binop->lhs = lhs; + binop->rhs = rhs; + ast_init(expr, binop, AST_BINOP); return PARSER_OK; } @@ -380,15 +377,13 @@ int parser_parse_rhs_start(Parser *self, Ast *rhs_expr) { try(parser_parse_function_decl(self, &func_decl)); if(func_decl) { - rhs_expr->type = AST_FUNCTION_DECL; - rhs_expr->value.func_decl = func_decl; + ast_init(rhs_expr, func_decl, AST_FUNCTION_DECL); return PARSER_OK; } try(parser_parse_import(self, &import)); if(import) { - rhs_expr->type = AST_IMPORT; - rhs_expr->value.import = import; + ast_init(rhs_expr, import, AST_IMPORT); try(parser_queue_file(self, import->path)); return PARSER_OK; } @@ -438,16 +433,14 @@ int parser_parse_body(Parser *self, Ast *ast) { try(parser_parse_lhs(self, &lhs_expr, &assignment)); if(!assignment) { - ast->type = AST_LHS; - ast->value.lhs_expr = lhs_expr; + ast_init(ast, lhs_expr, AST_LHS); return PARSER_OK; } try(parser_parse_rhs_start(self, &rhs_expr)); if(lhs_expr) { lhs_expr->rhs_expr = rhs_expr; - ast->type = AST_LHS; - ast->value.lhs_expr = lhs_expr; + ast_init(ast, lhs_expr, AST_LHS); } else { *ast = rhs_expr; } -- cgit v1.2.3