aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/extension/analysis/lux.lux
blob: efd917bd240e32495d10362ed7105ac7abb9625f (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
(.module:
  [lux #*
   [abstract
    ["." monad (#+ do)]]
   [control
    [io (#+ IO)]
    ["." try]
    ["." exception (#+ exception:)]
    ["<>" parser
     ["<c>" code (#+ Parser)]]]
   [data
    ["." maybe]
    ["." text
     ["%" format (#+ format)]]
    [collection
     ["." list ("#@." functor)]
     ["." dictionary (#+ Dictionary)]]]
   [type
    ["." check]]
   ["." macro]]
  ["." ///
   ["#." bundle]
   ["#/" //
    [analysis
     [".A" type]
     [".A" case]
     [".A" function]]
    ["#/" //
     [default
      [evaluation (#+ Eval)]]
     ["#." analysis (#+ Analysis Operation Phase Handler Bundle)]]]])

(def: #export (custom [syntax handler])
  (All [s]
    (-> [(Parser s)
         (-> Text Phase s (Operation Analysis))]
        Handler))
  (function (_ extension-name analyse args)
    (case (<c>.run syntax args)
      (#try.Success inputs)
      (handler extension-name analyse inputs)

      (#try.Failure _)
      (/////analysis.throw ///.invalid-syntax [extension-name %.code args]))))

(def: (simple inputsT+ outputT)
  (-> (List Type) Type Handler)
  (let [num-expected (list.size inputsT+)]
    (function (_ extension-name analyse args)
      (let [num-actual (list.size args)]
        (if (n/= num-expected num-actual)
          (do ////.monad
            [_ (typeA.infer outputT)
             argsA (monad.map @
                              (function (_ [argT argC])
                                (typeA.with-type argT
                                  (analyse argC)))
                              (list.zip2 inputsT+ args))]
            (wrap (#/////analysis.Extension extension-name argsA)))
          (/////analysis.throw ///.incorrect-arity [extension-name num-expected num-actual]))))))

(def: #export (nullary valueT)
  (-> Type Handler)
  (simple (list) valueT))

(def: #export (unary inputT outputT)
  (-> Type Type Handler)
  (simple (list inputT) outputT))

(def: #export (binary subjectT paramT outputT)
  (-> Type Type Type Handler)
  (simple (list subjectT paramT) outputT))

(def: #export (trinary subjectT param0T param1T outputT)
  (-> Type Type Type Type Handler)
  (simple (list subjectT param0T param1T) outputT))

## TODO: Get rid of this ASAP
(as-is
 (exception: #export (char-text-must-be-size-1 {text Text})
   (exception.report
    ["Text" (%.text text)]))
 
 (def: text-char
   (Parser text.Char)
   (do <>.monad
     [raw <c>.text]
     (case (text.size raw)
       1 (wrap (|> raw (text.nth 0) maybe.assume))
       _ (<>.fail (exception.construct ..char-text-must-be-size-1 [raw])))))

 (def: lux::syntax-char-case!
   (..custom
    [($_ <>.and
         <c>.any
         (<c>.tuple (<>.some (<>.and (<c>.tuple (<>.many ..text-char))
                                     <c>.any)))
         <c>.any)
     (function (_ extension-name phase [input conditionals else])
       (do ////.monad
         [input (typeA.with-type text.Char
                  (phase input))
          expectedT (///.lift macro.expected-type)
          conditionals (monad.map @ (function (_ [cases branch])
                                      (do @
                                        [branch (typeA.with-type expectedT
                                                  (phase branch))]
                                        (wrap [cases branch])))
                                  conditionals)
          else (typeA.with-type expectedT
                 (phase else))]
         (wrap (|> conditionals
                   (list@map (function (_ [cases branch])
                               (/////analysis.tuple
                                (list (/////analysis.tuple (list@map (|>> /////analysis.nat) cases))
                                      branch))))
                   (list& input else)
                   (#/////analysis.Extension extension-name)))))])))

## "lux is" represents reference/pointer equality.
(def: lux::is
  Handler
  (function (_ extension-name analyse args)
    (do ////.monad
      [[var-id varT] (typeA.with-env check.var)]
      ((binary varT varT Bit extension-name)
       analyse 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 args)
    (case args
      (^ (list opC))
      (do ////.monad
        [[var-id varT] (typeA.with-env check.var)
         _ (typeA.infer (type (Either Text varT)))
         opA (typeA.with-type (type (IO varT))
               (analyse opC))]
        (wrap (#/////analysis.Extension extension-name (list opA))))
      
      _
      (/////analysis.throw ///.incorrect-arity [extension-name 1 (list.size args)]))))

(def: lux::in-module
  Handler
  (function (_ extension-name analyse argsC+)
    (case argsC+
      (^ (list [_ (#.Text module-name)] exprC))
      (/////analysis.with-current-module module-name
        (analyse exprC))
      
      _
      (/////analysis.throw ///.invalid-syntax [extension-name %.code argsC+]))))

(def: (lux::check eval)
  (-> Eval Handler)
  (function (_ extension-name analyse args)
    (case args
      (^ (list typeC valueC))
      (do ////.monad
        [count (///.lift macro.count)
         actualT (:: @ map (|>> (:coerce Type))
                     (eval count Type typeC))
         _ (typeA.infer actualT)]
        (typeA.with-type actualT
          (analyse valueC)))

      _
      (/////analysis.throw ///.incorrect-arity [extension-name 2 (list.size args)]))))

(def: (lux::coerce eval)
  (-> Eval Handler)
  (function (_ extension-name analyse args)
    (case args
      (^ (list typeC valueC))
      (do ////.monad
        [count (///.lift macro.count)
         actualT (:: @ map (|>> (:coerce Type))
                     (eval count Type typeC))
         _ (typeA.infer actualT)
         [valueT valueA] (typeA.with-inference
                           (analyse valueC))]
        (wrap valueA))

      _
      (/////analysis.throw ///.incorrect-arity [extension-name 2 (list.size args)]))))

(def: (caster input output)
  (-> Type Type Handler)
  (function (_ extension-name analyse args)
    (case args
      (^ (list valueC))
      (do ////.monad
        [_ (typeA.infer output)]
        (typeA.with-type input
          (analyse valueC)))
      
      _
      (/////analysis.throw ///.incorrect-arity [extension-name 1 (list.size args)]))))

(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 "check" (lux::check eval))
      (///bundle.install "coerce" (lux::coerce eval))
      (///bundle.install "macro" (..caster .Macro' .Macro))
      (///bundle.install "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))
          (///bundle.install "current-time" (nullary Int)))))

(def: I64* (type (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 "logical-right-shift" (binary Nat I64* I64))
          (///bundle.install "arithmetic-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 "smallest" (nullary Frac))
          (///bundle.install "min" (nullary Frac))
          (///bundle.install "max" (nullary Frac))
          (///bundle.install "i64" (unary Frac Int))
          (///bundle.install "encode" (unary Frac Text))
          (///bundle.install "decode" (unary Text (type (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 (Maybe Nat))))
          (///bundle.install "size" (unary Text Nat))
          (///bundle.install "char" (binary Nat Text Nat))
          (///bundle.install "clip" (trinary Nat Nat Text Text))
          )))

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