Skip to content

Commit

Permalink
Add mul, prod, and, or, and xor
Browse files Browse the repository at this point in the history
  • Loading branch information
soveran committed Sep 18, 2024
1 parent 49c8dda commit 311e9b2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand Down
21 changes: 21 additions & 0 deletions clac.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 33 additions & 0 deletions clac.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit 311e9b2

Please sign in to comment.