commit 776bd6c3605cb3d645a5a1def2c597ec5baf963a
parent f2be4ec972f3c4c4ef87440f9f7456af117b1a53
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Wed, 1 Jun 2022 13:51:40 -0400
Add to
Diffstat:
4 files changed, 29 insertions(+), 7 deletions(-)
diff --git a/asm.py b/asm.py
@@ -81,13 +81,20 @@ def exitwr():
opwr('exit')
def _if_():
- opwr('_br_')
+ opwr('_cbr_')
ps.append(pc())
intwr(0)
def _then_():
intset(ps.pop(), pc())
+def _else_():
+ x = ps.pop()
+ opwr('_br_')
+ ps.append(pc())
+ intwr(0)
+ intset(x, pc())
+
def _begin_():
ps.append(pc())
@@ -120,6 +127,11 @@ def slitwr():
litwr(spc)
litwr(len(s))
+def _create_():
+ newword()
+ opwr('_call_')
+ intwr(labels[b'lblcell'])
+
def _value_():
newword()
opwr('_call_')
@@ -133,10 +145,12 @@ special = {
b'S"': slitwr,
b'if': _if_,
b'then': _then_,
+ b'else': _else_,
b'begin': _begin_,
b'again': _again_,
b'until': _until_,
b'next': _next_,
+ b'create': _create_,
b'value': _value_,
}
diff --git a/dusk.c b/dusk.c
@@ -26,6 +26,7 @@ byte mem[MEMSIZE];
struct stack ps, rs;
cell pc;
byte running;
+byte toflag;
/* Utilities */
static cell gc(cell addr) {
@@ -67,7 +68,11 @@ static void bye() { running = 0; }
/* memory */
static void cfetch() { cell a = pop(); push(mem[a]); }
+static void cstore() { cell a = pop(); mem[a] = pop(); }
static void fetch() { cell a = pop(); push(gc(a)); }
+static void store() { cell a = pop(); sc(a, pop()); }
+static void to() { toflag = 1; }
+static void isto() { push(toflag); toflag = 0; }
/* arithmetic */
static void inc() { push(pop()+1); }
@@ -81,7 +86,7 @@ static void add() { cell a = pop(); cell b = pop(); push(a+b); }
static void emit() { putchar(pop()); }
static void key() { push(getchar()); }
-static void (*ops[22])() = {
+static void (*ops[26])() = {
#include "ops.txt"
};
@@ -116,7 +121,7 @@ int main() {
c = getc(fp);
}
fclose(fp);
- ps.ptr = rs.ptr = pc = 0;
+ ps.ptr = rs.ptr = pc = toflag = 0;
running = 1;
while (step());
return 0;
diff --git a/forth.txt b/forth.txt
@@ -1,6 +1,9 @@
_br_ lblboot
-lblval: r2p fetch ;
-value hello
+lblcell: r2p ;
+lblval: r2p isto if store else fetch then ;
+create in(
+," xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+value in>
: c@+ dup inc swap cfetch ;
: << shl drop ;
: >> shr drop ;
@@ -9,4 +12,4 @@ lblhex: ," 0123456789abcdef"
: .x dup >> >> >> >> .h .h ;
: stype p2r begin c@+ emit next ;
: foo S" Dusk OS" stype ;
-: BOOT lblboot: foo hello .x bye
+: BOOT lblboot: foo 42 to in> in> .x bye
diff --git a/ops.txt b/ops.txt
@@ -1,5 +1,5 @@
_i_, drop, dup, swap, p2r, r2p,
_br_, _cbr_, _next_, _call_, exit, bye,
-cfetch, fetch,
+cfetch, cstore, fetch, store, to, isto,
inc, dec, shl, shr, and, add,
emit, key