-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumb-vau.lisp
54 lines (49 loc) · 2.1 KB
/
dumb-vau.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
;;;; dumb-vau.lisp
;;;; simplistic implementation of interpreted operatives
;;;; req. dumb-1parent-env stuff in dumb-1parent-env.lisp
(in-package #:mother)
(defclass dumb-vau (operative)
((static-env :accessor vau-env :initarg :env) ; i.e. the parent of the constructed env
(args-augmenter :accessor vau-augmenter :initarg :aug
:type ptree-matcher)
(code :accessor vau-code :initarg :code)
(envparam :accessor vau-envparam :initarg :envparam
:type (or symbol %ignore))))
(defun make-dumb-vau (env ptree envparam source)
;; typecheck for ptree is implicit in make-matcher
(check-type envparam (or symbol %ignore) "a valid environment parameter")
(make-instance 'dumb-vau
:env env
:aug (make-matcher ptree (list envparam))
;; CIRCULAR LISTS: replace copy-tree
:code (copy-tree source)
:envparam envparam))
(defmethod combine ((operator dumb-vau) operands env)
(let ((runenv (bindings->dumb-1parent-env
(let ((flat (funcall (vau-augmenter operator) operands))
(param (vau-envparam operator)))
(if (%ignore-p param)
flat
(acons param env flat)))
(vau-env operator))))
(eval-seq (vau-code operator) runenv)))
(defun make-matcher (ptree seen)
(declare (optimize (speed 3) (safety 0) (space 0))
(type list seen))
(etypecase ptree
(null (values (lambda (args) (if (null args) nil (error "could not match ~:a with ~a" nil args))) seen))
(symbol
(if (find ptree seen :test #'eq)
(error "duplicate parameter ~a in ptree" ptree)
(values (lambda (args) (list (cons ptree args))) (cons ptree seen))))
(%ignore (values (lambda (args) (declare (ignore args)) nil) seen))
(cons
(multiple-value-bind (car seen) (make-matcher (car ptree) seen)
(multiple-value-bind (cdr seen) (make-matcher (cdr ptree) seen)
(values (lambda (args)
(if (consp args)
(nconc (funcall car (car args)) (funcall cdr (cdr args)))
;; FIXME: dumb error, but i'd like to avoid closing over ptree
;; (both on principle and because it could be later modified by the user)
(error "could not match ~a" args)))
seen))))))