aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/extension/analysis/lux.lux
blob: 981d1a7a1d78b3d72a084e314cd47081685c88b4 (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
(.require
 [library
  [lux (.except)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["<>" parser]
    ["[0]" maybe]
    ["[0]" try]
    ["[0]" exception (.only Exception)]]
   [data
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor)]
     ["[0]" dictionary (.only Dictionary)]]]
   [math
    [number
     ["n" nat]]]
   ["[0]" meta (.only)
    ["[0]" code
     ["<[1]>" \\parser (.only Parser)]]
    [macro
     ["^" pattern]]
    [type
     ["[0]" check]]]]]
 ["[0]" /// (.only)
  ["[1][0]" bundle]
  ["/[1]" //
   [//
    ["[1][0]" analysis (.only Analysis Operation Phase Handler Bundle)
     [evaluation (.only Eval)]
     ["[0]A" type]]
    [///
     ["[1]" phase]
     [meta
      [archive (.only Archive)]]]]]])

(def .public (custom [syntax handler])
  (All (_ s)
    (-> [(Parser s)
         (-> Text Phase Archive s (Operation Analysis))]
        Handler))
  (function (_ extension_name analyse archive args)
    (when (<code>.result syntax args)
      {try.#Success inputs}
      (handler extension_name analyse archive inputs)

      {try.#Failure _}
      (////analysis.except ///.invalid_syntax [extension_name %.code args]))))

(def (simple inputsT+ outputT)
  (-> (List Type) Type Handler)
  (let [num_expected (list.size inputsT+)]
    (function (_ extension_name analyse archive args)
      (let [num_actual (list.size args)]
        (if (n.= num_expected num_actual)
          (do [! ////.monad]
            [_ (typeA.inference outputT)
             argsA (monad.each !
                               (function (_ [argT argC])
                                 (<| (typeA.expecting argT)
                                     (analyse archive argC)))
                               (list.zipped_2 inputsT+ args))]
            (in {////analysis.#Extension extension_name argsA}))
          (////analysis.except ///.incorrect_arity [extension_name num_expected num_actual]))))))

(def .public (nullary valueT)
  (-> Type Handler)
  (simple (list) valueT))

(def .public (unary inputT outputT)
  (-> Type Type Handler)
  (simple (list inputT) outputT))

(def .public (binary subjectT paramT outputT)
  (-> Type Type Type Handler)
  (simple (list subjectT paramT) outputT))

(def .public (trinary subjectT param0T param1T outputT)
  (-> Type Type Type Type Handler)
  (simple (list subjectT param0T param1T) outputT))

... TODO: Get rid of this ASAP
(these
 (exception.def .public (char_text_must_be_size_1 text)
   (Exception Text)
   (exception.report
    (list ["Text" (%.text text)])))
 
 (def text_char
   (Parser text.Char)
   (do <>.monad
     [raw <code>.text]
     (when (text.size raw)
       1 (in (|> raw (text.char 0) maybe.trusted))
       _ (<>.failure (exception.error ..char_text_must_be_size_1 [raw])))))

 (def lux::syntax_char_case!
   (..custom
    [(all <>.and
          <code>.any
          (<code>.tuple (<>.some (<>.and (<code>.tuple (<>.many ..text_char))
                                         <code>.any)))
          <code>.any)
     (function (_ extension_name phase archive [input conditionals else])
       (do [! ////.monad]
         [input (<| (typeA.expecting text.Char)
                    (phase archive input))
          expectedT (///.lifted meta.expected_type)
          conditionals (monad.each ! (function (_ [cases branch])
                                       (do !
                                         [branch (<| (typeA.expecting expectedT)
                                                     (phase archive branch))]
                                         (in [cases branch])))
                                   conditionals)
          else (<| (typeA.expecting expectedT)
                   (phase archive else))]
         (in (|> conditionals
                 (list#each (function (_ [cases branch])
                              (////analysis.tuple
                               (list (////analysis.tuple (list#each (|>> ////analysis.nat) cases))
                                     branch))))
                 (list.partial input else)
                 {////analysis.#Extension extension_name}))))])))

... "lux is" represents reference/pointer equality.
(def lux::is
  Handler
  (function (_ extension_name analyse archive args)
    (<| typeA.with_var
        (function (_ [@var :var:]))
        ((binary :var: :var: Bit extension_name)
         analyse archive args))))

... "lux try" provides a simple way to interact with the host platform's
... error_handling facilities.
(def lux::try
  Handler
  (function (_ extension_name analyse archive args)
    (when args
      (list opC)
      (<| typeA.with_var
          (function (_ [@var :var:]))
          (do [! ////.monad]
            [_ (typeA.inference (type_literal (Either Text :var:)))]
            (|> opC
                (analyse archive)
                (typeA.expecting (type_literal (-> .Any :var:)))
                (at ! each (|>> list {////analysis.#Extension extension_name})))))
      
      _
      (////analysis.except ///.incorrect_arity [extension_name 1 (list.size args)]))))

(def lux::in_module
  Handler
  (function (_ extension_name analyse archive argsC+)
    (when argsC+
      (list [_ {.#Text module_name}] exprC)
      (////analysis.with_current_module module_name
        (analyse archive exprC))
      
      _
      (////analysis.except ///.invalid_syntax [extension_name %.code argsC+]))))

(def (lux::type::check eval)
  (-> Eval Handler)
  (function (_ extension_name analyse archive args)
    (when args
      (list typeC valueC)
      (do [! ////.monad]
        [actualT (at ! each (|>> (as Type))
                     (eval archive Type typeC))
         _ (typeA.inference actualT)]
        (<| (typeA.expecting actualT)
            (analyse archive valueC)))

      _
      (////analysis.except ///.incorrect_arity [extension_name 2 (list.size args)]))))

(def (lux::type::as eval)
  (-> Eval Handler)
  (function (_ extension_name analyse archive args)
    (when args
      (list typeC valueC)
      (do [! ////.monad]
        [actualT (at ! each (|>> (as Type))
                     (eval archive Type typeC))
         _ (typeA.inference actualT)
         [valueT valueA] (typeA.inferring
                          (analyse archive valueC))]
        (in valueA))

      _
      (////analysis.except ///.incorrect_arity [extension_name 2 (list.size args)]))))

(def (caster input output)
  (-> Type Type Handler)
  (..custom
   [<code>.any
    (function (_ extension_name phase archive valueC)
      (do [! ////.monad]
        [_ (typeA.inference output)]
        (<| (typeA.expecting input)
            (phase archive valueC))))]))

(exception.def .public (not_a_type symbol)
  (Exception Symbol)
  (exception.report
   (list ["Symbol" (%.symbol symbol)])))

(def lux::macro
  Handler
  (..custom
   [<code>.any
    (function (_ extension_name phase archive valueC)
      (do [! ////.monad]
        [_ (typeA.inference .Macro)
         input_type (loop (again [input_name (symbol .Macro')])
                      (do !
                        [input_type (///.lifted (meta.definition (symbol .Macro')))]
                        (when input_type
                          (^.or {.#Definition [exported? def_type def_value]}
                                {.#Type [exported? def_value labels]})
                          (in (as Type def_value))

                          (^.or {.#Tag _}
                                {.#Slot _})
                          (////.failure (exception.error ..not_a_type [(symbol .Macro')]))

                          {.#Alias real_name}
                          (again real_name))))]
        (<| (typeA.expecting input_type)
            (phase archive valueC))))]))

(def (bundle::lux eval)
  (-> Eval Bundle)
  (|> ///bundle.empty
      (///bundle.install "syntax char case!" lux::syntax_char_case!)
      (///bundle.install "is" lux::is)
      (///bundle.install "try" lux::try)
      (///bundle.install "type check" (lux::type::check eval))
      (///bundle.install "type as" (lux::type::as eval))
      (///bundle.install "macro" ..lux::macro)
      (///bundle.install "type check type" (..caster .Type .Type))
      (///bundle.install "in-module" lux::in_module)))

(def bundle::io
  Bundle
  (<| (///bundle.prefix "io")
      (|> ///bundle.empty
          (///bundle.install "log" (unary Text Any))
          (///bundle.install "error" (unary Text Nothing))
          (///bundle.install "exit" (unary Int Nothing)))))

(def I64*
  (type_literal (I64 Any)))

(def bundle::i64
  Bundle
  (<| (///bundle.prefix "i64")
      (|> ///bundle.empty
          (///bundle.install "and" (binary I64* I64* I64))
          (///bundle.install "or" (binary I64* I64* I64))
          (///bundle.install "xor" (binary I64* I64* I64))
          (///bundle.install "left-shift" (binary Nat I64* I64))
          (///bundle.install "right-shift" (binary Nat I64* I64))
          (///bundle.install "=" (binary I64* I64* Bit))
          (///bundle.install "<" (binary Int Int Bit))
          (///bundle.install "+" (binary I64* I64* I64))
          (///bundle.install "-" (binary I64* I64* I64))
          (///bundle.install "*" (binary Int Int Int))
          (///bundle.install "/" (binary Int Int Int))
          (///bundle.install "%" (binary Int Int Int))
          (///bundle.install "f64" (unary Int Frac))
          (///bundle.install "char" (unary Int Text)))))

(def bundle::f64
  Bundle
  (<| (///bundle.prefix "f64")
      (|> ///bundle.empty
          (///bundle.install "+" (binary Frac Frac Frac))
          (///bundle.install "-" (binary Frac Frac Frac))
          (///bundle.install "*" (binary Frac Frac Frac))
          (///bundle.install "/" (binary Frac Frac Frac))
          (///bundle.install "%" (binary Frac Frac Frac))
          (///bundle.install "=" (binary Frac Frac Bit))
          (///bundle.install "<" (binary Frac Frac Bit))
          (///bundle.install "i64" (unary Frac Int))
          (///bundle.install "encode" (unary Frac Text))
          (///bundle.install "decode" (unary Text (type_literal (Maybe Frac)))))))

(def bundle::text
  Bundle
  (<| (///bundle.prefix "text")
      (|> ///bundle.empty
          (///bundle.install "=" (binary Text Text Bit))
          (///bundle.install "<" (binary Text Text Bit))
          (///bundle.install "concat" (binary Text Text Text))
          (///bundle.install "index" (trinary Nat Text Text (type_literal (Maybe Nat))))
          (///bundle.install "size" (unary Text Nat))
          (///bundle.install "char" (binary Nat Text Nat))
          (///bundle.install "clip" (trinary Nat Nat Text Text))
          )))

(def .public (bundle eval)
  (-> Eval Bundle)
  (<| (///bundle.prefix "lux")
      (|> ///bundle.empty
          (dictionary.composite (bundle::lux eval))
          (dictionary.composite bundle::i64)
          (dictionary.composite bundle::f64)
          (dictionary.composite bundle::text)
          (dictionary.composite bundle::io)
          )))