aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/meta/io/archive.lux
blob: fd19de547d736684378edefab43a5d3732d46895 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [abstract
    ["[0]" monad (.only Monad do)]]
   [control
    ["<>" parser]
    ["[0]" try (.only Try)]
    [concurrency
     ["[0]" async (.only Async) (.use "[1]#[0]" monad)]]]
   [data
    ["[0]" product]
    ["[0]" binary (.only Binary)
     ["<[1]>" \\parser (.only Parser)]]
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]
    [collection
     [set (.only Set)]
     ["[0]" list (.use "[1]#[0]" mix monad)]
     ["[0]" dictionary (.only Dictionary)]
     ["[0]" sequence (.only Sequence)]]]
   [meta
    ["[0]" configuration (.only Configuration)]
    ["[0]" version]
    [macro
     ["^" pattern]]
    [compiler
     ["@" target (.only Target)]]]
   [world
    ["[0]" file]]]]
 ["[0]" // (.only)
  ["[1][0]" context]
  ["/[1]" // (.only)
   [import (.only Import)]
   ["[0]" context (.only Context)]
   ["[0]" archive (.only Output Archive)
    [key (.only Key)]
    ["[0]" registry (.only Registry)]
    ["[0]" unit]
    ["[0]" artifact (.only Artifact)
     ["[0]" category (.only Category)]]
    ["[0]" module (.only)
     ["[0]" descriptor (.only Descriptor)]
     ["[0]" document (.only Document)]]]
   ["[0]" cache (.only)
    ["[1]/[0]" archive]
    ["[1]/[0]" module]
    ["[1]/[0]" purge (.only Cache Purge)]
    ["[0]" dependency
     ["[1]" module]]]
   [// (.only Custom)
    [language
     ["$" lux (.only)
      ["[0]" analysis]
      ["[0]" synthesis]
      ["[0]" translation]
      ["[0]" declaration]
      ["[1]/[0]" program]]]]]])

(def (module_parser key parser)
  (All (_ document)
    (-> (Key document) (Parser document) (Parser (module.Module document))))
  (all <>.and
       <binary>.nat
       descriptor.parser
       (document.parser key parser)))

(def (parser key parser)
  (All (_ document)
    (-> (Key document) (Parser document) (Parser [(module.Module document) Registry])))
  (all <>.and
       (..module_parser key parser)
       registry.parser))

(def (fresh_analysis_state host configuration)
  (-> Target Configuration .Lux)
  (analysis.state (analysis.info version.latest host configuration)))

