diff options
author | dan <[email protected]> | 2023-05-26 14:03:17 -0400 |
---|---|---|
committer | dan <[email protected]> | 2023-05-26 14:03:17 -0400 |
commit | 4e26b8c0a1b7fc843741105d57da863997e7284b (patch) | |
tree | 3439e39efca784ed34e2e22c9ef98ecd53e785b5 | |
parent | 1ac1b0c94195be3231a4aaa7335dc2f3a169820c (diff) | |
download | forth-4e26b8c0a1b7fc843741105d57da863997e7284b.tar.gz forth-4e26b8c0a1b7fc843741105d57da863997e7284b.tar.bz2 forth-4e26b8c0a1b7fc843741105d57da863997e7284b.zip |
refactor: pop and peek are no longer special cases; remove dump command
-rw-r--r-- | forth.c | 7 | ||||
-rw-r--r-- | optable.c | 25 |
2 files changed, 15 insertions, 17 deletions
@@ -27,13 +27,6 @@ char isnumber(char *text) { void eval(stack* s, int len, char* line); void exec(stack *s, char *word) { - if (!strcmp(word, "pop") || !strcmp(word, ".")) { - printf("%d\n", pop(s)); - return; - } else if (!strcmp(word, "peek")) { - printf("%d\n", peek(s)); - return; - } wordop* op = getop(word); if (op) { if (op->isscript) { @@ -1,7 +1,6 @@ #include "optable.h" #include <string.h> -static void dump(stack* s); static void not(stack *s); static void drop(stack *s); static void over(stack *s); @@ -13,14 +12,17 @@ static void mult(stack *s); static void s_div(stack *s); static void sub(stack *s); static void dup(stack *s); +static void popout(stack *s); +static void peekout(stack *s); static wordop optable[OPTABLE_MAX_SIZE] = { + {".", false, popout}, + {"peek", false, peekout}, {"+", false, add}, {"-", false, sub}, {"*", false, mult}, {"/", false, s_div}, {"dup", false, dup}, - {"dump", false, dump}, {"not", false, not}, {"=", false, eq}, {"swap", false, swap}, @@ -28,7 +30,7 @@ static wordop optable[OPTABLE_MAX_SIZE] = { {"over", false, over}, {"rot", false, rot}, }; -static int optablelen = 12; +static int optablelen = 13; int defineop(int starti, char *input) { // name by which the function will be called @@ -82,13 +84,6 @@ wordop* getop(char *word) { /* Implementations of builtin functions */ -static void dump(stack* s) { - for (int i = 0; i < s->size; i++) { - printf("%d,", s->start[i]); - } - printf("\n"); -} - static void not(stack *s) { int x = pop(s); push(s, !x); @@ -157,3 +152,13 @@ static void dup(stack *s) { push(s, x); push(s, x); } + +static void popout(stack *s) { + printf("%d\n", pop(s)); +} + +static void peekout(stack *s) { + int x = pop(s); + push(s, x); + printf("%d\n", x); +} |