From 902a81528b9d2edcf93226a2ca13da6fcc1839e5 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 23 Dec 2019 08:57:48 +0100 Subject: wip: function pointers and other stuff --- src/parser.c | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c index fb21b19..204e391 100644 --- a/src/parser.c +++ b/src/parser.c @@ -54,10 +54,6 @@ int parser_init(Parser *self, amal_compiler *compiler, ArenaAllocator *allocator return PARSER_OK; } -static bool parser_is_current_scope_file_scope(Parser *self) { - return self->current_scope == &self->struct_decl.body; -} - /* BODY_LOOP = BODY* @end_token */ @@ -410,7 +406,6 @@ static CHECK_RESULT LhsExpr* parser_parse_declaration_lhs(Parser *self) { } /* - CLOSURE = FUNC_SIGNATURE '{' BODY_LOOP '}' */ static CHECK_RESULT FunctionDecl* parser_parse_closure(Parser *self) { @@ -422,12 +417,6 @@ static CHECK_RESULT FunctionDecl* parser_parse_closure(Parser *self) { if(!signature) return NULL; - /* - TODO: Implement function declaration inside other functions. - Such functions should be moved to the file scope in the bytecode generation - */ - assert(parser_is_current_scope_file_scope(self) && "TODO: Implement function declaration inside other functions."); - throw_if_error(arena_allocator_alloc(self->allocator, sizeof(FunctionDecl), (void**)&result)); throw_if_error(funcdecl_init(result, signature, self->current_scope, self->allocator)); signature->func_decl = result; @@ -652,15 +641,17 @@ static CHECK_RESULT WhileStatement* parser_parse_while_statement(Parser *self) { return result; } +/* +NUMBER = TOK_NUMBER +*/ static CHECK_RESULT Ast* parser_parse_number(Parser *self) { Ast *result; bool match; Number *number; - result = NULL; throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_NUMBER, &match)); if(!match) - return result; + return NULL; throw_if_error(arena_allocator_alloc(self->allocator, sizeof(Number), (void**)&number)); number_init(number, &self->tokenizer.number, self->tokenizer.value.identifier); @@ -669,10 +660,29 @@ static CHECK_RESULT Ast* parser_parse_number(Parser *self) { } /* -RHS_S = STRING | NUMBER | FUNC_CALL_OR_VARIABLE +BOOL = TOK_BOOL +*/ +static CHECK_RESULT Ast* parser_parse_bool(Parser *self) { + Ast *result; + bool match; + AstBool *ast_bool; + + throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_BOOL, &match)); + if(!match) + return NULL; + + throw_if_error(arena_allocator_alloc(self->allocator, sizeof(AstBool), (void**)&ast_bool)); + ast_bool_init(ast_bool, self->tokenizer.bool_value, self->tokenizer.value.identifier); + throw_if_error(ast_create(self->allocator, ast_bool, AST_BOOL, &result)); + return result; +} + +/* +RHS_S = STRING | NUMBER | BOOL | CLOSURE | FUNC_CALL_OR_VARIABLE */ static CHECK_RESULT Ast* parser_parse_rhs_single_expr(Parser *self) { Ast *result; + FunctionDecl *func_decl; bool match; result = NULL; @@ -689,6 +699,16 @@ static CHECK_RESULT Ast* parser_parse_rhs_single_expr(Parser *self) { if(result) return result; + result = parser_parse_bool(self); + if(result) + return result; + + func_decl = parser_parse_closure(self); + if(func_decl) { + throw_if_error(ast_create(self->allocator, func_decl, AST_FUNCTION_DECL, &result)); + return result; + } + result = parser_parse_function_call_or_variable(self); if(result) return result; -- cgit v1.2.3