live/rpn-live.ss
#lang scheme/base

;; A late-bound dialect of the scat: language.  Identifiers are
;; translated using the (live ...) mapper macro into code that
;; performs lookup and interpretation at runtime.

;; This language is used to implement the target: language which has
;; similar late-bound semantics.

(require
 "../tools.ss"
 "../scat.ss"
 "../rpn.ss"
 "../ns.ss"
 (for-syntax
  "../ns-tx.ss"
  scheme/base))

(provide (all-defined-out))


(define (make-ns-defined? sym)
  (lambda (ns)
    (namespace-variable-value
     (ns-name ns sym) #t (lambda () #f))))

(define (live-interpret sym)
  (define defined? (make-ns-defined? sym))
  (cond
   ((defined? '(scat)) => (lambda (x) x))
   ((defined? '())     => (lambda (x) (scat-wrap-dynamic x)))
   (else (error 'undefined "~s" sym))))
   
;; Same as scat: but all identifiers are late-bound.  The main purpose
;; of this macro is to support the target: language which also employs
;; late binding.

(define-syntax-rule (live: code ...)
  (make-word
   (rpn-parse (rpn:-compile    ;; dictionary compiler
               (live)          ;; namespace lookup
               scat-apply      ;; function
               scat-push       ;; immediate
               scat-push       ;; immediate program
               live:           ;; anonymous compiler for recursive parse
               (rpn-lambda)    ;; dictionary init (only one anonymous entry)           
               ) code ...)))


(define-syntax (live stx)
  (syntax-case stx ()
    ((_ id)
     (identifier? #'id)
     #'(live-interpret 'id))))