summaryrefslogtreecommitdiff
path: root/src/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c54
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);