-
Notifications
You must be signed in to change notification settings - Fork 1
/
macros.hy
33 lines (30 loc) · 1 KB
/
macros.hy
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
(defn parse-indexing [sym]
(defn parse-colon [sym]
(list (map (fn [x]
(if (empty? x)
None
(int (eval (read-str x)))))
(.split (str sym) ":"))))
(cond
[(in ":" (str sym)) `(slice ~@(parse-colon sym))]
[(in "..." (str sym)) 'Ellipsis]
[True sym]))
(defmacro np-get [ar &rest keys]
`(get ~ar (, ~@(map parse-indexing keys))))
(deftag S [body]
`(np-get ~@body))
(defmacro defattrs [klass-name
&optional [super-klasses []] [options []] [attrs []]
&rest body]
`(do
(import attr)
(with-decorator ~(if options `(attr.s ~@options) 'attr.s)
(defclass ~klass-name ~super-klasses
~(list (interleave
(lfor a attrs
(if (coll? a) (first a) a))
(lfor a attrs
`(attr.ib
~@(if (coll? a)
(list (rest a)) [])))))
~@body))))