commit 3e449eac809e251b44ccc84636e97ec8afada326
parent e1101f39ffc6889db049fac88dbda2e813ff8347
Author: Christian Ermann <christianermann@gmail.com>
Date: Mon, 28 Oct 2024 15:07:00 -0700
Add ':' and ';' to define new words at runtime
Diffstat:
| M | forth.s | | | 62 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
1 file changed, 61 insertions(+), 1 deletion(-)
diff --git a/forth.s b/forth.s
@@ -20,6 +20,12 @@
#define load_cell lw
#define store_cell sw
+.equ state_immediate, 0
+.equ state_compile, 1
+
+.equ flag_hidden, 0x20
+.equ flag_immediate, 0x40
+
.macro next
load_cell w, 0(ip)
load_cell x, 0(w)
@@ -209,7 +215,26 @@ defcode "interpret", 9, 0, 0x1F98C57A, interpret, execute
jal word_impl
jal find_impl
beqz w, _not_found
+ mv t2, w
addi w, w, code_offset
+
+ la t0, _state
+ load_cell x, 0(t0)
+ li t1, state_immediate
+ beq x, t1, _execute_word
+
+ mv x, t2
+ addi x, x, flag_offset
+ lb x, 0(x)
+ andi x, x, flag_immediate
+ bnez x, _execute_word
+
+ # compile
+ push w
+ la w, comma
+ #addi w, w, code_offset
+
+_execute_word:
load_cell x, 0(w)
jr x
_not_found:
@@ -364,6 +389,39 @@ _hash_char:
mv w, t0
ret
+defcode "[", 1, 0, 0xDE0C1FBA, l_bracket, hash
+ la w, _state
+ li x, state_immediate
+ store_cell x, 0(w)
+ next
+
+defcode "]", 1, 0, 0xD80C1648, r_bracket, l_bracket
+ la w, _state
+ li x, state_compile
+ store_cell x, 0(w)
+ next
+
+defcode "hidden", 6, 0, 0xF618F139, hidden, r_bracket
+ pop w
+ addi w, w, flag_offset
+ lb x, 0(w)
+ xori x, x, flag_hidden
+ sb x, 0(w)
+ next
+
+defword ":", 1, 0, 0x3F0CB86D, colon, hidden
+ .int word, create # create header
+ .int lit, docol, comma # append 'docol'
+ .int latest, fetch, hidden # hide word
+ .int r_bracket # enter 'compile' mode
+ .int exit
+
+defword ";", 1, flag_immediate, 0x3E0CB6DA, semicolon, colon
+ .int lit, exit, comma # append 'exit'
+ .int latest, fetch, hidden # show word
+ .int l_bracket # enter 'immediate' mode
+ .int exit
+
.section ".rodata"
program:
.int type
@@ -375,10 +433,12 @@ version_string:
word_buffer: .space 255
.balign cell
-_latest: .int name_hash
+_latest: .int name_semicolon
_here: .int __here_start
_meta: .int __meta_start
+_state: .int state_immediate
+
.section ".text.boot"
start:
la psp, __stacktop