(seq-item
(map-in-order (lambda (p) (p))
deferred-items)))
- spar-discard-elt
- (spar* spar-push-deferred-classified-elt)
+ (spar-elt)
+ (spar* (spar-elt spar-push-deferred-classified))
spar-match-null))))
(define :if
(spar-promise->runtime
(delay
(spar-call-with-values if-item
- spar-discard-elt
- spar-push-classified-elt
- spar-push-classified-elt
- (spar-alt spar-push-classified-elt
- (spar-push-thunk-value unspecific-item))
+ (spar-elt)
+ (spar-elt spar-push-classified)
+ (spar-elt spar-push-classified)
+ (spar-or (spar-elt spar-push-classified)
+ (spar-push-value unspecific-item))
spar-match-null))))
(define :quote
(spar-promise->runtime
(delay
(spar-call-with-values constant-item
- spar-discard-elt
- (spar-elt (spar-push-mapped-form strip-syntactic-closures))
+ (spar-elt)
+ (spar-elt (spar-push-value strip-syntactic-closures spar-arg:form))
spar-match-null))))
(define :quote-identifier
(spar-promise->runtime
(delay
(spar-call-with-values quoted-id-item
- spar-discard-elt
- (spar-elt (spar-push-mapped-full lookup-identifier))
+ (spar-elt)
+ (spar-elt
+ (spar-match identifier? spar-arg:form)
+ (spar-push-value lookup-identifier spar-arg:form spar-arg:senv)
+ (spar-or (spar-match var-item? spar-arg:value)
+ (spar-error "Can't quote a keyword identifier:"
+ spar-arg:form)))
spar-match-null))))
-
+\f
(define :set!
(spar-promise->runtime
(delay
(access-assignment-item (access-item-name lhs-item)
(access-item-env lhs-item)
rhs-item)))
- spar-discard-elt
- spar-push-classified-elt
- (spar-match-value
- (lambda (lhs-item)
- (or (var-item? lhs-item)
- (access-item? lhs-item))))
- (spar-alt spar-push-classified-elt
- (spar-push-thunk-value unassigned-item))
+ (spar-elt)
+ (spar-elt
+ spar-push-classified
+ (spar-or (spar-match (lambda (lhs-item)
+ (or (var-item? lhs-item)
+ (access-item? lhs-item)))
+ spar-arg:value)
+ (spar-error "Variable required in this context:"
+ spar-arg:form)))
+ (spar-or (spar-elt spar-push-classified)
+ (spar-push-value unassigned-item))
spar-match-null))))
;; TODO: this is a classifier rather than a macro because it uses the
(spar-promise->runtime
(delay
(spar-encapsulate-values or-item
- spar-discard-elt
- (spar* spar-push-classified-elt)
+ (spar-elt)
+ (spar* (spar-elt spar-push-classified))
+ spar-match-null))))
+
+(define :delay
+ (spar-promise->runtime
+ (delay
+ (spar-call-with-values delay-item
+ (spar-elt)
+ (spar-elt spar-push-deferred-classified)
spar-match-null))))
\f
;;;; Definitions
(spar-promise->keyword
(delay
(spar-call-with-values defn-item
- spar-discard-elt
+ (spar-elt)
(spar-elt
- (spar-match-form identifier?)
- (spar-push-mapped-full bind-variable))
- spar-push-classified-elt
+ (spar-match identifier? spar-arg:form)
+ (spar-push-value bind-variable spar-arg:form spar-arg:senv))
+ (spar-elt spar-push-classified)
spar-match-null))))
(define :define-syntax
(senv-top-level? senv))
(syntax-defn-item id (keyword-item-expr item))
(seq-item '()))))
- spar-discard-elt
- spar-push-id-elt
- spar-push-senv
- spar-push-classified-elt
- (spar-match-value keyword-item?)
+ (spar-elt)
+ (spar-elt spar-push-id)
+ (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))))
;;;; Lambdas
(spar-call-with-values
(lambda (bvl body senv)
(assemble-lambda-item scode-lambda-name:unnamed bvl body senv))
- spar-discard-elt
- (spar-elt (spar-match-form mit-lambda-list?)
- spar-push-form)
- spar-push-body
- spar-push-senv))))
+ (spar-elt)
+ (spar-elt
+ (spar-match mit-lambda-list? spar-arg:form)
+ (spar-push spar-arg:form))
+ spar-push-body))))
(define :named-lambda
(spar-promise->runtime
(spar-call-with-values
(lambda (name bvl body senv)
(assemble-lambda-item (identifier->symbol name) bvl body senv))
- spar-discard-elt
- (spar-elt spar-push-id-elt
- (spar-match-form mit-lambda-list?)
- spar-push-form)
- spar-push-body
- spar-push-senv))))
+ (spar-elt)
+ (spar-elt
+ (spar-elt spar-push-id)
+ (spar-match mit-lambda-list? spar-arg:form)
+ (spar-push spar-arg:form))
+ spar-push-body))))
(define (assemble-lambda-item name bvl body senv)
(let ((frame-senv (make-internal-senv senv)))
bvl)
(lambda ()
(body-item (body frame-senv))))))
-
-(define :delay
- (spar-promise->runtime
- (delay
- (spar-call-with-values delay-item
- spar-discard-elt
- spar-push-deferred-classified-elt
- spar-match-null))))
\f
;;;; LET-like
(let-item ids
(map cdr bindings)
(body-item (body frame-senv)))))
- spar-discard-elt
+ (spar-elt)
(spar-elt
(spar-push-values
(spar*
(spar-call-with-values cons
- (spar-elt spar-push-id-elt
- spar-push-classified-elt
+ (spar-elt (spar-elt spar-push-id)
+ (spar-elt spar-push-classified)
spar-match-null))))
spar-match-null)
- spar-push-body
- spar-push-senv))))
+ spar-push-body))))
(define spar-promise:let-syntax
(delay
(bind-keyword (car binding) frame-senv (cdr binding)))
bindings)
(seq-item (body frame-senv))))
- spar-discard-elt
+ (spar-elt)
(spar-elt
- (spar-push-values
- (spar*
- (spar-call-with-values cons
- (spar-elt spar-push-id-elt
- spar-push-classified-elt
- spar-match-null))))
- spar-match-null)
- spar-push-body
- spar-push-senv)))
+ (spar-push-values
+ (spar*
+ (spar-call-with-values cons
+ (spar-elt (spar-elt spar-push-id)
+ (spar-elt spar-push-classified)
+ spar-match-null))))
+ spar-match-null)
+ spar-push-body)))
(define :let-syntax
(spar-promise->runtime spar-promise:let-syntax))
((cdr binding) frame-senv))
bindings))
(seq-item (body frame-senv))))
- spar-discard-elt
+ (spar-elt)
(spar-elt
(spar-push-values
(spar*
(spar-call-with-values cons
- (spar-elt spar-push-id-elt
- spar-push-open-classified-elt
+ (spar-elt (spar-elt spar-push-id)
+ (spar-elt spar-push-open-classified)
spar-match-null))))
spar-match-null)
- spar-push-body
- spar-push-senv))))
+ spar-push-body))))
\f
;;;; MIT-specific syntax
(spar-promise->keyword
(delay
(spar-call-with-values access-item
- spar-discard-elt
- spar-push-id-elt
- spar-push-classified-elt
+ (spar-elt)
+ (spar-elt spar-push-id)
+ (spar-elt spar-push-classified)
spar-match-null))))
(define-item-compiler access-item?
(spar-promise->runtime
(delay
(spar-seq
- (spar-match-senv senv-top-level?)
- spar-discard-elt
+ (spar-or (spar-match senv-top-level? spar-arg:senv)
+ (spar-error "This form allowed only at top level:"
+ spar-arg:form spar-arg:senv))
+ (spar-elt)
spar-match-null
- (spar-push-thunk-value the-environment-item)))))
+ (spar-push-value the-environment-item)))))
(define keyword:unspecific
(spar-promise->keyword
(delay
(spar-seq
- spar-discard-elt
+ (spar-elt)
spar-match-null
- (spar-push-thunk-value unspecific-item)))))
+ (spar-push-value unspecific-item)))))
(define keyword:unassigned
(spar-promise->keyword
(delay
(spar-seq
- spar-discard-elt
+ (spar-elt)
spar-match-null
- (spar-push-thunk-value unassigned-item)))))
-
+ (spar-push-value unassigned-item)))))
+\f
;;;; Declarations
(define :declare
(spar-promise->runtime
(delay
(spar-call-with-values
- (lambda (decls senv hist)
+ (lambda (senv hist decls)
(decl-item
(lambda ()
(smap (lambda (decl hist)
decl))
decls
(hist-cadr hist)))))
- spar-discard-elt
+ (spar-elt)
+ (spar-push spar-arg:senv)
+ (spar-push spar-arg:hist)
(spar-push-values
(spar*
(spar-elt
- (spar-match-form
- (lambda (form)
- (and (pair? form)
- (identifier? (car form))
- (list? (cdr form)))))
- spar-push-form)))
- spar-match-null
- spar-push-senv
- spar-push-hist))))
+ (spar-match (lambda (form)
+ (and (pair? form)
+ (identifier? (car form))
+ (list? (cdr form))))
+ spar-arg:form)
+ (spar-push spar-arg:form))))
+ spar-match-null))))
(define (classify-id id senv hist)
(let ((item (classify-form id senv hist)))
\f
;;;; Primitives
-(define (spar-match-form predicate)
+(define (%subst-args input senv output args)
+ (map (lambda (arg)
+ (%subst-arg input senv output arg))
+ args))
+
+(define (%subst-arg input senv output arg)
+ (cond ((eq? arg spar-arg:form) (%input-form input))
+ ((eq? arg spar-arg:hist) (%input-hist input))
+ ((eq? arg spar-arg:senv) senv)
+ ((eq? arg spar-arg:value) (%output-top output))
+ ((eq? arg spar-arg:values) (%output-all output))
+ (else arg)))
+
+(define-deferred spar-arg:form (string->uninterned-symbol ".form."))
+(define-deferred spar-arg:hist (string->uninterned-symbol ".hist."))
+(define-deferred spar-arg:senv (string->uninterned-symbol ".senv."))
+(define-deferred spar-arg:value (string->uninterned-symbol ".value."))
+(define-deferred spar-arg:values (string->uninterned-symbol ".values."))
+
+(define (spar-match predicate . args)
(lambda (input senv output success failure)
- (if (predicate (%input-form input))
+ (if (apply predicate (%subst-args input senv output args))
(success input senv output failure)
(failure))))
-(define (spar-match-senv predicate)
- (lambda (input senv output success failure)
- (if (predicate senv)
- (success input senv output failure)
- (failure))))
-
-(define (spar-match-full predicate)
- (lambda (input senv output success failure)
- (if (predicate (%input-form input) senv)
- (success input senv output failure)
- (failure))))
-
-(define (spar-match-value predicate)
- (lambda (input senv output success failure)
- (if (predicate (%output-top output))
- (success input senv output failure)
- (failure))))
-
-(define (spar-discard-form input senv output success failure)
- (declare (ignore input))
- (success (%null-input) senv output failure))
-
-(define (spar-push-form input senv output success failure)
- (success (%null-input)
- senv
- (%output-push output (%input-form input))
- failure))
-
-(define (spar-push-hist input senv output success failure)
- (success (%null-input)
- senv
- (%output-push output (%input-hist input))
- failure))
-
-(define (spar-push-senv input senv output success failure)
- (success input
- senv
- (%output-push output senv)
- failure))
-
-(define (spar-push-datum object)
+(define (spar-push . args)
(lambda (input senv output success failure)
(success input
senv
- (%output-push output object)
+ (%output-push-all output (%subst-args input senv output args))
failure)))
-(define (spar-push-thunk-value procedure)
+(define (spar-push-value procedure . args)
(lambda (input senv output success failure)
(success input
senv
- (%output-push output (procedure))
- failure)))
-
-(define (spar-push-mapped-form procedure)
- (lambda (input senv output success failure)
- (success (%null-input)
- senv
- (%output-push output (procedure (%input-form input)))
+ (%output-push output
+ (apply procedure
+ (%subst-args input senv output args)))
failure)))
-(define (spar-push-mapped-full procedure)
+(define (spar-error message . irritants)
(lambda (input senv output success failure)
- (success (%null-input)
- senv
- (%output-push output (procedure (%input-form input) senv))
- failure)))
+ (declare (ignore success failure))
+ (apply serror
+ (%input-form input)
+ senv
+ (%input-hist input)
+ message
+ (%subst-args input senv output irritants))))
-(define (%push-classified procedure)
- (lambda (input senv output success failure)
- (success (%null-input)
- senv
- (%output-push output
- (procedure (%input-form input)
- senv
- (%input-hist input)))
- failure)))
+(define (spar-discard-form input senv output success failure)
+ (declare (ignore input))
+ (success (%null-input) senv output failure))
\f
;;;; Repeat combinators
(s2 input* senv* output* success failure*))
failure)))
-(define (spar-alt . spars)
+(define (spar-or . spars)
(cond ((not (pair? spars)) spar-fail)
((not (pair? (cdr spars))) (car spars))
- (else (reduce-right %alt-combiner #f spars))))
+ (else (reduce-right %or-combiner #f spars))))
-(define (%alt-combiner s1 s2)
+(define (%or-combiner s1 s2)
(lambda (input senv output success failure)
(s1 input senv output success
(lambda ()
failure)
(failure)))))
-(define spar-discard-elt
- (spar-elt spar-discard-form))
-
-(define spar-match-null
- (spar-match-form null?))
-
-(define spar-push-elt
- (spar-elt spar-push-form))
+(define-deferred spar-match-null
+ (spar-match null? spar-arg:form))
;;;; Environment combinators
(success input* senv output* failure*))
failure))))
\f
-(define spar-push-closed-form
- (spar-push-mapped-full
- (lambda (form senv)
- (make-syntactic-closure senv '() form))))
-
-(define spar-push-closed-elt
- (spar-elt spar-push-closed-form))
-
-(define spar-push-partially-closed-form
- (spar-push-mapped-full
- (lambda (form senv)
- (lambda (free)
- (make-syntactic-closure senv free form)))))
-
-(define spar-push-partially-closed-elt
- (spar-elt spar-push-partially-closed-form))
-
-(define-deferred spar-push-classified-form
- (%push-classified classify-form))
-
-(define-deferred spar-push-classified-elt
- (spar-elt spar-push-classified-form))
-
-(define spar-push-deferred-classified-form
- (%push-classified
- (lambda (form senv hist)
- (lambda ()
- (classify-form form senv hist)))))
-
-(define spar-push-deferred-classified-elt
- (spar-elt spar-push-deferred-classified-form))
-
-(define spar-push-open-classified-form
- (%push-classified
- (lambda (form senv hist)
- (declare (ignore senv))
- (lambda (senv*)
- (classify-form form senv* hist)))))
-
-(define spar-push-open-classified-elt
- (spar-elt spar-push-open-classified-form))
-
-(define-deferred spar-push-id-elt
- (spar-elt (spar-match-form identifier?)
- spar-push-form))
-
-(define (spar-push-id-elt= id)
- (spar-elt (spar-match-full
- (lambda (form senv)
- (and (identifier? form)
- (identifier=? senv form senv id))))
- spar-push-form))
+(define-deferred spar-push-closed
+ (spar-push-value make-syntactic-closure
+ spar-arg:senv
+ '()
+ spar-arg:form))
+
+(define-deferred spar-push-partially-closed
+ (spar-push-value (lambda (senv form)
+ (lambda (free)
+ (make-syntactic-closure senv free form)))
+ spar-arg:senv
+ spar-arg:form))
+
+(define-deferred spar-push-classified
+ (spar-push-value classify-form
+ spar-arg:form
+ spar-arg:senv
+ spar-arg:hist))
+
+(define-deferred spar-push-deferred-classified
+ (spar-push-value (lambda (form senv hist)
+ (lambda ()
+ (classify-form form senv hist)))
+ spar-arg:form
+ spar-arg:senv
+ spar-arg:hist))
+
+(define-deferred spar-push-open-classified
+ (spar-push-value (lambda (form senv hist)
+ (declare (ignore senv))
+ (lambda (senv*)
+ (classify-form form senv* hist)))
+ spar-arg:form
+ spar-arg:senv
+ spar-arg:hist))
+
+(define-deferred spar-push-id
+ (spar-seq
+ (spar-match identifier? spar-arg:form)
+ (spar-push spar-arg:form)
+ spar-discard-form))
+
+(define (spar-push-id= id)
+ (spar-seq
+ (spar-match (lambda (form senv)
+ (and (identifier? form)
+ (identifier=? senv form senv id)))
+ spar-arg:form
+ spar-arg:senv)
+ (spar-push spar-arg:form)
+ spar-discard-form))
\f
;;;; Value combinators
failure*))
failure))))
-(define spar-push-body
- (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-push-open-classified-elt)
- spar-match-null))
\ No newline at end of file
+(define-deferred spar-push-body
+ (spar-seq
+ (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)
+ (spar-push spar-arg:senv)))
\ No newline at end of file