-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathbootstrap-macros.pxi
More file actions
219 lines (193 loc) · 8.58 KB
/
bootstrap-macros.pxi
File metadata and controls
219 lines (193 loc) · 8.58 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
(ns pixie.bootstrap-macros)
;;
;; 1) The compiler should translate pixie.bootstrap-macros/foo to pixie.stdlib/foo
;; 2) the compiler should allow these macros to override any other macros/locals but only during bootstrap compilation
;;
(defmacro when [test & body]
`(if ~test (do ~@body)))
(defmacro deftype
[nm fields & body]
(let [ctor-name (symbol (str "->" (name nm)))
fields (transduce (map (comp keyword name)) conj fields)
field-syms (transduce (map (comp symbol name)) conj fields)
mk-body (fn [body]
(let [fn-name (first body)
_ (assert (symbol? fn-name) "protocol override must have a name")
args (second body)
_ (assert (or (vector? args)
(seq? args)) "protocol override must have arguments")
self-arg (first args)
_ (assert (symbol? self-arg) "protocol override must have at least one `self' argument")
rest (next (next body))
body (reduce
(fn [body f]
`[(local-macro [~(symbol (name f))
(get-field ~self-arg ~(keyword (name f)))]
~@body)])
rest
fields)]
`(fn ~(symbol (str fn-name "_" nm)) ~args ~@body)))
all-fields fields
type-nm (str #_@(:ns env) "." (name nm))
type-decl `(def ~nm (create-type ~(keyword type-nm)
(array ~@all-fields)))
inst (gensym)
ctor `(defn ~ctor-name ~field-syms
(new ~nm
~@field-syms))
proto-bodies (transduce
(map (fn [body]
(cond
(symbol? body) `(satisfy ~body ~nm)
(seq? body) `(extend ~(first body)
~(symbol (str #_@(:ns env) "/" nm))
~(mk-body body))
:else (assert false "Unknown body element in deftype, expected symbol or seq"))))
conj
body)]
`(do ~type-decl
~ctor
~@proto-bodies)))
(defmacro defprotocol
[nm & sigs]
`(do (def ~nm (~'pixie.stdlib/protocol ~(str nm)))
~@(map (fn [[x]]
`(def ~x (~'pixie.stdlib/polymorphic-fn ~(str x) ~nm)))
sigs)))
(defmacro ns [nm & body]
(let [bmap (reduce (fn [m b]
(update-in m [(first b)] (fnil conj []) (next b)))
{}
body)
requires
(do
(assert (>= 1 (count (:require bmap)))
"Only one :require block can be defined per namespace")
(mapv (fn [r] `(require ~(keyword (name nm)) ~@r)) (first (:require bmap))))]
`(do (in-ns ~(keyword (name nm)))
~@requires)))
(defmacro require [ins ns & args]
`(do (load-ns (quote ~ns))
(assert (the-ns (quote ~ns))
(str "Couldn't find the namespace " (quote ~ns) " after loading the file"))
(apply refer ~ins (quote [~ns ~@args]))))
(defn -make-record-assoc-body [cname fields]
(let [k-sym (gensym "k")
v-sym (gensym "v")
this-sym (gensym "this")
result `(-assoc [~this-sym ~k-sym ~v-sym]
(condp identical? ~k-sym
~@(mapcat
(fn [k]
[k `(~cname ~@(mapv (fn [x]
(if (= x k)
v-sym
`(get-field ~this-sym ~x)))
fields))])
fields)
(throw [:pixie.stdlib/NotImplementedException
(str "Can't assoc to a unknown field: " ~k-sym)])))]
result))
(defmacro defrecord
{:doc "Define a record type.
Similar to `deftype`, but supports construction from a map using `map->Type`
and implements IAssociative, ILookup and IObject."
:added "0.1"}
[nm field-syms & body]
(let [ctor-name (symbol (str "->" (name nm)))
map-ctor-name (symbol (str "map" (name ctor-name)))
fields (transduce (map (comp keyword name)) conj field-syms)
type-from-map `(defn ~map-ctor-name [m]
(apply ~ctor-name (map #(get m %) ~fields)))
default-bodies ['IAssociative
(-make-record-assoc-body ctor-name fields)
`(-contains-key [self# k#]
(contains? ~(set fields) k#))
`(-dissoc [self k]
(throw [:pixie.stdlib/NotImplementedException
"dissoc is not supported on defrecords"]))
'ILookup
`(-val-at [self# k# not-found#]
(get-field self# k# not-found#))
'IObject
`(-str [self# sb#]
(sb# (str "<" ~(name nm) " >" )))
`(-eq [self other]
(and (instance? ~nm other)
~@(map (fn [field]
`(= (. self ~field) (. other ~field)))
fields)))
`(-hash [self]
(hash [~@field-syms]))]
deftype-decl `(deftype ~nm ~fields ~@default-bodies ~@body)]
`(do ~type-from-map
~deftype-decl)))
(defmacro when [test & body]
`(if ~test (do ~@body)))
(defmacro when-not [test & body]
`(if (not ~test) (do ~@body)))
(defmacro when-let [binding & body]
(let [bind (nth binding 0 nil)
test (nth binding 1 nil)]
`(let [tmp# ~test]
(when tmp#
(let [~bind tmp#]
~@body)))))
(defmacro set! [v val]
`(-dynamic-var-set dynamic-var-handler (var ~v) ~val))
(defmacro binding [bindings & body]
(let [parted (partition 2 bindings)]
`(let [vars# (-dynamic-get-vars dynamic-var-handler)]
(with-handler [_# dynamic-var-handler]
(-dynamic-set-vars dynamic-var-handler
(assoc vars#
~@(mapcat
(fn [[var-name binding]]
[`(var ~var-name)
binding])
parted)))
~@body))))
(defmacro fn
{:doc "Creates a function.
The following two forms are allowed:
(fn name? [param*] & body)
(fn name? ([param*] & body)+)
The params can be destructuring bindings, see `(doc let)` for details."}
[& decls]
(let [name (if (symbol? (first decls)) [(first decls)] nil)
decls (if name (next decls) decls)
decls (cond
(vector? (first decls)) (list decls)
;(satisfies? ISeqable (first decls)) decls
;:else (throw (str "expected a vector or a seq, got a " (type decls)))
:else decls)
decls (seq (map (fn* [decl]
(let [argv (first decl)
names (vec (map #(if (= % '&) '& (gensym "arg__")) argv))
bindings (loop [i 0 bindings []]
(if (< i (count argv))
(if (= (nth argv i) '&)
(recur (inc i) bindings)
(recur (inc i) (reduce conj bindings [(nth argv i) (nth names i)])))
bindings))
body (next decl)]
(if (every? symbol? argv)
`(~argv ~@body)
`(~names
(let ~bindings
~@body)))))
decls))]
(if (= (count decls) 1)
`(fn* ~@name ~(first (first decls)) ~@(next (first decls)))
`(fn* ~@name ~@decls))))
(defmacro assert
([test]
`(if ~test
nil
(throw [:pixie.stdlib/AssertionException
"Assert failed"])))
([test msg]
`(if ~test
nil
(throw [:pixie.stdlib/AssertionException
(str "Assert failed: " ~msg)]))))