aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/default/init.lux
blob: 40549f8d0483b6c1e682dace7056efed582e25b3 (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
(.module:
  [lux (#- Module)
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." try (#+ Try)]
    ["ex" exception (#+ exception:)]]
   [data
    ["." product]
    ["." text ("#@." hash)]
    [collection
     ["." list ("#@." functor)]
     ["." dictionary]
     ["." set]]]
   ["." macro]
   [world
    ["." file]]]
  ["." //
   ["#." syntax (#+ Aliases)]
   ["#." evaluation]
   ["/#" // (#+ Instancer)
    ["#." analysis]
    ["#." synthesis]
    ["#." directive (#+ Requirements)]
    ["#." phase
     [macro (#+ Expander)]
     [".P" analysis
      ["." module]]
     [".P" synthesis]
     ["." generation]
     [".P" directive]
     ["." extension
      [".E" analysis]
      [".E" synthesis]
      [directive
       [".S" lux]]]]
    [meta
     [archive
      ["." signature]
      ["." key (#+ Key)]
      ["." descriptor (#+ Module)]
      ["." document]]]]])

(def: #export (info target)
  (-> Text Info)
  {#.target target
   #.version //.version
   #.mode #.Build})

(def: #export (state target expander host-analysis host generate generation-bundle host-directive-bundle program)
  (All [anchor expression directive]
    (-> Text
        Expander
        ///analysis.Bundle
        (generation.Host expression directive)
        (generation.Phase anchor expression directive)
        (generation.Bundle anchor expression directive)
        (///directive.Bundle anchor expression directive)
        (-> expression directive)
        (///directive.State+ anchor expression directive)))
  (let [synthesis-state [synthesisE.bundle ///synthesis.init]
        generation-state [generation-bundle (generation.state host)]
        eval (//evaluation.evaluator expander synthesis-state generation-state generate)
        analysis-state [(analysisE.bundle eval host-analysis)
                        (///analysis.state (..info target) host)]]
    [(dictionary.merge (luxS.bundle expander host-analysis program)
                       host-directive-bundle)
     {#///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}}]))

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

(def: (reader current-module aliases [cursor offset source-code])
  (-> 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 [[cursor _] output]
        (#try.Success [[bundle (|> compiler
                                   (set@ #.source source')
                                   (set@ #.cursor cursor))]
                       [source' output]])))))

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

(def: (begin dependencies hash input)
  (-> (List Module) Nat ///.Input
      (All [anchor expression directive]
        (///directive.Operation anchor expression directive
                                [Source (generation.Buffer directive)])))
  (///directive.lift-analysis
   (do ///phase.monad
     [#let [module (get@ #///.module input)]
      _ (module.create hash module)
      _ (///analysis.set-current-module module)
      _ (monad.map @ module.import dependencies)
      #let [source (///analysis.source (get@ #///.module input) (get@ #///.code input))]
      _ (///analysis.set-source-code source)]
     (wrap [source generation.empty-buffer]))))

(def: (end module)
  (-> Module (Operation Any))
  (do ///phase.monad
    [_ (///directive.lift-analysis
        (module.set-compiled module))]
    (///directive.lift-generation
     (generation.save-buffer! module))))

## TODO: Inline ASAP
(def: (get-current-buffer old-buffer)
  (All [directive]
    (-> (generation.Buffer directive)
        (All [anchor expression]
          (///directive.Operation anchor expression directive
                                  (generation.Buffer directive)))))
  (///directive.lift-generation
   generation.buffer))

## TODO: Inline ASAP
(def: (process-directive expander pre-buffer code)
  (All [directive]
    (-> Expander (generation.Buffer directive) Code
        (All [anchor expression]
          (///directive.Operation anchor expression directive
                                  [Requirements (generation.Buffer directive)]))))
  (do ///phase.monad
    [_ (///directive.lift-generation
        (generation.set-buffer pre-buffer))
     requirements (let [execute! (directiveP.phase expander)]
                    (execute! code))
     post-buffer (..get-current-buffer pre-buffer)]
    (wrap [requirements post-buffer])))

(def: (iteration expander reader source pre-buffer)
  (All [directive]
    (-> Expander Reader Source (generation.Buffer directive)
        (All [anchor expression]
          (///directive.Operation anchor expression directive
                                  [Source Requirements (generation.Buffer directive)]))))
  (do ///phase.monad
    [[source code] (///directive.lift-analysis
                    (..read source reader))
     [requirements post-buffer] (process-directive expander pre-buffer code)]
    (wrap [source requirements post-buffer])))

(def: (iterate expander module source pre-buffer aliases)
  (All [directive]
    (-> Expander Module Source (generation.Buffer directive) Aliases
        (All [anchor expression]
          (///directive.Operation anchor expression directive
                                  (Maybe [Source Requirements (generation.Buffer directive)])))))
  (do ///phase.monad
    [reader (///directive.lift-analysis
             (..reader module aliases source))]
    (function (_ state)
      (case (///phase.run' state (..iteration expander reader source pre-buffer))
        (#try.Success [state source&requirements&buffer])
        (#try.Success [state (#.Some source&requirements&buffer)])

        (#try.Failure error)
        (if (ex.match? //syntax.end-of-file error)
          (#try.Success [state #.None])
          (ex.with-stack ///.cannot-compile module (#try.Failure error)))))))

(def: (default-dependencies prelude input)
  (-> Module ///.Input (List Module))
  (if (text@= prelude (get@ #///.module input))
    (list)
    (list prelude)))

(def: module-aliases
  (-> .Module Aliases)
  (|>> (get@ #.module-aliases) (dictionary.from-list text.hash)))

(def: #export (compiler expander prelude)
  (-> Expander Module
      (All [anchor expression directive]
        (Instancer (///directive.State+ anchor expression directive) .Module)))
  (let [execute! (directiveP.phase expander)]
    (function (_ key parameters input)
      (let [dependencies (default-dependencies prelude input)]
        {#///.dependencies dependencies
         #///.process (function (_ state archive)
                        (do try.monad
                          [#let [hash (text@hash (get@ #///.code input))]
                           [state [source buffer]] (<| (///phase.run' state)
                                                       (..begin dependencies hash input))
                           #let [module (get@ #///.module input)]]
                          (loop [iteration (<| (///phase.run' state)
                                               (..iterate expander module source buffer //syntax.no-aliases))]
                            (do @
                              [[state ?source&requirements&buffer] iteration]
                              (case ?source&requirements&buffer
                                #.None
                                (do @
                                  [[state analysis-module] (<| (///phase.run' state)
                                                               (do ///phase.monad
                                                                 [_ (..end module)]
                                                                 (<| (: (Operation .Module))
                                                                     ///directive.lift-analysis
                                                                     extension.lift
                                                                     macro.current-module)))
                                   #let [descriptor {#descriptor.hash hash
                                                     #descriptor.name module
                                                     #descriptor.file (get@ #///.file input)
                                                     #descriptor.references (set.from-list text.hash dependencies)
                                                     #descriptor.state #.Compiled}]]
                                  (wrap [state
                                         (#.Right [[descriptor (document.write key analysis-module)]
                                                   (dictionary.new text.hash)])]))

                                (#.Some [source requirements buffer])
                                (wrap [state
                                       (#.Left {#///.dependencies (|> requirements
                                                                      (get@ #///directive.imports)
                                                                      (list@map product.left))
                                                #///.process (function (_ state archive)
                                                               (recur (<| (///phase.run' state)
                                                                          (do ///phase.monad
                                                                            [analysis-module (<| (: (Operation .Module))
                                                                                                 ///directive.lift-analysis
                                                                                                 extension.lift
                                                                                                 macro.current-module)
                                                                             _ (///directive.lift-generation
                                                                                (generation.set-buffer buffer))
                                                                             _ (monad.map @ execute! (get@ #///directive.referrals requirements))
                                                                             buffer (..get-current-buffer buffer)]
                                                                            (..iterate expander module source buffer (..module-aliases analysis-module))))))})])
                                )))))}))))

(def: #export key
  (Key .Module)
  (key.key {#signature.name (name-of ..compiler)
            #signature.version //.version}
           (module.new 0)))