summaryrefslogtreecommitdiff
path: root/src/parser.c
diff options
context:
space:
mode:
authorCarlos Maniero <carlos@maniero.me>2023-05-02 23:45:49 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-05-03 22:38:25 +0200
commit77dbf3a5011566b4a6274b3cdfa075dd723642d3 (patch)
tree8bd9022499ab1d403ca3c59bbd05b09bd44ccbbd /src/parser.c
parent57cee82f9136f0a0246d3b1d3191226bd4234d43 (diff)
Parser: Make the parser function return the ast_node
In many situations, the parser is responsible for reserving memory for nodes, particularly during function body parsing. This commit introduces a new standard where parser functions not only allocate memory for ast_nodes, but also return them. In case of a parser error, a NULL pointer is returned. This standard will be extended to other parsers in future commits, ensuring consistency throughout the codebase. Signed-off-by: Carlos Maniero <carlos@maniero.me>
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c78
1 files changed, 50 insertions, 28 deletions
diff --git a/src/parser.c b/src/parser.c
index ad0fdc9..ca5600d 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -296,19 +296,26 @@ parser_parse_variable_definition(parser_t *parser, string_view_t variable_name,
return true;
}
-static bool
-parser_parse_block_declarations(parser_t *parser, vector_t *body)
+static vector_t *
+parser_parse_block_declarations(parser_t *parser)
{
+ if (!drop_expected_token(parser, TOKEN_OCURLY)) {
+ return NULL;
+ }
+
token_t current_token;
lexer_next_token(parser->lexer, &current_token);
scope_enter(parser->scope);
+ vector_t *body = vector_new();
+
while (current_token.kind != TOKEN_CCURLY && current_token.kind != TOKEN_EOF) {
if (current_token.kind != TOKEN_NAME) {
parser_error_push_unexpected_kind(parser, &current_token, TOKEN_NAME);
scope_leave(parser->scope);
- return false;
+ vector_destroy(body);
+ return NULL;
}
if (string_view_eq(current_token.value, string_view_from_str("return"))) {
@@ -318,7 +325,8 @@ parser_parse_block_declarations(parser_t *parser, vector_t *body)
if (!parsed_return) {
ast_node_destroy(return_node);
scope_leave(parser->scope);
- return false;
+ vector_destroy(body);
+ return NULL;
}
vector_push_back(body, return_node);
@@ -332,7 +340,8 @@ parser_parse_block_declarations(parser_t *parser, vector_t *body)
if (!parser_parse_variable_definition(parser, current_token.value, variable_node)) {
ast_node_destroy(variable_node);
- return false;
+ vector_destroy(body);
+ return NULL;
}
vector_push_back(body, variable_node);
@@ -343,7 +352,8 @@ parser_parse_block_declarations(parser_t *parser, vector_t *body)
if (!parser_parse_variable_assignment(parser, current_token, variable_assignment)) {
ast_node_destroy(variable_assignment);
- return false;
+ vector_destroy(body);
+ return NULL;
}
vector_push_back(body, variable_assignment);
@@ -369,10 +379,11 @@ parser_parse_block_declarations(parser_t *parser, vector_t *body)
lexer_next_token(parser->lexer, &current_token);
}
- if (current_token.kind != TOKEN_CCURLY) {
+ if (current_token.kind == TOKEN_EOF) {
parser_error_push_unexpected_kind(parser, &current_token, TOKEN_CCURLY);
scope_leave(parser->scope);
- return false;
+ vector_destroy(body);
+ return NULL;
}
ast_node_t *latest_node = vector_at(body, body->size - 1);
@@ -385,44 +396,55 @@ parser_parse_block_declarations(parser_t *parser, vector_t *body)
parser->errors[parser->errors_len++] = error;
scope_leave(parser->scope);
- return false;
+ vector_destroy(body);
+ return NULL;
}
+
scope_leave(parser->scope);
- return true;
+ return body;
}
-bool
-parser_parse_function_declaration(parser_t *parser, ast_node_t *node)
+static bool
+parser_parse_function_arguments(parser_t *parser)
+{
+ return drop_expected_token(parser, TOKEN_OPAREN) && drop_expected_token(parser, TOKEN_CPAREN);
+}
+
+ast_node_t *
+parser_parse_function_declaration(parser_t *parser)
{
token_t func_name_token;
if (!expected_token(&func_name_token, parser, TOKEN_NAME)) {
- return false;
+ return NULL;
}
- if (!drop_expected_token(parser, TOKEN_OPAREN))
- return false;
- if (!drop_expected_token(parser, TOKEN_CPAREN))
- return false;
- if (!drop_expected_token(parser, TOKEN_COLON))
- return false;
+ if (!parser_parse_function_arguments(parser)) {
+ return NULL;
+ }
+
+ if (!drop_expected_token(parser, TOKEN_COLON)) {
+ return NULL;
+ }
type_t return_type;
if (!parser_parse_type(parser, &return_type)) {
- return false;
+ return NULL;
}
- if (!drop_expected_token(parser, TOKEN_OCURLY))
- return false;
+ vector_t *body = parser_parse_block_declarations(parser);
- vector_t *body = vector_new();
+ if (body == NULL) {
+ return NULL;
+ }
+
+ ast_node_t *node = ast_node_new();
ast_node_init_function_declaration(node, func_name_token.value, return_type, body);
- scope_push(parser->scope, &node->data.function.identifier, node);
- if (!parser_parse_block_declarations(parser, body)) {
- return false;
- }
+ // TODO: When implementing function calls the scope must be pushed before the
+ // body to be parsed, otherwise recursion not gonna work.
+ scope_push(parser->scope, &node->data.function.identifier, node);
- return true;
+ return node;
}