aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/default/init.lux
blob: 8ad59fc2e71bf2b6f4404c20849bd12d822b8c99 (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
(.using
 [library
  [lux (.except)
   ["@" target (.only Target)]
   ["[0]" meta]
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" try (.only Try)]
    ["[0]" exception (.only exception:)]]
   [data
    [binary (.only Binary)]
    ["[0]" product]
    ["[0]" text (.open: "[1]#[0]" hash)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.open: "[1]#[0]" functor)]
     ["[0]" dictionary]
     ["[0]" set]
     ["[0]" sequence (.open: "[1]#[0]" functor)]]]
   [meta
    ["[0]" configuration (.only Configuration)]
    ["[0]" version]]
   [world
    ["[0]" file]]]]
 ["[0]" //
  ["/[1]" // (.only Instancer)
   ["[1][0]" phase]
   [language
    [lux
     [program (.only Program)]
     ["[1][0]" syntax (.only Aliases)]
     ["[1][0]" synthesis]
     ["[1][0]" directive (.only Requirements)]
     ["[1][0]" generation]
     ["[1][0]" analysis (.only)
      [macro (.only Expander)]
      ["[1]/[0]" evaluation]
      ["[0]A" module]]
     [phase
      ["[0]P" analysis]
      ["[0]P" synthesis]
      ["[0]P" directive]
      ["[0]" extension (.only Extender)
       ["[0]E" analysis]
       ["[0]E" synthesis]
       [directive
        ["[0]D" lux]]]]]]
   [meta
    ["[0]" archive (.only Archive)
     ["[0]" registry (.only Registry)]
     ["[0]" module (.only)
      ["[0]" descriptor]
      ["[0]" document]]]]]])

