aboutsummaryrefslogtreecommitdiff
path: root/lux-jvm/source/program.lux
blob: a6c07bbd43d0f3596239d86e74623fe1e423ded0 (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
(.require
 [library
  [lux (.except Definition)
   [program (.only program)]
   ["[0]" ffi (.only import)]
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" io (.only IO)]
    ["[0]" try (.only Try)]
    [concurrency
     ["[0]" async (.only Async)]]]
   [data
    ["[0]" product]
    [text
     ["%" \\format (.only format)]]
    [collection
     [array (.only Array)]]]
   [meta
    ["@" target (.only)
     ["[0]" jvm
      [bytecode (.only Bytecode)]
      ["[1]/[0]" type
       ["[1]/[0]" box]]]]
    [compiler (.only)
     [default
      ["[0]" platform (.only Platform)]]
     [meta
      [archive (.only Archive)]
      ["[0]" context]
      ["[0]" cli]
      ["[0]" packager
       ["[1]" jvm]]]
     [language
      [lux
       [translation (.only Operation)]
       [analysis
        [macro (.only Expander)]]
       ["[0]" phase (.only)
        ["[0]" extension (.only)
         ["[0]" analysis
          ["[1]" jvm]]
         ["[0]" translation
          ["[1]" jvm]]
         ["[0]" declaration
          ["[1]" jvm]]]
        [translation
         ["/" jvm (.only)
          ["[1][0]" runtime (.only Anchor Definition)]
          ["[1][0]" host]
          ["[1][0]" program]
          ["[1][0]" reference]]]]]]]]
   ["[0]" world
    ["[0]" file]
    ["[1]/[0]" environment]]]]
 [program
  ["[0]" compositor]])

(import java/lang/reflect/Method
  "[1]::[0]"
  (invoke [java/lang/Object [java/lang/Object]] "try" java/lang/Object))

(import java/lang/ClassLoader
  "[1]::[0]")

(import (java/lang/Class c)
  "[1]::[0]"
  (getMethod [java/lang/String [(java/lang/Class java/lang/Object)]] "try" java/lang/reflect/Method))

(import java/lang/Object
  "[1]::[0]"
  (getClass [] (java/lang/Class java/lang/Object)))

(def _object_class
  (java/lang/Class java/lang/Object)
  (ffi.class_for java/lang/Object))

(def _apply2_args
  (Array (java/lang/Class java/lang/Object))
  (|> (ffi.array (java/lang/Class java/lang/Object) 2)
      (ffi.write! 0 _object_class)
      (ffi.write! 1 _object_class)))

(def _apply4_args
  (Array (java/lang/Class java/lang/Object))
  (|> (ffi.array (java/lang/Class java/lang/Object) 4)
      (ffi.write! 0 _object_class)
      (ffi.write! 1 _object_class)
      (ffi.write! 2 _object_class)
      (ffi.write! 3 _object_class)))

(def .public (expander macro inputs lux)
  Expander
  (do try.monad
    [apply_method (|> macro
                      (as java/lang/Object)
                      (java/lang/Object::getClass)
                      (java/lang/Class::getMethod "apply" _apply2_args))]
    (as (Try (Try [Lux (List Code)]))
        (java/lang/reflect/Method::invoke
         (as java/lang/Object macro)
         (|> (ffi.array java/lang/Object 2)
             (ffi.write! 0 (as java/lang/Object inputs))
             (ffi.write! 1 (as java/lang/Object lux)))
         apply_method))))

(def phase_wrapper
  phase.Wrapper
  (|>>))

(def .public platform
  (IO [java/lang/ClassLoader
       /runtime.Host
       (Platform Anchor (Bytecode Any) Definition)])
  (do io.monad
    [[loader host] /host.host]
    (in [loader
         host
         [platform.#file_system (file.async file.default)
          platform.#host host

          platform.#phase /.translate
          platform.#runtime /runtime.translate

          platform.#phase_wrapper ..phase_wrapper
          platform.#write product.right]])))

(def extender
  extension.Extender
  ... TODO: Stop relying on coercions ASAP.
  (<| (as extension.Extender)
      (function (@self handler))
      (as extension.Handler)
      (function (@self phase))
      (as phase.Phase)
      (function (@self archive parameters))
      (as phase.Operation)
      (function (@self state))
      (as Try)
      try.trusted
      (as Try)
      (do try.monad
        [method (|> handler
                    (as java/lang/Object)
                    (java/lang/Object::getClass)
                    (java/lang/Class::getMethod "apply" _apply4_args))]
        (java/lang/reflect/Method::invoke
         (as java/lang/Object handler)
         (|> (ffi.array java/lang/Object 4)
             (ffi.write! 0 (as java/lang/Object (phase_wrapper phase)))
             (ffi.write! 1 (as java/lang/Object archive))
             (ffi.write! 2 (as java/lang/Object parameters))
             (ffi.write! 3 (as java/lang/Object state)))
         method))))

(def (declare_success! _)
  (-> Any (Async Any))
  (async.future (of world/environment.default exit +0)))

(def lux_compiler
  (-> Any platform.Custom)
  (|>> as_expected))

(def _
  (program [service cli.service]
    (let [context (context.jvm (cli.target service))]
      (exec (do async.monad
              [[loader host platform] (async.future ..platform)
               _ (compositor.compiler ..lux_compiler
                                      context
                                      ..expander
                                      (analysis.bundle loader host)
                                      (io.io platform)
                                      translation.bundle
                                      (declaration.bundle loader ..extender)
                                      (/program.program /runtime.class_name)
                                      /reference.constant
                                      ..extender
                                      service
                                      [(packager.package context)
                                       "program.jar"])]
              (..declare_success! []))
        (io.io [])))))