aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/scope.lux
blob: 4f99341c0cb1f510b37e9c5bc2bc09480ef1aabf (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
(.module:
  [library
   [lux {"-" [local]}
    [abstract
     monad]
    [control
     ["." maybe ("#\." monad)]
     ["." try]
     ["." exception {"+" [exception:]}]]
    [data
     ["." text ("#\." equivalence)]
     ["." product]
     [collection
      ["." list ("#\." functor mix monoid)]
      [dictionary
       ["." plist]]]]]]
  ["." /// "_"
   ["#." extension]
   [//
    ["/" analysis {"+" [Operation Phase]}]
    [///
     [reference
      ["." variable {"+" [Register Variable]}]]
     ["#" phase]]]])

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

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

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

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

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

(def: (captured name scope)
  (-> Text Scope (Maybe [Type Variable]))
  (loop [idx 0
         mappings (value@ [#.captured #.mappings] scope)]
    (case mappings
      (#.Item [_name [_source_type _source_ref]] mappings')
      (if (text\= name _name)
        (#.Some [_source_type (#variable.Foreign idx)])
        (recur (++ 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 (find name)
  (-> Text (Operation (Maybe [Type Variable])))
  (///extension.lifted
   (function (_ state)
     (let [[inner outer] (|> state
                             (value@ #.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 (: (-> Scope [Variable (List Scope)] [Variable (List Scope)])
                                         (function (_ scope ref+inner)
                                           [(#variable.Foreign (value@ [#.captured #.counter] scope))
                                            (#.Item (revised@ #.captured
                                                              (: (-> 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 [(with@ #.scopes scopes state)
                     (#.Some [ref_type ref])]))
         )))))

(exception: .public cannot_create_local_binding_without_a_scope)
(exception: .public invalid_scope_alteration)

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

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

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

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

(template [<name> <val_type>]
  [(def: <name>
     (Bindings Text [Type <val_type>])
     [#.counter 0
      #.mappings (list)])]

  [init_locals   Nat]
  [init_captured Variable]
  )

(def: (scope parent_name child_name)
  (-> (List Text) Text Scope)
  [#.name     (list& child_name parent_name)
   #.inner    0
   #.locals   init_locals
   #.captured init_captured])

(def: .public (with_scope name action)
  (All (_ a) (-> Text (Operation a) (Operation a)))
  (function (_ [bundle state])
    (let [parent_name (case (value@ #.scopes state)
                        #.End
                        (list)
                        
                        (#.Item top _)
                        (value@ #.name top))]
      (case (action [bundle (revised@ #.scopes
                                      (|>> (#.Item (scope parent_name name)))
                                      state)])
        (#try.Success [[bundle' state'] output])
        (#try.Success [[bundle' (revised@ #.scopes
                                          (|>> list.tail (maybe.else (list)))
                                          state')]
                       output])

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

(exception: .public cannot_get_next_reference_when_there_is_no_scope)

(def: .public next_local
  (Operation Register)
  (///extension.lifted
   (function (_ state)
     (case (value@ #.scopes state)
       (#.Item top _)
       (#try.Success [state (value@ [#.locals #.counter] top)])

       #.End
       (exception.except ..cannot_get_next_reference_when_there_is_no_scope [])))))

(def: (ref_variable ref)
  (-> Ref Variable)
  (case ref
    (#.Local register)
    (#variable.Local register)
    
    (#.Captured register)
    (#variable.Foreign register)))

(def: .public (environment scope)
  (-> Scope (List Variable))
  (|> scope
      (value@ [#.captured #.mappings])
      (list\each (function (_ [_ [_ ref]]) (ref_variable ref)))))