aboutsummaryrefslogtreecommitdiff
path: root/lux-cl/source/program.lux
blob: 1a3b767a463549812e576bcfc6e80e525fecf56a (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
(.module:
  [lux #*
   [program (#+ program:)]
   ["." ffi]
   ["." debug]
   [abstract
    ["." monad (#+ do)]]
   [control
    [pipe (#+ exec> case> new>)]
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]
    ["." io (#+ IO io)]
    [concurrency
     ["." promise (#+ Promise)]]]
   [data
    ["." maybe]
    ["." text ("#\." hash)
     ["%" format (#+ format)]
     ["." encoding]]
    [collection
     ["." array (#+ Array)]]]
   [macro
    ["." template]]
   [math
    [number (#+ hex)
     ["n" nat]
     ["." i64]]]
   ["." world #_
    ["." file]
    ["#/." program]]
   ["@" target
    ["_" common_lisp]]
   [tool
    [compiler
     [phase (#+ Operation Phase)]
     [reference
      [variable (#+ Register)]]
     [language
      [lux
       [program (#+ Program)]
       [generation (#+ Context Host)]
       ["." synthesis]
       [analysis
        [macro (#+ Expander)]]
       [phase
        ["." extension (#+ Extender Handler)
         ["#/." bundle]
         ["." analysis #_
          ["#" common_lisp]]
         ["." generation #_
          ["#" common_lisp]]]
        [generation
         ["." reference]
         ["." common_lisp
          ["." runtime]]]]]]
     [default
      ["." platform (#+ Platform)]]
     [meta
      ["." packager #_
       ["#" script]]]]]]
  [program
   ["/" compositor
    ["#." cli]
    ["#." static]]])

(ffi.import: java/lang/String)

(ffi.import: (java/lang/Class a)
  ["#::."
   (#static forName [java/lang/String] #try (java/lang/Class java/lang/Object))])

(ffi.import: java/lang/Object
  ["#::."
   (toString [] java/lang/String)
   (getClass [] (java/lang/Class java/lang/Object))])

(ffi.import: java/lang/Long
  ["#::."
   (intValue [] java/lang/Integer)])

(ffi.import: java/lang/Integer
  ["#::."
   (longValue [] long)])

(ffi.import: java/lang/Number
  ["#::."
   (intValue [] java/lang/Integer)
   (longValue [] long)
   (doubleValue [] double)])

(ffi.import: org/armedbear/lisp/LispObject
  ["#::."
   (length [] int)
   (NTH [int] org/armedbear/lisp/LispObject)
   (SVREF [int] org/armedbear/lisp/LispObject)
   (elt [int] org/armedbear/lisp/LispObject)
   (execute [org/armedbear/lisp/LispObject org/armedbear/lisp/LispObject] #try org/armedbear/lisp/LispObject)])

## The org/armedbear/lisp/Interpreter must be imported before the
## other ones, because there is an order dependency in their static initialization.
(ffi.import: org/armedbear/lisp/Interpreter
  ["#::."
   (#static getInstance [] org/armedbear/lisp/Interpreter)
   (#static createInstance [] #? org/armedbear/lisp/Interpreter)
   (eval [java/lang/String] #try org/armedbear/lisp/LispObject)])

(ffi.import: org/armedbear/lisp/Symbol
  ["#::."
   (#static T org/armedbear/lisp/Symbol)])

(ffi.import: org/armedbear/lisp/DoubleFloat
  ["#::."
   (new [double])
   (doubleValue [] double)])

(ffi.import: org/armedbear/lisp/SimpleString
  ["#::."
   (new [java/lang/String])
   (getStringValue [] java/lang/String)])

(ffi.import: org/armedbear/lisp/LispInteger)

(ffi.import: org/armedbear/lisp/Bignum
  ["#::."
   (longValue [] long)
   (#static getInstance [long] org/armedbear/lisp/LispInteger)])

(ffi.import: org/armedbear/lisp/Fixnum
  ["#::."
   (longValue [] long)
   (#static getInstance [int] org/armedbear/lisp/Fixnum)])

(ffi.import: org/armedbear/lisp/Nil
  ["#::."
   (#static NIL org/armedbear/lisp/Symbol)])

(ffi.import: org/armedbear/lisp/SimpleVector)

(ffi.import: org/armedbear/lisp/Cons)

(ffi.import: org/armedbear/lisp/Closure)

(ffi.interface: LuxADT
  (getValue [] java/lang/Object))

(ffi.import: program/LuxADT
  ["#::."
   (getValue [] java/lang/Object)])

(template [<name>]
  [(exception: (<name> {object java/lang/Object})
     (exception.report
      ["Class" (java/lang/Object::toString (java/lang/Object::getClass object))]
      ["Object" (java/lang/Object::toString object)]))]

  [unknown_kind_of_object]
  [cannot_apply_a_non_function]
  )

(def: host_bit
  (-> Bit org/armedbear/lisp/LispObject)
  (|>> (case> #0 (org/armedbear/lisp/Nil::NIL)
              #1 (org/armedbear/lisp/Symbol::T))))

(def: (host_value value)
  (-> Any org/armedbear/lisp/LispObject)
  (let [to_sub (: (-> Any org/armedbear/lisp/LispObject)
                  (function (_ sub_value)
                    (let [sub_value (:coerce java/lang/Object sub_value)]
                      (`` (<| (~~ (template [<type> <then>]
                                    [(case (ffi.check <type> sub_value)
                                       (#.Some sub_value)
                                       (`` (|> sub_value (~~ (template.splice <then>))))
                                       #.None)]

                                    [[java/lang/Object] [host_value]]
                                    [java/lang/Boolean [..host_bit]]
                                    [java/lang/Integer [java/lang/Integer::longValue org/armedbear/lisp/Fixnum::getInstance]]
                                    [java/lang/Long [org/armedbear/lisp/Bignum::getInstance]]
                                    [java/lang/Double [org/armedbear/lisp/DoubleFloat::new]]
                                    [java/lang/String [org/armedbear/lisp/SimpleString::new]]
                                    ))
                              ## else
                              (:coerce org/armedbear/lisp/LispObject sub_value))))))]
    (`` (ffi.object [] org/armedbear/lisp/LispObject [program/LuxADT]
          []
          ## Methods
          (program/LuxADT
           [] (getValue self) java/lang/Object
           (:coerce java/lang/Object value))

          (org/armedbear/lisp/LispObject
           [] (length self)
           int
           (|> value
               (:coerce (Array java/lang/Object))
               array.size
               (:coerce java/lang/Long)
               java/lang/Number::intValue))

          (~~ (template [<name>]
                [(org/armedbear/lisp/LispObject
                  [] (<name> self {idx int})
                  org/armedbear/lisp/LispObject
                  (case (array.read (|> idx java/lang/Integer::longValue (:coerce Nat))
                                    (:coerce (Array java/lang/Object) value))
                    (#.Some sub)
                    (to_sub sub)

                    #.None
                    (org/armedbear/lisp/Nil::NIL)))]

                [NTH] [SVREF] [elt]
                ))
          ))))

(type: (Reader a)
  (-> a (Try Any)))

(def: (read_variant read host_object)
  (-> (Reader org/armedbear/lisp/LispObject) (Reader org/armedbear/lisp/Cons))
  (do try.monad
    [tag (read (org/armedbear/lisp/LispObject::NTH +0 host_object))
     value (read (org/armedbear/lisp/LispObject::NTH +2 host_object))]
    (wrap [(java/lang/Long::intValue (:coerce java/lang/Long tag))
           (case (ffi.check org/armedbear/lisp/Nil (org/armedbear/lisp/LispObject::NTH +1 host_object))
             (#.Some _)
             (: Any (ffi.null))

             _
             (: Any synthesis.unit))
           value])))

(def: (read_tuple read host_object)
  (-> (Reader org/armedbear/lisp/LispObject) (Reader org/armedbear/lisp/SimpleVector))
  (let [size (.nat (org/armedbear/lisp/LispObject::length host_object))]
    (loop [idx 0
           output (:coerce (Array Any) (array.new size))]
      (if (n.< size idx)
        ## TODO: Start using "SVREF" instead of "elt" ASAP
        (case (read (org/armedbear/lisp/LispObject::elt (.int idx) host_object))
          (#try.Failure error)
          (#try.Failure error)

          (#try.Success member)
          (recur (inc idx) (array.write! idx (:coerce Any member) output)))
        (#try.Success output)))))

(def: (read host_object)
  (Reader org/armedbear/lisp/LispObject)
  (`` (<| (~~ (template [<class> <post_processing>]
                [(case (ffi.check <class> host_object)
                   (#.Some host_object)
                   (`` (|> host_object (~~ (template.splice <post_processing>))))

                   #.None)]

                [org/armedbear/lisp/Bignum [org/armedbear/lisp/Bignum::longValue #try.Success]]
                [org/armedbear/lisp/Fixnum [org/armedbear/lisp/Fixnum::longValue #try.Success]]
                [org/armedbear/lisp/DoubleFloat [org/armedbear/lisp/DoubleFloat::doubleValue #try.Success]]
                [org/armedbear/lisp/SimpleString [org/armedbear/lisp/SimpleString::getStringValue #try.Success]]
                [org/armedbear/lisp/Cons [(read_variant read)]]
                [org/armedbear/lisp/SimpleVector [(read_tuple read)]]
                [org/armedbear/lisp/Nil [(new> (#try.Success false) [])]]
                [org/armedbear/lisp/Closure [#try.Success]]
                [program/LuxADT [program/LuxADT::getValue #try.Success]]))
          (case (ffi.check org/armedbear/lisp/Symbol host_object)
            (#.Some host_object)
            (if (is? (org/armedbear/lisp/Symbol::T) host_object)
              (#try.Success true)
              (exception.throw ..unknown_kind_of_object (:coerce java/lang/Object host_object)))

            #.None)
          ## else
          (exception.throw ..unknown_kind_of_object (:coerce java/lang/Object host_object))
          )))

(def: ensure_macro
  (-> Macro (Maybe org/armedbear/lisp/Closure))
  (|>> (:coerce java/lang/Object) (ffi.check org/armedbear/lisp/Closure)))

(def: (call_macro inputs lux macro)
  (-> (List Code) Lux org/armedbear/lisp/Closure (Try (Try [Lux (List Code)])))
  (do try.monad
    [raw_output (org/armedbear/lisp/LispObject::execute (..host_value inputs) (..host_value lux) macro)]
    (:coerce (Try (Try [Lux (List Code)]))
             (..read raw_output))))

(def: (expander macro inputs lux)
  Expander
  (case (ensure_macro macro)
    (#.Some macro)
    (call_macro inputs lux macro)

    #.None
    (exception.throw ..cannot_apply_a_non_function (:coerce java/lang/Object macro))))

(def: host
  (IO (Host (_.Expression Any) (_.Expression Any)))
  (io (let [_ (org/armedbear/lisp/Interpreter::createInstance)
            interpreter (org/armedbear/lisp/Interpreter::getInstance)
            run! (: (-> (_.Code Any) (Try Any))
                    (function (_ code)
                      (do try.monad
                        [host_value (org/armedbear/lisp/Interpreter::eval (_.code code) interpreter)]
                        (read host_value))))]
        (: (Host (_.Expression Any) (_.Expression Any))
           (structure
            (def: (evaluate! context code)
              (run! code))
            
            (def: (execute! input)
              (org/armedbear/lisp/Interpreter::eval (_.code input) interpreter))
            
            (def: (define! context input)
              (let [global (reference.artifact context)
                    @global (_.var global)]
                (do try.monad
                  [#let [definition (_.defparameter @global input)]
                   _ (org/armedbear/lisp/Interpreter::eval (_.code definition) interpreter)
                   value (run! @global)]
                  (wrap [global value definition]))))

            (def: (ingest context content)
              (|> content (\ encoding.utf8 decode) try.assume (:coerce (_.Expression Any))))

            (def: (re_learn context content)
              (run! content))
            
            (def: (re_load context content)
              (do try.monad
                [_ (run! content)]
                (run! (_.var (reference.artifact context)))))
            )))))

(def: platform
  (IO (Platform [_.Tag Register] (_.Expression Any) (_.Expression Any)))
  (do io.monad
    [host ..host]
    (wrap {#platform.&file_system (file.async file.default)
           #platform.host host
           #platform.phase common_lisp.generate
           #platform.runtime runtime.generate
           #platform.write (|>> _.code (\ encoding.utf8 encode))})))

(def: get_ecl_cli_inputs
  (let [@idx (_.var "i")]
    (_.call/* (_.var "loop")
              (list (_.var "for") @idx
                    (_.var "from") (_.int +0)
                    (_.var "below") (_.call/* (_.var "si:argc") (list))
                    (_.var "collect") (_.call/* (_.var "si:argv") (list @idx))))))

(def: (program context program)
  (Program (_.Expression Any) (_.Expression Any))
  (let [raw_inputs (_.progn (list (_.conditional+ (list "clisp") (_.var "ext:*args*"))
                                  (_.conditional+ (list "sbcl") (_.var "sb-ext:*posix-argv*"))
                                  (_.conditional+ (list "clozure") (_.call/* (_.var "ccl::command-line-arguments") (list)))
                                  (_.conditional+ (list "gcl") (_.var "si:*command-args*"))
                                  (_.conditional+ (list "ecl") ..get_ecl_cli_inputs)
                                  (_.conditional+ (list "cmu") (_.var "extensions:*command-line-strings*"))
                                  (_.conditional+ (list "allegro") (_.call/* (_.var "sys:command-line-arguments") (list)))
                                  (_.conditional+ (list "lispworks") (_.var "sys:*line-arguments-list*"))
                                  (_.list/* (list))))]
    (_.call/2 [(runtime.lux//program_args raw_inputs) _.nil] program)))

(for {@.old
      (def: extender
        Extender
        ## TODO: Stop relying on coercions ASAP.
        (<| (:coerce Extender)
            (function (@self handler))
            (:coerce Handler)
            (function (@self name phase))
            (:coerce Phase)
            (function (@self archive parameters))
            (:coerce Operation)
            (function (@self state))
            (:coerce Try)
            try.assume
            (:coerce Try)
            (exec
              ("lux io log" "TODO: Extender")
              (#try.Failure "TODO: Extender"))))

      @.common_lisp
      (def: (extender handler)
        Extender
        (:assume handler))})

(def: (declare_success! _)
  (-> Any (Promise Any))
  (promise.future (\ world/program.default exit +0)))

(def: (then pre post)
  (-> (_.Expression Any) (_.Expression Any) (_.Expression Any))
  (_.manual (format (_.code pre)
                    text.new_line
                    (_.code post))))

(def: (scope body)
  (-> (_.Expression Any) (_.Expression Any))
  (let [@program (_.var "lux_program")]
    ($_ ..then
        (_.defun @program (_.args (list)) body)
        (_.call/* @program (list))
        )))

(`` (program: [{service /cli.service}]
      (let [extension ".cl"]
        (do io.monad
          [platform ..platform]
          (exec (do promise.monad
                  [_ (/.compiler {#/static.host @.common_lisp
                                  #/static.host_module_extension extension
                                  #/static.target (/cli.target service)
                                  #/static.artifact_extension extension}
                                 ..expander
                                 analysis.bundle
                                 (io.io platform)
                                 generation.bundle
                                 extension/bundle.empty
                                 ..program
                                 [(& _.Tag Register) (type (_.Expression Any)) (type (_.Expression Any))]
                                 ..extender
                                 service
                                 [(packager.package (_.manual "")
                                                    _.code
                                                    ..then
                                                    ..scope)
                                  (format (/cli.target service)
                                          (\ file.default separator)
                                          "program"
                                          extension)])]
                  (..declare_success! []))
            (io.io []))))))