aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/tool/compiler/language/lux/analysis/scope.lux
blob: 5137bd082424f31330bd32cdb170622a62eb9409 (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
(.using
 [library
  [lux "*"
   ["_" test (.only Test)]
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" pipe]
    ["[0]" maybe ("[1]#[0]" functor)]
    ["[0]" try ("[1]#[0]" functor)]
    ["[0]" exception]]
   [data
    ["[0]" product]
    [collection
     ["[0]" list]]]
   [math
    ["[0]" random (.only Random)]
    [number
     ["n" nat]]]
   ["[0]" type "_"
    ["$[1]" \\test]]]]
 [\\library
  ["[0]" /
   ["/[1]" //
    [//
     [phase
      ["[1][0]" extension]]
     [///
      ["[1][0]" phase ("[1]#[0]" monad)]
      [reference
       ["[1][0]" variable (.only Register Variable)]]]]]]]
 ["$[0]" // "_"
  ["[1][0]" type]])

(template [<name> <tag>]
  [(def: (<name> expected_type expected_register [actual_type actual_var])
     (-> Type Register [Type Variable] Bit)
     (and (same? expected_type actual_type)
          (case actual_var
            {<tag> actual_register}
            (n.= expected_register actual_register)
            
            _
            false)))]

  [local? //variable.#Local]
  [foreign? //variable.#Foreign]
  )

(def: .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [lux $//type.random_state
         .let [state [//extension.#bundle //extension.empty
                      //extension.#state lux]]
         name/0 (random.lower_case 1)
         name/1 (random.lower_case 2)
         type/0 ($type.random 0)
         type/1 ($type.random 0)]
        (all _.and
             (_.coverage [/.variable]
               (|> (/.variable name/0)
                   /.with
                   (//phase.result state)
                   (try#each (|>> product.right
                                  (pipe.case
                                    {.#None} true
                                    {.#Some _} false)))
                   (try.else false)))
             (_.coverage [/.with_local]
               (|> (/.with_local [name/0 type/0]
                     (/.variable name/0))
                   /.with
                   (//phase.result state)
                   (try#each (|>> product.right
                                  (maybe#each (..local? type/0 0))
                                  (maybe.else false)))
                   (try.else false)))
             (_.coverage [/.next]
               (|> (<| (do [! //phase.monad]
                         [register/0 /.next])
                       (/.with_local [name/0 type/0])
                       (do !
                         [var/0 (/.variable name/0)])
                       (do !
                         [register/1 /.next])
                       (/.with_local [name/1 type/1])
                       (do !
                         [var/1 (/.variable name/1)])
                       (in (do maybe.monad
                             [var/0 var/0
                              var/1 var/1]
                             (in [[register/0 var/0] [register/1 var/1]]))))
                   /.with
                   (//phase.result state)
                   (try#each (|>> product.right
                                  (maybe#each (function (_ [[register/0 var/0] [register/1 var/1]])
                                                (and (..local? type/0 register/0 var/0)
                                                     (..local? type/1 register/1 var/1))))
                                  (maybe.else false)))
                   (try.else false)))
             (_.coverage [/.no_scope]
               (and (|> (/.with_local [name/0 type/0]
                          (//phase#in false))
                        (//phase.result state)
                        (exception.otherwise (exception.match? /.no_scope)))
                    (|> (do //phase.monad
                          [_ /.next]
                          (in false))
                        (//phase.result state)
                        (exception.otherwise (exception.match? /.no_scope)))))
             (_.coverage [/.reset]
               (and (|> /.next
                        (/.with_local [name/0 type/0])
                        /.with
                        (//phase.result state)
                        (try#each (|>> product.right
                                       (n.= 1)))
                        (try.else false))
                    (|> /.next
                        /.reset
                        (/.with_local [name/0 type/0])
                        /.with
                        (//phase.result state)
                        (try#each (|>> product.right
                                       (n.= 0)))
                        (try.else false))))
             (_.coverage [/.drained]
               (|> (function (_ [bundle state])
                     {try.#Success [[bundle (has .#scopes (list) state)]
                                    false]})
                   (/.with_local [name/0 type/0])
                   /.with
                   (//phase#each product.right)
                   (//phase.result state)
                   (exception.otherwise (exception.match? /.drained))))
             (_.coverage [/.with]
               (|> (<| /.with
                       (/.with_local [name/0 type/0])
                       (do //phase.monad
                         [var/0' (/.variable name/0)
                          [scope/1 var/0''] (/.with (/.variable name/0))]
                         (<| //phase.lifted
                             try.of_maybe
                             (do maybe.monad
                               [var/0' var/0'
                                var/0'' var/0'']
                               (in [var/0' scope/1 var/0''])))))
                   (//phase.result state)
                   (try#each (function (_ [scope/0 var/0' scope/1 var/0''])
                               (and (local? type/0 0 var/0')
                                    (n.= 0 (list.size (the [.#locals .#mappings] scope/0)))
                                    (n.= 0 (list.size (the [.#captured .#mappings] scope/0)))

                                    (foreign? type/0 0 var/0'')
                                    (n.= 0 (list.size (the [.#locals .#mappings] scope/1)))
                                    (n.= 1 (list.size (the [.#captured .#mappings] scope/1))))))
                   (try.else false)))
             (_.coverage [/.environment]
               (let [(open "list#[0]") (list.equivalence //variable.equivalence)]
                 (and (|> (<| /.with
                              (/.with_local [name/0 type/0])
                              (/.with_local [name/1 type/1])
                              (do //phase.monad
                                [[scope/1 _] (/.with (in []))]
                                (in (/.environment scope/1))))
                          (//phase.result state)
                          (try#each (|>> product.right
                                         (list#= (list))))
                          (try.else false))
                      (|> (<| /.with
                              (do [! //phase.monad]
                                [register/0 /.next])
                              (/.with_local [name/0 type/0])
                              (/.with_local [name/1 type/1])
                              (do !
                                [[scope/1 _] (/.with (/.variable name/0))]
                                (in [register/0 (/.environment scope/1)])))
                          (//phase.result state)
                          (try#each (function (_ [_ [register/0 environment]])
                                      (list#= (list {//variable.#Local register/0})
                                              environment)))
                          (try.else false))
                      (|> (<| /.with
                              (do [! //phase.monad]
                                [register/0 /.next])
                              (/.with_local [name/0 type/0])
                              (do [! //phase.monad]
                                [register/1 /.next])
                              (/.with_local [name/1 type/1])
                              (do [! //phase.monad]
                                [[scope/1 _] (/.with (do !
                                                       [_ (/.variable name/1)
                                                        _ (/.variable name/0)]
                                                       (in [])))]
                                (in [register/0 register/1 (/.environment scope/1)])))
                          (//phase.result state)
                          (try#each (function (_ [_ [register/0 register/1 environment]])
                                      (list#= (list {//variable.#Local register/1}
                                                    {//variable.#Local register/0})
                                              environment)))
                          (try.else false)))))
             ))))