commit ac66cfcbb4072f2f4b4f2c282e8b29bcf554bd8d
parent e96b14c147c6749d0be977ace11d395bfdf4c50f
Author: Christian Ermann <christianermann@gmail.com>
Date: Thu, 7 Nov 2024 15:35:25 -0800
Replace 'parse-char' with high-level word 'char'
Diffstat:
| M | forth.f | | | 19 | +++++++++++-------- |
| M | forth.s | | | 28 | ---------------------------- |
2 files changed, 11 insertions(+), 36 deletions(-)
diff --git a/forth.f b/forth.f
@@ -14,16 +14,16 @@
: test-if if type then exit ;
: literal postpone lit , ; immediate
-: ')' [ parse-char ) ] literal ;
-: \ begin parse-char while repeat ; immediate
-: ( begin parse-char dup while ')' = if exit then repeat ; immediate
+: char parse-word if c@ else 0 then ;
+: [char] char postpone literal ; immediate
-: '"' [ parse-char " ] literal ; \ this is here to fix syntax highlighting: "
-: '\' [ parse-char \ ] literal ;
-: 'n' [ parse-char n ] literal ;
-: 'r' [ parse-char r ] literal ;
-: 'z' [ parse-char z ] literal ;
+: ')' [char] ) ;
+: '"' [char] " ;
+: '\' [char] \ ;
+: 'n' [char] n ;
+: 'r' [char] r ;
+: 'z' [char] z ;
: parse-until
delimiter !
@@ -33,6 +33,9 @@
2dup parse-offset source-offset !
drop r> tuck - ;
+: \ 0 parse-until 2drop ; immediate
+: ( ')' parse-until 2drop ; immediate
+
: cmove
( addr1 addr2 u -- )
swap >r
diff --git a/forth.s b/forth.s
@@ -326,34 +326,6 @@ defword "parse", parse, 0x423B42EC
defword "parse-word", parse_word, 0xB218226F
.int bl, parse, exit
-defcode "parse-char", parse_char, 0xDF4F729B
- la w, _tick_source
- load_cell x, 0(w)
- load_cell w, cell(w)
- la y, _source_offset
- load_cell y, 0(y)
- add w, w, y
- sub x, x, y
-
- li s3, 0x20 # space
-1: # find start of word
- beqz x, 3f # out of chars? -> stop!
- lb t0, 0(w) # next char!
- addi w, w, 1 # inc. addr
- addi y, y, 1 # inc. offset
- addi x, x, -1 # dec. len
- beq t0, s3, 1b # space? -> skip!
-
-2: # done
- la t1, _source_offset
- store_cell y, 0(t1)
- push t0
- next
-
-3: # out of chars
- mv t0, zero
- j 2b
-
defcode ">number", to_number, 0x2F770E4C
pop x # word len
pop w # word addr