From 874245962e04264ab3c8a993a109f1790ddb36e2 Mon Sep 17 00:00:00 2001 From: kdx Date: Sun, 18 Jun 2023 11:32:25 +0200 Subject: add modulo operation --- src/main.c | 10 +++++++++- testing.sh | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) 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.* -- cgit v1.2.3