From 311e9b2ba0c897892b8a789c9411fdf8995171e6 Mon Sep 17 00:00:00 2001 From: Michel Martens Date: Wed, 18 Sep 2024 09:28:27 +0200 Subject: [PATCH] Add mul, prod, and, or, and xor --- README.md | 20 ++++++++++++++++++++ clac.1 | 21 +++++++++++++++++++++ clac.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/README.md b/README.md index ecf09fa..684df90 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,15 @@ Pop all the values in the stack and push their sum. Pop the value `a` and remove that many items from the stack. Push their sum. +### Product + +`prod` +Pop all the values in the stack and push their product. + +`mul` +Pop the value `a` and remove that many items from the stack. Push +their product. + ### Rounding `ceil` @@ -153,6 +162,17 @@ Pop the value `a` and push the integer value closest to `a`. `abs` Pop the value `a` and push the non-negative value of `a`. +### Binary operations + +`and` +Pop two values `a` and `b` and push the binary AND of `a` and `b`. + +`or` +Pop two values `a` and `b` and push the binary OR of `a` and `b`. + +`xor` +Pop two values `a` and `b` and push the binary XOR of `a` and `b`. + ### Stack manipulation `swap` diff --git a/clac.1 b/clac.1 index 732dc4e..f948c52 100644 --- a/clac.1 +++ b/clac.1 @@ -133,6 +133,16 @@ Pop the value `a` and remove that many items from the stack. Push their sum. .El . +.Ss Product +. +.Bl -tag -width Fl +.It Ic prod +Pop all the values in the stack and push their product. +.It Ic mul +Pop the value `a` and remove that many items from the stack. Push +their product. +.El +. .Ss Rounding . .Bl -tag -width Fl @@ -153,6 +163,17 @@ Pop the value `a` and push the integer value closest to `a`. Pop the value `a` and push the non-negative value of `a`. .El . +.Ss Binary operations +. +.Bl -tag -width Fl +.It Ic and +Pop two values `a` and `b` and push the binary AND of `a` and `b`. +.It Ic or +Pop two values `a` and `b` and push the binary OR of `a` and `b`. +.It Ic xor +Pop two values `a` and `b` and push the binary XOR of `a` and `b`. +.El +. .Ss Stack manipulation . .Bl -tag -width Fl diff --git a/clac.c b/clac.c index 3fce9d2..93652bd 100644 --- a/clac.c +++ b/clac.c @@ -155,6 +155,17 @@ static double add(stack *s, int n) { return a; } +static double mul(stack *s, int n) { + double a = pop(s); + + while (!isempty(s) && n > 1) { + a *= pop(s); + n--; + } + + return a; +} + static node *get(sds word) { node *curr = head; @@ -321,10 +332,32 @@ static void process(sds word) { b = pop(s0); push(s0, pow(b, a)); } + } else if (!strcmp(word, "or")) { + if (count(s0) > 1) { + a = pop(s0); + b = pop(s0); + push(s0, (int)fabs(b)|(int)fabs(a)); + } + } else if (!strcmp(word, "and")) { + if (count(s0) > 1) { + a = pop(s0); + b = pop(s0); + push(s0, (int)fabs(b)&(int)fabs(a)); + } + } else if (!strcmp(word, "xor")) { + if (count(s0) > 1) { + a = pop(s0); + b = pop(s0); + push(s0, (int)fabs(b)^(int)fabs(a)); + } } else if (!strcasecmp(word, "sum")) { push(s0, add(s0, count(s0))); } else if (!strcasecmp(word, "add")) { push(s0, add(s0, pop(s0))); + } else if (!strcasecmp(word, "prod")) { + push(s0, mul(s0, count(s0))); + } else if (!strcasecmp(word, "mul")) { + push(s0, mul(s0, pop(s0))); } else if (!strcasecmp(word, "abs")) { if (count(s0) > 0) { push(s0, fabs(pop(s0)));