aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/php/eval.jvm.lux
blob: 3768bb5c7df1216fd2181b2f48d586a81c5d16b4 (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 (:coerce Text (Object::toString [] (Object::getClass [] (:coerce Object host-object))))
        text-representation (:coerce Text (Object::toString [] (:coerce 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 Any)) ArrayMemory (Error Any))
  (let [size (ArrayMemory::size [] host-object)]
    (loop [idx 0
           output (: (Array Any) (array.new (:coerce Nat size)))]
      (if (i/< size idx)
        (let [value (|> host-object
                        (ArrayMemory::get [(LongMemory::new [idx])])
                        (:coerce ReferenceMemory) (ReferenceMemory::getValue []))]
          (if (host.instance? php/runtime/memory/NullMemory value)
            (recur (inc idx)
                   (array.write (:coerce Nat idx) (host.null) output))
            (do e.Monad<Error>
              [lux-value (lux-object value)]
              (recur (inc idx)
                     (array.write (:coerce Nat idx) lux-value output)))))
        (ex.return output)))))

(def: (variant lux-object host-object)
  (-> (-> Object (Error Any)) ArrayMemory (Error Any))
  (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 (: Any
             [(Long::intValue [] (:coerce Long variant-tag))
              (: Any
                 (if (|> host-object
                         (ArrayMemory::get [(StringMemory::new [//.variant-flag-field])])
                         (:coerce ReferenceMemory)
                         (ReferenceMemory::getValue [])
                         (host.instance? php/runtime/memory/NullMemory))
                   (host.null)
                   ""))
              variant-value]))))

(def: (lux-object host-object)
  (-> Object (Error Any))
  (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 [] (:coerce LongMemory host-object)))

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

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

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

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

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

(def: #export (eval code)
  (-> Expression (Meta Any))
  (function (_ compiler)
    (let [interpreter (|> compiler (get@ #.host) (:coerce //.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))))))