From 35425aa5837543e4cc3fc82266dc2ae429cb2779 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Mon, 8 May 2023 23:53:42 -0300 Subject: parser: Ensure the expression types When assign a variable or returning a value it now ensures that the expression matches the expected type. To make this possible a %result_type% field was added to ast_node_t and this field is used whenever to make the comparison. Signed-off-by: Carlos Maniero Signed-off-by: Johnny Richard --- src/ast.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/ast.h') diff --git a/src/ast.h b/src/ast.h index 80148dc..f91632c 100644 --- a/src/ast.h +++ b/src/ast.h @@ -23,7 +23,8 @@ typedef enum { TYPE_I32, - TYPE_BOOL + TYPE_BOOL, + TYPE_VOID, } type_t; typedef struct ast_node_t ast_node_t; @@ -124,8 +125,12 @@ typedef struct ast_node_t { ast_node_kind_t kind; ast_node_data_t data; + type_t result_type; } ast_node_t; +char * +ast_type_to_str(type_t type); + ast_node_t * ast_node_new(void); @@ -136,7 +141,10 @@ void ast_node_destroy(ast_node_t *node); ast_node_t * -ast_node_new_binary_operation(ast_binary_operation_kind_t kind, ast_node_t *left, ast_node_t *right); +ast_node_new_binary_operation(ast_binary_operation_kind_t kind, + ast_node_t *left, + ast_node_t *right, + type_t result_type); ast_node_t * ast_node_new_function_declaration(string_view_t function_name, type_t return_type, vector_t *body); @@ -154,7 +162,7 @@ ast_node_t * ast_node_new_literal_bool(bool boolean); ast_node_t * -ast_node_new_variable(ast_identifier_t *identifier); +ast_node_new_variable(ast_identifier_t *identifier, type_t result_type); ast_node_t * ast_node_new_variable_assignment(ast_identifier_t *identifier, ast_node_t *expression); -- cgit v1.2.3