From aba7302e7e98fd7bca2056d8ad622d9ca81c495f Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Mon, 8 May 2023 23:53:41 -0300 Subject: parser: Add the bool type This commit introduces a new type for booleans. There is no code generation for this type yet. The intention of this commit is to enable flow control in the near future. Signed-off-by: Carlos Maniero Reviewed-by: Johnny Richard --- src/ast.c | 18 ++++++++++++++++++ src/ast.h | 10 ++++++++-- src/ast_pretty_printer.c | 5 +++++ src/lexer.c | 12 ++++++++++++ src/lexer.h | 2 ++ src/parser.c | 15 +++++++++++++++ 6 files changed, 60 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/ast.c b/src/ast.c index 5938e1d..8a46034 100644 --- a/src/ast.c +++ b/src/ast.c @@ -117,6 +117,24 @@ ast_node_new_literal_integer(uint32_t number) return node; } +ast_node_t * +ast_node_new_literal_bool(bool boolean) +{ + ast_node_t *node = ast_node_new(); + + *node = (ast_node_t){ + .kind = AST_LITERAL, + .data = { + .literal = { + .kind = AST_LITERAL_BOOL, + .value = { .boolean = boolean } + }, + }, + }; + + return node; +} + ast_node_t * ast_node_new_binary_operation(ast_binary_operation_kind_t kind, ast_node_t *left, ast_node_t *right) { diff --git a/src/ast.h b/src/ast.h index 69fc918..80148dc 100644 --- a/src/ast.h +++ b/src/ast.h @@ -22,7 +22,8 @@ typedef enum { - TYPE_I32 + TYPE_I32, + TYPE_BOOL } type_t; typedef struct ast_node_t ast_node_t; @@ -67,12 +68,14 @@ typedef struct ast_binary_operation_t typedef enum { - AST_LITERAL_INTEGER + AST_LITERAL_INTEGER, + AST_LITERAL_BOOL } ast_literal_kind_t; typedef union { uint32_t integer; + bool boolean; } ast_literal_value_t; typedef struct ast_literal_t @@ -147,6 +150,9 @@ ast_node_new_variable_declaration(string_view_t variable_name, type_t type, ast_ ast_node_t * ast_node_new_literal_integer(uint32_t number); +ast_node_t * +ast_node_new_literal_bool(bool boolean); + ast_node_t * ast_node_new_variable(ast_identifier_t *identifier); diff --git a/src/ast_pretty_printer.c b/src/ast_pretty_printer.c index ec8d2de..95f5993 100644 --- a/src/ast_pretty_printer.c +++ b/src/ast_pretty_printer.c @@ -117,6 +117,11 @@ ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast) ast_pretty_printer_printf(printer, "Literal type=i32 value='%d'\n", literal.value.integer); break; } + case AST_LITERAL_BOOL: + ast_pretty_printer_set_lst_children(printer); + ast_pretty_printer_printf( + printer, "Literal type=bool value='%s'\n", literal.value.boolean ? "true" : "false"); + break; } break; } diff --git a/src/lexer.c b/src/lexer.c index 72c27cd..f84163d 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -111,6 +111,14 @@ lexer_token_process_keyword(token_t *token) token->kind = TOKEN_KEYWORD_FN; return; } + if (string_view_eq(string_view_from_str("true"), token->value)) { + token->kind = TOKEN_TRUE; + return; + } + if (string_view_eq(string_view_from_str("false"), token->value)) { + token->kind = TOKEN_FALSE; + return; + } } void @@ -454,6 +462,10 @@ token_kind_to_str(token_kind_t kind) return "fn"; case TOKEN_KEYWORD_LET: return "let"; + case TOKEN_TRUE: + return "true"; + case TOKEN_FALSE: + return "false"; case TOKEN_EOF: return "TOKEN_EOF"; case TOKEN_UNKNOWN: diff --git a/src/lexer.h b/src/lexer.h index dd442cc..899c0aa 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -59,6 +59,8 @@ typedef enum TOKEN_KEYWORD_RETURN, TOKEN_KEYWORD_FN, TOKEN_KEYWORD_LET, + TOKEN_TRUE, + TOKEN_FALSE, TOKEN_UNKNOWN } token_kind_t; diff --git a/src/parser.c b/src/parser.c index baa2ef5..22313a6 100644 --- a/src/parser.c +++ b/src/parser.c @@ -84,6 +84,11 @@ parser_parse_type(parser_t *parser, type_t *type) return true; } + if (string_view_eq(token.value, string_view_from_str("bool"))) { + *type = TYPE_BOOL; + return true; + } + parser_error_t error; error.token = token; @@ -102,6 +107,12 @@ parser_literal_integer_node(token_t *token) return ast_node_new_literal_integer(atoi(number_as_str)); } +static ast_node_t * +parser_literal_bool_node(token_t *token) +{ + return ast_node_new_literal_bool(token->kind == TOKEN_TRUE); +} + static ast_binary_operation_kind_t token_to_binary_operation_kind(token_t *token) { @@ -128,6 +139,10 @@ parser_parse_factor(parser_t *parser) switch (token.kind) { case TOKEN_NUMBER: return parser_literal_integer_node(&token); + case TOKEN_TRUE: + return parser_literal_bool_node(&token); + case TOKEN_FALSE: + return parser_literal_bool_node(&token); case TOKEN_OPAREN: { ast_node_t *expression = parser_parse_expression(parser); -- cgit v1.2.3