aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/compiler/default/phase/analysis.lux
blob: 72d2a34852ee3f2d6ed2d3e63020b3fbf3e6ccbb (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
(.module:
  [lux (#- nat int rev)
   [data
    ["." product]
    ["." error]
    [text ("text/." Equivalence<Text>)]
    [collection
     ["." list ("list/." Fold<List>)]]]
   ["." function]]
  [//
   ["." extension (#+ Extension)]
   [//
    ["." reference (#+ Register Variable Reference)]]])

(type: #export #rec Primitive
  #Unit
  (#Bit Bit)
  (#Nat Nat)
  (#Int Int)
  (#Rev Rev)
  (#Frac Frac)
  (#Text Text))

(type: #export Tag Nat)

(type: #export (Composite a)
  (#Sum (Either a a))
  (#Product [a a]))

(type: #export #rec Pattern
  (#Simple Primitive)
  (#Complex (Composite Pattern))
  (#Bind Register))

(type: #export (Branch' e)
  {#when Pattern
   #then e})

(type: #export (Match' e)
  [(Branch' e) (List (Branch' e))])

(type: #export Environment
  (List Variable))

(type: #export #rec Analysis
  (#Primitive Primitive)
  (#Structure (Composite Analysis))
  (#Reference Reference)
  (#Case Analysis (Match' Analysis))
  (#Function Environment Analysis)
  (#Apply Analysis Analysis)
  (#Extension (Extension Analysis)))

(type: #export State+
  (extension.State .Lux Code Analysis))

(type: #export Operation
  (extension.Operation .Lux Code Analysis))

(type: #export Phase
  (extension.Phase .Lux Code Analysis))

(type: #export Handler
  (extension.Handler .Lux .Code Analysis))

(type: #export Bundle
  (extension.Bundle .Lux .Code Analysis))

(type: #export Branch
  (Branch' Analysis))

(type: #export Match
  (Match' Analysis))

(do-template [<name> <tag>]
  [(template: #export (<name> content)
     (<tag> content))]

  [control/case #Case]
  )

(do-template [<name> <type> <tag>]
  [(def: #export <name>
     (-> <type> Analysis)
     (|>> <tag> #Primitive))]

  [bit  Bit  #Bit]
  [nat  Nat  #Nat]
  [int  Int  #Int]
  [rev  Rev  #Rev]
  [frac Frac #Frac]
  [text Text #Text]
  )

(type: #export (Variant a)
  {#lefts Nat
   #right? Bit
   #value a})

(type: #export (Tuple a) (List a))

(type: #export Arity Nat)

(type: #export (Abstraction c) [Environment Arity c])

(type: #export (Application c) [c (List c)])

(def: (last? size tag)
  (-> Nat Tag Bit)
  (n/= (dec size) tag))

(template: #export (no-op value)
  (|> 1 #reference.Local #reference.Variable #..Reference
      (#..Function (list))
      (#..Apply value)))

(do-template [<name> <type> <structure> <prep-value>]
  [(def: #export (<name> size tag value)
     (-> Nat Tag <type> <type>)
     (let [left (function.constant (|>> #.Left #Sum <structure>))
           right (|>> #.Right #Sum <structure>)]
       (if (last? size tag)
         (if (n/= 1 tag)
           (right value)
           (list/fold left
                      (right value)
                      (list.n/range 0 (n/- 2 tag))))
         (list/fold left
                    (case value
                      (<structure> (#Sum _))
                      (<prep-value> value)

                      _
                      value)
                    (list.n/range 0 tag)))))]

  [sum-analysis Analysis #Structure no-op]
  [sum-pattern  Pattern  #Complex   id]
  )

(do-template [<name> <type> <primitive> <structure>]
  [(def: #export (<name> members)
     (-> (Tuple <type>) <type>)
     (case (list.reverse members)
       #.Nil
       (<primitive> #Unit)

       (#.Cons singleton #.Nil)
       singleton

       (#.Cons last prevs)
       (list/fold (function (_ left right) (<structure> (#Product left right)))
                  last prevs)))]

  [product-analysis Analysis #Primitive #Structure]
  [product-pattern  Pattern  #Simple    #Complex]
  )

(def: #export (apply [func args])
  (-> (Application Analysis) Analysis)
  (list/fold (function (_ arg func) (#Apply arg func)) func args))

(do-template [<name> <type> <tag>]
  [(def: #export (<name> value)
     (-> <type> (Tuple <type>))
     (case value
       (<tag> (#Product left right))
       (#.Cons left (<name> right))

       _
       (list value)))]

  [tuple         Analysis #Structure]
  [tuple-pattern Pattern  #Complex]
  )

(do-template [<name> <type> <tag>]
  [(def: #export (<name> value)
     (-> <type> (Maybe (Variant <type>)))
     (loop [lefts 0
            variantA value]
       (case variantA
         (<tag> (#Sum (#.Left valueA)))
         (case valueA
           (<tag> (#Sum _))
           (recur (inc lefts) valueA)

           _
           (#.Some {#lefts lefts
                    #right? #0
                    #value valueA}))
         
         (<tag> (#Sum (#.Right valueA)))
         (#.Some {#lefts lefts
                  #right? #1
                  #value valueA})

         _
         #.None)))]

  [variant         Analysis #Structure]
  [variant-pattern Pattern  #Complex]
  )

(def: #export (application analysis)
  (-> Analysis (Application Analysis))
  (case analysis
    (#Apply head func)
    (let [[func' tail] (application func)]
      [func' (#.Cons head tail)])

    _
    [analysis (list)]))

(template: #export (pattern/unit)
  (#..Simple #..Unit))

(do-template [<name> <tag>]
  [(template: #export (<name> content)
     (#..Simple (<tag> content)))]
  
  [pattern/bit  #..Bit]
  [pattern/nat  #..Nat]
  [pattern/int  #..Int]
  [pattern/rev  #..Rev]
  [pattern/frac #..Frac]
  [pattern/text #..Text]
  )

(def: #export (with-source-code source action)
  (All [a] (-> Source (Operation a) (Operation a)))
  (function (_ [bundle state])
    (let [old-source (get@ #.source state)]
      (case (action [bundle (set@ #.source source state)])
        (#error.Error error)
        (#error.Error error)

        (#error.Success [[bundle' state'] output])
        (#error.Success [[bundle' (set@ #.source old-source state')]
                         output])))))

(def: fresh-bindings
  (All [k v] (Bindings k v))
  {#.counter 0
   #.mappings (list)})

(def: fresh-scope
  Scope
  {#.name     (list)
   #.inner    0
   #.locals   fresh-bindings
   #.captured fresh-bindings})

(def: #export (with-scope action)
  (All [a] (-> (Operation a) (Operation [Scope a])))
  (function (_ [bundle state])
    (case (action [bundle (update@ #.scopes (|>> (#.Cons fresh-scope)) state)])
      (#error.Success [[bundle' state'] output])
      (case (get@ #.scopes state')
        #.Nil
        (#error.Error "Impossible error: Drained scopes!")

        (#.Cons head tail)
        (#error.Success [[bundle' (set@ #.scopes tail state')]
                         [head output]]))

      (#error.Error error)
      (#error.Error error))))

(def: #export (with-current-module name)
  (All [a] (-> Text (Operation a) (Operation a)))
  (extension.localized (get@ #.current-module)
                       (set@ #.current-module)
                       (function.constant (#.Some name))))

(def: #export (with-cursor cursor action)
  (All [a] (-> Cursor (Operation a) (Operation a)))
  (if (text/= "" (product.left cursor))
    action
    (function (_ [bundle state])
      (let [old-cursor (get@ #.cursor state)]
        (case (action [bundle (set@ #.cursor cursor state)])
          (#error.Success [[bundle' state'] output])
          (#error.Success [[bundle' (set@ #.cursor old-cursor state')]
                           output])

          (#error.Error error)
          (#error.Error error))))))