diff options
author | Carlos Maniero <carlos@maniero.me> | 2023-05-09 16:18:18 -0300 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2023-05-09 22:54:46 +0200 |
commit | ad54ee1182b1549880eddc8b1969d3992d9f7f1d (patch) | |
tree | 34d6a33a6a5ccba3ff6198d0e49f1e1d4701fc27 /src/parser.c | |
parent | 8c8fc8cc30b38ef00d606a4991b655df97a52fb6 (diff) |
parser: parses an if statement no code generation
This commit parses a if statement following the grammar bellow:
if boolean_expression {
n_epressions;
}
No else neither code generation was implemented.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
Diffstat (limited to 'src/parser.c')
-rw-r--r-- | src/parser.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/parser.c b/src/parser.c index 848bb4a..9167f1e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -30,6 +30,9 @@ static bool parser_expression_matches_the_expected_type(parser_t *parser, ast_node_t *expression, type_t type, token_t token); +static ast_node_t * +parser_parse_block_declarations(parser_t *parser, type_t result_type); + void parser_init(parser_t *parser, lexer_t *lexer, scope_t *scope) { @@ -463,6 +466,35 @@ parser_parse_variable_declaration(parser_t *parser) return node; } +static ast_node_t * +parser_parse_if_stmt(parser_t *parser, type_t result_type) +{ + token_t if_token; + if (!expected_token(&if_token, parser, TOKEN_KEYWORD_IF)) { + return NULL; + } + + ast_node_t *condition = parser_parse_expression(parser); + + if (condition == NULL) { + return NULL; + } + + if (!parser_expression_matches_the_expected_type(parser, condition, TYPE_BOOL, if_token)) { + ast_node_destroy(condition); + return NULL; + } + + ast_node_t *body = parser_parse_block_declarations(parser, result_type); + + if (body == NULL) { + ast_node_destroy(condition); + return NULL; + } + + return ast_node_new_if_stmt(condition, body); +} + static bool is_next_statement_a_variable_declaration(parser_t *parser) { @@ -488,6 +520,15 @@ is_next_statement_a_variable_assignement(parser_t *parser) } static bool +is_next_statement_a_if_stmt(parser_t *parser) +{ + token_t token; + lexer_peek_next_token(parser->lexer, &token); + + return token.kind == TOKEN_KEYWORD_IF; +} + +static bool is_next_statement_return(parser_t *parser) { token_t token; @@ -563,6 +604,19 @@ parser_parse_block_declarations(parser_t *parser, type_t result_type) continue; } + if (is_next_statement_a_if_stmt(parser)) { + ast_node_t *if_stmt = parser_parse_if_stmt(parser, result_type); + + if (if_stmt == NULL) { + scope_leave(parser->scope); + ast_node_destroy_vector(body); + return NULL; + } + + vector_push_back(body, if_stmt); + continue; + } + if (is_next_statement_a_variable_declaration(parser)) { ast_node_t *variable_node = parser_parse_variable_declaration(parser); |