summaryrefslogtreecommitdiff
path: root/src/parser.c
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2023-04-16 03:29:02 +0200
committerJohnny Richard <johnny@johnnyrichard.com>2023-04-16 03:29:02 +0200
commitd951985665bf3e0248286a30c67c28f505eb27c9 (patch)
treed8c77d73366d8a3eeb94f11163edc23201fff5c0 /src/parser.c
parentd55938b34d6b7ee2c2d7da8483aaed5c8b9078a0 (diff)
Start using string_view on lexer and parser
This change fixes the memory leak when token got created. Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c14
1 files changed, 8 insertions, 6 deletions
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)