Skip to content

Commit

Permalink
Rewrite with continuation passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramarren committed Oct 23, 2008
1 parent d9a26fb commit ad1cb46
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
25 changes: 12 additions & 13 deletions basic.lisp
Expand Up @@ -9,21 +9,20 @@

;;; lazy results
(defclass parse-result ()
((top-results :initform nil :initarg :top-results :accessor top-results-of)
(promise-list :initform nil :initarg :promise-list :accessor promise-list-of)))
((current-result :initform nil :initarg :top-results :accessor current-result-of)
(continuation :initform (constantly nil) :initarg :promise-list :accessor continuation-of)))

(defun current-result (parser-result)
(with-accessors ((current-result current-result-of)
(continuation continuation-of)) parse-result
(if current-result
current-result
(setf current-result (funcall continuation)))))

(defun next-result (parse-result)
(with-accessors ((top-results top-results-of)
(promise-list promise-list-of)) parse-result
(cond ((and (null top-results)
(null promise-list))
nil)
((and (null top-results)
promise-list)
(setf top-results (force (pop promise-list)))
(next-result parse-result))
(top-results
(pop top-results)))))
(with-accessors ((current-result current-result-of)
(continuation continuation-of)) parse-result
(setf current-result (funcall continuation))))

(defun gather-results (parse-result)
(iter (for result next (next-result parse-result))
Expand Down
19 changes: 11 additions & 8 deletions primitives.lisp
Expand Up @@ -54,8 +54,8 @@
(delay
#'(lambda (inp)
(make-instance 'parse-result
:top-results (list (make-instance 'parser-possibility
:tree v :suffix inp))))))
:current-result (make-instance 'parser-possibility
:tree v :suffix inp)))))

(def-cached-parser zero
"Primitive parser: parsing failure"
Expand All @@ -68,8 +68,8 @@
#'(lambda (inp)
(if inp
(make-instance 'parse-result
:top-results (list (make-instance 'parser-possibility
:tree (car inp) :suffix (cdr inp))))
:current-result (make-instance 'parser-possibility
:tree (car inp) :suffix (cdr inp)))
(make-instance 'parse-result)))))

(declaim (inline sat))
Expand All @@ -83,7 +83,10 @@
(defun force? (parser)
"Parser modifier: fully realize result from parser"
(delay
#'(lambda (inp)
(let ((result (funcall parser inp)))
(make-instance 'parse-result
:top-results (gather-results result))))))
#'(lambda (inp)
(let ((result (funcall parser inp)))
(let ((all-results (gather-results result)))
(make-instance 'parse-result
:current-result (car all-results)
:continuation #'(lambda ()
(pop all-results))))))))

0 comments on commit ad1cb46

Please sign in to comment.