aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/php/eval.jvm.lux
blob: c6ff1a880ea44adadac1e243589211a29db25c23 (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
(.module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:])
       (data [bit]
             [maybe]
             ["e" error #+ Error]
             text/format
             (coll [array]))
       [host])
  (luxc [lang]
        (lang (host ["_" php #+ Expression Statement])))
  [//])

(do-template [<name>]
  [(exception: #export (<name> {message Text})
     message)]

  [Not-A-Variant]
  [Null-Has-No-Lux-Representation]
  [Cannot-Evaluate]
  )

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

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

(exception: #export (Unknown-Kind-Of-Host-Object {host-object Object})
  (let [object-class (:! Text (Object::toString [] (Object::getClass [] (:! Object host-object))))
        text-representation (:! Text (Object::toString [] (:! Object host-object)))]
    (format object-class " --- " text-representation)))

(host.import php/runtime/Memory)

(host.import php/runtime/memory/NullMemory)

(host.import php/runtime/memory/FalseMemory)
(host.import php/runtime/memory/TrueMemory)

(host.import php/runtime/memory/LongMemory
  (new [long])
  (toLong [] long))

(host.import php/runtime/memory/DoubleMemory
  (toDouble [] double))

(host.import php/runtime/memory/StringMemory
  (new [String])
  (toString [] String))

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

(host.import php/runtime/memory/ArrayMemory
  (size [] int)
  (isMap [] boolean)
  (get [Memory] Memory))

(def: (tuple lux-object host-object)
  (-> (-> Object (Error Top)) ArrayMemory (Error Top))
  (let [size (ArrayMemory::size [] host-object)]
    (loop [idx 0
           output (: (Array Top) (array.new (:! Nat size)))]
      (if (i/< size idx)
        (let [value (|> host-object
                        (ArrayMemory::get [(LongMemory::new [idx])])
                        (:! ReferenceMemory) (ReferenceMemory::getValue []))]
          (if (host.instance? php/runtime/memory/NullMemory value)
            (recur (i/inc idx)
                   (array.write (:! Nat idx) (host.null) output))
            (do e.Monad<Error>
              [lux-value (lux-object value)]
              (recur (i/inc idx)
                     (array.write (:! Nat idx) lux-value output)))))
        (ex.return output)))))

(def: (variant lux-object host-object)
  (-> (-> Object (Error Top)) ArrayMemory (Error Top))
  (do e.Monad<Error>
    [variant-tag (lux-object (ArrayMemory::get [(StringMemory::new [//.variant-tag-field])] host-object))
     variant-value (lux-object (ArrayMemory::get [(StringMemory::new [//.variant-value-field])] host-object))]
    (wrap (: Top
             [(Long::intValue [] (:! Long variant-tag))
              (: Top
                 (if (|> host-object
                         (ArrayMemory::get [(StringMemory::new [//.variant-flag-field])])
                         (:! ReferenceMemory)
                         (ReferenceMemory::getValue [])
                         (host.instance? php/runtime/memory/NullMemory))
                   (host.null)
                   ""))
              variant-value]))))

(def: (lux-object host-object)
  (-> Object (Error Top))
  (cond (host.instance? php/runtime/memory/FalseMemory host-object)
        (ex.return false)

        (host.instance? php/runtime/memory/TrueMemory host-object)
        (ex.return true)

        (host.instance? php/runtime/memory/LongMemory host-object)
        (ex.return (LongMemory::toLong [] (:! LongMemory host-object)))

        (host.instance? php/runtime/memory/DoubleMemory host-object)
        (ex.return (DoubleMemory::toDouble [] (:! DoubleMemory host-object)))

        (host.instance? php/runtime/memory/StringMemory host-object)
        (ex.return (StringMemory::toString [] (:! StringMemory host-object)))

        (host.instance? php/runtime/memory/ReferenceMemory host-object)
        (lux-object (ReferenceMemory::getValue [] (:! ReferenceMemory host-object)))

        (host.instance? php/runtime/memory/ArrayMemory host-object)
        (if (ArrayMemory::isMap [] (:! ArrayMemory host-object))
          (variant lux-object (:! ArrayMemory host-object))
          (tuple lux-object (:! ArrayMemory host-object)))

        ## else
        (ex.throw Unknown-Kind-Of-Host-Object host-object)))

(def: #export (eval code)
  (-> Expression (Meta Top))
  (function (_ compiler)
    (let [interpreter (|> compiler (get@ #.host) (:! //.Host) (get@ #//.interpreter))]
      (case (interpreter code)
        (#e.Error error)
        ((lang.throw Cannot-Evaluate error) compiler)

        (#e.Success output)
        (case (lux-object output)
          (#e.Success parsed-output)
          (#e.Success [compiler parsed-output])

          (#e.Error error)
          ((lang.throw Cannot-Evaluate error) compiler))))))