aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/compositor.lux
blob: 7bcc07d7cbad72003be2ce0b672d3525185a9039 (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
(.module:
  [lux #*
   [type (#+ :share)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." io (#+ IO io)]
    ["." try (#+ Try)]
    [parser
     [cli (#+ program:)]]
    [security
     ["!" capability]]
    [concurrency
     ["." promise (#+ Promise)]]]
   [data
    [binary (#+ Binary)]
    ["." product]
    ["." text
     ["%" format (#+ format)]]
    [collection
     ["." dictionary]
     ["." row]
     ["." list ("#@." functor fold)]]]
   [time
    ["." instant (#+ Instant)]]
   [world
    ["." file (#+ File Path)]
    ["." console]]
   [tool
    [compiler
     ["." phase]
     [default
      ["." platform (#+ Platform)]]
     [language
      [lux
       ["." syntax]
       ["." analysis
        [macro (#+ Expander)]]
       ["." generation]
       ["." directive]
       [phase
        [extension (#+ Extender)]]]]
     [meta
      ["." archive (#+ Archive)]]]
    ## ["." interpreter]
    ]]
  [/
   ["." cli (#+ Service)]])

(def: (or-crash! failure-description action)
  (All [a]
    (-> Text (Promise (Try a)) (Promise a)))
  (do promise.monad
    [?output action]
    (case ?output
      (#try.Failure error)
      (exec (log! (format text.new-line
                          failure-description text.new-line
                          error text.new-line))
        (io.run (io.exit +1)))

      (#try.Success output)
      (wrap output))))

(with-expansions [<parameters> (as-is anchor expression artifact)]
  (def: (save-artifacts! system state [packager package])
    (All [<parameters>]
      (-> (file.System Promise)
          (directive.State+ <parameters>)
          [(-> (generation.Output artifact) Binary) Path]
          (Promise (Try Any))))
    (let [?outcome (phase.run' state
                               (:share [<parameters>]
                                       {(directive.State+ <parameters>)
                                        state}
                                       {(directive.Operation <parameters>
                                                             (generation.Output artifact))
                                        (directive.lift-generation generation.output)}))]
      (case ?outcome
        (#try.Success [state output])
        (do (try.with promise.monad)
          [file (: (Promise (Try (File Promise)))
                   (file.get-file promise.monad system package))]
          (!.use (:: file over-write) (packager output)))

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

  (def: #export (compiler target partial-host-extension
                          expander host-analysis platform generation-bundle host-directive-bundle program extender
                          service
                          packager,package)
    (All [<parameters>]
      (-> Text
          Text
          Expander
          analysis.Bundle
          (IO (Platform <parameters>))
          (generation.Bundle <parameters>)
          (directive.Bundle <parameters>)
          (-> expression artifact)
          Extender
          Service
          [(-> (generation.Output artifact) Binary) Path]
          (Promise Any)))
    (do promise.monad
      [platform (promise.future platform)
       console (|> console.system
                   promise.future
                   (:: @ map (|>> try.assume console.async)))]
      (case service
        (#cli.Compilation configuration)
        (<| (or-crash! "Compilation failed:")
            (do (try.with promise.monad)
              [state (:share [<parameters>]
                             {(Platform <parameters>)
                              platform}
                             {(Promise (Try (directive.State+ <parameters>)))
                              (platform.initialize target expander host-analysis platform generation-bundle host-directive-bundle program extender)})
               [archive state] (:share [<parameters>]
                                       {(Platform <parameters>)
                                        platform}
                                       {(Promise (Try [Archive (directive.State+ <parameters>)]))
                                        (platform.compile partial-host-extension expander platform configuration archive.empty state)})
               _ (save-artifacts! (get@ #platform.&file-system platform) state packager,package)
               ## _ (cache/io.clean target ...)
               ]
              (wrap (log! "Compilation complete!"))))
        
        (#cli.Interpretation configuration)
        ## TODO: Fix the interpreter...
        (undefined)
        ## (<| (or-crash! "Interpretation failed:")
        ##     (interpreter.run (try.with promise.monad) console platform configuration generation-bundle))
        ))))