From 12e05d464eb05202b00e65553d920984cb86123d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 9 Mar 2019 18:10:09 +0100 Subject: Use ast_init --- src/parser.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) (limited to 'src/parser.c') 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