#lang scheme/base
(require
"../target.ss"
"../scat.ss"
"../ns.ss"
"../rpn.ss"
"../macro.ss"
"tethered.ss"
"commands.ss"
"../forth/forth-lex.ss"
(for-syntax
"../ns-tx.ss"
scheme/base))
(provide target:
target
forth-command)
(define (target-interpret fn state)
(cond
((procedure? fn) (fn state))
((target-word? fn) ((scat: ',(target-word-address fn) texec/w) state))
(else (error 'target-interpret))))
(define (target-simulated macro)
(lambda (state)
(simulate-macro macro) state))
(define-syntax-rule (target-apply fn p sub) (let ((p (target-interpret fn p))) sub))
(define-syntax-rule (target-push im p sub) (let ((p ((scat: 'im >t) p))) sub))
(define-syntax (target stx)
(syntax-case stx ()
((_ id) (and (identifier? #'id)
(not (identifier-binding
(ns-prefixed #'(target) #'id))))
#`(target-simulated (macro id)))
((_ form)
#`(ns (target) form))))
(define-syntax-rule (target: code ...)
(rpn-parse (rpn:-compile
(target)
target-apply
target-push
target-apply target:
(rpn-lambda)
) code ...))
(define-syntax-rule (target> code ...)
(void ((target: code ...) (state:stack))))
(define-syntax-rule (forth-command str)
(forth-lex-string/cps target> str))