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

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

  [Unknown-Kind-Of-Host-Object]
  [Null-Has-No-Lux-Representation]
  [Cannot-Evaluate]
  )

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

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

(host.import org/renjin/sexp/SEXP)

(host.import org/renjin/sexp/StringArrayVector
  (getElementAsString [int] String))

(host.import org/renjin/sexp/LogicalArrayVector
  (getElementAsRawLogical [int] int))

(host.import org/renjin/sexp/IntArrayVector
  (getElementAsInt [int] int))

(host.import org/renjin/sexp/DoubleArrayVector
  (getElementAsDouble [int] double))

(host.import org/renjin/sexp/ListVector
  (length [] int)
  (getElementAsSEXP [int] #try SEXP)
  (getElementAsSEXP #as get-field-sexp [String] #try SEXP))

(host.import org/renjin/sexp/Null) 

(def: (parse-tuple lux-object host-object)
  (-> (-> Object (Error Top)) ListVector (Error Top))
  (let [size (:! Nat (ListVector::length [] host-object))]
    (loop [idx +0
           output (:! (Array Top) (array.new size))]
      (if (n/< size idx)
        (case (ListVector::getElementAsSEXP [(:! Int idx)] host-object)
          (#e.Error error)
          (#e.Error error)
          
          (#e.Success value)
          (case (lux-object (:! Object value))
            (#e.Error error)
            (#e.Error error)

            (#e.Success lux-value)
            (recur (n/inc idx) (array.write idx (:! Top lux-value) output))))
        (#e.Success output)))))

(def: (parse-variant lux-object host-object)
  (-> (-> Object (Error Top)) ListVector (Error Top))
  (do e.Monad<Error>
    [tag (ListVector::get-field-sexp [//.variant-tag-field] host-object)
     flag (ListVector::get-field-sexp [//.variant-flag-field] host-object)
     value (ListVector::get-field-sexp [//.variant-value-field] host-object)
     value (lux-object (:! Object value))]
    (wrap [(|> tag
               (:! IntArrayVector)
               (IntArrayVector::getElementAsInt [0])
               (Long::intValue []))
           (: Top
              (if (host.instance? Null flag)
                (host.null)
                //.unit))
           value])))

(def: (parse-int host-object)
  (-> ListVector (Error Int))
  (do e.Monad<Error>
    [high (ListVector::get-field-sexp [//.int-high-field] host-object)
     low (ListVector::get-field-sexp [//.int-low-field] host-object)
     #let [get-int-32 (|>> (IntArrayVector::getElementAsInt [0]) (:! Nat))
           high (get-int-32 (:! IntArrayVector high))
           low (get-int-32 (:! IntArrayVector low))]]
    (wrap (:! Int
              (n/+ (|> high (bit.shift-left +32))
                   (if (i/< 0 (:! Int low))
                     (|> low (bit.shift-left +32) (bit.shift-right +32))
                     low))))))

(def: (lux-object host-object)
  (-> Object (Error Top))
  (cond (host.instance? StringArrayVector host-object)
        (#e.Success (StringArrayVector::getElementAsString [0] (:! StringArrayVector host-object)))

        (host.instance? LogicalArrayVector host-object)
        (#e.Success (i/= 1 (LogicalArrayVector::getElementAsRawLogical [0] (:! LogicalArrayVector host-object))))

        (host.instance? IntArrayVector host-object)
        (#e.Success (IntArrayVector::getElementAsInt [0] (:! IntArrayVector host-object)))

        (host.instance? DoubleArrayVector host-object)
        (#e.Success (DoubleArrayVector::getElementAsDouble [0] (:! DoubleArrayVector host-object)))

        (host.instance? ListVector host-object)
        (case (parse-int (:! ListVector host-object))
          (#e.Error error)
          (case (parse-variant lux-object (:! ListVector host-object))
            (#e.Error error)
            (parse-tuple lux-object (:! ListVector host-object))

            output
            output)

          output
          output)

        ## else
        (let [object-class (:! Text (Object::toString [] (Object::getClass [] (:! Object host-object))))
              text-representation (:! Text (Object::toString [] (:! Object host-object)))]
          (ex.throw Unknown-Kind-Of-Host-Object (format object-class " --- " text-representation)))))

(def: #export (eval code)
  (-> Expression (Meta Top))
  (function (_ compiler)
    (let [interpreter (|> compiler (get@ #.host) (:! //.Host) (get@ #//.interpreter))]
      (case (interpreter code)
        (#e.Error error)
        (exec (log! (format "eval #e.Error\n"
                            "<< " (r.expression code) "\n"
                            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)
          (exec (log! (format "eval #e.Error\n"
                              "<< " (r.expression code) "\n"
                              error))
            ((lang.throw Cannot-Evaluate error) compiler)))))))