fixed operator precedence issue

This commit is contained in:
Zander Thannhauser 2025-08-03 23:13:33 -05:00
parent dbd3ff2acd
commit 8e5675a53f

97
main.c
View file

@ -766,7 +766,9 @@ struct expression* parse(const char* text)
{
struct expression* parse_comparision(void)
{
struct expression* parse_arithmetic(void)
struct expression* parse_additive(void)
{
struct expression* parse_multiplicative(void)
{
struct expression* parse_prefix(void)
{
@ -896,42 +898,6 @@ struct expression* parse(const char* text)
again: switch (tokenkind)
{
case tk_plus:
{
next_token();
struct expression* right =
parse_prefix();
struct expression* retval =
new_binary_expression(ek_add, left, right);
free_expression(left);
free_expression(right);
left = retval;
goto again;
}
case tk_minus:
{
next_token();
struct expression* right =
parse_prefix();
struct expression* retval =
new_binary_expression(ek_subtract, left, right);
free_expression(left);
free_expression(right);
left = retval;
goto again;
}
case tk_asterisk:
{
next_token();
@ -975,7 +941,54 @@ struct expression* parse(const char* text)
return left;
}
struct expression* left = parse_arithmetic();
struct expression* left = parse_multiplicative();
again: switch (tokenkind)
{
case tk_plus:
{
next_token();
struct expression* right =
parse_multiplicative();
struct expression* retval =
new_binary_expression(ek_add, left, right);
free_expression(left);
free_expression(right);
left = retval;
goto again;
}
case tk_minus:
{
next_token();
struct expression* right =
parse_multiplicative();
struct expression* retval =
new_binary_expression(ek_subtract, left, right);
free_expression(left);
free_expression(right);
left = retval;
goto again;
}
default:
break;
}
return left;
}
struct expression* left = parse_additive();
again: switch (tokenkind)
{
@ -984,7 +997,7 @@ struct expression* parse(const char* text)
next_token();
struct expression* right =
parse_arithmetic();
parse_additive();
struct expression* retval =
new_binary_expression(ek_less_than, left, right);
@ -1002,7 +1015,7 @@ struct expression* parse(const char* text)
next_token();
struct expression* right =
parse_arithmetic();
parse_additive();
struct expression* retval =
new_binary_expression(ek_less_than_equal_to, left, right);
@ -1020,7 +1033,7 @@ struct expression* parse(const char* text)
next_token();
struct expression* right =
parse_arithmetic();
parse_additive();
struct expression* retval =
new_binary_expression(ek_greater_than, left, right);
@ -1038,7 +1051,7 @@ struct expression* parse(const char* text)
next_token();
struct expression* right =
parse_arithmetic();
parse_additive();
struct expression* retval =
new_binary_expression(ek_greater_than_equal_to, left, right);