aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/default/platform.lux
blob: 05e645e58f9729893bf460dfd419497dcf0302c0 (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
(.module:
  [lux (#- Module)
   [type (#+ :share)]
   [control
    ["." monad (#+ Monad do)]]
   [data
    ["." bit]
    ["." product]
    ["." error (#+ Error)]
    [text
     format]
    [collection
     ["." list]]]
   [world
    ["." file (#+ File)]]]
  ["." // #_
   ["#." init]
   ["#." syntax]
   ["#/" //
    ["#." analysis]
    ["#." statement]
    ["#." phase
     [macro (#+ Expander)]
     ## TODO: Get rid of this import ASAP
     [extension (#+)]
     ["." generation]
     [analysis
      ["." module]]]
    [meta
     ["." archive (#+ Archive)
      [descriptor (#+ Module)]]
     [io
      ["." context]]]]]
  [program
   [compositor
    ["." cli (#+ Configuration)]]])

(type: #export (Platform ! anchor expression statement)
  {#&monad (Monad !)
   #&file-system (file.System !)
   #host (generation.Host expression statement)
   #phase (generation.Phase anchor expression statement)
   #runtime (generation.Operation anchor expression statement Any)})

## (def: (write-module target-dir file-name module-name module outputs)
##   (-> File Text Text Module Outputs (Process Any))
##   (do (error.with io.monad)
##     [_ (monad.map @ (product.uncurry (&io.write target-dir))
##                   (dictionary.entries outputs))]
##     (&io.write target-dir
##                (format module-name "/" cache.descriptor-name)
##                (encoding.to-utf8 (%code (cache/description.write file-name module))))))

(with-expansions [<type-vars> (as-is [! anchor expression statement])
                  <Platform> (as-is (Platform ! anchor expression statement))
                  <State+> (as-is (///statement.State+ anchor expression statement))
                  <Bundle> (as-is (generation.Bundle anchor expression statement))]

  (def: #export (initialize expander platform generation-bundle)
    (All <type-vars>
      (-> Expander <Platform> <Bundle> (! (Error <State+>))))
    (|> platform
        (get@ #runtime)
        ///statement.lift-generation
        (///phase.run' (//init.state expander
                                     (get@ #host platform)
                                     (get@ #phase platform)
                                     generation-bundle))
        (:: error.functor map product.left)
        (:: (get@ #&monad platform) wrap))

    ## (case (runtimeT.generate ## (initL.compiler (io.run js.init))
    ##        (initL.compiler (io.run hostL.init-host))
    ##        )
    ##   ## (#error.Success [state disk-write])
    ##   ## (do @
    ##   ##   [_ (&io.prepare-target target)
    ##   ##    _ disk-write
    ##   ##    ## _ (cache/io.pre-load sources target (commonT.load-definition state))
    ##   ##    ]
    ##   ##   (wrap (|> state
    ##   ##             (set@ [#.info #.mode] #.Build))))

    ##   (#error.Success [state [runtime-bc function-bc]])
    ##   (do @
    ##     [_ (&io.prepare-target target)
    ##      ## _ (&io.write target (format hostL.runtime-class ".class") runtime-bc)
    ##      ## _ (&io.write target (format hostL.function-class ".class") function-bc)
    ##      ## _ (cache/io.pre-load sources target (commonT.load-definition state))
    ##      ]
    ##     (wrap (|> state
    ##               (set@ [#.info #.mode] #.Build))))

    ##   (#error.Failure error)
    ##   (io.fail error))
    )

  (def: #export (compile expander platform configuration archive state)
    (All <type-vars>
      (-> Expander <Platform> Configuration Archive <State+> (! (Error [Archive <State+>]))))
    (let [monad (get@ #&monad platform)
          source-module (get@ #cli.module configuration)
          compiler (:share [anchor expression statement]
                           {<State+>
                            state}
                           {(///.Compiler <State+> .Module Any)
                            ((//init.compiler expander //syntax.prelude) //init.key (list))})]
      (loop [module source-module
             [archive state] [archive state]]
        (if (archive.archived? archive module)
          (:: monad wrap (#error.Success [archive state]))
          (let [import! (:share <type-vars>
                                {<Platform>
                                 platform}
                                {(-> Module [Archive <State+>]
                                     (! (Error [Archive <State+>])))
                                 recur})]
            (do (error.with monad)
              [input (context.read monad
                                   (get@ #&file-system platform)
                                   (get@ #cli.sources configuration)
                                   module)
               ## _ (&io.prepare-module target-dir (get@ #cli.module configuration))
               ## _ (write-module target-dir file-name (get@ #cli.module configuration) module outputs)
               ]
              (loop [archive archive
                     state state
                     compilation (compiler (:coerce ///.Input input))]
                (do @
                  [#let [dependencies (get@ #///.dependencies compilation)]
                   archive+state (monad.fold @
                                             import!
                                             [archive state]
                                             (list.filter (bit.complement (archive.archived? archive))
                                                          dependencies))
                   #let [[archive state] (:share <type-vars>
                                                 {<Platform>
                                                  platform}
                                                 {[Archive <State+>]
                                                  archive+state})
                         continue! (:share <type-vars>
                                           {<Platform>
                                            platform}
                                           {(-> Archive <State+> (///.Compilation <State+> .Module Any)
                                                (! (Error [Archive <State+>])))
                                            recur})]]
                  (case ((get@ #///.process compilation)
                         (case dependencies
                           #.Nil
                           state

                           _
                           ## TODO: The "///analysis.set-current-module" below shouldn't be necessary. Remove it ASAP.
                           (|> (///analysis.set-current-module module)
                               ///statement.lift-analysis
                               (///phase.run' state)
                               error.assume
                               product.left))
                         archive)
                    (#error.Success [state more|done])
                    (case more|done
                      (#.Left more)
                      (continue! archive state more)

                      (#.Right [descriptor+document output])
                      (case (archive.add module descriptor+document archive)
                        (#error.Success archive)
                        (wrap [archive state])
                        
                        (#error.Failure error)
                        (:: monad wrap (#error.Failure error))))

                    (#error.Failure error)
                    (:: monad wrap (#error.Failure error)))))))))))
  )