aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/program.lux
blob: cee6277087a2c9f3082aae19b6a0a226515537ba (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
(.module:
  [lux #*
   [cli (#+ program:)]
   [control
    [monad (#+ do)]]
   [data
    ["." error (#+ Error)]
    ["." text
     format]]
   ["." io (#+ IO io)]
   [time
    ["." instant (#+ Instant)]]
   [world
    ["." file (#+ File)]
    ["." console]]
   [tool
    ["." compiler
     ["." cli (#+ Configuration)]
     [meta
      ["." archive]
      [io
       ["." context]]]
     ["." phase
      ["." translation]
      ["." statement]]
     [default
      ["." platform (#+ Platform)]
      ["." init]
      ["." syntax]]]
    ## ["." interpreter]
    ]]
  [luxc
   [lang
    ["." host/jvm]
    [translation
     ["." jvm
      ["." runtime]
      ["." expression]
      [procedure
       ["." common]]]]]])

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

      (#error.Success output)
      (wrap output))))

(def: (timed action)
  (All [a]
    (-> (-> Any (IO (Error a))) (IO (Error a))))
  (do (error.with-error io.monad)
    [start (: (IO (Error Instant))
              (error.lift io.monad instant.now))
     result (action [])
     finish (: (IO (Error Instant))
               (error.lift io.monad instant.now))
     #let [elapsed-time (instant.span start finish)
           _ (log! (format text.new-line
                           "Elapsed time: " (%duration elapsed-time)))]]
    (wrap result)))

(def: jvm-platform
  (IO (Platform IO host/jvm.Anchor host/jvm.Inst host/jvm.Definition))
  (do io.monad
    [host jvm.init]
    (wrap {#platform.&monad io.monad
           #platform.&file-system file.system
           #platform.host host
           #platform.phase expression.translate
           #platform.runtime runtime.translate})))

(program: [{service cli.service}]
  (do io.monad
    [platform (: (IO (Platform IO host/jvm.Anchor host/jvm.Inst host/jvm.Definition))
                 ..jvm-platform)
     console (:: @ map error.assume console.system)]
    (case service
      (#cli.Compilation configuration)
      (<| (or-crash! "Compilation failed:")
          ..timed
          (function (_ _)
            (do (error.with-error io.monad)
              [state (: (IO (Error (statement.State+ host/jvm.Anchor host/jvm.Inst host/jvm.Definition)))
                        (platform.initialize platform common.bundle))
               ## _ (platform.compile platform (set@ #cli.module syntax.prelude configuration) state)
               _ (: (IO (Error Any))
                    (platform.compile platform configuration state))
               ## _ (cache/io.clean target ...)
               ]
              (wrap (log! "Compilation complete!")))))
      
      (#cli.Interpretation configuration)
      ## TODO: Fix the interpreter...
      (undefined)
      ## (<| (or-crash! "Interpretation failed:")
      ##     (interpreter.run (error.with-error io.monad) console platform configuration common.bundle))
      )))