summaryrefslogtreecommitdiff
path: root/src/parser.c
diff options
context:
space:
mode:
authorCarlos Maniero <carlosmaniero@gmail.com>2023-04-26 01:57:40 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-04-26 09:11:11 +0200
commitb8e76aa1e888a4602d4220206bbea8d04882fae2 (patch)
tree03c5e03377edec588066b0d7d7ad9d1a80092b08 /src/parser.c
parent127dae1c3b48c3a4dceddb9000309bfeaa8d0a01 (diff)
lexer: Split operation tokens into their own token
The +, -, *, and / tokens used to be TOKEN_OP, but the TOKEN_OP has been removed and a token for each operation has been introduced. Python's token names were followed: https://docs.python.org/3/library/token.html Signed-off-by: Carlos Maniero <carlosmaniero@gmail.com> Reviewed-by: Johnny Richard <johnny@johnnyrichar.com>
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c18
1 files changed, 3 insertions, 15 deletions
diff --git a/src/parser.c b/src/parser.c
index f3c6328..fd836af 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -138,8 +138,7 @@ parser_parse_term(parser_t *parser, ast_node_t *node)
token_t token;
lexer_next_token(parser->lexer, &token);
- while (token.kind == TOKEN_OP && (string_view_eq(token.value, string_view_from_str("*")) ||
- string_view_eq(token.value, string_view_from_str("/")))) {
+ while (token.kind == TOKEN_STAR || token.kind == TOKEN_SLASH) {
ast_node_t *left = ast_node_new();
*left = *node;
@@ -173,8 +172,7 @@ parser_parse_expression(parser_t *parser, ast_node_t *node)
token_t token;
lexer_next_token(parser->lexer, &token);
- while (token.kind == TOKEN_OP && (string_view_eq(token.value, string_view_from_str("+")) ||
- string_view_eq(token.value, string_view_from_str("-")))) {
+ while (token.kind == TOKEN_PLUS || token.kind == TOKEN_MINUS) {
ast_node_t *left = ast_node_new();
*left = *node;
@@ -220,19 +218,9 @@ parser_parse_variable_definition(parser_t *parser, string_view_t variable_name,
token_t equal_token;
- if (!expected_token(&equal_token, parser, TOKEN_OP))
+ if (!expected_token(&equal_token, parser, TOKEN_EQUAL))
return false;
- if (!string_view_eq(equal_token.value, string_view_from_str("="))) {
- parser_error_t error;
- error.token = equal_token;
-
- sprintf(error.message, "expected '=' but got " SVFMT, SVARG(&equal_token.value));
-
- parser->errors[parser->errors_len++] = error;
- return false;
- }
-
ast_node_t *expression = ast_node_new();
if (!parser_parse_expression(parser, expression) || !drop_expected_token(parser, TOKEN_SEMICOLON)) {