(def (analysis_state host configuration archive)
  (-> Target Configuration Archive (Try .Lux))
  (do [! try.monad]
    [modules (is (Try (List [descriptor.Module .Module]))
                 (monad.each ! (function (_ module)
                                 (do !
                                   [entry (archive.find module archive)
                                    content (|> entry
                                                (the [archive.#module module.#document])
                                                (document.content $.key))]
                                   (in [module content])))
                             (archive.archived archive)))]
    (in (has .#modules modules (fresh_analysis_state host configuration)))))

(type Definitions
  (Dictionary Text Any))

(def (loaded_document extension host @module expected actual document)
  (All (_ expression declaration)
    (-> Text (translation.Host expression declaration) module.ID (Sequence [Artifact (Set unit.ID)]) (Dictionary Text Binary) (Document .Module)
        (Try [(Document .Module) Output])))
  (do [! try.monad]
    [[definitions output] (is (Try [Definitions Output])
                              (loop (again [input (sequence.list expected)
                                            definitions (is Definitions
                                                            (dictionary.empty text.hash))
                                            output (is Output sequence.empty)])
                                (when input
                                  {.#Item [[[@artifact artifact_category mandatory_artifact?] artifact_dependencies] input']}
                                  (when (do !
                                          [data (try.of_maybe (dictionary.value (format (%.nat @artifact) extension) actual))
                                           .let [context [@module @artifact]
                                                 declaration (of host ingest context data)]]
                                          (when artifact_category
                                            {category.#Anonymous}
                                            (do !
                                              [.let [output (sequence.suffix [@artifact {.#None} data] output)]
                                               _ (of host re_learn context {.#None} declaration)]
                                              (in [definitions
                                                   output]))
                                            
                                            {category.#Definition [name function_artifact]}
                                            (let [output (sequence.suffix [@artifact {.#None} data] output)]
                                              (if (text#= $/program.name name)
                                                (in [definitions
                                                     output])
                                                (do !
                                                  [value (of host re_load context {.#None} declaration)]
                                                  (in [(dictionary.has name value definitions)
                                                       output]))))

                                            {category.#Custom name}
                                            (do !
                                              [.let [output (sequence.suffix [@artifact {.#Some name} data] output)]
                                               _ (of host re_learn context {.#Some name} declaration)]
                                              (in [definitions
                                                   output]))))
                                    {try.#Success [definitions' output']}
                                    (again input' definitions' output')

                                    failure
                                    failure)
                                  
                                  {.#End}
                                  {try.#Success [definitions output]})))
     content (document.content $.key document)
     definitions (monad.each ! (function (_ [def_name [exported? def_global]])
                                 (when def_global
                                   {.#Alias payload}
                                   (in (list [def_name [exported? def_global]]))
                                   
                                   {.#Definition [type _]}
                                   (|> definitions
                                       (dictionary.value def_name)
                                       try.of_maybe
                                       (of ! each (|>> [type]
                                                       {.#Definition}
                                                       [exported?]
                                                       [def_name]
                                                       (list))))

                                   {.#Default [type _]}
                                   (in (list))))
                             (the .#definitions content))]
    (in [(document.document $.key (has .#definitions (list#conjoint definitions) content))
         output])))

(def (load_definitions fs context @module host_environment entry)
  (All (_ expression declaration)
    (-> (file.System Async) Context module.ID (translation.Host expression declaration)
        (archive.Entry .Module)
        (Async (Try (archive.Entry .Module)))))
  (do (try.with async.monad)
    [actual (is (Async (Try (Dictionary Text Binary)))
                (cache/module.artifacts async.monad fs context @module))
     .let [expected (registry.artifacts (the archive.#registry entry))]
     [document output] (|> (the [archive.#module module.#document] entry)
                           (loaded_document (the context.#artifact_extension context) host_environment @module expected actual)
                           async#in)]
    (in (|> entry
            (has [archive.#module module.#document] document)
            (has archive.#output output)))))

(def pseudo_module
  Text
  "(Lux Caching System)")

(def (cache_parser customs)
  (-> (List Custom) (Parser [(module.Module Any) Registry]))
  (when (for @.old (as (List (Custom Any Any))
                       customs)
             customs)
    {.#End}
    (..parser $.key $.parser)
    
    {.#Item [custom_state custom_key custom_format custom_parser custom_compiler] tail}
    (all <>.either
         (..parser custom_key custom_parser)
         (cache_parser tail)
         )))

(def (valid_cache customs fs context import contexts [module_name @module])
  (-> (List Custom) (file.System Async) Context Import (List //.Context)
      [descriptor.Module module.ID]
      (Async (Try Cache)))
  (with_expansions [<cache> (these module_name @module module registry)]
    (do [! (try.with async.monad)]
      [data (is (Async (Try Binary))
                (cache/module.cache fs context @module))
       [module registry] (async#in (<binary>.result (..cache_parser customs) data))]
      (if (text#= descriptor.runtime module_name)
        (in [true <cache>])
        (do !
          [input (//context.read fs ..pseudo_module import contexts (the context.#host_module_extension context) module_name)]
          (in [(cache/purge.valid? (the module.#descriptor module) input) <cache>]))))))

(def (pre_loaded_caches customs fs context import contexts archive)
  (-> (List Custom) (file.System Async) Context Import (List //.Context) Archive
      (Async (Try (List Cache))))
  (do [! (try.with async.monad)]
    [... TODO: Stop needing to wrap this expression in an unnecessary "do" expression.
     it (|> archive
            archive.reservations
            (monad.each ! (..valid_cache customs fs context import contexts)))]
    (in it)))

(def (load_order archive pre_loaded_caches)
  (-> Archive (List Cache)
      (Try (dependency.Order .Module)))
  (|> pre_loaded_caches
      (monad.mix try.monad
                 (function (_ [_ [module @module |module| registry]] archive)
                   (archive.has module
                                [archive.#module |module|
                                 archive.#output (is Output sequence.empty)
                                 archive.#registry registry]
                                archive))
                 archive)
      (of try.monad each (dependency.load_order $.key))
      (of try.monad conjoint)))

(def (loaded_caches host_environment fs context purge load_order)
  (All (_ expression declaration)
    (-> (translation.Host expression declaration) (file.System Async) Context
        Purge (dependency.Order .Module)
        (Async (Try (List [descriptor.Module (archive.Entry .Module)])))))
  (do [! (try.with async.monad)]
    [... TODO: Stop needing to wrap this expression in an unnecessary "do" expression.
     it (|> load_order
            (list.only (|>> product.left (dictionary.key? purge) not))
            (monad.each ! (function (_ [module_name [@module entry]])
                            (do !
                              [entry (with_expansions [<it> (..load_definitions fs context @module host_environment entry)]
                                       (for @.old (as (Async (Try (archive.Entry .Module)))
                                                      <it>)
                                            <it>))]
                              (in (with_expansions [<it> [module_name entry]]
                                    (for @.old (as [descriptor.Module (archive.Entry .Module)]
                                                   <it>)
                                         <it>)))))))]
    (in it)))

(def (load_every_reserved_module customs configuration host_environment fs context import contexts archive)
  (All (_ expression declaration)
    (-> (List Custom) Configuration (translation.Host expression declaration) (file.System Async) Context Import (List //.Context) Archive
        (Async (Try [Archive .Lux]))))
  (do [! (try.with async.monad)]
    [pre_loaded_caches (..pre_loaded_caches customs fs context import contexts archive)
     load_order (async#in (load_order archive pre_loaded_caches))
     .let [purge (cache/purge.purge pre_loaded_caches load_order)]
     _ (|> purge
           dictionary.entries
           (monad.each ! (|>> product.right (cache/purge.purge! fs context))))
     loaded_caches (..loaded_caches host_environment fs context purge load_order)]
    (async#in
     (do [! try.monad]
       [archive (monad.mix !
                           (function (_ [module entry] archive)
                             (archive.has module entry archive))
                           archive
                           loaded_caches)
        analysis_state (..analysis_state (the context.#host context) configuration archive)]
       (in [archive analysis_state])))))

(def .public (thaw customs configuration host_environment fs context import contexts)
  (All (_ expression declaration)
    (-> (List Custom) Configuration (translation.Host expression declaration) (file.System Async) Context Import (List //.Context)
        (Async (Try [Archive .Lux]))))
  (do async.monad
    [binary (of fs read (cache/archive.descriptor fs context))]
    (when binary
      {try.#Success binary}
      (do (try.with async.monad)
        [archive (async#in (archive.import ///.version configuration binary))]
        (..load_every_reserved_module customs configuration host_environment fs context import contexts archive))
      
      {try.#Failure error}
      (in {try.#Success [archive.empty
                         (fresh_analysis_state (the context.#host context) configuration)]}))))