aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/compositor.lux
blob: fdd985f2a486cbe3bd32d5796df0e4bf78aab64c (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
(.module:
  [lux (#- Module)
   [type (#+ :share)]
   ["@" target (#+ Host)]
   [abstract
    [monad (#+ Monad do)]]
   [control
    ["." io (#+ IO io)]
    ["." try (#+ Try)]
    [parser
     [cli (#+ program:)]]
    [security
     ["!" capability]]
    [concurrency
     ["." promise (#+ Promise) ("#\." monad)]]]
   [data
    [binary (#+ Binary)]
    ["." product]
    ["." text
     ["%" format (#+ format)]]
    [collection
     ["." dictionary]
     ["." row (#+ Row)]]]
   ["." world #_
    ["." file (#+ File Path)]
    ["#/." program]
    ## ["." console]
    ]
   [tool
    [compiler
     ["." phase]
     [default
      ["." platform (#+ Platform)]]
     [language
      ["$" lux
       ["#/." program (#+ Program)]
       ["." syntax]
       ["." analysis
        [macro (#+ Expander)]]
       ["." generation (#+ Buffer Context)]
       ["." directive]
       [phase
        [extension (#+ Extender)]]]]
     [meta
      [packager (#+ Packager)]
      [archive (#+ Archive)
       [descriptor (#+ Module)]]
      [cache
       ["." dependency]]
      [io
       ["ioW" archive]]]]
    ## ["." interpreter]
    ]]
  ["." / #_
   ["#." cli (#+ Service)]
   ["#." static (#+ Static)]
   ["#." export]
   ["#." import]])

(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 (\ world/program.default exit +1)))

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

(def: (package! monad file-system [packager package] static archive context)
  (All [!] (-> (Monad !) (file.System !) [Packager Path] Static Archive Context (! (Try Any))))
  (for {@.old
        (do (try.with monad)
          [#let [packager (:share [!] {(Monad !) monad} {(Packager !) packager})]
           content (packager monad file-system static archive context)
           package (:share [!]
                           {(Monad !)
                            monad}
                           {(! (Try (File !)))
                            (:assume (file.get-file monad file-system package))})]
          (!.use (\ (:share [!]
                            {(Monad !)
                             monad}
                            {(File !)
                             (:assume package)})
                    over-write)
                 [content]))}
       ## TODO: Fix whatever type-checker bug is forcing me into this compromise...
       (:assume
        (: (Promise (Try Any))
           (let [monad (:coerce (Monad Promise) monad)
                 file-system (:coerce (file.System Promise) file-system)
                 packager (:coerce (Packager Promise) packager)]
             (do (try.with monad)
               [content (packager monad file-system static archive context)
                package (: (Promise (Try (File Promise)))
                           (file.get-file monad file-system package))]
               (!.use (\ (: (File Promise) package) over-write) [content])))))))

(with-expansions [<parameters> (as-is anchor expression artifact)]
  (def: #export (compiler static
                          expander host-analysis platform generation-bundle host-directive-bundle program anchorT,expressionT,directiveT extender
                          service
                          packager,package)
    (All [<parameters>]
      (-> Static
          Expander
          analysis.Bundle
          (IO (Platform <parameters>))
          (generation.Bundle <parameters>)
          (directive.Bundle <parameters>)
          (Program expression artifact)
          [Type Type Type]
          Extender
          Service
          [Packager Path]
          (Promise Any)))
    (do {! promise.monad}
      [platform (promise.future platform)]
      (case service
        (#/cli.Compilation compilation)
        (<| (or-crash! "Compilation failed:")
            (do (try.with promise.monad)
              [#let [[compilation-sources compilation-libraries compilation-target compilation-module] compilation]
               import (/import.import (get@ #platform.&file-system platform) compilation-libraries)
               [state archive] (:share [<parameters>]
                                       {(Platform <parameters>)
                                        platform}
                                       {(Promise (Try [(directive.State+ <parameters>)
                                                       Archive]))
                                        (:assume (platform.initialize static compilation-module expander host-analysis platform generation-bundle host-directive-bundle program anchorT,expressionT,directiveT extender
                                                                      import compilation-sources))})
               [archive state] (:share [<parameters>]
                                       {(Platform <parameters>)
                                        platform}
                                       {(Promise (Try [Archive (directive.State+ <parameters>)]))
                                        (:assume (platform.compile import static expander platform compilation [archive state]))})
               _ (ioW.freeze (get@ #platform.&file-system platform) static archive)
               program-context (promise\wrap ($/program.context archive))
               _ (promise.future (..package! io.monad file.default packager,package static archive program-context))]
              (wrap (log! "Compilation complete!"))))

        (#/cli.Export export)
        (<| (or-crash! "Export failed:")
            (do (try.with promise.monad)
              [_ (/export.export (get@ #platform.&file-system platform)
                                 export)]
              (wrap (log! "Export complete!"))))
        
        (#/cli.Interpretation interpretation)
        ## TODO: Fix the interpreter...
        (undefined)
        ## (<| (or-crash! "Interpretation failed:")
        ##     (do {! promise.monad}
        ##       [console (|> console.default
        ##                    promise.future
        ##                    (\ ! map (|>> try.assume console.async)))]
        ##       (interpreter.run (try.with promise.monad) console platform interpretation generation-bundle)))
        ))))