forth: SP@ / SP! (+4; 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 21:07:10 +00:00
parent acf9c273a2
commit 387a6e7f5d
5 changed files with 42 additions and 3 deletions

View File

@@ -393,6 +393,19 @@
(s)
(let ((a (forth-peek s))) (when (not (= a 0)) (forth-push s a)))))
(forth-def-prim! state "DEPTH" (fn (s) (forth-push s (forth-depth s))))
(forth-def-prim! state "SP@" (fn (s) (forth-push s (forth-depth s))))
(forth-def-prim!
state
"SP!"
(fn
(s)
(let
((n (forth-pop s)))
(let
((cur (forth-depth s)))
(when
(> cur n)
(dict-set! s "dstack" (drop (get s "dstack") (- cur n))))))))
(forth-def-prim!
state
"PICK"

View File

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

View File

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

View File

@@ -171,6 +171,24 @@
((r (forth-run "1000 2 ACCEPT")))
(let ((stk (nth r 2))) (forth-p4-assert "ACCEPT empty buf -> 0" (list 0) stk)))))
(define
forth-p4-sp-tests
(fn
()
(forth-p4-check-top "SP@ returns depth (0)" "SP@" 0)
(forth-p4-check-top
"SP@ after pushes"
"1 2 3 SP@ SWAP DROP SWAP DROP SWAP DROP"
3)
(forth-p4-check-stack-size
"SP! truncates"
"1 2 3 4 5 2 SP!"
2)
(forth-p4-check-top
"SP! leaves base items intact"
"1 2 3 4 5 2 SP!"
2)))
(define
forth-p4-base-tests
(fn
@@ -227,6 +245,7 @@
(forth-p4-char-tests)
(forth-p4-key-accept-tests)
(forth-p4-base-tests)
(forth-p4-sp-tests)
(dict
"passed"
forth-p4-passed

View File

@@ -81,7 +81,7 @@ Representation:
- [x] `S"`, `C"`, `."`, `TYPE`, `COUNT`, `CMOVE`, `FILL`, `BLANK`
- [x] `CHAR`, `[CHAR]`, `KEY`, `ACCEPT`
- [x] `BASE` manipulation: `DECIMAL`, `HEX`
- [ ] `DEPTH`, `SP@`, `SP!`
- [x] `DEPTH`, `SP@`, `SP!`
- [ ] Drive Hayes Core pass-rate up
### Phase 5 — Core Extension + optional word sets
@@ -99,6 +99,13 @@ Representation:
_Newest first._
- **Phase 4 — `SP@`/`SP!` (+4; Hayes unchanged; `DEPTH` was already present).**
`SP@` pushes the current data-stack depth (our closest analogue to a
stack pointer — SX lists have no addressable backing). `SP!` pops a
target depth and truncates the stack via `drop` on the dstack list.
This preserves the save/restore idiom `SP@ … SP!` even though the
returned "pointer" is really a count.
- **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.