diff options
author | kdx <kikoodx@paranoici.org> | 2023-06-18 11:32:25 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-06-18 11:32:25 +0200 |
commit | 874245962e04264ab3c8a993a109f1790ddb36e2 (patch) | |
tree | 38c9400384dc15e4f56825e642ddb1bae51947d1 | |
parent | 1e3a3ba3c6b71c6d62c15920f2f57f36b07cc0c7 (diff) | |
download | golem-874245962e04264ab3c8a993a109f1790ddb36e2.tar.gz |
add modulo operation
-rw-r--r-- | src/main.c | 10 | ||||
-rwxr-xr-x | testing.sh | 1 |
2 files changed, 10 insertions, 1 deletions
@@ -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); @@ -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.* |