Recover agent-loop progress: lua/prolog/forth/erlang/haskell phases 1-2
Salvaged from worktree-agent-* branches killed during sx-tree MCP outage: - lua: tokenizer + parser + phase-2 transpile (~157 tests) - prolog: tokenizer + parser + unification (72 tests, plan update lost to WIP) - forth: phase-1 reader/interpreter + phase-2 colon/VARIABLE (134 tests) - erlang: tokenizer + parser (114 tests) - haskell: tokenizer + parse tests (43 tests) Cherry-picked file contents only, not branch history, to avoid pulling in unrelated ocaml-vm merge commits that were in those branches' bases.
This commit is contained in:
48
lib/forth/interpreter.sx
Normal file
48
lib/forth/interpreter.sx
Normal file
@@ -0,0 +1,48 @@
|
||||
;; Forth interpreter loop — interpret mode only (Phase 1).
|
||||
;; Reads whitespace-delimited words, looks them up, executes.
|
||||
;; Numbers (parsed via BASE) push onto the data stack.
|
||||
;; Unknown words raise "?".
|
||||
|
||||
(define
|
||||
forth-execute-word
|
||||
(fn (state word) (let ((body (get word "body"))) (body state))))
|
||||
|
||||
(define
|
||||
forth-interpret-token
|
||||
(fn
|
||||
(state tok)
|
||||
(let
|
||||
((w (forth-lookup state tok)))
|
||||
(if
|
||||
(not (nil? w))
|
||||
(forth-execute-word state w)
|
||||
(let
|
||||
((n (forth-parse-number tok (get state "base"))))
|
||||
(if
|
||||
(not (nil? n))
|
||||
(forth-push state n)
|
||||
(forth-error state (str tok " ?"))))))))
|
||||
|
||||
(define
|
||||
forth-interpret
|
||||
(fn
|
||||
(state src)
|
||||
(for-each
|
||||
(fn (tok) (forth-interpret-token state tok))
|
||||
(forth-tokens src))
|
||||
state))
|
||||
|
||||
;; Convenience: build a fresh state with primitives loaded.
|
||||
(define
|
||||
forth-boot
|
||||
(fn () (let ((s (forth-make-state))) (forth-install-primitives! s) s)))
|
||||
|
||||
;; Run source on a fresh state and return (state, output, stack-top-to-bottom).
|
||||
(define
|
||||
forth-run
|
||||
(fn
|
||||
(src)
|
||||
(let
|
||||
((s (forth-boot)))
|
||||
(forth-interpret s src)
|
||||
(list s (get s "output") (reverse (get s "dstack"))))))
|
||||
Reference in New Issue
Block a user