#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.


(provide (all-defined-out))

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

(define (live-interpret sym)
  (define defined? (make-ns-defined? sym))
   ((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 ...)
   (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))))