aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordan <[email protected]>2023-05-26 14:03:17 -0400
committerdan <[email protected]>2023-05-26 14:03:17 -0400
commit4e26b8c0a1b7fc843741105d57da863997e7284b (patch)
tree3439e39efca784ed34e2e22c9ef98ecd53e785b5
parent1ac1b0c94195be3231a4aaa7335dc2f3a169820c (diff)
downloadforth-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.c7
-rw-r--r--optable.c25
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 <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);
+}