aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordan <[email protected]>2023-05-23 21:25:59 -0400
committerdan <[email protected]>2023-05-23 21:25:59 -0400
commit2a960c5081782989e46a0c158841d404aa0a4227 (patch)
tree9ca42d6e05b018f21cc29458b080d9d49a91e511
parent1d83b15617c9553c536df3cb1a59d3b6ae821a7b (diff)
downloadforth-2a960c5081782989e46a0c158841d404aa0a4227.tar.gz
forth-2a960c5081782989e46a0c158841d404aa0a4227.tar.bz2
forth-2a960c5081782989e46a0c158841d404aa0a4227.zip
add if,not,=,drop,rot,swap,over words
-rw-r--r--forth.c70
1 files changed, 67 insertions, 3 deletions
diff --git a/forth.c b/forth.c
index fd65bde..56918d9 100644
--- a/forth.c
+++ b/forth.c
@@ -60,6 +60,45 @@ void push(stack *s, stackitem si) {
}
}
+void not(stack *s) {
+ int x = pop(s);
+ push(s, !x);
+}
+
+void drop(stack *s) {
+ pop(s);
+}
+
+void over(stack *s) {
+ int x = pop(s);
+ int y = pop(s);
+ push(s, y);
+ push(s, x);
+ push(s, y);
+}
+
+void rot(stack *s) {
+ int x = pop(s);
+ int y = pop(s);
+ int z = pop(s);
+ push(s, y);
+ push(s, x);
+ push(s, z);
+}
+
+void swap(stack *s) {
+ int x = pop(s);
+ int y = pop(s);
+ push(s, x);
+ push(s, y);
+}
+
+void eq(stack *s) {
+ int x = pop(s);
+ int y = pop(s);
+ push(s, x == y);
+}
+
void add(stack *s) {
int x = pop(s);
@@ -100,8 +139,14 @@ wordop optable[OPTABLE_MAX_SIZE] = {
{"-", false, sub},
{"dup", false, s_dup},
{"dump", false, dump},
+ {"not", false, not},
+ {"=", false, eq},
+ {"swap", false, swap},
+ {"drop", false, drop},
+ {"over", false, over},
+ {"rot", false, rot},
};
-int optablelen = 4;
+int optablelen = 10;
const int DEFINED_FUNC_MAX_LENGTH = 1024;
const int WORD_LEN_LIMIT = 255;
@@ -138,7 +183,7 @@ int defineop(int starti, char *input) {
exit(1);
}
// add op to end of table, and increment size
- optable[optablelen].word = opcode; //{opcode, true, funcscript};
+ optable[optablelen].word = opcode;
optable[optablelen].isscript = true;
optable[optablelen].script = funcscript;
optable[optablelen].scriptlen = funcscripti;
@@ -211,7 +256,26 @@ void eval(stack* s, int len, char* line) {
} else { // end of word
if (wordi > 0) { // don't exec an empty string
word[wordi] = '\0';
- exec(s, word);
+ if (!strcmp(word, "if")) {
+ stackitem predicate = pop(s);
+ if (!predicate) {
+ while (len > i &&
+ !(
+ line[i-3] == 't' &&
+ line[i-2] == 'h' &&
+ line[i-1] == 'e' &&
+ line[i] == 'n'
+ )) {
+ i++;
+ }
+ // if (len < i + 3) {
+ // // error no 'then' before EOL
+ // exit(1);
+ // }
+ }
+ } else {
+ exec(s, word);
+ }
}
// start new word
wordi = 0;