From fe9971d379766a4f7d4576495caff9ffaa80cfef Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 26 Mar 2019 20:54:54 +0100 Subject: Synchronize funccall generate. Uses correct func index now --- src/parser.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c index 7b69ee7..bac7b3a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -59,16 +59,14 @@ int parser_init(Parser *self, amal_compiler *compiler, ScopedAllocator *allocato self->error.str = NULL; self->error_context = ERROR_CONTEXT_NONE; return_if_error(structdecl_init(&self->struct_decl, &compiler->root_scope, allocator)); + self->struct_decl.body.parser = self; return_if_error(lhsexpr_init(&self->file_decl, bool_true, bool_true, create_buffer_view_null(), self->allocator)); return_if_error(ast_create(self->allocator, &self->struct_decl, AST_STRUCT_DECL, &self->file_decl.rhs_expr)); self->current_scope = &self->struct_decl.body; + self->has_func_parent = bool_false; return PARSER_OK; } -static bool parser_is_global_scope(Parser *self) { - return self->current_scope == &self->struct_decl.body; -} - /* BODY_LOOP = BODY* @end_token */ @@ -159,10 +157,10 @@ static CHECK_RESULT LhsExpr* parser_parse_lhs(Parser *self, bool *assignment_or_ *assignment_or_rhs = bool_true; throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_PUB, &is_pub)); - if(is_pub && !parser_is_global_scope(self)) { + if(is_pub && self->has_func_parent) { self->error = tokenizer_create_error(&self->tokenizer, tokenizer_get_code_reference_index(&self->tokenizer, self->tokenizer.value.identifier.data), - "Only declarations in global scope can be public"); + "Only declarations in structs can be public"); throw(PARSER_UNEXPECTED_TOKEN); } @@ -213,11 +211,12 @@ static CHECK_RESULT LhsExpr* parser_parse_lhs(Parser *self, bool *assignment_or_ /* TODO: Implement params and return types -CLOSURE = 'fn' ('(' PARAM* ')')? '{' BODY_LOOP '}' +CLOSURE = 'fn' ('(' PARAM? (',' PARAM)* ')')? '{' BODY_LOOP '}' */ static CHECK_RESULT FunctionDecl* parser_parse_closure(Parser *self) { FunctionDecl *result; bool match; + bool prev_has_func_parent; result = NULL; throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_FN, &match)); @@ -237,8 +236,11 @@ static CHECK_RESULT FunctionDecl* parser_parse_closure(Parser *self) { throw_if_error(funcdecl_init(result, self->current_scope, self->allocator)); self->current_scope = &result->body; + prev_has_func_parent = self->has_func_parent; + self->has_func_parent = bool_true; parser_parse_body_loop(self, self->current_scope, TOK_CLOSING_BRACE); self->current_scope = result->body.parent; + self->has_func_parent = prev_has_func_parent; return result; } -- cgit v1.2.3