commit f2be4ec972f3c4c4ef87440f9f7456af117b1a53
parent 2d30242d2d779d2165eef7c3a925d9cdfff76bfb
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Wed, 1 Jun 2022 13:51:40 -0400
Add value mechanism
Diffstat:
4 files changed, 54 insertions(+), 23 deletions(-)
diff --git a/asm.py b/asm.py
@@ -41,6 +41,8 @@ def litparse(s):
return s[1]
elif s.isdigit():
return int(s)
+ elif s.startswith(b'$'):
+ return int(s[1:], 16)
return None
def nextt():
@@ -53,6 +55,20 @@ def nextt():
c = fp.read(1)
return bytes(r)
+def labelwr(t):
+ if t.endswith(b':'):
+ t = t[:-1]
+ for name, off in fwlabels:
+ if name == t:
+ intset(off, pc())
+ labels[t] = pc()
+ else:
+ if t in labels:
+ intwr(labels[t])
+ else:
+ fwlabels.append((t, pc()))
+ intwr(0)
+
def newword():
global prevword
name = nextt()
@@ -61,6 +77,9 @@ def newword():
prevword = pc()
words[name] = prevword
+def exitwr():
+ opwr('exit')
+
def _if_():
opwr('_br_')
ps.append(pc())
@@ -84,35 +103,33 @@ def _next_():
opwr('_next_')
intwr(ps.pop())
-def slitwr():
+def strwr():
s = bytearray()
c = fp.read(1)
while c and c != b'"':
s.append(c[0])
c = fp.read(1)
+ outbuf.extend(s)
+ return s
+
+def slitwr():
_if_()
spc = pc()
- outbuf.extend(s)
+ s = strwr()
_then_()
litwr(spc)
litwr(len(s))
-def labelwr(t):
- if t.endswith(b':'):
- t = t[:-1]
- for name, off in fwlabels:
- if name == t:
- intset(off, pc())
- labels[t] = pc()
- else:
- if t in labels:
- intwr(labels[t])
- else:
- fwlabels.append((t, pc()))
- intwr(0)
+def _value_():
+ newword()
+ opwr('_call_')
+ intwr(labels[b'lblval'])
+ intwr(0)
special = {
b':': newword,
+ b';': exitwr,
+ b',"': strwr,
b'S"': slitwr,
b'if': _if_,
b'then': _then_,
@@ -120,6 +137,7 @@ special = {
b'again': _again_,
b'until': _until_,
b'next': _next_,
+ b'value': _value_,
}
t = nextt()
diff --git a/dusk.c b/dusk.c
@@ -47,6 +47,7 @@ static cell pc32() { cell n = gc(pc); pc+=CSIZE; return n; }
/* Native words */
/* stack */
static void _i_() { push(pc32()); }
+static void drop() { pop(); }
static void dup() { push(peek()); }
static void swap() { cell a = pop(); cell b = pop(); push(a); push(b); }
static void p2r() { pushRS(pop()); }
@@ -66,16 +67,21 @@ static void bye() { running = 0; }
/* memory */
static void cfetch() { cell a = pop(); push(mem[a]); }
+static void fetch() { cell a = pop(); push(gc(a)); }
/* arithmetic */
static void inc() { push(pop()+1); }
static void dec() { push(pop()-1); }
+static void shl() { cell n = pop(); push(n<<1); push(n>>31); }
+static void shr() { cell n = pop(); push(n>>1); push(n&1); }
+static void and() { cell a = pop(); cell b = pop(); push(a&b); }
+static void add() { cell a = pop(); cell b = pop(); push(a+b); }
/* I/O */
static void emit() { putchar(pop()); }
static void key() { push(getchar()); }
-static void (*ops[16])() = {
+static void (*ops[22])() = {
#include "ops.txt"
};
diff --git a/forth.txt b/forth.txt
@@ -1,5 +1,12 @@
_br_ lblboot
-: c@+ dup inc swap cfetch exit
-: stype p2r begin c@+ emit next exit
-: foo S" Dusk OS" stype exit
-: BOOT lblboot: foo bye
+lblval: r2p fetch ;
+value hello
+: c@+ dup inc swap cfetch ;
+: << shl drop ;
+: >> shr drop ;
+lblhex: ," 0123456789abcdef"
+: .h $f and _i_ lblhex add cfetch emit ;
+: .x dup >> >> >> >> .h .h ;
+: stype p2r begin c@+ emit next ;
+: foo S" Dusk OS" stype ;
+: BOOT lblboot: foo hello .x bye
diff --git a/ops.txt b/ops.txt
@@ -1,5 +1,5 @@
-_i_, dup, swap, p2r, r2p,
+_i_, drop, dup, swap, p2r, r2p,
_br_, _cbr_, _next_, _call_, exit, bye,
-cfetch,
-inc, dec,
+cfetch, fetch,
+inc, dec, shl, shr, and, add,
emit, key