aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/common-lisp/runtime.jvm.lux
blob: 3e8d2c5140a7d55091d58ae01c77c80c271cc0a9 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
(.module:
  lux
  (lux (control ["p" parser "p/" Monad<Parser>]
                [monad #+ do])
       (data [bit]
             [number #+ hex]
             text/format
             (coll [list "list/" Monad<List>]))
       [macro]
       (macro [code]
              ["s" syntax #+ syntax:])
       [io #+ Process])
  [//]
  (luxc [lang]
        (lang (host ["_" common-lisp #+ SVar Expression @@]))))

(def: prefix Text "LuxRuntime")

(def: #export unit Expression (_.string //.unit))

(def: (flag value)
  (-> Bit Expression)
  (if value
    (_.string "")
    _.nil))

(def: (variant' tag last? value)
  (-> Expression Expression Expression Expression)
  (<| (_.cons (_.symbol //.variant-tag))
      (_.cons tag)
      (_.cons last?)
      value))

(def: #export (variant tag last? value)
  (-> Nat Bit Expression Expression)
  (variant' (_.int (:coerce Int tag)) (flag last?) value))

(def: #export none
  Expression
  (variant +0 #0 unit))

(def: #export some
  (-> Expression Expression)
  (variant +1 #1))

(def: #export left
  (-> Expression Expression)
  (variant +0 #0))

(def: #export right
  (-> Expression Expression)
  (variant +1 #1))

(type: Runtime Expression)

(def: declaration
  (s.Syntax [Text (List Text)])
  (p.either (p.seq s.local-identifier (p/wrap (list)))
            (s.form (p.seq s.local-identifier (p.some s.local-identifier)))))

(syntax: (runtime: {[name args] declaration}
           definition)
  (let [implementation (code.local-identifier (format "@@" name))
        runtime (format prefix "__" (lang.normalize-name name))
        $runtime (` (_.var (~ (code.text runtime))))
        @runtime (` (@@ (~ $runtime)))
        argsC+ (list/map code.local-identifier args)
        argsLC+ (list/map (|>> lang.normalize-name (format "LRV__") code.text (~) (_.var) (`))
                          args)
        declaration (` ((~ (code.local-identifier name))
                        (~+ argsC+)))
        type (` (-> (~+ (list.repeat (list.size argsC+) (` _.Expression)))
                    _.Expression))]
    (wrap (list (` (def: (~' #export) (~ declaration)
                     (~ type)
                     (~ (case argsC+
                          #.Nil
                          @runtime

                          _
                          (` (_.$apply (~ @runtime) (list (~+ argsC+))))))))
                (` (def: (~ implementation)
                     _.Expression
                     (~ (case argsC+
                          #.Nil
                          (` (_.defparameter (~ $runtime) (~ definition)))

                          _
                          (` (let [(~+ (|> (list.zip2 argsC+ argsLC+)
                                           (list/map (function (_ [left right])
                                                       (list left right)))
                                           list/join))]
                               (_.defun (~ $runtime) (list (~+ argsLC+))
                                        (~ definition))))))))))))

(syntax: #export (with-vars {vars (s.tuple (p.many s.local-identifier))}
                   body)
  (wrap (list (` (let [(~+ (|> vars
                               (list/map (function (_ var)
                                           (list (code.local-identifier var)
                                                 (` (_.var (~ (code.text (format "LRV__" (lang.normalize-name var)))))))))
                               list/join))]
                   (~ body))))))

(runtime: (lux//try op)
  (with-vars [error]
    (_.handler-case
     (list [(_.bool #1) error
            (..left (_.format/3 _.nil (_.string "~A") (@@ error)))])
     (..right (_.funcall (list ..unit) (@@ op))))))

(runtime: (lux//program-args program-args)
  (with-vars [loop input output]
    (_.labels (list [loop [(_.poly (list input output))
                           (_.if (_.null (@@ input))
                             (@@ output)
                             (_.funcall (list (_.cdr (@@ input))
                                              (..some (_.vector (list (_.car (@@ input)) (@@ output)))))
                                        (_.function (@@ loop))))]])
              (_.funcall (list (_.reverse (@@ program-args))
                               ..none)
                         (_.function (@@ loop))))))

(def: runtime//lux
  Runtime
  (_.progn (list @@lux//try
                 @@lux//program-args)))

(def: minimum-index-length
  (-> Expression Expression)
  (|>> (_.+ (_.int 1))))

(def: product-element
  (-> Expression Expression Expression)
  _.svref)

(def: (product-tail product)
  (-> Expression Expression)
  (_.svref product (|> (_.length product) (_.- (_.int 1)))))

(def: (updated-index min-length product)
  (-> Expression Expression Expression)
  (|> min-length (_.- (_.length product))))

(runtime: (product//left product index)
  (with-vars [$index_min_length]
    (_.let (list [$index_min_length (minimum-index-length (@@ index))])
      (_.if (|> (_.length (@@ product)) (_.> (@@ $index_min_length)))
        ## No need for recursion
        (product-element (@@ product) (@@ index))
        ## Needs recursion
        (product//left (product-tail (@@ product))
                       (updated-index (@@ $index_min_length) (@@ product)))))))

(runtime: (product//right product index)
  (with-vars [$index_min_length $product_length]
    (_.let (list [$index_min_length (minimum-index-length (@@ index))]
                 [$product_length (_.length (@@ product))])
      (<| (_.if (|> (@@ $product_length) (_.= (@@ $index_min_length)))
            ## Last element.
            (product-element (@@ product) (@@ index)))
          (_.if (|> (@@ $product_length) (_.< (@@ $index_min_length)))
            ## Needs recursion
            (product//right (product-tail (@@ product))
                            (updated-index (@@ $index_min_length) (@@ product))))
          ## Must slice
          (_.subseq/3 (@@ product) (@@ index) (@@ $product_length))))))

(runtime: (sum//get sum wanted_tag wants_last)
  (with-vars [variant-tag sum-tag sum-flag sum-value]
    (let [no-match _.nil
          is-last? (|> (@@ sum-flag) (_.equal (_.string "")))
          test-recursion (_.if is-last?
                           ## Must recurse.
                           (sum//get (@@ sum-value)
                                     (|> (@@ wanted_tag) (_.- (@@ sum-tag)))
                                     (@@ wants_last))
                           no-match)]
      (<| (_.destructuring-bind [(_.poly (list variant-tag sum-tag sum-flag sum-value))
                                 (@@ sum)])
          (_.if (|> (@@ wanted_tag) (_.= (@@ sum-tag)))
            (_.if (|> (@@ sum-flag) (_.equal (@@ wants_last)))
              (@@ sum-value)
              test-recursion))
          (_.if (|> (@@ wanted_tag) (_.> (@@ sum-tag)))
            test-recursion)
          (_.if (_.and (list (|> (@@ wants_last) (_.equal (_.string "")))
                             (|> (@@ wanted_tag) (_.< (@@ sum-tag)))))
            (variant' (|> (@@ sum-tag) (_.- (@@ wanted_tag))) (@@ sum-flag) (@@ sum-value)))
          no-match))))

(def: runtime//adt
  Runtime
  (_.progn (list @@product//left
                 @@product//right
                 @@sum//get)))

(runtime: (bit//logical-right-shift shift input)
  (_.if (_.= (_.int 0) (@@ shift))
    (@@ input)
    (|> (@@ input)
        (_.ash (_.* (_.int -1) (@@ shift)))
        (_.logand (_.int (hex "7FFFFFFFFFFFFFFF"))))))

(def: runtime//bit
  Runtime
  (_.progn (list @@bit//logical-right-shift)))

(do-template [<name> <top-cmp>]
  [(def: (<name> top value)
     (-> Expression Expression Expression)
     (_.and (list (|> value (_.>= (_.int 0)))
                  (|> value (<top-cmp> top)))))]

  [within? _.<]
  [up-to?  _.<=]
  )

(runtime: (text//char idx text)
  (_.if (|> (@@ idx) (within? (_.length (@@ text))))
    (..some (_.char-int/1 (_.char/2 (@@ text) (@@ idx))))
    ..none))

(runtime: (text//clip from to text)
  (_.if (_.and (list (|> (@@ to) (within? (_.length (@@ text))))
                     (|> (@@ from) (up-to? (@@ to)))))
    (..some (_.subseq/3 (@@ text) (@@ from) (@@ to)))
    ..none))

(runtime: (text//index reference start space)
  (with-vars [index]
    (_.let (list [index (_.search/start2 (@@ reference) (@@ space) (@@ start))])
      (_.if (@@ index)
        (..some (@@ index))
        ..none))))

(def: runtime//text
  Runtime
  (_.progn (list @@text//index
                 @@text//clip
                 @@text//char)))

(def: (check-index-out-of-bounds array idx body)
  (-> Expression Expression Expression Expression)
  (_.if (|> idx (_.<= (_.length array)))
    body
    (_.error (_.string "Array index out of bounds!"))))

(runtime: (array//get array idx)
  (with-vars [temp]
    (<| (check-index-out-of-bounds (@@ array) (@@ idx))
        (_.let (list [temp (_.svref (@@ array) (@@ idx))])
          (_.if (_.null (@@ temp))
            ..none
            (..some (@@ temp)))))))

(runtime: (array//put array idx value)
  (<| (check-index-out-of-bounds (@@ array) (@@ idx))
      (_.progn
       (list (_.setf! (_.svref (@@ array) (@@ idx)) (@@ value))
             (@@ array)))))

(def: runtime//array
  Runtime
  (_.progn
   (list @@array//get
         @@array//put)))

(runtime: (box//write value box)
  (_.progn
   (list
    (_.setf! (_.svref (@@ box) (_.int 0)) (@@ value))
    ..unit)))

(def: runtime//box
  Runtime
  (_.progn (list @@box//write)))

(runtime: (io//exit code)
  (_.progn
   (list (_.conditional+ (list "sbcl")
                         (_.$apply (_.global "sb-ext:quit") (list (@@ code))))
         (_.conditional+ (list "clisp")
                         (_.$apply (_.global "ext:exit") (list (@@ code))))
         (_.conditional+ (list "ccl")
                         (_.$apply (_.global "ccl:quit") (list (@@ code))))
         (_.conditional+ (list "allegro")
                         (_.$apply (_.global "excl:exit") (list (@@ code))))
         (_.$apply (_.global "cl-user::quit") (list (@@ code))))))

(runtime: (io//current-time _)
  (|> _.get-universal-time
      (_.* (_.int 1_000))))

(def: runtime//io
  (_.progn (list @@io//exit
                 @@io//current-time)))

(def: runtime
  Runtime
  (_.progn (list runtime//lux
                 runtime//bit
                 runtime//adt
                 runtime//text
                 runtime//array
                 runtime//box
                 runtime//io)))

(def: #export artifact Text (format prefix //.file-extension))

(def: #export translate
  (Meta (Process Any))
  (do macro.Monad<Meta>
    [_ //.init-module-buffer
     _ (//.save runtime)]
    (//.save-module! artifact)))