From 4e26b8c0a1b7fc843741105d57da863997e7284b Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 26 May 2023 14:03:17 -0400 Subject: refactor: pop and peek are no longer special cases; remove dump command --- forth.c | 7 ------- optable.c | 25 +++++++++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/forth.c b/forth.c index e9f7637..0b4a99f 100644 --- a/forth.c +++ b/forth.c @@ -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) { diff --git a/optable.c b/optable.c index fe7409b..d74e2e9 100644 --- a/optable.c +++ b/optable.c @@ -1,7 +1,6 @@ #include "optable.h" #include -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); +} -- cgit v1.2.3