summaryrefslogtreecommitdiff
path: root/macros/core.fnl
blob: def2f4c8b1e3f5c4fcf1e37418aedac2a8c0d76a (plain) (blame)
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
220
221
222
223
224
225
226
227
228
(require-macros :macros.fn)
(local core {})
(local unpack (or table.unpack _G.unpack))
(local insert table.insert)
(local meta-enabled (pcall _SCOPE.specials.doc (list (sym :doc) (sym :doc)) _SCOPE _CHUNK))

(fn multisym->sym [s]
  (if (multi-sym? s)
      (values (sym (string.gsub (tostring s) ".*[.]" "")) true)
      (values s false)))

(fn check-bindings [bindings]
  (and (assert-compile (sequence? bindings) "expected binding table" [])
       (assert-compile (= (length bindings) 2) "expected exactly two forms in binding vector." bindings)))

(fn* core.if-let
  ([bindings then]
   (if-let bindings then nil))
  ([bindings then else]
   (check-bindings bindings)
   (let [[form test] bindings]
     `(let [tmp# ,test]
        (if tmp#
            (let [,form tmp#]
              ,then)
            ,else)))))

(fn* core.when-let
  [bindings & body]
  (check-bindings bindings)
  (let [[form test] bindings]
    `(let [tmp# ,test]
       (if tmp#
           (let [,form tmp#]
             ,(unpack body))))))

(fn* core.if-some
  ([bindings then]
   (if-some bindings then nil))
  ([bindings then else]
   (check-bindings bindings)
   (let [[form test] bindings]
     `(let [tmp# ,test]
        (if (= tmp# nil)
            ,else
            (let [,form tmp#]
              ,then))))))

(fn* core.when-some
  [bindings & body]
  (check-bindings bindings)
  (let [[form test] bindings]
    `(let [tmp# ,test]
       (if (= tmp# nil)
           nil
           (let [,form tmp#]
             ,(unpack body))))))


(fn table-type [tbl]
  (if (sequence? tbl) :seq
      (table? tbl) :table
      :else))

(fn table-type-fn []
  `(fn [tbl#]
     (let [t# (type tbl#)]
       (if (= t# :table)
           (let [(k# _#) (next tbl#)]
             (if (and (= (type k#) :number) (= k# 1)) :seq
                 (= k# nil) :empty
                 :table))
           :else))))

(fn seq-fn []
  `(fn [tbl#]
     (var assoc# false)
     (let [res# []
           insert# table.insert]
       (each [k# v# (pairs tbl#)]
         (if (and (not assoc#)
                  (not (= (type k#) :number)))
             (set assoc# true))
         (insert# res# [k# v#]))
       (if assoc# res# tbl#))))

(fn& core.into [to from]
  (let [to-type (table-type to)
        from-type (table-type from)]
    (if (and (= to-type :seq) (= from-type :seq))
        `(let [to# ,to
               insert# table.insert]
           (each [_# v# (ipairs ,from)]
             (insert# to# v#))
           to#)
        (= to-type :seq)
        `(let [to# ,to
               seq# ,(seq-fn)
               insert# table.insert]
           (each [_# v# (ipairs (seq# ,from))]
             (insert# to# v#))
           to#)
        (and (= to-type :table) (= from-type :seq))
        `(let [to# ,to]
           (each [_# [k# v#] (ipairs ,from)]
             (tset to# k# v#))
           to#)
        (and (= to-type :table) (= from-type :table))
        `(let [to# ,to
               from# ,from]
           (each [k# v# (pairs from#)]
             (tset to# k# v#))
           to#)
        (= to-type :table)
        `(let [to# ,to
               from# ,from]
           (match (,(table-type-fn) from#)
             :seq (each [_# [k# v#] (ipairs from#)]
                    (tset to# k# v#))
             :table (each [k# v# (pairs from#)]
                      (tset to# k# v#))
             :else (error "expected table as second argument"))
           to#)
        `(let [to# ,to
               from# ,from
               insert# table.insert
               table-type# ,(table-type-fn)
               seq# ,(seq-fn)]
           (match (table-type# to#)
             :seq (each [_# v# (ipairs (seq# from#))]
                    (insert# to# v#))
             :table (match (table-type# from#)
                      :seq (each [_# [k# v#] (ipairs from#)]
                             (tset to# k# v#))
                      :table (each [k# v# (pairs from#)]
                               (tset to# k# v#))
                      :else (error "expected table as second argument"))
             ;; If we could not deduce type, it means that
             ;; we've got empty table.  We use will default
             ;; to sequential table, because it will never
             ;; break when converting into
             :empty (each [_# v# (ipairs (seq# from#))]
                      (insert# to# v#))
             :else (error "expected table as first argument"))
           to#))))

(fn first [tbl]
  (. tbl 1))

(fn rest [tbl]
  [(unpack tbl 2)])

(fn string? [x]
  (= (type x) :string))

(fn& core.when-meta [...]
  (when meta-enabled `(do ,...)))

(fn* core.with-meta [val meta]
  (if (not meta-enabled) val
      `(let [val# ,val
             (res# fennel#) (pcall require :fennel)]
         (if res#
             (each [k# v# (pairs ,meta)]
               (fennel#.metadata:set val# k# v#)))
         val#)))

(fn* core.meta [v]
  (when-meta
    `(let [(res# fennel#) (pcall require :fennel)]
       (if res# (. fennel#.metadata ,v)))))

(fn* core.defmulti
  [name & opts]
  (let [docstring (if (string? (first opts)) (first opts))
        opts (if docstring (rest opts) opts)
        dispatch-fn (first opts)]
    (if (in-scope? name)
        nil
        `(local ,name
                (let [multimethods# {}]
                  (setmetatable
                   ,(with-meta {} {:fnl/docstring docstring})
                   {:__call
                    (fn [_# ...]
                      ,docstring
                      (let [dispatch-value# (,dispatch-fn ...)]
                        ((or (. multimethods# dispatch-value#)
                             (. multimethods# :default)
                             (error (.. "No method in multimethod '"
                                        ,(tostring name)
                                        "' for dispatch value: "
                                        dispatch-value#) 2)) ...)))
                    :multimethods multimethods#}))))))

(fn* core.defmethod
  [multifn dispatch-val & fn-tail]
  `(let [multifn# ,multifn]
     (tset (. (getmetatable multifn#) :multimethods)
           ,dispatch-val
           (fn ,(unpack fn-tail)))
     multifn#))

(fn* core.def
  ([name expr] (def {} name expr))
  ([attr-map name expr]
   (let [attr-map (if (table? attr-map) attr-map
                      (string? attr-map) {attr-map true}
                      (error "def: expected keyword or literal table as first argument" 2))
         (s multi) (multisym->sym name)
         docstring (or (. attr-map :doc)
                       (. attr-map :fnl/docstring))
         f (if (. attr-map :dynamic) 'var 'local)]
     (if multi
         `(,f ,s (do (,f ,s ,expr)
                     (set ,name ,s)
                     ,(with-meta s {:fnl/docstring docstring})))
         `(,f ,name ,(with-meta expr {:fnl/docstring docstring}))))))

(fn* core.defonce
  ([name expr]
   (defonce {} name expr))
  ([attr-map name expr]
   (if (in-scope? name)
       nil
       (def attr-map name expr))))

core