aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/extension/analysis/common.lux
blob: bff1d8527cf66b1745033d77a6cdbf83cdd6d839 (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
(.module:
  [lux #*
   [control
    ["." monad (#+ do)]]
   [data
    ["." text
     format]
    [collection
     ["." list ("#;." functor)]
     ["." dictionary (#+ Dictionary)]]]
   [type
    ["." check]]
   ["." macro]
   [io (#+ IO)]]
  ["." ///
   ["#." bundle]
   ["#/" //
    [analysis
     [".A" type]
     [".A" case]
     [".A" function]]
    ["#/" //
     [default
      [evaluation (#+ Eval)]]
     ["#." analysis (#+ Analysis Handler Bundle)]]]])

## [Utils]
(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)))
          (////.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))

## [Analysers]
## "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))))
      
      _
      (////.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))
      
      _
      (////.throw ///.invalid-syntax [extension-name]))))

(do-template [<name> <type>]
  [(def: (<name> 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 <type>
             (analyse valueC)))

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

  [lux::check  actualT]
  [lux::coerce Any]
  )

(def: lux::check::type
  Handler
  (function (_ extension-name analyse args)
    (case args
      (^ (list valueC))
      (do ////.monad
        [_ (typeA.infer Type)
         valueA (typeA.with-type Type
                  (analyse valueC))]
        (wrap valueA))
      
      _
      (////.throw ///.incorrect-arity [extension-name 1 (list.size args)]))))

(def: (bundle::lux eval)
  (-> Eval Bundle)
  (|> ///bundle.empty
      (///bundle.install "is" lux::is)
      (///bundle.install "try" lux::try)
      (///bundle.install "check" (lux::check eval))
      (///bundle.install "coerce" (lux::coerce eval))
      (///bundle.install "check type" lux::check::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* I64))
          (///bundle.install "-" (binary I64* I64* I64))
          (///bundle.install "=" (binary I64* I64* Bit)))))

(def: bundle::int
  Bundle
  (<| (///bundle.prefix "int")
      (|> ///bundle.empty
          (///bundle.install "*" (binary Int Int Int))
          (///bundle.install "/" (binary Int Int Int))
          (///bundle.install "%" (binary Int Int Int))
          (///bundle.install "<" (binary Int Int Bit))
          (///bundle.install "frac" (unary Int Frac))
          (///bundle.install "char" (unary Int Text)))))

(def: bundle::frac
  Bundle
  (<| (///bundle.prefix "frac")
      (|> ///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 "int" (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 Text Text Nat (type (Maybe Nat))))
          (///bundle.install "size" (unary Text Nat))
          (///bundle.install "char" (binary Text Nat Nat))
          (///bundle.install "clip" (trinary Text Nat Nat Text))
          )))

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