From d951985665bf3e0248286a30c67c28f505eb27c9 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Sun, 16 Apr 2023 03:29:02 +0200 Subject: Start using string_view on lexer and parser This change fixes the memory leak when token got created. Signed-off-by: Johnny Richard --- src/parser.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c index 4867201..593f75a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -65,11 +65,11 @@ parser_parse_type(parser_t *parser) { token_t token = expected_token(parser, TOKEN_NAME); - if (strcmp(token.value, "i32") == 0) { + if (string_view_eq(token.value, string_view_from_str("i32"))) { return TYPE_I32; } - fprintf(stderr, "[ERROR]: expected type 'i32' but got '%s'\n", token.value); + fprintf(stderr, "[ERROR]: expected type 'i32' but got '"SVFMT"'\n", SVARG(&token.value)); exit(EXIT_FAILURE); } @@ -79,9 +79,9 @@ parser_parse_return_stmt(parser_t *parser) expected_token(parser, TOKEN_OCURLY); token_t return_keyword_token = expected_token(parser, TOKEN_NAME); - if (strcmp(return_keyword_token.value, "return") != 0) { + if (!string_view_eq(return_keyword_token.value, string_view_from_str("return"))) { // TODO: Add filename:row:col prefix to expected token exceptions - fprintf(stderr, "[ERROR]: expected 'return' keyword but got '%s'\n", return_keyword_token.value); + fprintf(stderr, "[ERROR]: expected 'return' keyword but got '"SVFMT"'\n", SVARG(&return_keyword_token.value)); exit(EXIT_FAILURE); } @@ -89,8 +89,11 @@ parser_parse_return_stmt(parser_t *parser) expected_token(parser, TOKEN_SEMICOLON); expected_token(parser, TOKEN_CCURLY); + char number_as_str[number_token.value.size]; + string_view_to_str(&number_token.value, number_as_str); + return (ast_return_stmt_t) { - .number = atoi(number_token.value) + .number = atoi(number_as_str) }; } @@ -104,7 +107,6 @@ parser_parse_function(parser_t *parser) type_t return_type = parser_parse_type(parser); return (ast_function_t) { - // FIXME: alloc memory for the ast_function.name, it's not a good idea to use the token.value .name = func_name_token.value, .return_type = return_type, .body = parser_parse_return_stmt(parser) -- cgit v1.2.3