forth: Phase 4 strings — S"/C"/."/TYPE/COUNT/CMOVE/FILL/BLANK (+16; Hayes 168/590)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
@@ -21,8 +21,97 @@
|
||||
(dict-set! s "base" 10)
|
||||
(dict-set! s "vars" (dict))
|
||||
(dict-set! s "cstack" (list))
|
||||
(dict-set! s "mem" (dict))
|
||||
(dict-set! s "here" 0)
|
||||
s)))
|
||||
|
||||
(define
|
||||
forth-mem-write!
|
||||
(fn (state addr u) (dict-set! (get state "mem") (str addr) u)))
|
||||
|
||||
(define
|
||||
forth-mem-read
|
||||
(fn
|
||||
(state addr)
|
||||
(or (get (get state "mem") (str addr)) 0)))
|
||||
|
||||
(define
|
||||
forth-alloc-bytes!
|
||||
(fn
|
||||
(state n)
|
||||
(let
|
||||
((addr (get state "here")))
|
||||
(dict-set! state "here" (+ addr n))
|
||||
addr)))
|
||||
|
||||
(define
|
||||
forth-mem-write-string!
|
||||
(fn
|
||||
(state addr s)
|
||||
(let
|
||||
((n (len s)))
|
||||
(forth-mem-write-string-loop! state addr s 0 n))))
|
||||
|
||||
(define
|
||||
forth-mem-write-string-loop!
|
||||
(fn
|
||||
(state addr s i n)
|
||||
(when
|
||||
(< i n)
|
||||
(begin
|
||||
(forth-mem-write! state (+ addr i) (char-code (substr s i 1)))
|
||||
(forth-mem-write-string-loop! state addr s (+ i 1) n)))))
|
||||
|
||||
(define
|
||||
forth-mem-read-string
|
||||
(fn
|
||||
(state addr n)
|
||||
(forth-mem-read-string-loop state addr 0 n "")))
|
||||
|
||||
(define
|
||||
forth-mem-read-string-loop
|
||||
(fn
|
||||
(state addr i n acc)
|
||||
(if
|
||||
(>= i n)
|
||||
acc
|
||||
(forth-mem-read-string-loop
|
||||
state
|
||||
addr
|
||||
(+ i 1)
|
||||
n
|
||||
(str acc (char-from-code (forth-mem-read state (+ addr i))))))))
|
||||
|
||||
(define
|
||||
forth-fill-loop
|
||||
(fn
|
||||
(state addr u char i)
|
||||
(when
|
||||
(< i u)
|
||||
(begin
|
||||
(forth-mem-write! state (+ addr i) char)
|
||||
(forth-fill-loop state addr u char (+ i 1))))))
|
||||
|
||||
(define
|
||||
forth-cmove-loop
|
||||
(fn
|
||||
(state src dst u i)
|
||||
(when
|
||||
(< i u)
|
||||
(begin
|
||||
(forth-mem-write! state (+ dst i) (forth-mem-read state (+ src i)))
|
||||
(forth-cmove-loop state src dst u (+ i 1))))))
|
||||
|
||||
(define
|
||||
forth-cmove-loop-desc
|
||||
(fn
|
||||
(state src dst u i)
|
||||
(when
|
||||
(>= i 0)
|
||||
(begin
|
||||
(forth-mem-write! state (+ dst i) (forth-mem-read state (+ src i)))
|
||||
(forth-cmove-loop-desc state src dst u (- i 1))))))
|
||||
|
||||
(define
|
||||
forth-cpush
|
||||
(fn (state v) (dict-set! state "cstack" (cons v (get state "cstack")))))
|
||||
@@ -487,4 +576,82 @@
|
||||
(forth-error s "return stack underflow"))
|
||||
(forth-push s (nth rs 1))
|
||||
(forth-push s (nth rs 0)))))
|
||||
(forth-def-prim!
|
||||
state
|
||||
"C@"
|
||||
(fn
|
||||
(s)
|
||||
(let ((addr (forth-pop s))) (forth-push s (forth-mem-read s addr)))))
|
||||
(forth-def-prim!
|
||||
state
|
||||
"C!"
|
||||
(fn
|
||||
(s)
|
||||
(let
|
||||
((addr (forth-pop s)) (v (forth-pop s)))
|
||||
(forth-mem-write! s addr v))))
|
||||
(forth-def-prim! state "CHAR+" (fn (s) (forth-push s (+ (forth-pop s) 1))))
|
||||
(forth-def-prim! state "CHARS" (fn (s) nil))
|
||||
(forth-def-prim!
|
||||
state
|
||||
"TYPE"
|
||||
(fn
|
||||
(s)
|
||||
(let
|
||||
((u (forth-pop s)) (addr (forth-pop s)))
|
||||
(forth-emit-str s (forth-mem-read-string s addr u)))))
|
||||
(forth-def-prim!
|
||||
state
|
||||
"COUNT"
|
||||
(fn
|
||||
(s)
|
||||
(let
|
||||
((addr (forth-pop s)))
|
||||
(let
|
||||
((u (forth-mem-read s addr)))
|
||||
(forth-push s (+ addr 1))
|
||||
(forth-push s u)))))
|
||||
(forth-def-prim!
|
||||
state
|
||||
"FILL"
|
||||
(fn
|
||||
(s)
|
||||
(let
|
||||
((char (forth-pop s)) (u (forth-pop s)) (addr (forth-pop s)))
|
||||
(forth-fill-loop s addr u char 0))))
|
||||
(forth-def-prim!
|
||||
state
|
||||
"BLANK"
|
||||
(fn
|
||||
(s)
|
||||
(let
|
||||
((u (forth-pop s)) (addr (forth-pop s)))
|
||||
(forth-fill-loop s addr u 32 0))))
|
||||
(forth-def-prim!
|
||||
state
|
||||
"CMOVE"
|
||||
(fn
|
||||
(s)
|
||||
(let
|
||||
((u (forth-pop s)) (dst (forth-pop s)) (src (forth-pop s)))
|
||||
(forth-cmove-loop s src dst u 0))))
|
||||
(forth-def-prim!
|
||||
state
|
||||
"CMOVE>"
|
||||
(fn
|
||||
(s)
|
||||
(let
|
||||
((u (forth-pop s)) (dst (forth-pop s)) (src (forth-pop s)))
|
||||
(forth-cmove-loop-desc s src dst u (- u 1)))))
|
||||
(forth-def-prim!
|
||||
state
|
||||
"MOVE"
|
||||
(fn
|
||||
(s)
|
||||
(let
|
||||
((u (forth-pop s)) (dst (forth-pop s)) (src (forth-pop s)))
|
||||
(if
|
||||
(or (<= dst src) (>= dst (+ src u)))
|
||||
(forth-cmove-loop s src dst u 0)
|
||||
(forth-cmove-loop-desc s src dst u (- u 1))))))
|
||||
state))
|
||||
|
||||
Reference in New Issue
Block a user