aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/lua/runtime.jvm.lux
blob: ce9c37db54091264b280210fc34c0a7564137b39 (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
(.module:
  lux
  (lux (control ["p" parser "p/" Monad<Parser>]
                [monad #+ do])
       (data text/format
             (coll [list "list/" Monad<List>]))
       [macro]
       (macro [code]
              ["s" syntax #+ syntax:])
       [io #+ Process])
  [//]
  (luxc [lang]
        (lang (host [lua #+ Lua Expression Statement]))))

(def: prefix Text "LuxRuntime")

(def: #export unit Expression (%t //.unit))

(def: (flag value)
  (-> Bit Lua)
  (if value
    (lua.string "")
    lua.nil))

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

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

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

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

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

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

(type: Runtime Lua)

(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 (code.text (format "__" prefix "__" (lang.normalize-name name)))
        argsC+ (list/map code.local-identifier args)
        argsLC+ (list/map (|>> lang.normalize-name code.text) args)
        declaration (` ((~ (code.local-identifier name))
                        (~+ argsC+)))
        type (` (-> (~+ (list.repeat (list.size argsC+) (` lua.Lua)))
                    lua.Lua))]
    (wrap (list (` (def: #export (~ declaration)
                     (~ type)
                     (lua.apply (~ runtime) (list (~+ argsC+)))))
                (` (def: (~ implementation)
                     Lua
                     (~ (case argsC+
                          #.Nil
                          (` (lua.global! (~ runtime) (#.Some (~ definition))))

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

(runtime: (array//copy array)
  (lua.block! (list (lua.local! "temp" (#.Some (lua.array (list))))
                    (lua.for-step! "idx" (lua.int 1) (lua.length array) (lua.int 1)
                                   (lua.apply "table.insert" (list "temp" (lua.nth "idx" array))))
                    (lua.return! "temp"))))

(runtime: (array//sub array from to)
  (lua.block! (list (lua.local! "temp" (#.Some (lua.array (list))))
                    (lua.for-step! "idx" from to (lua.int 1)
                                   (lua.apply "table.insert" (list "temp" (lua.nth "idx" array))))
                    (lua.return! "temp"))))

(runtime: (array//concat left right)
  (let [copy! (function (_ input output)
                (lua.for-step! "idx" (lua.int 1) (format input ".n") (lua.int 1)
                               (lua.apply "table.insert" (list output (lua.nth "idx" input)))))]
    (lua.block! (list (lua.local! "temp" (#.Some (lua.array (list))))
                      (copy! left "temp")
                      (copy! right "temp")
                      (lua.return! "temp")))))

(runtime: (lux//try op)
  (lua.block! (list (format "local success, value = " (lua.apply "pcall" (list (lua.function (list) (lua.return! (lua.apply op (list unit)))))) ";")
                    (lua.if! "success"
                             (lua.return! (right "value"))
                             (lua.return! (left "value"))))))

(runtime: (lux//program-args program-args)
  (lua.block! (list (lua.local! "inputs" (#.Some none))
                    (lua.for-step! "idx" (lua.length program-args) (lua.int 1) (lua.int -1)
                                   (lua.set! "inputs" (some (lua.array (list (lua.nth "idx" program-args)
                                                                             "inputs")))))
                    (lua.return! "inputs"))))

(def: runtime//lux
  Runtime
  (format @@lux//try
          @@lux//program-args))

(runtime: (product//left product index)
  (lua.block! (list (lua.local! "index_min_length" (#.Some (lua.+ (lua.int 1) index)))
                    (lua.if! (lua.>= "index_min_length" (lua.length product))
                             ## No need for recursion
                             (lua.return! (lua.nth "index_min_length" product))
                             ## Needs recursion
                             (lua.return! (product//left (lua.nth (lua.length product)
                                                                  product)
                                                         (lua.- (lua.length product)
                                                                "index_min_length")))))))

(runtime: (product//right product index)
  (lua.block! (list (lua.local! "index_min_length" (#.Some (lua.+ (lua.int 1) index)))
                    (lua.cond! (list [(lua.= "index_min_length" (lua.length product))
                                      ## Last element.
                                      (lua.return! (lua.nth "index_min_length" product))]
                                     [(lua.< "index_min_length" (lua.length product))
                                      ## Needs recursion
                                      (lua.return! (product//right (lua.nth (lua.length product)
                                                                            product)
                                                                   (lua.- (lua.length product)
                                                                          "index_min_length")))])
                               ## Must slice
                               (lua.return! (array//sub product "index_min_length" (lua.length product)))))))

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

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

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

               no-match!)))

(def: runtime//adt
  Runtime
  (format @@product//left
          @@product//right
          @@sum//get))

(runtime: (bit//logical-right-shift param subject)
  (let [mask (|> (lua.int 1)
                 (lua.bit-shl (lua.- param (lua.int 64)))
                 (lua.- (lua.int 1)))]
    (lua.return! (|> subject
                     (lua.bit-shr param)
                     (lua.bit-and mask)))))

(def: runtime//bit
  Runtime
  @@bit//logical-right-shift)

(runtime: (text//index subject param start)
  (lua.block! (list (lua.local! "idx" (#.Some (lua.apply "string.find" (list subject param start (lua.bool #1)))))
                    (lua.if! (lua.= lua.nil "idx")
                             (lua.return! none)
                             (lua.return! (some "idx"))))))

(runtime: (text//clip text from to)
  (lua.block! (list (lua.local! "size" (#.Some (lua.apply "string.len" (list text))))
                    (lua.if! (lua.or (lua.> "size" from)
                                     (lua.> "size" to))
                             (lua.return! none)
                             (lua.return! (some (lua.apply "string.sub" (list text from to))))))))

(runtime: (text//char text idx)
  (lua.block! (list (lua.local! "char" (#.Some (lua.apply "string.byte" (list text idx))))
                    (lua.if! (lua.= lua.nil "char")
                             (lua.return! none)
                             (lua.return! (some "char"))))))

(def: runtime//text
  Runtime
  (format @@text//index
          @@text//clip
          @@text//char))

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

(runtime: (array//new size)
  (lua.block! (list (lua.local! "output" (#.Some (lua.array (list))))
                    (lua.for-step! "idx" (lua.int 1) size (lua.int 1)
                                   (lua.apply "table.insert" (list "output" unit)))
                    (lua.return! "output"))))

(runtime: (array//get array idx)
  (<| (check-index-out-of-bounds array idx)
      (lua.block! (list (lua.local! "temp" (#.Some (lua.nth idx array)))
                        (lua.if! (lua.or (lua.= lua.nil "temp")
                                         (lua.= unit "temp"))
                                 (lua.return! none)
                                 (lua.return! (some "temp")))))))

(runtime: (array//put array idx value)
  (<| (check-index-out-of-bounds array idx)
      (lua.block! (list (lua.set! (lua.nth idx array) value)
                        (lua.return! array)))))

(def: runtime//array
  Runtime
  (format @@array//sub
          @@array//concat
          @@array//copy
          @@array//new
          @@array//get
          @@array//put
          ))

(runtime: (box//write value box)
  (lua.block! (list (lua.set! (lua.nth (lua.int 0) box)
                              value)
                    (lua.return! unit))))

(def: runtime//box
  Runtime
  (format @@box//write))

(runtime: (lua//get object field)
  (lua.block! (list (lua.local! "value" (#.Some (lua.nth field object)))
                    (lua.if! (lua.= lua.nil "value")
                             (lua.return! none)
                             (lua.return! (some "value"))))))

(runtime: (lua//set object field value)
  (lua.block! (list (lua.set! (lua.nth field object) value)
                    (lua.return! object))))

(def: runtime//lua
  Runtime
  (format @@lua//get
          @@lua//set))

(def: runtime
  Runtime
  (format runtime//lux
          runtime//adt
          runtime//bit
          runtime//text
          runtime//array
          runtime//box
          runtime//lua))

(def: #export artifact Text (format prefix ".lua"))

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