#lang scheme/base
(require
scheme/pretty
scheme/control
scheme/match
"../tools.ss"
"../scat.ss"
"../target.ss")
(provide (all-defined-out))
(define (find-target-word/false name)
(namespace-variable-value
(ns-name '(target) name)
#f (lambda () #f)))
(define (tfind/false name)
(let ((word (find-target-word/false name)))
(and word (target-word-address word))))
(define (tfind name)
(or (tfind/false name)
(error 'target-word-not-found "~s" name)))
(define (target-mapped-symbols)
(filter tfind/false (ns-mapped-symbols '(target))))
(define (set-target-words! words)
(for-each*
(lambda (name realm address)
(let ((word
(eval
`(new-target-word #:name ',name
#:realm ',realm
#:address ,address))))
(eval
`(begin
(define-ns (target) ,name ,word)
(define-ns (macro) ,name
,(case realm
((code) `(scat: ',word compile))
((data) `(scat: ',word literal))))))))
words))
(define (target-words)
(for/list ((name (target-mapped-symbols)))
(let ((word (find-target-word/false name)))
(list name
(target-word-realm word)
(target-word-address word)))))
(define (reverse-lookup dict realm address)
(prompt
(for-each*
(lambda (name r a)
(when (and (eq? r realm)
(eq? a address))
(abort name)))
dict) #f))
(define (write-dict [port (current-output-port)])
(for-each
pretty-print
`(,@(eval '(macros))
(words! ',(target-words))
(console! ',(eval '(current-console)))
(pointers! ',(eval '(pointers)))
)))
(define (load-dict filename)
(load filename))
(define (save-dict filename)
(with-output-to-file/safe filename
(lambda () (write-dict))))
(define (forth-load file)
(eval `(forth-load/compile ,file)))
(snarf as-push (scat)
((x) (tfind forth-load)))