aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/macro/pattern.lux
blob: e642b1cd31eda813d96f3197923c28b1f8150d2e (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
(.require
 [library
  [lux (.except or let with_template |>
                ` , ,*
                UnQuote unquote unquote_macro
                Spliced_UnQuote spliced_unquote spliced_unquote_macro)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" maybe]
    ["[0]" try]]
   [data
    [collection
     ["[0]" list (.use "[1]#[0]" monoid monad mix)]]]]]
 ["[0]" // (.only with_symbols)
  [vocabulary (.only vocabulary)]
  ["/[1]" // (.use "[1]#[0]" monad)]])

(def locally
  (macro (_ tokens lux)
    (.let [[prelude _] (symbol ._)]
      (when tokens
        (list [@ {.#Symbol ["" name]}])
        {.#Right [lux (list (.` (.in_module# (., [@ {.#Text prelude}])
                                             (., [@ {.#Symbol [prelude name]}]))))]}

        _
        {.#Left ""}))))

(.with_template [<name>]
  [(def <name> (..locally <name>))]

  [function#composite]

  [Replacement_Environment]
  [realized_template]
  [replacement_environment]

  [symbol_short]
  [tuple_list]

  [text$]
  [type_definition]
  [record_slots]
  [text#composite]
  [type#encoded]
  [module_alias]
  [symbol$]
  [tuple$]
  [zipped_2]

  [type_code]
  [expected_type]

  [wrong_syntax_error]
  [local$]

  [untemplated_list]
  [bit$]
  [nat$]
  [int$]
  [rev$]
  [frac$]

  [one_expansion]
  )

(def .public or
  (pattern
   (macro (_ tokens)
     (when tokens
       (list.partial [_ {.#Form patterns}] body branches)
       (when patterns
         {.#End}
         (///.failure (..wrong_syntax_error (symbol ..or)))

         _
         (.let [pairs (.|> patterns
                           (list#each (function (_ pattern) (list pattern body)))
                           list#conjoint)]
           (///#in (list#composite pairs branches))))
       _
       (///.failure (..wrong_syntax_error (symbol ..or)))))))

(def .public with_template
  (pattern
   (macro (_ tokens)
     (when tokens
       (list.partial [_ {.#Form (list [_ {.#Tuple bindings}]
                                      [_ {.#Tuple templates}])}]
                     [_ {.#Form data}]
                     branches)
       (when (is (Maybe (List Code))
                 (do maybe.monad
                   [bindings' (monad.each maybe.monad symbol_short bindings)
                    data' (monad.each maybe.monad tuple_list data)]
                   (.let [num_bindings (list.size bindings')]
                     (if (list.every? (|>> (.i64_=# num_bindings))
                                      (list#each list.size data'))
                       (.let [apply (is (-> Replacement_Environment (List Code))
                                        (function (_ env) (list#each (realized_template env) templates)))]
                         (.|> data'
                              (list#each (function#composite apply (replacement_environment bindings')))
                              list#conjoint
                              in))
                       {.#None}))))
         {.#Some output}
         (///#in (list#composite output branches))
         
         {.#None}
         (///.failure (..wrong_syntax_error (symbol ..with_template))))
       
       _
       (///.failure (..wrong_syntax_error (symbol ..with_template)))))))

(type Level
  [Code Code])

(def (level it)
  (-> Code (Meta Level))
  (///#in (when it
            [_ {.#Tuple (list expr binding)}]
            [expr binding]

            _
            [it (.` #1)])))

(type Multi
  [Code (List Level)])

(def (multiP levels)
  (-> (List Code) (Meta Multi))
  (when levels
    {.#End}
    (///.failure "Multi-level patterns cannot be empty.")

    {.#Item init extras}
    (do ///.monad
      [extras' (monad.each ///.monad ..level extras)]
      (in [init extras']))))

(def (multiG g!_ [[init_pattern levels] body])
  (-> Code [Multi Code] (List Code))
  (.let [inner_pattern_body (list#mix (function (_ [calculation pattern] success)
                                        (.let [bind? (when pattern
                                                       [_ {.#Symbol _}]
                                                       true

                                                       _
                                                       false)]
                                          (.` (when (., calculation)
                                                (., pattern)
                                                (., success)

                                                (.,* (if bind?
                                                       (list)
                                                       (list g!_ (.` {.#None}))))))))
                                      (.` {.#Some (., body)})
                                      (list.reversed levels))]
    (list init_pattern inner_pattern_body)))

(def .public multi
  (pattern
   (macro (_ tokens)
     (when tokens
       (list.partial [_meta {.#Form levels}] body next_branches)
       (with_symbols [g!temp]
         (do ///.monad
           [mlc (multiP levels)
            .let [initial_bind? (when mlc
                                  [[_ {.#Symbol _}] _]
                                  true

                                  _
                                  false)]
            expected ..expected_type]
           (in (list g!temp
                     (.` (.when (.is# {.#Apply (., (type_code expected)) Maybe}
                                      (.when (., g!temp)
                                        (.,* (multiG g!temp [mlc body]))

                                        (.,* (if initial_bind?
                                               (list)
                                               (list g!temp (.` {.#None}))))))
                           {.#Some (., g!temp)}
                           (., g!temp)

                           {.#None}
                           (.when (., g!temp)
                             (.,* next_branches))))))))
       
       _
       (///.failure (..wrong_syntax_error (symbol ..multi)))))))

(def .public let
  (pattern
   (macro (_ tokens)
     (when tokens
       (list.partial [_meta {.#Form (list [_ {.#Symbol ["" name]}] pattern)}] body branches)
       (.let [g!whole (local$ name)]
         (///#in (list.partial g!whole
                               (.` (when (., g!whole) (., pattern) (., body)))
                               branches)))
       
       _
       (///.failure (..wrong_syntax_error (symbol ..let)))))))

(def .public |>
  (pattern
   (macro (_ tokens)
     (when tokens
       (list.partial [_meta {.#Form (list [_ {.#Symbol ["" name]}] [_ {.#Tuple steps}])}] body branches)
       (.let [g!name (local$ name)]
         (///#in (list.partial g!name
                               (.` (.let [(., g!name) (.|> (., g!name) (.,* steps))]
                                     (., body)))
                               branches)))
       
       _
       (///.failure (..wrong_syntax_error (symbol ..|>)))))))

(def (name$ [module name])
  (-> Symbol Code)
  (.` [(., (text$ module)) (., (text$ name))]))

(def (untemplated_partial_list last inits)
  (-> Code (List Code) Code)
  (when inits
    {.#End}
    last

    {.#Item [init inits']}
    (.` {.#Item (., init) (., (untemplated_partial_list last inits'))})))

(vocabulary
 [.public Spliced_UnQuote]
 [.public spliced_unquote]
 [.public spliced_unquote_macro]
 [.private named_spliced_unquote])

(def (untemplated_composite <tag> g!meta untemplated_pattern elems)
  (-> Code Code (-> Code (Meta Code))
      (-> (List Code) (Meta Code)))
  (with_expansions [<default> (do ///.monad
                                [=elems (monad.each ///.monad untemplated_pattern elems)]
                                (in (.` [(., g!meta) {(., <tag>) (., (untemplated_list =elems))}])))]
    (when (list.reversed elems)
      {.#Item [_ {.#Form {.#Item [_ {.#Symbol global}] parameters}}]
              inits}
      (do ///.monad
        [micro (///.try (..named_spliced_unquote global))]
        (when micro
          {try.#Success micro}
          (do ///.monad
            [output (..one_expansion ((//.function micro) parameters))
             =inits (monad.each ///.monad untemplated_pattern (list.reversed inits))]
            (in (.` [(., g!meta) {(., <tag>) (., (untemplated_partial_list output =inits))}])))
          
          {try.#Failure error}
          <default>))

      _
      <default>)))

(def .public ,*
  (..spliced_unquote
   (macro (_ tokens)
     ({{.#Item it {.#End}}
       (at ///.monad in (list it))

       _
       (///.failure (..wrong_syntax_error (symbol ..,*)))}
      tokens))))

(vocabulary
 [.public UnQuote]
 [.public unquote]
 [.public unquote_macro]
 [.private named_unquote])

(def (untemplated_pattern pattern)
  (-> Code (Meta Code))
  (with_symbols [g!meta]
    (when pattern
      (..with_template [<tag> <gen>]
        [[_ {<tag> value}]
         (///#in (.` [(., g!meta) {<tag> (., (<gen> value))}]))])
      ([.#Bit    bit$]
       [.#Nat    nat$]
       [.#Int    int$]
       [.#Rev    rev$]
       [.#Frac   frac$]
       [.#Text   text$]
       [.#Symbol name$])

      [@composite {.#Form {.#Item [@global {.#Symbol global}] parameters}}]
      (do [! ///.monad]
        [micro (///.try (..named_unquote global))]
        (when micro
          {try.#Success micro}
          (do !
            [[_ output] (..one_expansion ((//.function micro) parameters))]
            (in [@composite output]))
          
          {try.#Failure error}
          (untemplated_composite (.` .#Form) g!meta untemplated_pattern (list.partial [@global {.#Symbol global}] parameters))))

      (..with_template [<tag>]
        [[_ {<tag> it}]
         (untemplated_composite (.` <tag>) g!meta untemplated_pattern it)])
      ([.#Form]
       [.#Variant]
       [.#Tuple])
      )))

(def .public `
  (pattern
   (macro (_ tokens)
     (when tokens
       (list.partial [_meta {.#Form (list template)}] body branches)
       (do ///.monad
         [pattern (untemplated_pattern template)]
         (in (list.partial pattern body branches)))

       (list template)
       (do ///.monad
         [pattern (untemplated_pattern template)]
         (in (list pattern)))

       _
       (///.failure (..wrong_syntax_error (symbol ..`)))))))

(def .public ,
  UnQuote
  (..unquote
   (macro (_ tokens)
     ({{.#Item it {.#End}}
       (at ///.monad in (list it))

       _
       (///.failure (..wrong_syntax_error (symbol ..,)))}
      tokens))))