aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/macro/pattern.lux
blob: 4c78c8c36d4e1233724ef1345681c411b942b542 (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
(.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)]]]]]
 ["[0]" // (.only)
  [vocabulary (.only vocabulary)]
  ["/[1]" // (.use "[1]#[0]" monad)]])

(def locally
  (macro (_ tokens lux)
    (.let [[prelude _] (symbol ._)]
      (case tokens
        (list [@ {.#Symbol ["" name]}])
        {.#Right [lux (list (.` ("lux 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$]
  [generated_symbol]
  [type_definition]
  [record_slots]
  [text#composite]
  [type#encoded]
  [module_alias]
  [symbol$]
  [tuple$]
  [zipped_2]

  [multi_level_case^]
  [multi_level_case$]
  [type_code]
  [expected_type]

  [wrong_syntax_error]
  [local$]

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

  [one_expansion]
  )

(def .public or
  (pattern
   (macro (_ tokens)
     (case tokens
       (list.partial [_ {.#Form patterns}] body branches)
       (case 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)
     (case tokens
       (list.partial [_ {.#Form (list [_ {.#Tuple bindings}]
                                      [_ {.#Tuple templates}])}]
                     [_ {.#Form data}]
                     branches)
       (case (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? (|>> ("lux 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)))))))

(def .public multi
  (pattern
   (macro (_ tokens)
     (case tokens
       (list.partial [_meta {.#Form levels}] body next_branches)
       (do ///.monad
         [mlc (multi_level_case^ levels)
          .let [initial_bind? (case mlc
                                [[_ {.#Symbol _}] _]
                                #1

                                _
                                #0)]
          expected ..expected_type
          g!temp (..generated_symbol "temp")]
         (in (list g!temp
                   (.` ({{.#Some (., g!temp)}
                         (., g!temp)

                         {.#None}
                         (.case (., g!temp)
                           (.,* next_branches))}
                        ("lux type check" {.#Apply (., (type_code expected)) Maybe}
                         (.case (., g!temp)
                           (.,* (multi_level_case$ g!temp [mlc body]))

                           (.,* (if initial_bind?
                                  (list)
                                  (list g!temp (.` {.#None})))))))))))
       
       _
       (///.failure (..wrong_syntax_error (symbol ..multi)))))))

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

(def .public |>
  (pattern
   (macro (_ tokens)
     (case 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)
  (case 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))}])))]
    (case (list.reversed elems)
      {.#Item [_ {.#Form {.#Item [_ {.#Symbol global}] parameters}}]
              inits}
      (do ///.monad
        [micro (///.try (..named_spliced_unquote global))]
        (case 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))
  (do ///.monad
    [g!meta (..generated_symbol "g!meta")]
    (case 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))]
        (case micro
          {try.#Success micro}
          (do ///.monad
            [[_ 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)
     (case 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))))