aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/common-lisp/eval.jvm.lux
blob: cb6f03d17de76b0975077a21e596cd384fc19fc3 (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
(.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 ["_" common-lisp #+ Expression])))
  [//])

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

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

(host.import (java/lang/Class ?)
  (#static forName [String] #try (Class Object)))

(def: _0
  Any
  (case (Class::forName "org.armedbear.lisp.Symbol")
    (#e.Success _)
    (log! "LOADED")

    (#e.Error error)
    (log! error)))

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

  [Null-Has-No-Lux-Representation]
  [Cannot-Evaluate]
  [invalid-variant]
  )

(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 org/armedbear/lisp/LispObject)

(host.import org/armedbear/lisp/SimpleString
  (getStringValue [] String))

(host.import org/armedbear/lisp/Symbol
  (#static T Symbol)
  (getName [] String))

(host.import org/armedbear/lisp/DoubleFloat
  (doubleValue [] double))

(host.import org/armedbear/lisp/Bignum
  (longValue [] long))

(host.import org/armedbear/lisp/Fixnum
  (longValue [] long))

(host.import org/armedbear/lisp/Nil)

(host.import org/armedbear/lisp/SimpleVector
  (length [] int)
  (elt [int] LispObject))

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

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

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

(host.import org/armedbear/lisp/Cons
  (car LispObject)
  (cdr LispObject))

(def: (parse-variant lux-object host-object)
  (-> (-> Object (Error Any)) Cons (Error Any))
  (let [variant-tag (Cons::car host-object)]
    (if (and (host.instance? org/armedbear/lisp/Symbol variant-tag)
             (text/= //.variant-tag (Symbol::getName [] (:! Symbol variant-tag))))
      (do e.Monad<Error>
        [#let [host-object (:! Cons (Cons::cdr host-object))]
         tag (lux-object (Cons::car host-object))
         #let [host-object (:! Cons (Cons::cdr host-object))]
         #let [flag (host.instance? org/armedbear/lisp/SimpleString
                                    (Cons::car host-object))]
         value (lux-object (Cons::cdr host-object))]
        (wrap (..variant (:! Nat tag) flag value)))
      (ex.throw invalid-variant (:! Text (Object::toString [] (:! Object host-object)))))))

(def: (lux-object host-object)
  (-> Object (Error Any))
  (cond (host.instance? org/armedbear/lisp/Bignum host-object)
        (#e.Success (Bignum::longValue [] (:! Bignum host-object)))

        (host.instance? org/armedbear/lisp/Fixnum host-object)
        (#e.Success (Fixnum::longValue [] (:! Fixnum host-object)))

        (host.instance? org/armedbear/lisp/DoubleFloat host-object)
        (#e.Success (DoubleFloat::doubleValue [] (:! DoubleFloat host-object)))

        (host.instance? org/armedbear/lisp/Nil host-object)
        (#e.Success false)

        (host.instance? org/armedbear/lisp/Symbol host-object)
        (if (is? Symbol::T (:! Symbol host-object))
          (#e.Success true)
          (ex.throw Unknown-Kind-Of-Host-Object (:! Object host-object)))

        (host.instance? org/armedbear/lisp/SimpleString host-object)
        (#e.Success (SimpleString::getStringValue [] (:! SimpleString host-object)))

        (host.instance? org/armedbear/lisp/SimpleVector host-object)
        (parse-tuple lux-object (:! SimpleVector host-object))

        (host.instance? org/armedbear/lisp/Cons host-object)
        (parse-variant lux-object (:! Cons host-object))

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

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