aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/scheme/eval.jvm.lux
blob: cbdd43d943064ee33832ee7c4fa54f8415ea6139 (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 "text/" Eq<Text>]
             text/format
             (coll [array]))
       [host])
  (luxc [lang]
        (lang (host [scheme #+ Expression])))
  [//])

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

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

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

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

(host.import: java/lang/Boolean)
(host.import: java/lang/String)

(host.import: gnu/math/IntNum
  (longValue [] long))

(host.import: gnu/math/DFloNum
  (doubleValue [] double))

(host.import: (gnu/lists/FVector E)
  (getBufferLength [] int)
  (get [int] E))

(host.import: gnu/lists/EmptyList)

(host.import: gnu/lists/FString
  (toString [] String))

(host.import: gnu/lists/Pair
  (getCar [] Object)
  (getCdr [] Object)
  (get [int] Object))

(host.import: gnu/mapping/Symbol
  (getName [] String))

(host.import: gnu/mapping/SimpleSymbol)

(def: (parse-tuple lux-object host-object)
  (-> (-> Object (Error Any)) (FVector Object) (Error Any))
  (let [size (:coerce Nat (FVector::getBufferLength [] host-object))]
    (loop [idx +0
           output (:coerce (Array Any) (array.new size))]
      (if (n/< size idx)
        (case (lux-object (FVector::get [(:coerce Int idx)] host-object))
          (#e.Error error)
          (#e.Error error)

          (#e.Success lux-value)
          (recur (inc idx) (array.write idx (:coerce Any lux-value) output)))
        (#e.Success output)))))

(def: (variant tag flag value)
  (-> Nat Bool Any Any)
  [(Long::intValue [] (:coerce Long tag))
   (: Any
      (if flag
        //.unit
        (host.null)))
   value])

(def: (to-text value)
  (-> Any Text)
  (let [value-text (:coerce Text (Object::toString [] (:coerce Object value)))
        class-text (:coerce Text (Object::toString [] (Object::getClass [] (:coerce Object value))))]
    (format value-text " : " class-text)))

(def: (parse-variant lux-object host-object)
  (-> (-> Object (Error Any)) Pair (Error Any))
  (let [variant-tag (Pair::getCar [] host-object)]
    (if (and (host.instance? gnu/mapping/SimpleSymbol variant-tag)
             (text/= //.variant-tag (Symbol::getName [] (:coerce Symbol variant-tag))))
      (do e.Monad<Error>
        [#let [host-object (:coerce Pair (Pair::getCdr [] host-object))]
         tag (lux-object (Pair::getCar [] host-object))
         #let [host-object (:coerce Pair (Pair::getCdr [] host-object))]
         #let [flag (host.instance? java/lang/String
                                    (Pair::getCar [] host-object))]
         value (lux-object (Pair::getCdr [] host-object))]
        (wrap (..variant (:coerce Nat tag) flag value)))
      (ex.throw invalid-variant (:coerce Text (Object::toString [] (:coerce Object host-object)))))))

(def: (lux-object host-object)
  (-> Object (Error Any))
  (cond (or (host.instance? java/lang/Boolean host-object)
            (host.instance? java/lang/String host-object))
        (#e.Success host-object)

        (host.instance? gnu/math/IntNum host-object)
        (#e.Success (IntNum::longValue [] (:coerce IntNum host-object)))

        (host.instance? gnu/math/DFloNum host-object)
        (#e.Success (DFloNum::doubleValue [] (:coerce DFloNum host-object)))

        (host.instance? gnu/lists/FString host-object)
        (#e.Success (FString::toString [] (:coerce FString host-object)))

        (host.instance? gnu/lists/FVector host-object)
        (parse-tuple lux-object (:coerce (FVector Object) host-object))

        (host.instance? gnu/lists/EmptyList host-object)
        (#e.Success //.unit)

        (host.instance? gnu/lists/Pair host-object)
        (parse-variant lux-object (:coerce Pair host-object))

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

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