aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/language/compiler/analysis/module.lux
blob: 2a2aef5c3a35e650c2863cbd45573035a46a5617 (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
(.module:
  [lux #*
   [control
    [monad (#+ do)]
    ["ex" exception (#+ exception:)]
    pipe]
   [data
    [text ("text/" Equivalence<Text>)
     format]
    ["e" error]
    [collection
     [list ("list/" Fold<List> Functor<List>)]
     [dictionary
      [plist]]]]
   [macro]]
  [// (#+ Operation)
   ["/." //
    [extension]]])

(type: #export Tag Text)

(exception: #export (unknown-module {module Text})
  module)

(exception: #export (cannot-declare-tag-twice {module Text} {tag Text})
  (ex.report ["Module" module]
             ["Tag" tag]))

(do-template [<name>]
  [(exception: #export (<name> {tags (List Text)} {owner Type})
     (ex.report ["Tags" (text.join-with " " tags)]
                ["Type" (%type owner)]))]

  [cannot-declare-tags-for-unnamed-type]
  [cannot-declare-tags-for-foreign-type]
  )

(exception: #export (cannot-define-more-than-once {name Ident})
  (%ident name))

(exception: #export (can-only-change-state-of-active-module {module Text} {state Module-State})
  (ex.report ["Module" module]
             ["Desired state" (case state
                                #.Active   "Active"
                                #.Compiled "Compiled"
                                #.Cached   "Cached")]))

(exception: #export (cannot-set-module-annotations-more-than-once {module Text} {old Code} {new Code})
  (ex.report ["Module" module]
             ["Old annotations" (%code old)]
             ["New annotations" (%code new)]))

(def: (new hash)
  (-> Nat Module)
  {#.module-hash        hash
   #.module-aliases     (list)
   #.definitions        (list)
   #.imports            (list)
   #.tags               (list)
   #.types              (list)
   #.module-annotations #.None
   #.module-state       #.Active})

(def: #export (set-annotations annotations)
  (-> Code (Operation Any))
  (do ///.Monad<Operation>
    [self-name (extension.lift macro.current-module-name)
     self (extension.lift macro.current-module)]
    (case (get@ #.module-annotations self)
      #.None
      (extension.lift
       (function (_ state)
         (#e.Success [(update@ #.modules
                               (plist.put self-name (set@ #.module-annotations (#.Some annotations) self))
                               state)
                      []])))
      
      (#.Some old)
      (///.throw cannot-set-module-annotations-more-than-once [self-name old annotations]))))

(def: #export (import module)
  (-> Text (Operation Any))
  (do ///.Monad<Operation>
    [self-name (extension.lift macro.current-module-name)]
    (extension.lift
     (function (_ state)
       (#e.Success [(update@ #.modules
                             (plist.update self-name (update@ #.imports (|>> (#.Cons module))))
                             state)
                    []])))))

(def: #export (alias alias module)
  (-> Text Text (Operation Any))
  (do ///.Monad<Operation>
    [self-name (extension.lift macro.current-module-name)]
    (extension.lift
     (function (_ state)
       (#e.Success [(update@ #.modules
                             (plist.update self-name (update@ #.module-aliases (: (-> (List [Text Text]) (List [Text Text]))
                                                                                  (|>> (#.Cons [alias module])))))
                             state)
                    []])))))

(def: #export (exists? module)
  (-> Text (Operation Bool))
  (extension.lift
   (function (_ state)
     (|> state
         (get@ #.modules)
         (plist.get module)
         (case> (#.Some _) true #.None false)
         [state] #e.Success))))

(def: #export (define name definition)
  (-> Text Definition (Operation []))
  (do ///.Monad<Operation>
    [self-name (extension.lift macro.current-module-name)
     self (extension.lift macro.current-module)]
    (extension.lift
     (function (_ state)
       (case (plist.get name (get@ #.definitions self))
         #.None
         (#e.Success [(update@ #.modules
                               (plist.put self-name
                                          (update@ #.definitions
                                                   (: (-> (List [Text Definition]) (List [Text Definition]))
                                                      (|>> (#.Cons [name definition])))
                                                   self))
                               state)
                      []])

         (#.Some already-existing)
         ((///.throw cannot-define-more-than-once [self-name name]) state))))))

(def: #export (create hash name)
  (-> Nat Text (Operation []))
  (extension.lift
   (function (_ state)
     (let [module (new hash)]
       (#e.Success [(update@ #.modules
                             (plist.put name module)
                             state)
                    []])))))

(def: #export (with-module hash name action)
  (All [a] (-> Nat Text (Operation a) (Operation [Module a])))
  (do ///.Monad<Operation>
    [_ (create hash name)
     output (//.with-current-module name
              action)
     module (extension.lift (macro.find-module name))]
    (wrap [module output])))

(do-template [<setter> <asker> <tag>]
  [(def: #export (<setter> module-name)
     (-> Text (Operation Any))
     (extension.lift
      (function (_ state)
        (case (|> state (get@ #.modules) (plist.get module-name))
          (#.Some module)
          (let [active? (case (get@ #.module-state module)
                          #.Active true
                          _     false)]
            (if active?
              (#e.Success [(update@ #.modules
                                    (plist.put module-name (set@ #.module-state <tag> module))
                                    state)
                           []])
              ((///.throw can-only-change-state-of-active-module [module-name <tag>])
               state)))

          #.None
          ((///.throw unknown-module module-name) state)))))
   
   (def: #export (<asker> module-name)
     (-> Text (Operation Bool))
     (extension.lift
      (function (_ state)
        (case (|> state (get@ #.modules) (plist.get module-name))
          (#.Some module)
          (#e.Success [state
                       (case (get@ #.module-state module)
                         <tag> true
                         _     false)])

          #.None
          ((///.throw unknown-module module-name) state)))))]

  [set-active   active?   #.Active]
  [set-compiled compiled? #.Compiled]
  [set-cached   cached?   #.Cached]
  )

(do-template [<name> <tag> <type>]
  [(def: (<name> module-name)
     (-> Text (Operation <type>))
     (extension.lift
      (function (_ state)
        (case (|> state (get@ #.modules) (plist.get module-name))
          (#.Some module)
          (#e.Success [state (get@ <tag> module)])

          #.None
          ((///.throw unknown-module module-name) state)))))]

  [tags  #.tags        (List [Text [Nat (List Ident) Bool Type]])]
  [types #.types       (List [Text [(List Ident) Bool Type]])]
  [hash  #.module-hash Nat]
  )

(def: (ensure-undeclared-tags module-name tags)
  (-> Text (List Tag) (Operation Any))
  (do ///.Monad<Operation>
    [bindings (..tags module-name)
     _ (monad.map @
                  (function (_ tag)
                    (case (plist.get tag bindings)
                      #.None
                      (wrap [])

                      (#.Some _)
                      (///.throw cannot-declare-tag-twice [module-name tag])))
                  tags)]
    (wrap [])))

(def: #export (declare-tags tags exported? type)
  (-> (List Tag) Bool Type (Operation Any))
  (do ///.Monad<Operation>
    [self-name (extension.lift macro.current-module-name)
     [type-module type-name] (case type
                               (#.Named type-ident _)
                               (wrap type-ident)

                               _
                               (///.throw cannot-declare-tags-for-unnamed-type [tags type]))
     _ (ensure-undeclared-tags self-name tags)
     _ (///.assert cannot-declare-tags-for-foreign-type [tags type]
                   (text/= self-name type-module))]
    (extension.lift
     (function (_ state)
       (case (|> state (get@ #.modules) (plist.get self-name))
         (#.Some module)
         (let [namespaced-tags (list/map (|>> [self-name]) tags)]
           (#e.Success [(update@ #.modules
                                 (plist.update self-name
                                               (|>> (update@ #.tags (function (_ tag-bindings)
                                                                      (list/fold (function (_ [idx tag] table)
                                                                                   (plist.put tag [idx namespaced-tags exported? type] table))
                                                                                 tag-bindings
                                                                                 (list.enumerate tags))))
                                                    (update@ #.types (plist.put type-name [namespaced-tags exported? type]))))
                                 state)
                        []]))
         #.None
         ((///.throw unknown-module self-name) state))))))