From 2dba790a5b0d1e61acee6da4b85a486b036446ba Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 6 Nov 2021 18:19:38 +0100 Subject: added some functions --- cmds.c | 19 +++++++++++++++++++ main.c | 8 ++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/cmds.c b/cmds.c index 2a88041..7960834 100644 --- a/cmds.c +++ b/cmds.c @@ -16,9 +16,28 @@ stack ffloor(stack stack) { return stack; } +stack flogB(stack stack) { + stack.val[stack.count-2] = log2(stack.val[stack.count-2])/log2(stack.val[stack.count-1]); + stack.val[--stack.count] = 0; + return stack; +} + +stack fnegate(stack stack) { + stack.val[stack.count-1] *= -1; + return stack; +} + +stack fpop(stack stack) { + stack.val[--stack.count] = 0; + return stack; +} + command CMD_LIST[] = { + {"p", &fpop, "pop last element"}, {"ceil", &fceil, "truncate to the next integer"}, {"floor", &ffloor, "truncate to the previous integer"}, + {"log", &flogB, "calculate logarithm in base LAST_NUM of LAST_NUM-1"}, + {"neg", &fnegate, "change last element's sign"}, {0, 0, 0} }; diff --git a/main.c b/main.c index e81d50b..cb5dada 100644 --- a/main.c +++ b/main.c @@ -40,7 +40,6 @@ int main() { if (interpreted == (double)0 && endptr == buf) { /* strtod returned the char it could not read */ TYPE t = discriminate(buf); if (t == OPERATOR ) { - printf("func\n"); if (s.stk.count <= 1) { fprintf(s.defout, "not enough arguments\n"); continue; @@ -64,13 +63,18 @@ int main() { s.last_op = '/'; } else; } else if (t == FUNCTION) { + /* check for special commands, else pass to exec() */ if (strcmp(buf, "quit") == 0) { fprintf(s.defout, "quitting, bye!\n"); exit(0); + } else if (strcmp(buf, "list") == 0) { + for (int i = 0; s.sorted[i].name != 0; i++) { + fprintf(s.defout, "[%s]\t->\t%s\n", s.sorted[i].name, s.sorted[i].description); + continue; + } } else exec(buf, &s); } } else { /* we found a number */ - printf("number\n"); s.stk.val[s.stk.count++] = interpreted; } -- cgit 1.4.1-2-gfad0