aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/generation/lua/runtime.lux
blob: 57f8e11dfe9f5cd781707e47ff5245a090d27f29 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
(.module:
  [lux (#- inc)
   [abstract
    [monad (#+ do)]]
   [control
    ["." function]
    ["p" parser
     ["s" code]]]
   [data
    [number (#+ hex)
     ["." i64]]
    ["." text
     format]
    [collection
     ["." list ("#@." functor)]]]
   ["." macro
    ["." code]
    [syntax (#+ syntax:)]]
   [host
    ["_" lua (#+ Expression Location Var Computation Literal Statement)]]]
  ["." ///
   ["//." //
    [//
     ["/////." name]
     ["." synthesis]]]]
  )

(template [<name> <base>]
  [(type: #export <name>
     (<base> Var (Expression Any) Statement))]

  [Operation ///.Operation]
  [Phase ///.Phase]
  [Handler ///.Handler]
  [Bundle ///.Bundle]
  )

(def: prefix Text "LuxRuntime")

(def: #export unit (_.string synthesis.unit))

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

(def: #export variant-tag-field "_lux_tag")
(def: #export variant-flag-field "_lux_flag")
(def: #export variant-value-field "_lux_value")

(def: (variant' tag last? value)
  (-> (Expression Any) (Expression Any) (Expression Any) Literal)
  (_.table (list [..variant-tag-field tag]
                 [..variant-flag-field last?]
                 [..variant-value-field value])))

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

(def: #export none
  Literal
  (..variant 0 #0 unit))

(def: #export some
  (-> (Expression Any) Literal)
  (..variant 1 #1))

(def: #export left
  (-> (Expression Any) Literal)
  (..variant 0 #0))

(def: #export right
  (-> (Expression Any) Literal)
  (..variant 1 #1))

(def: runtime-name
  (-> Text Var)
  (|>> /////name.normalize
       (format ..prefix "_")
       _.var))

(def: (feature name definition)
  (-> Var (-> Var Statement) Statement)
  (definition name))

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

(syntax: (runtime: {declaration (p.or s.local-identifier
                                      (s.form (p.and s.local-identifier
                                                     (p.some s.local-identifier))))}
           code)
  (case declaration
    (#.Left name)
    (macro.with-gensyms [g!_]
      (let [nameC (code.local-identifier name)
            code-nameC (code.local-identifier (format "@" name))
            runtime-nameC (` (runtime-name (~ (code.text name))))]
        (wrap (list (` (def: #export (~ nameC) Var (~ runtime-nameC)))
                    (` (def: (~ code-nameC)
                         Statement
                         (..feature (~ runtime-nameC)
                                    (function ((~ g!_) (~ nameC))
                                      (_.set (~ nameC) (~ code))))))))))
    
    (#.Right [name inputs])
    (macro.with-gensyms [g!_]
      (let [nameC (code.local-identifier name)
            code-nameC (code.local-identifier (format "@" name))
            runtime-nameC (` (runtime-name (~ (code.text name))))
            inputsC (list@map code.local-identifier inputs)
            inputs-typesC (list@map (function.constant (` (_.Expression Any)))
                                    inputs)]
        (wrap (list (` (def: #export ((~ nameC) (~+ inputsC))
                         (-> (~+ inputs-typesC) (Computation Any))
                         (_.apply/* (list (~+ inputsC)) (~ runtime-nameC))))
                    (` (def: (~ code-nameC)
                         Statement
                         (..feature (~ runtime-nameC)
                                    (function ((~ g!_) (~ g!_))
                                      (..with-vars [(~+ inputsC)]
                                        (_.function (~ g!_) (list (~+ inputsC))
                                          (~ code)))))))))))))

(def: (nth index table)
  (-> (Expression Any) (Expression Any) (Location Any))
  (_.nth (_.+ (_.int +1) index) table))

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

## No need to turn tuple//left and tuple//right into loops, as Lua
## does tail-call optimization.
## https://www.lua.org/pil/6.3.html
(runtime: (tuple//left lefts tuple)
  (with-vars [last-right]
    ($_ _.then
        (_.let (list last-right) (..last-index tuple))
        (_.if (_.> lefts last-right)
          ## No need for recursion
          (_.return (..nth lefts tuple))
          ## Needs recursion
          (_.return (tuple//left (_.- last-right lefts)
                                 (..nth last-right tuple)))))))

(runtime: (array//sub from to array)
  (with-vars [temp idx]
    ($_ _.then
        (_.let (list temp) (_.array (list)))
        (_.for-step idx from (_.- (_.int +1) to) (_.int +1)
                    (|> (_.var "table.insert")
                        (_.apply/* (list temp (..nth idx array)))
                        _.statement))
        (_.return temp))))

(runtime: (tuple//right lefts tuple)
  (with-vars [last-right right-index]
    ($_ _.then
        (_.let (list last-right) (..last-index tuple))
        (_.let (list right-index) (_.+ (_.int +1) lefts))
        (_.cond (list [(_.= right-index last-right)
                       (_.return (..nth right-index tuple))]
                      [(_.> right-index last-right)
                       ## Needs recursion.
                       (_.return (tuple//right (_.- last-right lefts)
                                               (..nth last-right tuple)))])
                (_.return (array//sub right-index (_.length tuple) tuple)))
        )))

(runtime: (sum//get sum wantsLast wantedTag)
  (let [no-match! (_.return _.nil)
        sum-tag (_.the ..variant-tag-field sum)
        sum-flag (_.the ..variant-flag-field sum)
        sum-value (_.the ..variant-value-field sum)
        is-last? (_.= (_.string "") sum-flag)
        test-recursion! (_.if is-last?
                          ## Must recurse.
                          (_.return (sum//get sum-value wantsLast (_.- sum-tag wantedTag)))
                          no-match!)]
    (_.cond (list [(_.= sum-tag wantedTag)
                   (_.if (_.= wantsLast sum-flag)
                     (_.return sum-value)
                     test-recursion!)]

                  [(_.> sum-tag wantedTag)
                   test-recursion!]

                  [(_.and (_.< sum-tag wantedTag)
                          (_.= (_.string "") wantsLast))
                   (_.return (variant' (_.- wantedTag sum-tag) sum-flag sum-value))])

            no-match!)))

(runtime: (array//copy array)
  (with-vars [temp idx]
    ($_ _.then
        (_.let (list temp) (_.array (list)))
        (<| (_.for-step idx (_.int +1) (_.length array) (_.int +1))
            (_.statement (|> (_.var "table.insert") (_.apply/* (list temp (_.nth idx array))))))
        (_.return temp))))

(runtime: (array//concat left right)
  (with-vars [temp idx]
    (let [copy! (function (_ input output)
                  (<| (_.for-step idx (_.int +1) (_.the "n" input) (_.int +1))
                      (_.statement (|> (_.var "table.insert") (_.apply/* (list output (_.nth idx input)))))))]
      ($_ _.then
          (_.let (list temp) (_.array (list)))
          (copy! left temp)
          (copy! right temp)
          (_.return temp)))))

(def: runtime//adt
  Statement
  ($_ _.then
      @tuple//left
      @array//sub
      @tuple//right
      @sum//get
      @array//copy
      @array//concat))

(runtime: (lux//try risky)
  (with-vars [success value]
    ($_ _.then
        (_.let (list success value) (|> risky (_.apply/* (list ..unit))
                                        _.return (_.closure (list))
                                        list _.apply/* (|> (_.var "pcall"))))
        (_.if success
          (_.return (..right value))
          (_.return (..left value))))))

(runtime: (lux//program-args raw)
  (with-vars [tail head idx]
    ($_ _.then
        (_.let (list tail) ..none)
        (<| (_.for-step idx (_.length raw) (_.int +1) (_.int -1))
            (_.set (list tail) (..some (_.array (list (_.nth idx raw)
                                                      tail)))))
        (_.return tail))))

(def: runtime//lux
  Statement
  ($_ _.then
      @lux//try
      @lux//program-args))

(runtime: (i64//logic-right-shift param subject)
  (let [mask (|> (_.int +1)
                 (_.bit-shl (_.- param (_.int +64)))
                 (_.- (_.int +1)))]
    (_.return (|> subject
                  (_.bit-shr param)
                  (_.bit-and mask)))))

(def: runtime//i64
  Statement
  ($_ _.then
      @i64//logic-right-shift
      ))

(runtime: (text//index subject param start)
  (with-vars [idx]
    ($_ _.then
        (_.let (list idx) (_.apply/* (list subject param start (_.bool #1))
                                     (_.var "string.find")))
        (_.if (_.= _.nil idx)
          (_.return ..none)
          (_.return (..some idx))))))

(runtime: (text//clip text from to)
  (with-vars [size]
    ($_ _.then
        (_.let (list size) (_.apply/* (list text) (_.var "string.len")))
        (_.if (_.or (_.> size from)
                    (_.> size to))
          (_.return ..none)
          (_.return (..some (_.apply/* (list text from to) (_.var "string.sub")))))
        )))

(runtime: (text//char idx text)
  (with-vars [char]
    ($_ _.then
        (_.let (list char) (_.apply/* (list text idx) (_.var "string.byte")))
        (_.if (_.= _.nil char)
          (_.return ..none)
          (_.return (..some char))))))

(def: runtime//text
  Statement
  ($_ _.then
      @text//index
      @text//clip
      @text//char))

(runtime: (array//new size)
  (with-vars [output idx]
    ($_ _.then
        (_.let (list output) (_.array (list)))
        (_.for-step idx (_.int +1) size (_.int +1)
                    (_.statement (_.apply/* (list output ..unit) (_.var "table.insert"))))
        (_.return output))))

(runtime: (array//get array idx)
  (with-vars [temp]
    ($_ _.then
        (_.let (list temp) (..nth idx array))
        (_.if (_.or (_.= _.nil temp)
                    (_.= ..unit temp))
          (_.return ..none)
          (_.return (..some temp))))))

(runtime: (array//put array idx value)
  ($_ _.then
      (_.set (list (..nth idx array)) value)
      (_.return array)))

(def: runtime//array
  Statement
  ($_ _.then
      @array//new
      @array//get
      @array//put
      ))

(runtime: (box//write value box)
  ($_ _.then
      (_.set (list (_.nth (_.int +1) box)) value)
      (_.return ..unit)))

(def: runtime//box
  Statement
  @box//write)

(def: runtime
  Statement
  ($_ _.then
      runtime//adt
      runtime//lux
      runtime//i64
      runtime//text
      runtime//array
      runtime//box
      ))

(def: #export artifact ..prefix)

(def: #export generate
  (Operation Any)
  (///.with-buffer
    (do ////.monad
      [_ (///.save! true ["" ..prefix]
                    ..runtime)]
      (///.save-buffer! ..artifact))))