forked from Ramarren/cl-parser-combinators
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemoize.lisp
More file actions
15 lines (14 loc) · 756 Bytes
/
memoize.lisp
File metadata and controls
15 lines (14 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(in-package :parser-combinators)
(defun memoize? (parser &optional (label (gensym)))
"Create identical, but memoized, parser."
(unless (gethash label *memo-table*)
(setf (gethash label *memo-table*) (make-hash-table)))
(let ((memo-table (gethash label *memo-table*)))
#'(lambda (inp)
(multiple-value-bind (result result-p) (gethash (position-of inp) memo-table)
(let ((new-result (if result-p
(copy-parse-result result)
(copy-parse-result (setf (gethash (position-of inp) memo-table)
(make-parse-result (funcall parser inp)))))))
#'(lambda ()
(next-result new-result)))))))