aboutsummaryrefslogtreecommitdiff
path: root/lux-scheme/source/program.lux
blob: b6d06f34198c147f39c6b6a532d808ab481330c9 (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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
(.require
 [lux (.except)
  [program (.only program)]
  ["[0]" ffi]
  ["[0]" debug]
  [abstract
   ["[0]" monad (.only do)]]
  [control
   ["[0]" pipe]
   ["[0]" maybe]
   ["[0]" try (.only Try)]
   ["[0]" exception (.only exception)]
   ["[0]" io (.only IO io)]
   [concurrency
    ["[0]" promise (.only Promise)]]]
  [data
   ["[0]" text (.use "[1]#[0]" hash)
    ["%" \\format (.only format)]
    ["[0]" encoding]]
   [collection
    ["[0]" array (.only Array)]]]
  [macro
   ["[0]" template]]
  [math
   [number (.only hex)
    ["n" nat]
    ["[0]" i64]]]
  ["[0]" world
   ["[0]" file]
   ["[1]/[0]" program]
   [time
    ["[0]" instant]]]
  ["@" target
   ["_" scheme]]
  [meta
   [compiler
    [phase (.only Operation Phase)]
    [reference
     [variable (.only Register)]]
    [language
     [lux
      [program (.only Program)]
      [generation (.only Context Host)]
      ["[0]" synthesis]
      [analysis
       [macro (.only Expander)]]
      [phase
       ["[0]" extension (.only Extender Handler)
        ["[1]/[0]" bundle]
        ["[0]" analysis
         ["[1]" scheme]]
        ["[0]" generation
         ["[1]" scheme]]]
       [generation
        ["[0]" reference]
        ["[0]" scheme
         ["[0]" runtime]]]]]]
    [default
     ["[0]" platform (.only Platform)]]
    [meta
     ["[0]" packager
      ["[1]" scheme]]]]]]
 [program
  ["/" compositor
   ["[1][0]" cli]
   ["[1][0]" static]]])

(ffi.import java/lang/Boolean
  "[1]::[0]")

(ffi.import java/lang/String
  "[1]::[0]")

(ffi.import (java/lang/Class a)
  "[1]::[0]")

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

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

(ffi.import java/lang/Integer
  "[1]::[0]"
  (longValue [] java/lang/Long))

(ffi.import gnu/math/IntNum
  "[1]::[0]"
  (new "manual" [int])
  (longValue [] long))

(ffi.import gnu/math/DFloNum
  "[1]::[0]"
  (doubleValue [] double))

(ffi.import gnu/lists/FString
  "[1]::[0]"
  (toString [] String))

(ffi.import gnu/lists/IString
  "[1]::[0]"
  (toString [] String))

(ffi.import gnu/lists/Pair
  "[1]::[0]"
  (getCar [] java/lang/Object)
  (getCdr [] java/lang/Object))

(ffi.import gnu/lists/EmptyList
  "[1]::[0]"
  ("static" emptyList gnu/lists/EmptyList))

(ffi.import (gnu/lists/FVector E)
  "[1]::[0]"
  (getBufferLength [] int)
  (getRaw [int] E))

(ffi.import gnu/lists/U8Vector
  "[1]::[0]")

(ffi.import gnu/mapping/Procedure
  "[1]::[0]"
  (apply2 [java/lang/Object java/lang/Object] "try" java/lang/Object)
  (applyN [[java/lang/Object]] "try" java/lang/Object))

(ffi.import gnu/mapping/Environment
  "[1]::[0]")

(ffi.import gnu/expr/Language
  "[1]::[0]"
  (eval [java/lang/String] "try" java/lang/Object))

(ffi.import kawa/standard/Scheme
  "[1]::[0]"
  ("static" getR7rsInstance [] kawa/standard/Scheme))

(def (variant? value)
  (-> Any Bit)
  (case (ffi.as [java/lang/Object] (as java/lang/Object value))
    {.#Some array}
    ... TODO: Get rid of this coercion ASAP.
    (let [array (as (Array java/lang/Object) array)]
      (and (n.= 3 (array.size array))
           (case (array.read! 0 array)
             {.#Some tag}
             (case (ffi.as java/lang/Integer tag)
               {.#Some _}
               true

               {.#None}
               false)
             
             {.#None}
             false)))
    
    {.#None}
    false))

(with_template [<name>]
  [(ffi.interface <name>
     (getValue [] java/lang/Object))

   (`` (ffi.import (,, (template.symbol ["program/" <name>]))
         "[1]::[0]"
         (getValue [] java/lang/Object)))]

  [VariantValue]
  [TupleValue]
  )

(def (variant_value lux_value cdr? value)
  (-> (-> java/lang/Object java/lang/Object) Bit (Array java/lang/Object) gnu/lists/Pair)
  (ffi.object [] gnu/lists/Pair [program/VariantValue]
    []
    ... Methods
    (program/VariantValue
     [] (getValue self []) java/lang/Object
     (as java/lang/Object value))
    (gnu/lists/Pair
     [] (getCar self []) java/lang/Object
     (if cdr?
       (case (array.read! 1 value)
         {.#Some flag_is_set}
         true
         
         {.#None}
         false)
       (|> value
           (array.read! 0)
           maybe.trusted
           (as java/lang/Integer)
           gnu/math/IntNum::new)))
    (gnu/lists/Pair
     [] (getCdr self []) java/lang/Object
     (if cdr?
       (|> value
           (array.read! 2)
           maybe.trusted
           lux_value)
       (variant_value lux_value true value)))))

(def (tuple_value lux_value value)
  (-> (-> java/lang/Object java/lang/Object) (Array java/lang/Object) gnu/lists/FVector)
  (ffi.object [] gnu/lists/SimpleVector [program/TupleValue gnu/lists/GVector]
    []
    ... Methods
    (program/TupleValue
     [] (getValue self []) java/lang/Object
     (as java/lang/Object value))
    (gnu/lists/SimpleVector
     [] (getBufferLength self []) int
     (ffi.long_to_int (array.size value)))
    (gnu/lists/SimpleVector
     [] (getRaw self [idx int]) java/lang/Object
     (|> value
         (array.read! (|> idx java/lang/Integer::longValue (as Nat)))
         maybe.trusted
         lux_value))
    (gnu/lists/SimpleVector
     [] (getBuffer self []) java/lang/Object
     (undefined))
    (gnu/lists/SimpleVector
     [] (setBuffer self [_ java/lang/Object]) void
     (undefined))
    (gnu/lists/SimpleVector
     [] (clearBuffer self [_ int _ int]) void
     (undefined))
    (gnu/lists/SimpleVector
     [] (copyBuffer self [_ int]) void
     (undefined))
    (gnu/lists/SimpleVector
     [] (newInstance self [_ int]) gnu/lists/SimpleVector
     (undefined))
    ))

(exception (unknown_kind_of_host_object [object java/lang/Object])
  (exception.report
   (list ["Class" (java/lang/Object::toString (java/lang/Object::getClass object))]
         ["Object" (java/lang/Object::toString object)])))

(exception (cannot_apply_a_non_function [object java/lang/Object])
  (exception.report
   (list ["Class" (java/lang/Object::toString (java/lang/Object::getClass object))]
         ["Object" (java/lang/Object::toString object)])))

(def (lux_value value)
  (-> java/lang/Object java/lang/Object)
  (<| (case (ffi.as [java/lang/Object] value)
        {.#Some value}
        ... TODO: Get rid of the coercions below.
        (if (variant? value)
          (variant_value lux_value false (as (Array java/lang/Object) value))
          (tuple_value lux_value (as (Array java/lang/Object) value)))

        {.#None})
      value))

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

(def (variant tag flag value)
  (-> Nat Bit Any Any)
  [(java/lang/Long::intValue (as java/lang/Long tag))
   (is Any
       (if flag
         synthesis.unit
         (ffi.null)))
   value])

(def (read_variant read host_object)
  (-> (Reader java/lang/Object) (Reader gnu/lists/Pair))
  (do try.monad
    [tag (read (gnu/lists/Pair::getCar host_object))
     #let [host_object (as gnu/lists/Pair (gnu/lists/Pair::getCdr host_object))
           flag (case (ffi.as java/lang/Boolean (gnu/lists/Pair::getCar host_object))
                  {.#Some flag}
                  (as Bit flag)
                  
                  {.#None}
                  (undefined))]
     value (read (gnu/lists/Pair::getCdr host_object))]
    (wrap (..variant (as Nat tag) flag value))))

(def (read_tuple read host_object)
  (-> (Reader java/lang/Object) (Reader (gnu/lists/FVector java/lang/Object)))
  (let [size (.nat (gnu/lists/FVector::getBufferLength host_object))]
    (loop (again [idx 0
                  output (is (Array Any)
                             (array.new size))])
      (if (n.< size idx)
        (case (read (gnu/lists/FVector::getRaw (.int idx) host_object))
          {try.#Failure error}
          {try.#Failure error}

          {try.#Success lux_value}
          (again (++ idx) (array.write! idx (is Any lux_value) output)))
        {try.#Success output}))))

(def (read host_object)
  (Reader java/lang/Object)
  (`` (<| (,, (with_template [<class>]
                [(case (ffi.as <class> host_object)
                   {.#Some host_object}
                   {try.#Success host_object}

                   {.#None})]

                [java/lang/Boolean] [java/lang/Long] [java/lang/Double] [java/lang/String]
                [gnu/mapping/Procedure] [gnu/lists/U8Vector]
                ))
          (,, (with_template [<class> <processing>]
                [(case (ffi.as <class> host_object)
                   {.#Some host_object}
                   {try.#Success (<| <processing> host_object)}

                   {.#None})]

                [java/lang/Integer java/lang/Integer::longValue]

                [gnu/lists/EmptyList (pipe.new [] [])]
                [gnu/math/IntNum gnu/math/IntNum::longValue]
                [gnu/math/DFloNum gnu/math/DFloNum::doubleValue]
                [gnu/lists/FString gnu/lists/FString::toString]
                [gnu/lists/IString gnu/lists/IString::toString]
                
                [program/VariantValue program/VariantValue::getValue]
                [program/TupleValue program/TupleValue::getValue]
                ))
          (case (ffi.as gnu/lists/Pair host_object)
            {.#Some host_object}
            (read_variant read host_object)

            {.#None})
          (case (ffi.as gnu/lists/FVector host_object)
            {.#Some host_object}
            (read_tuple read (as (gnu/lists/FVector java/lang/Object) host_object))

            {.#None})
          ... else
          (exception.throw ..unknown_kind_of_host_object host_object))))

(def ensure_macro
  (-> Macro (Maybe gnu/mapping/Procedure))
  (|>> (as java/lang/Object) (ffi.as gnu/mapping/Procedure)))

(def (expander macro inputs lux)
  Expander
  (case (..ensure_macro macro)
    {.#Some macro}
    (case (gnu/mapping/Procedure::apply2 (lux_value (as java/lang/Object inputs))
                                         (lux_value (as java/lang/Object lux))
                                         macro)
      {try.#Success output}
      (|> output
          ..read
          (as (Try (Try [Lux (List Code)]))))

      {try.#Failure error}
      {try.#Failure error})

    {.#None}
    (exception.throw ..cannot_apply_a_non_function (as java/lang/Object macro))))

(def host
  (IO (Host _.Expression _.Expression))
  (io (let [interpreter (kawa/standard/Scheme::getR7rsInstance)
            run! (is (-> (_.Code Any) (Try Any))
                     (function (_ input)
                       (do try.monad
                         [output (gnu/expr/Language::eval (_.code input) interpreter)]
                         (read output))))]
        (is (Host _.Expression _.Expression)
            (structure
             (def (evaluate! context code)
               (run! code))
             
             (def (execute! input)
               (gnu/expr/Language::eval (_.code input) interpreter))
             
             (def (define! context input)
               (let [global (reference.artifact context)
                     @global (_.var global)]
                 (do try.monad
                   [#let [definition (_.define_constant @global input)]
                    _ (gnu/expr/Language::eval (_.code definition) interpreter)
                    value (run! @global)]
                   (wrap [global value definition]))))

             (def (ingest context content)
               (|> content (at encoding.utf8 decoded) try.trusted (as _.Expression)))

             (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 _.Var _.Expression _.Expression))
  (do io.monad
    [host ..host]
    (wrap [platform.#file_system (file.async file.default)
           platform.#host host
           platform.#phase scheme.generate
           platform.#runtime runtime.generate
           platform.#write (|>> _.code (at encoding.utf8 encoded))])))

(def (program context program)
  (Program _.Expression _.Expression)
  (_.apply/2 program
             ... TODO: Figure out how to always get the command-line
             ... arguments.
             ... It appears that it differs between Scheme implementations.
             (runtime.lux//program_args _.nil)
             _.nil))

(for @.old
     (def extender
       Extender
       ... TODO: Stop relying on coercions ASAP.
       (<| (as Extender)
           (function (@self handler))
           (as Handler)
           (function (@self name phase))
           (as Phase)
           (function (@self archive parameters))
           (as Operation)
           (function (@self state))
           (as Try)
           try.trusted
           (as Try)
           (do try.monad
             [handler (try.from_maybe (..ensure_macro (as Macro handler)))
              output (gnu/mapping/Procedure::applyN (array.from_list (list (lux_value (as java/lang/Object name))
                                                                           (lux_value (as java/lang/Object phase))
                                                                           (lux_value (as java/lang/Object archive))
                                                                           (lux_value (as java/lang/Object parameters))
                                                                           (lux_value (as java/lang/Object state))))
                                                    handler)]
             (..read output))))

     @.scheme
     (def (extender handler)
       Extender
       (as_expected handler)))

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

(`` (def _
      (program [service /cli.service]
        (let [extension ".scm"]
          (do io.monad
            [platform ..platform
             now instant.now]
            (exec (do promise.monad
                    [_ (/.compiler [/static.#host @.scheme
                                    /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
                                   [_.Var _.Expression _.Expression]
                                   ..extender
                                   service
                                   [(packager.package now)
                                    (format (/cli.target service)
                                            (at file.default separator)
                                            "program.tar")])]
                    (..declare_success! []))
              (io.io [])))))))