(def .public (state target module configuration expander host_analysis host generate generation_bundle)
  (All (_ anchor expression directive)
    (-> Target
        descriptor.Module
        Configuration
        Expander
        ///analysis.Bundle
        (///generation.Host expression directive)
        (///generation.Phase anchor expression directive)
        (///generation.Bundle anchor expression directive)
        (///directive.State+ anchor expression directive)))
  (let [synthesis_state [synthesisE.bundle ///synthesis.init]
        generation_state [generation_bundle (///generation.state host module)]
        eval (///analysis/evaluation.evaluator expander synthesis_state generation_state generate)
        analysis_state [(analysisE.bundle eval host_analysis)
                        (///analysis.state (///analysis.info version.latest target configuration))]]
    [extension.empty
     [///directive.#analysis [///directive.#state analysis_state
                              ///directive.#phase (analysisP.phase expander)]
      ///directive.#synthesis [///directive.#state synthesis_state
                               ///directive.#phase synthesisP.phase]
      ///directive.#generation [///directive.#state generation_state
                                ///directive.#phase generate]]]))

(def .public (with_default_directives expander host_analysis program anchorT,expressionT,directiveT extender)
  (All (_ anchor expression directive)
    (-> Expander
        ///analysis.Bundle
        (Program expression directive)
        [Type Type Type]
        Extender
        (-> (///directive.State+ anchor expression directive)
            (///directive.State+ anchor expression directive))))
  (function (_ [directive_extensions sub_state])
    [(dictionary.composite directive_extensions
                           (luxD.bundle expander host_analysis program anchorT,expressionT,directiveT extender))
     sub_state]))

(type: Reader
  (-> Source (Either [Source Text] [Source Code])))

(def (reader current_module aliases [location offset source_code])
  (-> descriptor.Module Aliases Source (///analysis.Operation Reader))
  (function (_ [bundle state])
    {try.#Success [[bundle state]
                   (///syntax.parse current_module aliases ("lux text size" source_code))]}))

(def (read source reader)
  (-> Source Reader (///analysis.Operation [Source Code]))
  (function (_ [bundle compiler])
    (case (reader source)
      {.#Left [source' error]}
      {try.#Failure error}

      {.#Right [source' output]}
      (let [[location _] output]
        {try.#Success [[bundle (|> compiler
                                   (has .#source source')
                                   (has .#location location))]
                       [source' output]]}))))

(type: (Operation a)
  (All (_ anchor expression directive)
    (///directive.Operation anchor expression directive a)))

(type: (Payload directive)
  [(///generation.Buffer directive)
   Registry])

(def (begin dependencies hash input)
  (-> (List descriptor.Module) Nat ///.Input
      (All (_ anchor expression directive)
        (///directive.Operation anchor expression directive
                                [Source (Payload directive)])))
  (do ///phase.monad
    [.let [module (the ///.#module input)]
     _ (///directive.set_current_module module)]
    (///directive.lifted_analysis
     (do [! ///phase.monad]
       [_ (moduleA.create hash module)
        _ (monad.each ! moduleA.import dependencies)
        .let [source (///analysis.source (the ///.#module input) (the ///.#code input))]
        _ (///analysis.set_source_code source)]
       (in [source [///generation.empty_buffer
                    registry.empty]])))))

(def (end module)
  (-> descriptor.Module
      (All (_ anchor expression directive)
        (///directive.Operation anchor expression directive [.Module (Payload directive)])))
  (do ///phase.monad
    [_ (///directive.lifted_analysis
        (moduleA.set_compiled module))
     analysis_module (<| (is (Operation .Module))
                         ///directive.lifted_analysis
                         extension.lifted
                         meta.current_module)
     final_buffer (///directive.lifted_generation
                   ///generation.buffer)
     final_registry (///directive.lifted_generation
                     ///generation.get_registry)]
    (in [analysis_module [final_buffer
                          final_registry]])))

... TODO: Inline ASAP
(def (get_current_payload _)
  (All (_ directive)
    (-> (Payload directive)
        (All (_ anchor expression)
          (///directive.Operation anchor expression directive
                                  (Payload directive)))))
  (do ///phase.monad
    [buffer (///directive.lifted_generation
             ///generation.buffer)
     registry (///directive.lifted_generation
               ///generation.get_registry)]
    (in [buffer registry])))

... TODO: Inline ASAP
(def (process_directive wrapper archive expander pre_payoad code)
  (All (_ directive)
    (-> ///phase.Wrapper Archive Expander (Payload directive) Code
        (All (_ anchor expression)
          (///directive.Operation anchor expression directive
                                  [Requirements (Payload directive)]))))
  (do ///phase.monad
    [.let [[pre_buffer pre_registry] pre_payoad]
     _ (///directive.lifted_generation
        (///generation.set_buffer pre_buffer))
     _ (///directive.lifted_generation
        (///generation.set_registry pre_registry))
     requirements (let [execute! (directiveP.phase wrapper expander)]
                    (execute! archive code))
     post_payload (..get_current_payload pre_payoad)]
    (in [requirements post_payload])))

(def (iteration' wrapper archive expander reader source pre_payload)
  (All (_ directive)
    (-> ///phase.Wrapper Archive Expander Reader Source (Payload directive)
        (All (_ anchor expression)
          (///directive.Operation anchor expression directive
                                  [Source Requirements (Payload directive)]))))
  (do ///phase.monad
    [[source code] (///directive.lifted_analysis
                    (..read source reader))
     [requirements post_payload] (process_directive wrapper archive expander pre_payload code)]
    (in [source requirements post_payload])))

(def (iteration wrapper archive expander module source pre_payload aliases)
  (All (_ directive)
    (-> ///phase.Wrapper Archive Expander descriptor.Module Source (Payload directive) Aliases
        (All (_ anchor expression)
          (///directive.Operation anchor expression directive
                                  (Maybe [Source Requirements (Payload directive)])))))
  (do ///phase.monad
    [reader (///directive.lifted_analysis
             (..reader module aliases source))]
    (function (_ state)
      (case (///phase.result' state (..iteration' wrapper archive expander reader source pre_payload))
        {try.#Success [state source&requirements&buffer]}
        {try.#Success [state {.#Some source&requirements&buffer}]}

        {try.#Failure error}
        (if (exception.match? ///syntax.end_of_file error)
          {try.#Success [state {.#None}]}
          (exception.with ///.cannot_compile module {try.#Failure error}))))))

(def (default_dependencies prelude input)
  (-> descriptor.Module ///.Input (List descriptor.Module))
  (list.partial descriptor.runtime
                (if (text#= prelude (the ///.#module input))
                  (list)
                  (list prelude))))

(def module_aliases
  (-> .Module Aliases)
  (|>> (the .#module_aliases) (dictionary.of_list text.hash)))

(def .public (compiler wrapper expander prelude write_directive)
  (All (_ anchor expression directive)
    (-> ///phase.Wrapper Expander descriptor.Module (-> directive Binary)
        (Instancer (///directive.State+ anchor expression directive) .Module)))
  (let [execute! (directiveP.phase wrapper expander)]
    (function (_ key parameters input)
      (let [dependencies (default_dependencies prelude input)]
        [///.#dependencies dependencies
         ///.#process (function (_ state archive)
                        (do [! try.monad]
                          [.let [hash (text#hash (the ///.#code input))]
                           [state [source buffer]] (<| (///phase.result' state)
                                                       (..begin dependencies hash input))
                           .let [module (the ///.#module input)]]
                          (loop (again [iteration (<| (///phase.result' state)
                                                      (..iteration wrapper archive expander module source buffer ///syntax.no_aliases))])
                            (do !
                              [[state ?source&requirements&temporary_payload] iteration]
                              (case ?source&requirements&temporary_payload
                                {.#None}
                                (do !
                                  [[state [analysis_module [final_buffer final_registry]]] (///phase.result' state (..end module))
                                   .let [descriptor [descriptor.#hash hash
                                                     descriptor.#name module
                                                     descriptor.#file (the ///.#file input)
                                                     descriptor.#references (set.of_list text.hash dependencies)
                                                     descriptor.#state {.#Compiled}]]]
                                  (in [state
                                       {.#Right [[module.#id (try.else module.runtime (archive.id module archive))
                                                  module.#descriptor descriptor
                                                  module.#document (document.document key analysis_module)]
                                                 (sequence#each (function (_ [artifact_id custom directive])
                                                                  [artifact_id custom (write_directive directive)])
                                                                final_buffer)
                                                 final_registry]}]))

                                {.#Some [source requirements temporary_payload]}
                                (let [[temporary_buffer temporary_registry] temporary_payload]
                                  (in [state
                                       {.#Left [///.#dependencies (|> requirements
                                                                      (the ///directive.#imports)
                                                                      (list#each product.left))
                                                ///.#process (function (_ state archive)
                                                               (again (<| (///phase.result' state)
                                                                          (do [! ///phase.monad]
                                                                            [analysis_module (<| (is (Operation .Module))
                                                                                                 ///directive.lifted_analysis
                                                                                                 extension.lifted
                                                                                                 meta.current_module)
                                                                             _ (///directive.lifted_generation
                                                                                (///generation.set_buffer temporary_buffer))
                                                                             _ (///directive.lifted_generation
                                                                                (///generation.set_registry temporary_registry))
                                                                             _ (|> requirements
                                                                                   (the ///directive.#referrals)
                                                                                   (monad.each ! (execute! archive)))
                                                                             temporary_payload (..get_current_payload temporary_payload)]
                                                                            (..iteration wrapper archive expander module source temporary_payload (..module_aliases analysis_module))))))]}]))
                                )))))]))))