aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/lang/analysis.lux
blob: 223f2fb296d4fc4f38a301a17f696ae663eff4fd (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
(.module:
  lux
  (lux [function]
       (data (coll [list "list/" Fold<List>]))))

(type: #export #rec Primitive
  #Unit
  (#Bool Bool)
  (#Nat Nat)
  (#Int Int)
  (#Deg Deg)
  (#Frac Frac)
  (#Text Text))

(type: #export Tag Nat)

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

(type: #export Register Nat)

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

(type: #export Variable
  (#Local Register)
  (#Foreign Register))

(type: #export (Match p e)
  [[p e] (List [p e])])

(type: #export Environment
  (List Variable))

(type: #export (Special e)
  [Text (List e)])

(type: #export #rec Analysis
  (#Primitive Primitive)
  (#Structure (Composite Analysis))
  (#Case Analysis (Match Pattern Analysis))
  (#Function Environment Analysis)
  (#Apply Analysis Analysis)
  (#Variable Variable)
  (#Constant Ident)
  (#Special (Special Text)))

(type: #export Variant
  {#lefts Nat
   #right? Bool
   #value Analysis})

(type: #export Tuple (List Analysis))

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

(do-template [<name> <tag>]
  [(def: <name>
     (-> Analysis Analysis)
     (|>> <tag> #Sum #Structure))]

  [left #.Left]
  [right #.Right]
  )

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

(def: #export (no-op value)
  (-> Analysis Analysis)
  (let [identity (#Function (list) (#Variable (#Local +1)))]
    (#Apply value identity)))

(def: #export (sum size tag value)
  (-> Nat Tag Analysis Analysis)
  (if (last? size tag)
    (if (n/= +1 tag)
      (..right value)
      (list/fold (function.const ..left)
                 (..right value)
                 (list.n/range +0 (n/- +2 tag))))
    (list/fold (function.const ..left)
               (case value
                 (#Structure (#Sum _))
                 (no-op value)

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

(def: #export (product members)
  (-> Tuple Analysis)
  (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)))

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

(type: #export Analyser
  (-> Code (Meta Analysis)))

(def: #export (tuple analysis)
  (-> Analysis Tuple)
  (case analysis
    (#Structure (#Product left right))
    (#.Cons left (tuple right))

    _
    (list analysis)))

(def: #export (variant analysis)
  (-> Analysis (Maybe Variant))
  (loop [lefts +0
         variantA analysis]
    (case variantA
      (#Structure (#Sum (#.Left valueA)))
      (case valueA
        (#Structure (#Sum _))
        (recur (inc lefts) valueA)

        _
        (#.Some {#lefts lefts
                 #right? false
                 #value valueA}))
      
      (#Structure (#Sum (#.Right valueA)))
      (#.Some {#lefts lefts
               #right? true
               #value valueA})

      _
      #.None)))

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

    _
    [analysis (list)]))