- (labels ((type-dispatch (name args &rest spec)
- (acond ((find :invalid args :key #'car)
- (cons :invalid nil))
- ((find-if (lambda (item)
- (every (lambda (type arg)
- (eql type (car arg)))
- (cddr item)
- args))
- spec)
- (cons (car it) (apply (cadr it)
- (mapcar #'cdr args))))
- (t
- (cerror* "Type mismatch: operator `~A' applied to ~
- types ~{~(~A~)~#[~; and ~;, ~]~}"
- name
- (mapcar #'car args))
- (cons :invalid nil))))
- (add (x y) (type-dispatch "+" (list x y)
- (list :integer #'+ :integer :integer)))
- (sub (x y) (type-dispatch "-" (list x y)
- (list :integer #'- :integer :integer)))
- (mul (x y) (type-dispatch "*" (list x y)
- (list :integer #'* :integer :integer)))
- (div (x y) (type-dispatch "/" (list x y)
- (list :integer
- (lambda (x y)
- (cond ((zerop y)
- (cerror*
- "Division by zero")
- (cons :invalid nil))
- (t
- (floor x y))))
- :integer :integer)))
- (nop (x) (type-dispatch "+" (list x)
- (list :integer #'+ :integer)))
- (neg (x) (type-dispatch "-" (list x)
- (list :integer #'- :integer))))
-
- (with-parser-context (token-scanner-context :scanner scanner)
- (parse (expr (lisp (flet ((prop (type value)
- (scanner-step scanner)
- (values (cons type value) t t)))
- (case (token-type scanner)
- (:int
- (prop :integer (token-value scanner)))
- ((:id :char :string)
- (prop (token-type scanner) (token-value scanner)))
- (#\?
- (let* ((stream (make-scanner-stream scanner))
- (sexp (read stream t)))
- (scanner-step scanner)
- (values (cons (property-type sexp) sexp)
- t t)))
- (t
- (values (list :int :id :char :string #\?)
- nil nil)))))
-
+ ;; The expression parser works in two stages. First, the parser proper
+ ;; builds a thunk as its `value'. If this is successful, then the thunk is
+ ;; invoked to return a property type and value. Primitive expressions
+ ;; produce thunks which just return their values; operators combine their
+ ;; argument thunks together, evaluating them (or not) on demand.
+
+ (macrolet ((oplambda (&body body)
+ ;; Like `lambda', but (a) always produces a function with no
+ ;; arguments, and (b) captures the current location so that
+ ;; errors are attributed correctly.
+
+ (with-gensyms (floc)
+ `(let ((,floc (file-location scanner)))
+ (lambda ()
+ (with-default-error-location (,floc)
+ ,@body))))))
+
+ (flet ((dispatch (name args &rest spec)
+ ;; Evaluate the ARGS to determine their types and values. Find
+ ;; the first SPEC, of the form (RETTY OP ARGTY*), where the
+ ;; ARGTYs match the argument types, in order, and apply OP to
+ ;; the argument values, return this as a result of type RETTY.
+ ;; If no SPEC matches, then report an error.
+
+ (oplambda
+ (let ((args (mapcar (compose #'funcall #'cons) args)))
+ (aif (find-if (lambda (item)
+ (every (lambda (type arg)
+ (eql type (car arg)))
+ (cddr item) args))
+ spec)
+ (values (car it) (apply (cadr it) (mapcar #'cdr args)))
+ (error "Type mismatch: operator `~A' applied to ~
+ types ~{~(~A~)~#[~; and ~;, ~]~}"
+ name (mapcar #'car args)))))))
+
+ (with-parser-context (token-scanner-context :scanner scanner)
+ (when-parse ()
+
+ ;; Parse the expression, producing a thunk.
+ (expr (:nestedp nestedp)
+
+ (lisp (case (token-type scanner)
+
+ ((:int :id :char :string)
+ ;; A simple literal.
+ (let ((type (token-type scanner))
+ (value (token-value scanner)))
+ (scanner-step scanner)
+ (values (lambda () (values type value)) t t)))