From 75639fbf01bd6ae1212521b6cf822025eb8b598d Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Wed, 10 May 2023 16:07:39 -0300 Subject: namespaces: Add a namespace structure that represents a file We have been always parsing a single function. Since we want to have multiple functions in a near future, this patch introduces an namespace that represents an entire file. To ensure a function is defined inside a namespace, a helper function was created. Today our ast_node structure is highly exposed, and this is something that Johnny and I have been discussed. So then, this is a first step to try to protected the code generation from our ast tree. Signed-off-by: Carlos Maniero --- src/parser.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/parser.h') diff --git a/src/parser.h b/src/parser.h index ef5dff5..7bebee7 100644 --- a/src/parser.h +++ b/src/parser.h @@ -46,4 +46,7 @@ parser_parse_function_declaration(parser_t *parser); ast_node_t * parser_parse_expression(parser_t *parser); +ast_node_t * +parser_parse_ns(parser_t *parser); + #endif /* PARSER_H */ -- cgit v1.2.3 From ea7f65fe1250be8f49edcaaedd3410aed1401648 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Thu, 11 May 2023 15:00:10 -0300 Subject: gas: implement recursion and late evaluation Until now the below code was not valid for pipac. fn main(): i32 { return fib(13); } fn fib(n: i32): i32 { if n <= 1 { return n; } return fib(n - 1) + fib(n - 2); } Pipa's parser was adding a function to scope after they were fully parsed which means that fib's self-reference would not work. Also, functions were required to follow the be called in the order they are declared for the same scope reason so, the main function was required to be defined after fib. And how it is working now? When a TOKEN_NAME is not found in the scope, instead of returning an error, an unknown token is created as placeholder. The parser stores the node reference and the token it was trying to parse. During type checks, if the parser detects an unknown node, instead of returning an error, it stores in that node what was the expected type. After the NS is fully parsed a reevaluation is made on those unknown nodes by setting the lexer back on the node's token position and parsing the TOKEN_NAME again. Ps: There is a typo on the unknown token. It will be addressed in another commit since this issue was not introduced by this change. Signed-off-by: Carlos Maniero --- src/parser.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/parser.h') diff --git a/src/parser.h b/src/parser.h index 7bebee7..cfeb1f0 100644 --- a/src/parser.h +++ b/src/parser.h @@ -28,10 +28,17 @@ typedef struct parser_error_t char message[256]; } parser_error_t; +typedef struct reevaluation_node_t +{ + ast_node_t *node; + token_t token; +} reevaluation_node_t; + typedef struct parser_t { lexer_t *lexer; scope_t *scope; + vector_t *to_be_reevaluated; int errors_len; // FIXME: replace with vector parser_error_t errors[1]; -- cgit v1.2.3