aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/analysis.lux
blob: 5d9d899ab40160039e80834d929b07e1c8a41610 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
(.module:
  [lux (#- nat int rev)
   [abstract
    [monad (#+ do)]]
   [control
    ["." function]
    ["." try]
    ["." exception (#+ Exception)]]
   [data
    ["." product]
    ["." maybe]
    [number
     ["n" nat]]
    ["." text ("#@." equivalence)
     ["%" format (#+ Format format)]]
    [collection
     ["." list ("#@." functor fold)]]]]
  [//
   [arity (#+ Arity)]
   ["." reference (#+ Register Variable Reference)]
   ["." phase
    ["." extension (#+ Extension)]]])

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

(type: #export Tag Nat)

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

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

(type: #export (Composite a)
  (#Variant (Variant a))
  (#Tuple (Tuple 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 Branch
  (Branch' Analysis))

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

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

  [control/case #..Case]
  )

(template [<name> <type> <tag>]
  [(template: #export (<name> value)
     (#..Primitive (<tag> value)))]

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

(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)))

(def: #export (apply [abstraction inputs])
  (-> (Application Analysis) Analysis)
  (list@fold (function (_ input abstraction')
               (#Apply input abstraction'))
             abstraction
             inputs))

(def: #export (application analysis)
  (-> Analysis (Application Analysis))
  (loop [abstraction analysis
         inputs (list)]
    (case abstraction
      (#Apply input next)
      (recur next (#.Cons input inputs))

      _
      [abstraction inputs])))

(template [<name> <tag>]
  [(template: #export (<name> content)
     (.<| #..Reference
          <tag>
          content))]

  [variable         #reference.Variable]
  [constant         #reference.Constant]

  [variable/local   reference.local]
  [variable/foreign reference.foreign]
  )

(template [<name> <tag>]
  [(template: #export (<name> content)
     (.<| #..Complex
          <tag>
          content))]

  [pattern/variant #..Variant]
  [pattern/tuple   #..Tuple]
  )

(template [<name> <tag>]
  [(template: #export (<name> content)
     (.<| #..Structure
          <tag>
          content))]

  [variant #..Variant]
  [tuple   #..Tuple]
  )

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

(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]
  )

(template: #export (pattern/bind register)
  (#..Bind register))

(def: #export (%analysis analysis)
  (Format Analysis)
  (case analysis
    (#Primitive primitive)
    (case primitive
      #Unit
      "[]"

      (^template [<tag> <format>]
        (<tag> value)
        (<format> value))
      ([#Bit %.bit]
       [#Nat %.nat]
       [#Int %.int]
       [#Rev %.rev]
       [#Frac %.frac]
       [#Text %.text]))
    
    (#Structure structure)
    (case structure
      (#Variant [lefts right? value])
      (format "(" (%.nat lefts) " " (%.bit right?) " " (%analysis value) ")")
      
      (#Tuple members)
      (|> members
          (list@map %analysis)
          (text.join-with " ")
          (text.enclose ["[" "]"])))
    
    (#Reference reference)
    (case reference
      (#reference.Variable variable)
      (reference.%variable variable)
      
      (#reference.Constant constant)
      (%.name constant))
    
    (#Case analysis match)
    "{?}"
    
    (#Function environment body)
    (|> (%analysis body)
        (format " ")
        (format (|> environment
                    (list@map reference.%variable)
                    (text.join-with " ")
                    (text.enclose ["[" "]"])))
        (text.enclose ["(" ")"]))
    
    (#Apply _)
    (|> analysis
        ..application
        #.Cons
        (list@map %analysis)
        (text.join-with " ")
        (text.enclose ["(" ")"]))
    
    (#Extension name parameters)
    (|> parameters
        (list@map %analysis)
        (text.join-with " ")
        (format (%.text name) " ")
        (text.enclose ["(" ")"]))))

(template [<special> <general>]
  [(type: #export <special>
     (<general> .Lux Code Analysis))]

  [State+    extension.State]
  [Operation extension.Operation]
  [Phase     extension.Phase]
  [Handler   extension.Handler]
  [Bundle    extension.Bundle]
  )

(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)])
        (#try.Success [[bundle' state'] output])
        (#try.Success [[bundle' (set@ #.source old-source state')]
                       output])

        (#try.Failure error)
        (#try.Failure error)))))

(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)])
      (#try.Success [[bundle' state'] output])
      (case (get@ #.scopes state')
        (#.Cons head tail)
        (#try.Success [[bundle' (set@ #.scopes tail state')]
                       [head output]])

        #.Nil
        (#try.Failure "Impossible error: Drained scopes!"))

      (#try.Failure error)
      (#try.Failure 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)])
          (#try.Success [[bundle' state'] output])
          (#try.Success [[bundle' (set@ #.cursor old-cursor state')]
                         output])

          (#try.Failure error)
          (#try.Failure error))))))

(def: (locate-error cursor error)
  (-> Cursor Text Text)
  (format "@ " (%.cursor cursor) text.new-line
          error))

(def: #export (fail error)
  (-> Text Operation)
  (function (_ [bundle state])
    (#try.Failure (locate-error (get@ #.cursor state) error))))

(def: #export (throw exception parameters)
  (All [e] (-> (Exception e) e Operation))
  (..fail (exception.construct exception parameters)))

(def: #export (assert exception parameters condition)
  (All [e] (-> (Exception e) e Bit (Operation Any)))
  (if condition
    (:: phase.monad wrap [])
    (..throw exception parameters)))

(def: #export (fail' error)
  (-> Text (phase.Operation Lux))
  (function (_ state)
    (#try.Failure (locate-error (get@ #.cursor state) error))))

(def: #export (throw' exception parameters)
  (All [e] (-> (Exception e) e (phase.Operation Lux)))
  (..fail' (exception.construct exception parameters)))

(def: #export (with-stack exception message action)
  (All [e o] (-> (Exception e) e (Operation o) (Operation o)))
  (function (_ bundle,state)
    (case (action bundle,state)
      (#try.Success output)
      (#try.Success output)
      
      (#try.Failure error)
      (let [[bundle state] bundle,state]
        (#try.Failure (<| (locate-error (get@ #.cursor state))
                          (exception.decorate (exception.construct exception message))
                          error))))))

(template [<name> <type> <field> <value>]
  [(def: #export (<name> value)
     (-> <type> (Operation Any))
     (extension.update (set@ <field> <value>)))]

  [set-source-code    Source #.source         value]
  [set-current-module Text   #.current-module (#.Some value)]
  [set-cursor         Cursor #.cursor         value]
  )

(def: #export (cursor file)
  (-> Text Cursor)
  [file 1 0])

(def: #export (source file code)
  (-> Text Text Source)
  [(cursor file) 0 code])

(def: dummy-source
  Source
  [.dummy-cursor 0 ""])

(def: type-context
  Type-Context
  {#.ex-counter 0
   #.var-counter 0
   #.var-bindings (list)})

(def: #export (state info host)
  (-> Info Any Lux)
  {#.info            info
   #.source          ..dummy-source
   #.cursor          .dummy-cursor
   #.current-module  #.None
   #.modules         (list)
   #.scopes          (list)
   #.type-context    ..type-context
   #.expected        #.None
   #.seed            0
   #.scope-type-vars (list)
   #.extensions      []
   #.host            host})