summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-06-18 11:32:25 +0200
committerkdx <kikoodx@paranoici.org>2023-06-18 11:32:25 +0200
commit874245962e04264ab3c8a993a109f1790ddb36e2 (patch)
tree38c9400384dc15e4f56825e642ddb1bae51947d1
parent1e3a3ba3c6b71c6d62c15920f2f57f36b07cc0c7 (diff)
downloadgolem-874245962e04264ab3c8a993a109f1790ddb36e2.tar.gz
add modulo operation
-rw-r--r--src/main.c10
-rwxr-xr-xtesting.sh1
2 files changed, 10 insertions, 1 deletions
diff --git a/src/main.c b/src/main.c
index a28d2bc..1445c4e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -175,7 +175,7 @@ is_punct(const char *p)
if (strchr("|=", p[0]) != NULL && p[1] == '>')
return 2;
- return (strchr("+-/*()<>,;{}[]=&^|", p[0]) != NULL);
+ return (strchr("+-*/%()<>,;{}[]=&^|", p[0]) != NULL);
}
static unsigned char
@@ -281,6 +281,7 @@ typedef enum {
NOD_SUB, // -
NOD_MUL, // *
NOD_DIV, // /
+ NOD_MOD, // %
NOD_EQU, // ==
NOD_NEQ, // !=
NOD_LT, // <
@@ -674,6 +675,11 @@ mul(Token **rest, Token *tok)
continue;
}
+ if (equal(tok, "%")) {
+ node = new_binary(NOD_MOD, tok, node, primary(&tok, tok->next));
+ continue;
+ }
+
*rest = tok;
return node;
}
@@ -1024,6 +1030,7 @@ gen_expr(Node *node)
case NOD_SUB: op = "SUB"; break;
case NOD_MUL: op = "MUL"; break;
case NOD_DIV: op = "DIV"; break;
+ case NOD_MOD: op = "MOD"; break;
case NOD_EQU: op = "EQU"; break;
case NOD_NEQ: op = "NEQ"; break;
case NOD_LT: op = "LTH"; break;
@@ -1111,6 +1118,7 @@ const_expr(Node *node)
case NOD_SUB: return const_expr(lhs) - const_expr(rhs);
case NOD_MUL: return const_expr(lhs) * const_expr(rhs);
case NOD_DIV: return const_expr(lhs) / const_expr(rhs);
+ case NOD_MOD: return const_expr(lhs) % const_expr(rhs);
case NOD_EQU: return const_expr(lhs) == const_expr(rhs);
case NOD_NEQ: return const_expr(lhs) != const_expr(rhs);
case NOD_LT: return const_expr(lhs) < const_expr(rhs);
diff --git a/testing.sh b/testing.sh
index c8f4ad3..aa3945d 100755
--- a/testing.sh
+++ b/testing.sh
@@ -64,4 +64,5 @@ test "$1" "main() {;;;;;;;;;;;;;;;;;}"
test "$1" "global a = 8, b, c = 4; main() { dbg a; dbg c; }"
test "$1" "global a, b = 6, c = 4; main() { dbg b; dbg c; }"
test "$1" "main() { local a = 5, b = 7, c; dbg a; dbg b; }"
+test "$1" "main() { dbg 5 % 3; }"
rm -f build/tmp.*