-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.lisp
60 lines (47 loc) · 1.72 KB
/
registry.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
55
56
57
58
59
60
(defvar *constructors*)
(defvar *functions*)
(defvar *vars*)
(defmacro in-fresh-context (&body body)
`(let ((*constructors* (make-hash-table))
(*functions* (make-hash-table))
(*vars* (list)))
(register-builtin-types)
(register-prim-functions)
,@body))
(defun add-constructors (constructors)
(loop for (cons-name . cons-type) in constructors
do (setf (gethash cons-name *constructors*) cons-type)))
(defun add-vars (vars)
(setf *vars*
(append
(loop for (var-name . var-expr) in vars
collect (list var-name var-expr nil))
*vars*)))
(defun constructorp (x)
(typecase x
(integer t)
(string t)
(atom
(nth-value 1 (gethash x *constructors*)))))
(defclass function-info ()
())
(defclass proto-function-info (function-info)
((arity :initarg :arity :reader function-arity)))
(defclass prim-function-info (proto-function-info)
((fun :initarg :fun :reader prim-function)))
(defclass match-function-info (function-info)
((matches :initarg :matches :reader function-matches)))
(defmethod function-arity ((function-info match-function-info))
(length (caar (function-matches function-info))))
(defun register-proto-function (fun-name arity)
(setf (gethash fun-name *functions*)
(make-instance 'proto-function-info :arity arity)))
(defun register-match-function (fun-name matches)
(setf (gethash fun-name *functions*)
(make-instance 'match-function-info :matches matches)))
(defun register-prim-function (fun-name arity fun)
(setf (gethash fun-name *functions*)
(make-instance 'prim-function-info :arity arity :fun fun)))
(defun lookup-function (fun-name)
(format nil "~A" *functions*)
(gethash fun-name *functions*))