aboutsummaryrefslogtreecommitdiff
path: root/lux-php/source/program.lux
blob: 716405587a62f58b153bc9b3af6fa1e2e3d9d319 (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
(.module:
  [lux #*
   [program (#+ program:)]
   ["." host]
   ["." debug]
   [abstract
    ["." monad (#+ do)]]
   [control
    [pipe (#+ exec> case>)]
    ["." 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
    ["_" php]]
   [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 #_
          ["#" php]]
         ["." generation #_
          ["#" php]]]
        [generation
         ["." reference]
         ["." php
          ["." runtime]]]]]]
     [default
      ["." platform (#+ Platform)]]
     [meta
      ["." packager #_
       ["#" script]]]]]]
  [program
   ["/" compositor
    ["#." cli]
    ["#." static]]])

(host.import: java/lang/String)

(host.import: java/lang/reflect/Field
  ["#::."
   (get [java/lang/Object] java/lang/Object)])

(host.import: java/lang/reflect/AccessibleObject
  ["#::."
   (setAccessible [boolean] void)])

(host.import: (java/lang/Class a)
  ["#::."
   (getDeclaredField [java/lang/String] java/lang/reflect/Field)])

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

(host.import: java/lang/Integer)

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

(host.import: php/runtime/Memory
  ["#::."
   (valueOfIndex #as generic_valueOfIndex [php/runtime/env/TraceInfo php/runtime/Memory] php/runtime/Memory)
   (valueOfIndex #as long_valueOfIndex [php/runtime/env/TraceInfo long] php/runtime/Memory)
   (valueOfIndex #as string_valueOfIndex [php/runtime/env/TraceInfo java/lang/String] php/runtime/Memory)])

(host.import: php/runtime/Memory$Type
  ["#::."
   (#enum ARRAY)])

(host.import: php/runtime/memory/NullMemory
  ["#::."
   (#static INSTANCE php/runtime/memory/NullMemory)])

(host.import: php/runtime/memory/FalseMemory
  ["#::."
   (#static INSTANCE php/runtime/memory/FalseMemory)])

(host.import: php/runtime/memory/TrueMemory
  ["#::."
   (#static INSTANCE php/runtime/memory/TrueMemory)])

(host.import: php/runtime/memory/LongMemory
  ["#::."
   (new [long])
   (toLong [] long)
   (#static valueOf #manual [int] php/runtime/Memory)])

(host.import: php/runtime/memory/DoubleMemory
  ["#::."
   (new [double])
   (toDouble [] double)])

(host.import: php/runtime/memory/StringMemory
  ["#::."
   (new [java/lang/String])
   (toString [] java/lang/String)])

(host.import: php/runtime/memory/ReferenceMemory
  ["#::."
   (getValue [] php/runtime/Memory)])

(host.import: php/runtime/memory/ArrayMemory
  ["#::."
   (new [[java/lang/Object]])
   (size [] int)
   (isMap [] boolean)
   (get [php/runtime/Memory] #? php/runtime/Memory)])

(host.import: php/runtime/lang/IObject)

(host.import: php/runtime/memory/ObjectMemory
  ["#::."
   (value php/runtime/lang/IObject)])

(host.import: php/runtime/env/Environment)

(host.import: php/runtime/env/TraceInfo
  ["#::."
   (new [java/lang/String int int])])

(host.import: php/runtime/reflection/FunctionEntity)

(host.import: php/runtime/invoke/InvokeHelper
  ["#::."
   (#static callAny [php/runtime/Memory [php/runtime/Memory] php/runtime/env/Environment php/runtime/env/TraceInfo]
            #try php/runtime/Memory)])

(host.import: php/runtime/lang/Closure
  ["#::."
   (call [php/runtime/env/Environment [php/runtime/Memory]] #try php/runtime/Memory)])

(template [<name>]
  [(host.interface: <name>
     (getValue [] java/lang/Object))

   (`` (host.import: (~~ (template.identifier ["program/" <name>]))
         ["#::."
          (getValue [] java/lang/Object)]))]

  [StructureValue]
  )

(type: Reader
  (-> java/lang/Object (Try Any)))

(exception: (unknown_kind_of_object {object java/lang/Object})
  (exception.report
   ["Class" (java/lang/Object::toString (java/lang/Object::getClass object))]
   ["Object" (java/lang/Object::toString object)]))

(def: (value_wrapper lux_structure value)
  (-> (-> (Array java/lang/Object) php/runtime/Memory) java/lang/Object php/runtime/Memory)
  (<| (if (host.null? value)
        (php/runtime/memory/NullMemory::INSTANCE))
      (case (host.check java/lang/Boolean value)
        (#.Some value)
        (if (:coerce Bit value)
          (php/runtime/memory/TrueMemory::INSTANCE)
          (php/runtime/memory/FalseMemory::INSTANCE))

        #.None)
      (case (host.check java/lang/Long value)
        (#.Some value)
        (php/runtime/memory/LongMemory::new value)

        #.None)
      (case (host.check java/lang/Double value)
        (#.Some value)
        (php/runtime/memory/DoubleMemory::new value)

        #.None)
      (case (host.check java/lang/String value)
        (#.Some value)
        (php/runtime/memory/StringMemory::new value)

        #.None)
      (case (host.check [java/lang/Object] value)
        (#.Some value)
        (lux_structure (:coerce (Array java/lang/Object) value))

        #.None)
      (case (host.check php/runtime/memory/ObjectMemory value)
        (#.Some value)
        value

        #.None)
      (undefined)
      ))

(def: unit
  (php/runtime/memory/StringMemory::new ""))

(def: (lux_structure value)
  (-> (Array java/lang/Object) php/runtime/Memory)
  (`` (host.object [] php/runtime/Memory
        [program/StructureValue]
        [{php/runtime/Memory$Type php/runtime/Memory$Type::ARRAY}]
        ## Methods
        (program/StructureValue
         [] (getValue self) java/lang/Object
         (:assume value))

        (php/runtime/Memory
         [] (toString self)
         java/lang/String
         (debug.inspect value))

        (php/runtime/Memory
         [] (valueOfIndex self
                          {trace php/runtime/env/TraceInfo}
                          {index php/runtime/Memory})
         php/runtime/Memory
         (`` (<| (~~ (template [<class> <method> <extractor>]
                       [(case (host.check <class> index)
                          (#.Some index)
                          (<method> trace (<extractor> index) self)
                          
                          #.None)]

                       [php/runtime/memory/ReferenceMemory
                        php/runtime/Memory::generic_valueOfIndex
                        php/runtime/memory/ReferenceMemory::getValue]
                       [php/runtime/memory/LongMemory
                        php/runtime/Memory::long_valueOfIndex
                        php/runtime/memory/LongMemory::toLong]
                       [php/runtime/memory/StringMemory
                        php/runtime/Memory::string_valueOfIndex
                        php/runtime/memory/StringMemory::toString]
                       ))
                 (undefined))))

        (php/runtime/Memory
         [] (valueOfIndex self
                          {trace php/runtime/env/TraceInfo}
                          {index long})
         php/runtime/Memory
         (|> value
             (array.read index)
             maybe.assume
             (..value_wrapper lux_structure)))

        (php/runtime/Memory
         [] (valueOfIndex self
                          {trace php/runtime/env/TraceInfo}
                          {index java/lang/String})
         php/runtime/Memory
         (case (:coerce Text index)
           (^ (static runtime.variant_tag_field))
           (|> value
               (array.read 0)
               maybe.assume
               (:coerce java/lang/Integer)
               php/runtime/memory/LongMemory::valueOf)

           (^ (static runtime.variant_flag_field))
           (case (array.read 1 value)
             #.None
             (php/runtime/memory/NullMemory::INSTANCE)
             
             (#.Some value)
             ..unit)

           (^ (static runtime.variant_value_field))
           (|> value
               (array.read 2)
               maybe.assume
               (..value_wrapper lux_structure))

           (^ (static runtime.tuple_size_field))
           (php/runtime/memory/LongMemory::new (array.size value))

           _
           (undefined)))
        )))

(def: (read_tuple read host_object)
  (-> Reader php/runtime/memory/ArrayMemory (Try Any))
  (let [size (|> host_object
                 php/runtime/memory/ArrayMemory::size
                 (:coerce Nat)
                 dec)]
    (loop [idx 0
           output (:coerce (Array Any) (array.new size))]
      (if (n.< size idx)
        (let [value (|> host_object
                        (php/runtime/memory/ArrayMemory::get (php/runtime/memory/LongMemory::new (.int idx)))
                        maybe.assume
                        (:coerce php/runtime/memory/ReferenceMemory)
                        php/runtime/memory/ReferenceMemory::getValue)]
          (case (host.check php/runtime/memory/NullMemory value)
            (#.Some _)
            (recur (inc idx) output)

            #.None
            (case (read value)
              (#try.Success lux_value)
              (recur (inc idx) (array.write! idx lux_value output))

              error
              error)))
        (#try.Success output)))))

(def: variant_tag_field
  (php/runtime/memory/StringMemory::new runtime.variant_tag_field))

(def: variant_value_field
  (php/runtime/memory/StringMemory::new runtime.variant_value_field))

(def: variant_flag_field
  (php/runtime/memory/StringMemory::new runtime.variant_flag_field))

(def: (read_variant read host_object)
  (-> Reader php/runtime/memory/ArrayMemory (Try Any))
  (case [(|> host_object
             (php/runtime/memory/ArrayMemory::get ..variant_tag_field)
             maybe.assume
             read)
         (|> host_object
             (php/runtime/memory/ArrayMemory::get ..variant_value_field)
             maybe.assume
             read)]
    [(#try.Success tag) (#try.Success value)]
    (#try.Success [(java/lang/Long::intValue (:coerce java/lang/Long tag))
                   (: Any
                      (case (|> host_object
                                (php/runtime/memory/ArrayMemory::get ..variant_flag_field)
                                maybe.assume
                                (:coerce php/runtime/memory/ReferenceMemory)
                                php/runtime/memory/ReferenceMemory::getValue
                                (host.check php/runtime/memory/NullMemory))
                        (#.Some _)
                        (host.null)

                        #.None
                        synthesis.unit))
                   value])

    _
    (exception.throw ..unknown_kind_of_object host_object)))

(exception: #export nulll_has_no_lux_representation)

(def: tuple_size_field
  (php/runtime/memory/StringMemory::new runtime.tuple_size_field))

(def: (read host_object)
  Reader
  (`` (<| (~~ (template [<class> <constant>]
                [(case (host.check <class> host_object)
                   (#.Some _)
                   (#try.Success <constant>)

                   #.None)]

                [php/runtime/memory/FalseMemory false]
                [php/runtime/memory/TrueMemory true]
                ))
          (~~ (template [<class> <post>]
                [(case (host.check <class> host_object)
                   (#.Some value)
                   (`` (|> value (~~ (template.splice <post>))))

                   #.None)]

                [program/StructureValue [program/StructureValue::getValue #try.Success]]
                [php/runtime/memory/LongMemory [php/runtime/memory/LongMemory::toLong #try.Success]]
                [php/runtime/memory/DoubleMemory [php/runtime/memory/DoubleMemory::toDouble #try.Success]]
                [php/runtime/memory/StringMemory [php/runtime/memory/StringMemory::toString #try.Success]]
                [php/runtime/memory/ReferenceMemory [php/runtime/memory/ReferenceMemory::getValue read]]
                [php/runtime/memory/ObjectMemory [#try.Success]]
                ))
          (case (host.check php/runtime/memory/ArrayMemory host_object)
            (#.Some value)
            (if (|> value
                    (php/runtime/memory/ArrayMemory::get ..tuple_size_field)
                    (case> (#.Some _) false #.None true))
              (read_variant read value)
              (read_tuple read value))

            #.None)
          (exception.throw ..unknown_kind_of_object host_object)
          )))

(exception: (cannot_apply_a_non_function {object java/lang/Object})
  (exception.report
   ["Class" (java/lang/Object::toString (java/lang/Object::getClass object))]
   ["Non-function" (java/lang/Object::toString object)]))

(host.import: javax/script/ScriptEngine
  ["#::."
   (eval [java/lang/String] #try Object)])

(host.import: org/develnext/jphp/scripting/JPHPScriptEngine
  ["#::."
   (new [])])

(def: (ensure_macro macro)
  (-> Macro (Maybe php/runtime/memory/ObjectMemory))
  (|> macro
      (:coerce java/lang/Object)
      (host.check php/runtime/memory/ObjectMemory)))

(def: interpreter
  (org/develnext/jphp/scripting/JPHPScriptEngine::new))

(def: default_environment
  php/runtime/env/Environment
  (|> ..interpreter
      java/lang/Object::getClass
      (java/lang/Class::getDeclaredField "environment")
      (exec> [(java/lang/reflect/AccessibleObject::setAccessible true)])
      (java/lang/reflect/Field::get ..interpreter)
      (:coerce php/runtime/env/Environment)))

(def: (call_macro inputs lux macro)
  (-> (List Code) Lux php/runtime/memory/ObjectMemory (Try (Try [Lux (List Code)])))
  (<| :assume
      (do try.monad
        [output (php/runtime/lang/Closure::call ..default_environment
                                                (|> (host.array php/runtime/Memory 3)
                                                    (host.array_write 0 macro)
                                                    (host.array_write 1 (lux_structure (:coerce (Array java/lang/Object) inputs)))
                                                    (host.array_write 2 (lux_structure (:coerce (Array java/lang/Object) lux))))
                                                (:coerce php/runtime/lang/Closure
                                                         (php/runtime/memory/ObjectMemory::value macro)))]
        (..read (:coerce java/lang/Object 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 _.Statement))
  (io (let [run! (: (-> (_.Code Any) (Try Any))
                    (function (recur code)
                      (do try.monad
                        [output (javax/script/ScriptEngine::eval (format "<?php " (_.code code)) ..interpreter)]
                        (..read output))))]
        (: (Host _.Expression _.Statement)
           (structure
            (def: (evaluate! context code)
              (run! (_.return code)))
            
            (def: execute! run!)
            
            (def: (define! context input)
              (let [global (reference.artifact context)
                    @global (_.global global)]
                (do try.monad
                  [#let [definition (_.set! @global input)]
                   _ (run! definition)
                   value (run! (_.return @global))]
                  (wrap [global value definition]))))

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

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

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

(def: (program context program)
  (Program _.Expression _.Statement)
  (_.; (_.apply/2 program
                  [(runtime.lux//program_args _.command_line_arguments)
                   _.null])))

(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)
            (do try.monad
              [handler (try.from_maybe (..ensure_macro handler))
               #let [to_php (: (-> Any php/runtime/Memory)
                               (|>> (:coerce (Array java/lang/Object)) lux_structure (:coerce php/runtime/Memory)))]
               output (php/runtime/lang/Closure::call ..default_environment
                                                      (|> (host.array php/runtime/Memory 6)
                                                          (host.array_write 0 handler)
                                                          (host.array_write 1 (php/runtime/memory/StringMemory::new name))
                                                          (host.array_write 2 (to_php phase))
                                                          (host.array_write 3 (to_php archive))
                                                          (host.array_write 4 (to_php parameters))
                                                          (host.array_write 5 (to_php state)))
                                                      (:coerce php/runtime/lang/Closure
                                                               (php/runtime/memory/ObjectMemory::value handler)))]
              (..read output))))

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

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

(`` (program: [{service /cli.service}]
      (let [extension ".php"]
        (do io.monad
          [platform ..platform]
          (exec (do promise.monad
                  [_ (/.compiler {#/static.host @.php
                                  #/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
                                 [(& Nat _.Label) _.Expression _.Statement]
                                 ..extender
                                 service
                                 [(packager.package (_.manual "<?php")
                                                    _.code
                                                    _.then
                                                    (|>>))
                                  (format (/cli.target service)
                                          (\ file.default separator)
                                          "program"
                                          extension)])]
                  (..declare_success! []))
            (io.io []))))))