forth: BASE/DECIMAL/HEX/BIN/OCTAL (+9; Hayes 174/590)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 20:40:11 +00:00
parent 35ce18eb97
commit acf9c273a2
8 changed files with 76 additions and 9 deletions

View File

@@ -24,7 +24,7 @@
(forth-execute-word state w) (forth-execute-word state w)
(forth-compile-call state tok)) (forth-compile-call state tok))
(let (let
((n (forth-parse-number tok (get state "base")))) ((n (forth-parse-number tok (get (get state "vars") "base"))))
(if (if
(not (nil? n)) (not (nil? n))
(forth-compile-lit state n) (forth-compile-lit state n)
@@ -219,7 +219,7 @@
(not (nil? w)) (not (nil? w))
(forth-execute-word state w) (forth-execute-word state w)
(let (let
((n (forth-parse-number tok (get state "base")))) ((n (forth-parse-number tok (get (get state "vars") "base"))))
(if (if
(not (nil? n)) (not (nil? n))
(forth-push state n) (forth-push state n)

View File

@@ -87,8 +87,7 @@
"actual" "actual"
(str hayes-actual)))))))))) (str hayes-actual))))))))))
(forth-def-prim! state "TESTING" (fn (s) nil)) (forth-def-prim! state "TESTING" (fn (s) nil))
(forth-def-prim! state "HEX" (fn (s) (dict-set! s "base" 16))) ;; HEX/DECIMAL are real primitives now (runtime.sx) — no stub needed.
(forth-def-prim! state "DECIMAL" (fn (s) (dict-set! s "base" 10)))
state)) state))
(define (define

View File

@@ -17,7 +17,7 @@
(not (nil? w)) (not (nil? w))
(forth-execute-word state w) (forth-execute-word state w)
(let (let
((n (forth-parse-number tok (get state "base")))) ((n (forth-parse-number tok (get (get state "vars") "base"))))
(if (if
(not (nil? n)) (not (nil? n))
(forth-push state n) (forth-push state n)

View File

@@ -18,8 +18,8 @@
(dict-set! s "output" "") (dict-set! s "output" "")
(dict-set! s "compiling" false) (dict-set! s "compiling" false)
(dict-set! s "current-def" nil) (dict-set! s "current-def" nil)
(dict-set! s "base" 10)
(dict-set! s "vars" (dict)) (dict-set! s "vars" (dict))
(dict-set! (get s "vars") "base" 10)
(dict-set! s "cstack" (list)) (dict-set! s "cstack" (list))
(dict-set! s "mem" (dict)) (dict-set! s "mem" (dict))
(dict-set! s "here" 0) (dict-set! s "here" 0)
@@ -538,6 +538,23 @@
(> n 0) (> n 0)
(for-each (fn (_) (forth-emit-str s " ")) (range 0 n)))))) (for-each (fn (_) (forth-emit-str s " ")) (range 0 n))))))
(forth-def-prim! state "BL" (fn (s) (forth-push s 32))) (forth-def-prim! state "BL" (fn (s) (forth-push s 32)))
(forth-def-prim!
state
"DECIMAL"
(fn (s) (dict-set! (get s "vars") "base" 10)))
(forth-def-prim!
state
"HEX"
(fn (s) (dict-set! (get s "vars") "base" 16)))
(forth-def-prim!
state
"BIN"
(fn (s) (dict-set! (get s "vars") "base" 2)))
(forth-def-prim!
state
"OCTAL"
(fn (s) (dict-set! (get s "vars") "base" 8)))
(forth-def-prim! state "BASE" (fn (s) (forth-push s "base")))
(forth-def-prim! state "I" (fn (s) (forth-push s (forth-rpeek s)))) (forth-def-prim! state "I" (fn (s) (forth-push s (forth-rpeek s))))
(forth-def-prim! (forth-def-prim!
state state

View File

@@ -1,6 +1,6 @@
{ {
"source": "gerryjackson/forth2012-test-suite src/core.fr", "source": "gerryjackson/forth2012-test-suite src/core.fr",
"generated_at": "2026-04-24T20:12:09Z", "generated_at": "2026-04-24T20:39:51Z",
"chunks_available": 638, "chunks_available": 638,
"chunks_fed": 590, "chunks_fed": 590,
"total": 590, "total": 590,

View File

@@ -11,7 +11,7 @@
| percent | 29% | | percent | 29% |
- **Source**: `gerryjackson/forth2012-test-suite` `src/core.fr` - **Source**: `gerryjackson/forth2012-test-suite` `src/core.fr`
- **Generated**: 2026-04-24T20:12:09Z - **Generated**: 2026-04-24T20:39:51Z
- **Note**: completed - **Note**: completed
A "chunk" is any preprocessed segment ending at a `}T` (every Hayes test A "chunk" is any preprocessed segment ending at a `}T` (every Hayes test

View File

@@ -171,6 +171,47 @@
((r (forth-run "1000 2 ACCEPT"))) ((r (forth-run "1000 2 ACCEPT")))
(let ((stk (nth r 2))) (forth-p4-assert "ACCEPT empty buf -> 0" (list 0) stk))))) (let ((stk (nth r 2))) (forth-p4-assert "ACCEPT empty buf -> 0" (list 0) stk)))))
(define
forth-p4-base-tests
(fn
()
(forth-p4-check-top
"BASE default is 10"
"BASE @"
10)
(forth-p4-check-top
"HEX switches base to 16"
"HEX BASE @"
16)
(forth-p4-check-top
"DECIMAL resets to 10"
"HEX DECIMAL BASE @"
10)
(forth-p4-check-top
"HEX parses 10 as 16"
"HEX 10"
16)
(forth-p4-check-top
"HEX parses FF as 255"
"HEX FF"
255)
(forth-p4-check-top
"DECIMAL parses 10 as 10"
"HEX DECIMAL 10"
10)
(forth-p4-check-top
"BIN parses 1010 as 10"
"BIN 1010"
10)
(forth-p4-check-top
"OCTAL parses 17 as 15"
"OCTAL 17"
15)
(forth-p4-check-top
"BASE @ ; 16 BASE ! ; BASE @"
"BASE @ 16 BASE ! BASE @ SWAP DROP"
16)))
(define (define
forth-p4-run-all forth-p4-run-all
(fn (fn
@@ -185,6 +226,7 @@
(forth-p4-charplus-tests) (forth-p4-charplus-tests)
(forth-p4-char-tests) (forth-p4-char-tests)
(forth-p4-key-accept-tests) (forth-p4-key-accept-tests)
(forth-p4-base-tests)
(dict (dict
"passed" "passed"
forth-p4-passed forth-p4-passed

View File

@@ -80,7 +80,7 @@ Representation:
### Phase 4 — strings + more Core ### Phase 4 — strings + more Core
- [x] `S"`, `C"`, `."`, `TYPE`, `COUNT`, `CMOVE`, `FILL`, `BLANK` - [x] `S"`, `C"`, `."`, `TYPE`, `COUNT`, `CMOVE`, `FILL`, `BLANK`
- [x] `CHAR`, `[CHAR]`, `KEY`, `ACCEPT` - [x] `CHAR`, `[CHAR]`, `KEY`, `ACCEPT`
- [ ] `BASE` manipulation: `DECIMAL`, `HEX` - [x] `BASE` manipulation: `DECIMAL`, `HEX`
- [ ] `DEPTH`, `SP@`, `SP!` - [ ] `DEPTH`, `SP@`, `SP!`
- [ ] Drive Hayes Core pass-rate up - [ ] Drive Hayes Core pass-rate up
@@ -99,6 +99,15 @@ Representation:
_Newest first._ _Newest first._
- **Phase 4 — `BASE`/`DECIMAL`/`HEX`/`BIN`/`OCTAL` (+9; Hayes unchanged).**
Moved `base` from its top-level state slot into `state.vars["base"]`
so the regular `@`/`!`/VARIABLE machinery works on it.
`BASE` pushes the sentinel address `"base"`; `DECIMAL`/`HEX`/`BIN`/
`OCTAL` are thin primitives that write into that slot. Parser
reads through `vars` now. Hayes unchanged because the runner had
already been stubbing `HEX`/`DECIMAL` — now real words, stubs
removed from `hayes-runner.sx`.
- **Phase 4 — `CHAR`/`[CHAR]`/`KEY`/`ACCEPT` (+7 / Hayes 168→174).** - **Phase 4 — `CHAR`/`[CHAR]`/`KEY`/`ACCEPT` (+7 / Hayes 168→174).**
`CHAR` parses the next token and pushes the first-char code. `[CHAR]` `CHAR` parses the next token and pushes the first-char code. `[CHAR]`
is IMMEDIATE: in compile mode it embeds the code as a compiled push is IMMEDIATE: in compile mode it embeds the code as a compiled push