(transformer->keyword-item
(transformer-eval transformer senv)
senv
- (expr-item
- (lambda ()
- (output/top-level-syntax-expander transformer->expander-name
- transformer)))))))
+ (expr-item (serror-ctx form senv hist)
+ (lambda ()
+ (output/top-level-syntax-expander transformer->expander-name
+ transformer)))))))
(define :sc-macro-transformer
;; "Syntactic Closures" transformer
(define :begin
(spar-classifier->runtime
(delay
- (spar-encapsulate-values
- (lambda (deferred-items)
- (seq-item
- (map-in-order (lambda (p) (p))
- deferred-items)))
+ (spar-call-with-values
+ (lambda (ctx . deferred-items)
+ (seq-item ctx
+ (map-in-order (lambda (p) (p))
+ deferred-items)))
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar* (spar-elt spar-push-deferred-classified))
(spar-match-null)))))
(delay
(spar-call-with-values if-item
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-elt spar-push-classified)
(spar-elt spar-push-classified)
(spar-or (spar-elt spar-push-classified)
- (spar-push-value unspecific-item))
+ (spar-push-value unspecific-item spar-arg:ctx))
(spar-match-null)))))
(define :quote
(delay
(spar-call-with-values constant-item
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-elt (spar-push-value strip-syntactic-closures spar-arg:form))
(spar-match-null)))))
(delay
(spar-call-with-values quoted-id-item
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-elt
(spar-match identifier? spar-arg:form)
(spar-push-value lookup-identifier spar-arg:form spar-arg:senv)
(spar-classifier->runtime
(delay
(spar-call-with-values
- (lambda (lhs-item rhs-item)
+ (lambda (ctx lhs-item rhs-item)
(if (var-item? lhs-item)
- (assignment-item (var-item-id lhs-item) rhs-item)
- (access-assignment-item (access-item-name lhs-item)
+ (assignment-item ctx (var-item-id lhs-item) rhs-item)
+ (access-assignment-item ctx
+ (access-item-name lhs-item)
(access-item-env lhs-item)
rhs-item)))
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-elt
spar-push-classified
(spar-or (spar-match (lambda (lhs-item)
(spar-error "Variable required in this context:"
spar-arg:form)))
(spar-or (spar-elt spar-push-classified)
- (spar-push-value unassigned-item))
+ (spar-push-value unassigned-item spar-arg:ctx))
(spar-match-null)))))
;; TODO: this is a classifier rather than a macro because it uses the
(define :or
(spar-classifier->runtime
(delay
- (spar-encapsulate-values or-item
+ (spar-call-with-values or-item
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar* (spar-elt spar-push-classified))
(spar-match-null)))))
(delay
(spar-call-with-values delay-item
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-elt spar-push-deferred-classified)
(spar-match-null)))))
\f
(delay
(spar-call-with-values defn-item
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-elt
(spar-match identifier? spar-arg:form)
(spar-push-value bind-variable spar-arg:form spar-arg:senv))
(spar-classifier->runtime
(delay
(spar-call-with-values
- (lambda (id senv item)
+ (lambda (ctx id item)
(receive (id senv)
(if (closed-identifier? id)
(values (syntactic-closure-form id)
(syntactic-closure-senv id))
- (values id senv))
+ (values id (serror-ctx-senv ctx)))
(bind-keyword id senv item)
;; User-defined macros at top level are preserved in the output.
(if (and (keyword-item-has-expr? item)
(senv-top-level? senv))
- (syntax-defn-item id (keyword-item-expr item))
- (seq-item '()))))
+ (syntax-defn-item ctx id (keyword-item-expr item))
+ (seq-item ctx '()))))
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-push-elt-if identifier? spar-arg:form)
- (spar-push spar-arg:senv)
(spar-elt
spar-push-classified
(spar-or (spar-match keyword-item? spar-arg:value)
(spar-error "Keyword binding value must be a keyword:"
spar-arg:form)))
(spar-match-null)))))
-
+\f
;;;; Lambdas
(define :lambda
(spar-classifier->runtime
(delay
(spar-call-with-values
- (lambda (bvl body senv)
- (assemble-lambda-item scode-lambda-name:unnamed bvl body senv))
+ (lambda (ctx bvl body-ctx body)
+ (assemble-lambda-item ctx scode-lambda-name:unnamed bvl
+ body-ctx body))
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-push-elt-if mit-lambda-list? spar-arg:form)
- spar-push-body))))
+ (spar-push-body)))))
(define :named-lambda
(spar-classifier->runtime
(delay
(spar-call-with-values
- (lambda (name bvl body senv)
- (assemble-lambda-item (identifier->symbol name) bvl body senv))
+ (lambda (ctx name bvl body-ctx body)
+ (assemble-lambda-item ctx (identifier->symbol name) bvl
+ body-ctx body))
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-elt
(spar-push-elt-if identifier? spar-arg:form)
(spar-push-form-if mit-lambda-list? spar-arg:form))
- spar-push-body))))
-
-(define (assemble-lambda-item name bvl body senv)
- (let ((frame-senv (make-internal-senv senv)))
- (lambda-item name
+ (spar-push-body)))))
+
+(define (spar-push-body)
+ (spar-and
+ (spar-push spar-arg:ctx)
+ (spar-encapsulate-values
+ (lambda (elts)
+ (lambda (frame-senv)
+ (let ((body-senv (make-internal-senv frame-senv)))
+ (map-in-order (lambda (elt) (elt body-senv))
+ elts))))
+ (spar+ (spar-elt spar-push-open-classified))
+ (spar-match-null))))
+
+(define (assemble-lambda-item ctx name bvl body-ctx body)
+ (let ((frame-senv (make-internal-senv (serror-ctx-senv ctx))))
+ (lambda-item ctx
+ name
(map-mit-lambda-list (lambda (id)
(bind-variable id frame-senv))
bvl)
(lambda ()
- (body-item (body frame-senv))))))
+ (body-item body-ctx (body frame-senv))))))
\f
;;;; LET-like
(define spar-promise:let-syntax
(delay
(spar-call-with-values
- (lambda (bindings body senv)
- (let ((frame-senv (make-internal-senv senv)))
+ (lambda (ctx bindings body-ctx body)
+ (let ((frame-senv (make-internal-senv (serror-ctx-senv ctx))))
(for-each (lambda (binding)
(bind-keyword (car binding) frame-senv (cdr binding)))
bindings)
- (seq-item (body frame-senv))))
+ (seq-item body-ctx (body frame-senv))))
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-elt
(spar-call-with-values list
(spar*
(spar-elt spar-push-classified)
(spar-match-null)))))
(spar-match-null))
- spar-push-body)))
+ (spar-push-body))))
(define :let-syntax
(spar-classifier->runtime spar-promise:let-syntax))
(spar-classifier->runtime
(delay
(spar-call-with-values
- (lambda (bindings body senv)
- (let ((frame-senv (make-internal-senv senv))
+ (lambda (ctx bindings body-ctx body)
+ (let ((frame-senv (make-internal-senv (serror-ctx-senv ctx)))
(ids (map car bindings)))
(for-each (lambda (id)
(reserve-identifier id frame-senv))
(map (lambda (binding)
((cdr binding) frame-senv))
bindings))
- (seq-item (body frame-senv))))
+ (seq-item body-ctx (body frame-senv))))
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-elt
(spar-call-with-values list
(spar*
(spar-elt spar-push-open-classified)
(spar-match-null)))))
(spar-match-null))
- spar-push-body))))
+ (spar-push-body)))))
\f
;;;; MIT-specific syntax
(define-record-type <access-item>
- (access-item name env)
+ (access-item ctx name env)
access-item?
+ (ctx access-item-ctx)
(name access-item-name)
(env access-item-env))
(delay
(spar-call-with-values access-item
(spar-elt)
+ (spar-push spar-arg:ctx)
(spar-push-elt-if identifier? spar-arg:form)
(spar-elt spar-push-classified)
(spar-match-null)))))
spar-arg:form spar-arg:senv))
(spar-elt)
(spar-match-null)
- (spar-push-value the-environment-item)))))
+ (spar-push-value the-environment-item spar-arg:ctx)))))
(define keyword:unspecific
(spar-classifier->keyword
(spar-and
(spar-elt)
(spar-match-null)
- (spar-push-value unspecific-item)))))
+ (spar-push-value unspecific-item spar-arg:ctx)))))
(define keyword:unassigned
(spar-classifier->keyword
(spar-and
(spar-elt)
(spar-match-null)
- (spar-push-value unassigned-item)))))
+ (spar-push-value unassigned-item spar-arg:ctx)))))
\f
;;;; Declarations
(spar-classifier->runtime
(delay
(spar-call-with-values
- (lambda (senv hist decls)
- (decl-item
- (lambda ()
- (smap (lambda (decl hist)
- (map-decl-ids (lambda (id selector)
- (classify-id id
- senv
- (hist-select selector hist)))
- decl))
- decls
- (hist-cadr hist)))))
+ (lambda (ctx decls)
+ (let ((senv (serror-ctx-senv ctx))
+ (hist (serror-ctx-hist ctx)))
+ (decl-item ctx
+ (lambda ()
+ (smap (lambda (decl hist)
+ (map-decl-ids (lambda (id selector)
+ (classify-id id
+ senv
+ (hist-select selector
+ hist)))
+ decl))
+ decls
+ (hist-cadr hist))))))
(spar-elt)
- (spar-push spar-arg:senv)
- (spar-push spar-arg:hist)
+ (spar-push spar-arg:ctx)
(spar-call-with-values list
(spar*
(spar-push-elt-if (lambda (form)
(if (not (var-item? item))
(serror (serror-ctx id senv hist)
"Variable required in this context:" id))
- (var-item-id item)))
\ No newline at end of file
+ (var-item-id item)))
+\f
+;;;; Specific expression items
+
+(define (access-assignment-item ctx name env-item rhs-item)
+ (expr-item ctx
+ (lambda ()
+ (output/access-assignment name
+ (compile-expr-item env-item)
+ (compile-expr-item rhs-item)))))
+
+(define (assignment-item ctx id rhs-item)
+ (expr-item ctx
+ (lambda ()
+ (output/assignment id (compile-expr-item rhs-item)))))
+
+(define (decl-item ctx classify)
+ (expr-item ctx
+ (lambda ()
+ (output/declaration (classify)))))
+
+(define (delay-item ctx classify)
+ (expr-item ctx
+ (lambda ()
+ (output/delay (compile-expr-item (classify))))))
+
+(define (if-item ctx predicate consequent alternative)
+ (expr-item ctx
+ (lambda ()
+ (output/conditional (compile-expr-item predicate)
+ (compile-expr-item consequent)
+ (compile-expr-item alternative)))))
+
+(define (lambda-item ctx name bvl classify-body)
+ (expr-item ctx
+ (lambda ()
+ (output/lambda name bvl (compile-item (classify-body))))))
+
+(define (or-item ctx . items)
+ (expr-item ctx
+ (lambda ()
+ (output/disjunction (map compile-expr-item items)))))
+
+(define (quoted-id-item ctx var-item)
+ (expr-item ctx
+ (lambda ()
+ (output/quoted-identifier (var-item-id var-item)))))
+
+(define (the-environment-item ctx)
+ (expr-item ctx output/the-environment))
+
+(define (unspecific-item ctx)
+ (expr-item ctx output/unspecific))
+
+(define (unassigned-item ctx)
+ (expr-item ctx output/unassigned))
\ No newline at end of file
(declare (usual-integrations))
\f
-;;; These items can be stored in a syntactic environment.
+;;; These items (and keyword-item) can be stored in a syntactic environment.
;;; Variable items represent run-time variables.
;;; Definition items, whether top-level or internal, keyword or variable.
-(define (syntax-defn-item id value)
+(define (syntax-defn-item ctx id value)
(guarantee identifier? id 'syntax-defn-item)
(guarantee defn-item-value? value 'syntax-defn-item)
- (%defn-item id value #t))
+ (%defn-item ctx id value #t))
-(define (defn-item id value)
+(define (defn-item ctx id value)
(guarantee identifier? id 'defn-item)
(guarantee defn-item-value? value 'defn-item)
- (%defn-item id value #f))
+ (%defn-item ctx id value #f))
(define (defn-item-value? object)
(not (reserved-name-item? object)))
(register-predicate! defn-item-value? 'defn-item-value)
(define-record-type <defn-item>
- (%defn-item id value syntax?)
+ (%defn-item ctx id value syntax?)
defn-item?
+ (ctx defn-item-ctx)
(id defn-item-id)
(value defn-item-value)
(syntax? defn-item-syntax?))
;;; Sequence items.
-(define (seq-item elements)
+(define (seq-item ctx elements)
(let ((elements (flatten-items elements)))
(if (and (pair? elements)
(null? (cdr elements)))
(car elements)
- (%seq-item elements))))
+ (%seq-item ctx elements))))
(define-record-type <seq-item>
- (%seq-item elements)
+ (%seq-item ctx elements)
seq-item?
+ (ctx seq-item-ctx)
(elements seq-item-elements))
(define (flatten-items items)
;;; run-time variable or a sequence.
(define-record-type <expr-item>
- (expr-item compiler)
+ (expr-item ctx compiler)
expr-item?
+ (ctx expr-item-ctx)
(compiler expr-item-compiler))
-\f
-;;;; Specific expression items
-
-(define (combination-item operator operands)
- (expr-item
- (lambda ()
- (output/combination (compile-expr-item operator)
- (map compile-expr-item operands)))))
-
-(define (constant-item datum)
- (expr-item
- (lambda ()
- (output/constant datum))))
-
-(define (lambda-item name bvl classify-body)
- (expr-item
- (lambda ()
- (output/lambda name bvl (compile-item (classify-body))))))
-
-(define (let-item names value-items body-item)
- (expr-item
- (lambda ()
- (output/let names
- (map compile-expr-item value-items)
- (compile-item body-item)))))
-
-(define (body-item items)
- (expr-item
- (lambda ()
- (output/body (map compile-item (flatten-items items))))))
-
-(define (if-item predicate consequent alternative)
- (expr-item
- (lambda ()
- (output/conditional (compile-expr-item predicate)
- (compile-expr-item consequent)
- (compile-expr-item alternative)))))
-
-(define (quoted-id-item var-item)
- (expr-item
- (lambda ()
- (output/quoted-identifier (var-item-id var-item)))))
-
-(define (assignment-item id rhs-item)
- (expr-item
- (lambda ()
- (output/assignment id (compile-expr-item rhs-item)))))
-
-(define (access-assignment-item name env-item rhs-item)
- (expr-item
- (lambda ()
- (output/access-assignment name
- (compile-expr-item env-item)
- (compile-expr-item rhs-item)))))
-
-(define (delay-item classify)
- (expr-item
- (lambda ()
- (output/delay (compile-expr-item (classify))))))
-
-(define (or-item items)
- (expr-item
- (lambda ()
- (output/disjunction (map compile-expr-item items)))))
-
-(define (decl-item classify)
- (expr-item
- (lambda ()
- (output/declaration (classify)))))
-
-(define (the-environment-item)
- (expr-item output/the-environment))
-
-(define (unspecific-item)
- (expr-item output/unspecific))
-
-(define (unassigned-item)
- (expr-item output/unassigned))
+
+(define (body-item ctx items)
+ (expr-item ctx
+ (lambda ()
+ (output/body (map compile-item (flatten-items items))))))
+
+(define (combination-item ctx operator operands)
+ (expr-item ctx
+ (lambda ()
+ (output/combination (compile-expr-item operator)
+ (map compile-expr-item operands)))))
+
+(define (constant-item ctx datum)
+ (expr-item ctx
+ (lambda ()
+ (output/constant datum))))
\f
;;;; Compiler