aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/default/platform.lux
blob: 4ed6d6d42863a2a8a8d4956961f8e32c3102466c (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
(.module:
  [lux (#- Module)
   [type (#+ :share)]
   [abstract
    ["." monad (#+ Monad do)]]
   [control
    ["." try (#+ Try)]]
   [data
    ["." bit]
    ["." product]
    [text
     ["%" format (#+ format)]]
    [collection
     ["." list]]]
   [world
    ["." file (#+ File)]]]
  ["." // #_
   ["#." init]
   ["#." syntax]
   ["/#" //
    ["#." analysis]
    ["#." directive]
    ["#." 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 directive)
  {#&monad (Monad !)
   #&file-system (file.System !)
   #host (generation.Host expression directive)
   #phase (generation.Phase anchor expression directive)
   #runtime (generation.Operation anchor expression directive Any)})

## (def: (write-module target-dir file-name module-name module outputs)
##   (-> File Text Text Module Outputs (Process Any))
##   (do (try.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 directive])
                  <Platform> (as-is (Platform ! anchor expression directive))
                  <State+> (as-is (///directive.State+ anchor expression directive))
                  <Bundle> (as-is (generation.Bundle anchor expression directive))]

  (def: #export (initialize target expander host-analysis platform generation-bundle host-directive-bundle program)
    (All <type-vars>
      (-> Text
          Expander
          ///analysis.Bundle
          <Platform>
          <Bundle>
          (///directive.Bundle anchor expression directive)
          (-> expression directive)
          (! (Try <State+>))))
    (|> platform
        (get@ #runtime)
        ///directive.lift-generation
        (///phase.run' (//init.state target
                                     expander
                                     host-analysis
                                     (get@ #host platform)
                                     (get@ #phase platform)
                                     generation-bundle
                                     host-directive-bundle
                                     program))
        (:: try.functor map product.left)
        (:: (get@ #&monad platform) wrap))

    ## (case (runtimeT.generate ## (initL.compiler (io.run js.init))
    ##        (initL.compiler (io.run hostL.init-host))
    ##        )
    ##   ## (#try.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))))

    ##   (#try.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))))

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

  (def: #export (compile partial-host-extension expander platform configuration archive state)
    (All <type-vars>
      (-> Text Expander <Platform> Configuration Archive <State+> (! (Try [Archive <State+>]))))
    (let [monad (get@ #&monad platform)
          source-module (get@ #cli.module configuration)
          compiler (:share [anchor expression directive]
                           {<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 (#try.Success [archive state]))
          (let [import! (:share <type-vars>
                                {<Platform>
                                 platform}
                                {(-> Module [Archive <State+>]
                                     (! (Try [Archive <State+>])))
                                 recur})]
            (do (try.with monad)
              [input (context.read monad
                                   (get@ #&file-system platform)
                                   (get@ #cli.sources configuration)
                                   partial-host-extension
                                   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)
                                                (! (Try [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)
                               ///directive.lift-analysis
                               (///phase.run' state)
                               try.assume
                               product.left))
                         archive)
                    (#try.Success [state more|done])
                    (case more|done
                      (#.Left more)
                      (continue! archive state more)

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

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