aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/analysis/scope.lux
blob: 03edb5564e59a2207d25b7bad21d7a4f93359cb6 (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
(.require
 [library
  [lux (.except local with)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" maybe (.use "[1]#[0]" monad)]
    ["[0]" try]
    ["[0]" exception (.only exception:)]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)]
    ["[0]" product]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix monoid)]
     [dictionary
      ["[0]" plist]]]]]]
 ["/" // (.only Environment Operation Phase)
  [//
   [phase
    ["[0]" extension]]
   [///
    ["[0]" phase]
    [reference
     ["[0]" variable (.only Register Variable)]]]]])

(type: Local
  (Bindings Text [Type Register]))

(type: Foreign
  (Bindings Text [Type Variable]))

(def (local? name scope)
  (-> Text Scope Bit)
  (|> scope
      (the [.#locals .#mappings])
      (plist.contains? name)))

(def (local name scope)
  (-> Text Scope (Maybe [Type Variable]))
  (|> scope
      (the [.#locals .#mappings])
      (plist.value name)
      (maybe#each (function (_ [type value])
                    [type {variable.#Local value}]))))

(def (captured? name scope)
  (-> Text Scope Bit)
  (|> scope
      (the [.#captured .#mappings])
      (plist.contains? name)))

(def (captured name scope)
  (-> Text Scope (Maybe [Type Variable]))
  (loop (again [idx 0
                mappings (the [.#captured .#mappings] scope)])
    (case mappings
      {.#Item [_name [_source_type _source_ref]] mappings'}
      (if (text#= name _name)
        {.#Some [_source_type {variable.#Foreign idx}]}
        (again (++ idx) mappings'))

      {.#End}
      {.#None})))

(def (reference? name scope)
  (-> Text Scope Bit)
  (or (local? name scope)
      (captured? name scope)))

(def (reference name scope)
  (-> Text Scope (Maybe [Type Variable]))
  (case (..local name scope)
    {.#Some type}
    {.#Some type}

    _
    (..captured name scope)))

(def .public (variable name)
  (-> Text (Operation (Maybe [Type Variable])))
  (extension.lifted
   (function (_ state)
     (let [[inner outer] (|> state
                             (the .#scopes)
                             (list.split_when (|>> (reference? name))))]
       (case outer
         {.#End}
         {.#Right [state {.#None}]}

         {.#Item top_outer _}
         (let [[ref_type init_ref] (maybe.else (undefined)
                                               (..reference name top_outer))
               [ref inner'] (list#mix (is (-> Scope [Variable (List Scope)] [Variable (List Scope)])
                                          (function (_ scope ref+inner)
                                            [{variable.#Foreign (the [.#captured .#counter] scope)}
                                             {.#Item (revised .#captured
                                                              (is (-> Foreign Foreign)
                                                                  (|>> (revised .#counter ++)
                                                                       (revised .#mappings (plist.has name [ref_type (product.left ref+inner)]))))
                                                              scope)
                                                     (product.right ref+inner)}]))
                                      [init_ref {.#End}]
                                      (list.reversed inner))
               scopes (list#composite inner' outer)]
           {.#Right [(has .#scopes scopes state)
                     {.#Some [ref_type ref]}]})
         )))))

(exception: .public no_scope)
(exception: .public drained)

(def .public (with_local [name type] action)
  (All (_ a) (-> [Text Type] (Operation a) (Operation a)))
  (function (_ [bundle state])
    (case (the .#scopes state)
      {.#Item head tail}
      (let [old_mappings (the [.#locals .#mappings] head)
            new_var_id (the [.#locals .#counter] head)
            new_head (revised .#locals
                              (is (-> Local Local)
                                  (|>> (revised .#counter ++)
                                       (revised .#mappings (plist.has name [type new_var_id]))))
                              head)]
        (case (phase.result' [bundle (has .#scopes {.#Item new_head tail} state)]
                             action)
          {try.#Success [[bundle' state'] output]}
          (case (the .#scopes state')
            {.#Item head' tail'}
            (let [scopes' {.#Item (has .#locals (the .#locals head) head')
                                  tail'}]
              {try.#Success [[bundle' (has .#scopes scopes' state')]
                             output]})

            _
            (exception.except ..drained []))

          {try.#Failure error}
          {try.#Failure error}))

      _
      (exception.except ..no_scope []))))

(def empty
  Scope
  (let [bindings (is Bindings
                     [.#counter 0
                      .#mappings (list)])]
    [.#name     (list)
     .#inner    0
     .#locals   bindings
     .#captured bindings]))

(def .public (reset action)
  (All (_ a) (-> (Operation a) (Operation a)))
  (function (_ [bundle state])
    (case (action [bundle (has .#scopes (list ..empty) state)])
      {try.#Success [[bundle' state'] output]}
      {try.#Success [[bundle' (has .#scopes (the .#scopes state) state')]
                     output]}

      failure
      failure)))

(def .public (with action)
  (All (_ a) (-> (Operation a) (Operation [Scope a])))
  (function (_ [bundle state])
    (case (action [bundle (revised .#scopes (|>> {.#Item ..empty}) state)])
      {try.#Success [[bundle' state'] output]}
      (case (the .#scopes state')
        {.#Item head tail}
        {try.#Success [[bundle' (has .#scopes tail state')]
                       [head output]]}

        {.#End}
        (exception.except ..drained []))

      {try.#Failure error}
      {try.#Failure error})))

(def .public next
  (Operation Register)
  (extension.lifted
   (function (_ state)
     (case (the .#scopes state)
       {.#Item top _}
       {try.#Success [state (the [.#locals .#counter] top)]}

       {.#End}
       (exception.except ..no_scope [])))))

(def .public environment
  (-> Scope (Environment Variable))
  (|>> (the [.#captured .#mappings])
       (list#each (function (_ [_ [_ ref]]) ref))))