-
Notifications
You must be signed in to change notification settings - Fork 3
/
cx-dynamic-variables.lisp
191 lines (161 loc) · 6.15 KB
/
cx-dynamic-variables.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(in-package :contextl)
#-cx-disable-dynamic-environments
(progn
(defvar %unbound '%unbound)
(defstruct (dbox (:constructor make-dbox (value)))
value)
(defmethod print-object ((object dbox) stream)
(print-unreadable-object (object stream :type t :identity t)
(princ (dbox-value object)))))
(defvar *dynamic-symbol*
(make-symbol-mapper 'dynamic-symbol))
(defun make-dynamic-symbol (&optional (x "DYNAMIC-SYMBOL-"))
#-cx-disable-dynamic-environments
(let ((symbol (gensym x)))
(setf (symbol-value symbol)
(make-dbox %unbound))
symbol)
#+cx-disable-dynamic-environments
(gensym x))
(defun dynamic-symbol (symbol)
(map-symbol *dynamic-symbol* symbol
#-cx-disable-dynamic-environments
#'make-dynamic-symbol))
(declaim (inline dynamic-symbol-p))
(defun dynamic-symbol-p (symbol)
#-cx-disable-dynamic-environments
(and (symbolp symbol)
(boundp symbol)
(dbox-p (symbol-value symbol)))
#+cx-disable-dynamic-environments
(symbolp symbol))
(declaim (inline dynamic-symbol-value (setf dynamic-symbol-value)
dynamic-symbol-boundp dynamic-symbol-makunbound))
(defun dynamic-symbol-value (symbol)
#-cx-disable-dynamic-environments
(let ((value (dbox-value (symbol-value symbol))))
(if (eq value %unbound)
(error 'unbound-variable :name symbol)
value))
#+cx-disable-dynamic-environments
(symbol-value symbol))
(defun (setf dynamic-symbol-value) (value symbol)
#-cx-disable-dynamic-environments
(setf (dbox-value (symbol-value symbol)) value)
#+cx-disable-dynamic-environments
(setf (symbol-value symbol) value))
(defun dynamic-symbol-boundp (symbol)
#-cx-disable-dynamic-environments
(not (eq (dbox-value (symbol-value symbol)) %unbound))
#+cx-disable-dynamic-environments
(boundp symbol))
(defun dynamic-symbol-makunbound (symbol)
#-cx-disable-dynamic-environments
(setf (dbox-value (symbol-value symbol)) %unbound)
#+cx-disable-dynamic-environments
(makunbound symbol))
#-cx-disable-dynamic-environments
(progn
(declaim (inline compute-bindings))
(defun compute-bindings (symbols values)
(loop for nil in symbols
if values collect (make-dbox (pop values))
else collect (make-dbox %unbound))))
(defmacro dynamic-progv (symbols values &body body)
#-cx-disable-dynamic-environments
(with-unique-names (fixed-symbols fixed-bindings proceed)
`(let* ((,fixed-symbols ,symbols)
(,fixed-bindings (compute-bindings ,fixed-symbols ,values)))
(dynamic-wind :proceed ,proceed
(progv ,fixed-symbols ,fixed-bindings
(,proceed ,@body)))))
#+cx-disable-dynamic-environments
`(progv ,symbols ,values ,@body))
(defmacro dynamic-reprogv (symbols values &body body)
#-cx-disable-dynamic-environments
(with-unique-names (computed-symbols computed-bindings proceed)
`(dynamic-wind :proceed ,proceed
(let* ((,computed-symbols ,symbols)
(,computed-bindings (compute-bindings ,computed-symbols ,values)))
(progv ,computed-symbols ,computed-bindings
(,proceed ,@body)))))
#+cx-disable-dynamic-environments
`(progv ,symbols ,values ,@body))
(declaim (inline %dynamic-symbol))
(defun %dynamic-symbol (symbol)
(map-symbol *dynamic-symbol* symbol))
(defmacro defdynamic (name &body form)
(assert (and (consp form) (null (cdr form))))
`(progn
(defparameter ,(%dynamic-symbol name)
#-cx-disable-dynamic-environments (make-dbox ,@form)
#+cx-disable-dynamic-environments ,@form)
',name))
(defmacro dynamic (var)
#-cx-disable-dynamic-environments
`(dbox-value ,(%dynamic-symbol var))
#+cx-disable-dynamic-environments
(%dynamic-symbol var))
(defmacro set-dynamic (form var)
`(setf (dynamic ,var) ,form))
(defmacro dynamic-let ((&rest bindings) &body body)
(assert (and (every #'consp bindings)
(notany #'cddr bindings)))
#-cx-disable-dynamic-environments
(loop with proceed = (gensym)
for (var form) in bindings
collect (copy-symbol var) into stores
collect (%dynamic-symbol var) into symbols
collect form into forms
finally
(return `(let ,(loop for store in stores
for form in forms
collect `(,store (make-dbox ,form)))
(dynamic-wind :proceed ,proceed
(let ,(loop for symbol in symbols
for store in stores
collect `(,symbol ,store))
(declare (special ,@symbols))
(,proceed ,@body))))))
#+cx-disable-dynamic-environments
`(let ,(loop for (var form) in bindings
collect `(,(%dynamic-symbol var) ,form))
,@body))
(defmacro dlet ((&rest bindings) &body body)
`(dynamic-let ,bindings ,@body))
(defmacro dynamic-let* ((&rest bindings) &body body)
(if bindings
`(dynamic-let (,(first bindings))
(dynamic-let* ,(rest bindings)
,@body))
`(progn ,@body)))
(defmacro dlet* ((&rest bindings) &body body)
`(dynamic-let* ,bindings ,@body))
(defmacro dynamic-relet ((&rest bindings) &body body)
(assert (and (every #'consp bindings)
(notany #'cddr bindings)))
#-cx-disable-dynamic-environments
(with-unique-names (proceed)
(loop for (var form) in bindings
for symbol = (%dynamic-symbol var)
collect symbol into symbols
collect `(,symbol (make-dbox ,form)) into new-bindings
finally (return
`(dynamic-wind :proceed ,proceed
(let ,new-bindings
(declare (special ,@symbols))
(,proceed ,@body))))))
#+cx-disable-dynamic-environments
`(let ,(loop for (var form) in bindings
collect `(,(%dynamic-symbol var) ,form))
,@body))
(defmacro drelet ((&rest bindings) &body body)
`(dynamic-relet ,bindings ,@body))
(defmacro dynamic-relet* ((&rest bindings) &body body)
(if bindings
`(dynamic-relet (,(first bindings))
(dynamic-relet* ,(rest bindings)
,@body))
`(progn ,@body)))
(defmacro drelet* ((&rest bindings) &body body)
`(dynamic-relet* ,bindings ,@body))