aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/lua/procedure/common.jvm.lux
blob: 2f1b652e3e17d9c3cfbacab42f81f826fe8e1749 (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
364
365
366
367
368
369
370
371
372
373
374
(.module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:]
                ["p" parser])
       (data ["e" error]
             [text]
             text/format
             (coll [list "list/" Functor<List>]
                   (dictionary ["dict" unordered #+ Dict])))
       [macro #+ with-gensyms]
       (macro [code]
              ["s" syntax #+ syntax:])
       [host])
  (luxc ["&" lang]
        (lang ["la" analysis]
              ["ls" synthesis]
              (host [lua #+ Lua Expression Statement])))
  [///]
  (/// [".T" runtime]
       [".T" case]
       [".T" function]
       [".T" loop]))

## [Types]
(type: #export Translator
  (-> ls.Synthesis (Meta Expression)))

(type: #export Proc
  (-> Translator (List ls.Synthesis) (Meta Expression)))

(type: #export Bundle
  (Dict Text Proc))

(syntax: (Vector {size s.nat} elemT)
  (wrap (list (` [(~+ (list.repeat size elemT))]))))

(type: #export Nullary (-> (Vector +0 Expression) Expression))
(type: #export Unary   (-> (Vector +1 Expression) Expression))
(type: #export Binary  (-> (Vector +2 Expression) Expression))
(type: #export Trinary (-> (Vector +3 Expression) Expression))
(type: #export Variadic (-> (List Expression) Expression))

## [Utils]
(def: #export (install name unnamed)
  (-> Text (-> Text Proc)
      (-> Bundle Bundle))
  (dict.put name (unnamed name)))

(def: #export (prefix prefix bundle)
  (-> Text Bundle Bundle)
  (|> bundle
      dict.entries
      (list/map (function (_ [key val]) [(format prefix " " key) val]))
      (dict.from-list text.Hash<Text>)))

(def: (wrong-arity proc expected actual)
  (-> Text Nat Nat Text)
  (format "Wrong number of arguments for " (%t proc) "\n"
          "Expected: " (|> expected .int %i) "\n"
          "  Actual: " (|> actual .int %i)))

(syntax: (arity: {name s.local-identifier} {arity s.nat})
  (with-gensyms [g_ g!proc g!name g!translate g!inputs]
    (do @
      [g!input+ (monad.seq @ (list.repeat arity (macro.gensym "input")))]
      (wrap (list (` (def: #export ((~ (code.local-identifier name)) (~ g!proc))
                       (-> (-> (..Vector (~ (code.nat arity)) Expression) Expression)
                           (-> Text ..Proc))
                       (function ((~ g_ ) (~ g!name))
                         (function ((~ g_ ) (~ g!translate) (~ g!inputs))
                           (case (~ g!inputs)
                             (^ (list (~+ g!input+)))
                             (do macro.Monad<Meta>
                               [(~+ (|> g!input+
                                        (list/map (function (_ g!input)
                                                    (list g!input (` ((~ g!translate) (~ g!input))))))
                                        list.concat))]
                               ((~' wrap) ((~ g!proc) [(~+ g!input+)])))

                             (~' _)
                             (macro.fail (wrong-arity (~ g!name) +1 (list.size (~ g!inputs))))))))))))))

(arity: nullary +0)
(arity: unary +1)
(arity: binary +2)
(arity: trinary +3)

(def: #export (variadic proc)
  (-> Variadic (-> Text Proc))
  (function (_ proc-name)
    (function (_ translate inputsS)
      (do macro.Monad<Meta>
        [inputsI (monad.map @ translate inputsS)]
        (wrap (proc inputsI))))))

## [Procedures]
## [[Lux]]
(def: (lux//is [leftO rightO])
  Binary
  (lua.= leftO rightO))

(def: (lux//if [testO thenO elseO])
  Trinary
  (caseT.translate-if testO thenO elseO))

(def: (lux//try riskyO)
  Unary
  (runtimeT.lux//try riskyO))

(exception: #export (Wrong-Syntax {message Text})
  message)

(def: #export (wrong-syntax procedure args)
  (-> Text (List ls.Synthesis) Text)
  (format "Procedure: " procedure "\n"
          "Arguments: " (%code (code.tuple args))))

(def: lux//loop
  (-> Text Proc)
  (function (_ proc-name)
    (function (_ translate inputsS)
      (case (s.run inputsS ($_ p.seq s.nat (s.tuple (p.many s.any)) s.any))
        (#e.Success [offset initsS+ bodyS])
        (loopT.translate-loop translate offset initsS+ bodyS)

        (#e.Error error)
        (&.throw Wrong-Syntax (wrong-syntax proc-name inputsS)))
      )))

(def: lux//recur
  (-> Text Proc)
  (function (_ proc-name)
    (function (_ translate inputsS)
      (loopT.translate-recur translate inputsS))))

## [[Bits]]
(template [<name> <op>]
  [(def: (<name> [subjectO paramO])
     Binary
     (<op> paramO subjectO))]

  [bit//and lua.bit-and]
  [bit//or  lua.bit-or]
  [bit//xor lua.bit-xor]
  )

(template [<name> <op>]
  [(def: (<name> [subjectO paramO])
     Binary
     (<op> paramO subjectO))]

  [bit//left-shift           lua.bit-shl]
  [bit//arithmetic-right-shift          lua.bit-shr]
  [bit//logical-right-shift  runtimeT.bit//logical-right-shift]
  )

## [[Numbers]]
(host.import: java/lang/Double
  (#static MIN_VALUE Double)
  (#static MAX_VALUE Double)
  (#static NaN Double)
  (#static POSITIVE_INFINITY Double)
  (#static NEGATIVE_INFINITY Double))

(template [<name> <const> <encode>]
  [(def: (<name> _)
     Nullary
     (<encode> <const>))]

  [frac//smallest          Double::MIN_VALUE            lua.float]
  [frac//min               (f/* -1.0 Double::MAX_VALUE) lua.float]
  [frac//max               Double::MAX_VALUE            lua.float]
  )

(template [<name> <op>]
  [(def: (<name> [subjectO paramO])
     Binary
     (<op> paramO subjectO))]

  [int//add        lua.+]
  [int//sub        lua.-]
  [int//mul        lua.*]
  [int//div        lua.//]
  [int//rem        lua.%]
  )

(template [<name> <op>]
  [(def: (<name> [subjectO paramO])
     Binary
     (<op> paramO subjectO))]

  [frac//add lua.+]
  [frac//sub lua.-]
  [frac//mul lua.*]
  [frac//div lua./]
  [frac//rem lua.%]
  [frac//=   lua.=]
  [frac//<   lua.<]

  [text//=   lua.=]
  [text//<   lua.<]
  )

(template [<name> <cmp>]
  [(def: (<name> [subjectO paramO])
     Binary
     (<cmp> paramO subjectO))]

  [int//= lua.=]
  [int//< lua.<]
  )

(def: frac//encode
  Unary
  (|>> (list) (lua.apply "tostring")))

(def: (frac//decode inputO)
  Unary
  (lux//try (lua.function (list)
              (lua.return! (lua.apply "tonumber" (list inputO))))))

(template [<name> <divisor>]
  [(def: (<name> inputO)
     Unary
     (lua./ <divisor> inputO))]

  [int//to-frac (lua.float 1.0)]
  )

(template [<name> <transform>]
  [(def: (<name> inputO)
     Unary
     (|> inputO <transform>))]

  [frac//to-int (<| (lua.apply "math.floor") (list))]
  )

(def: int//char
  Unary
  (|>> (list) (lua.apply "string.char")))

## [[Text]]
(template [<name> <op>]
  [(def: <name>
     Unary
     (|>> (list) (lua.apply <op>)))]

  [text//size  "string.len"]
  )

(def: (text//concat [subjectO paramO])
  Binary
  (format "(" subjectO " .. " paramO ")"))

(def: (text//char [subjectO paramO])
  Binary
  (runtimeT.text//char subjectO paramO))

(template [<name> <runtime>]
  [(def: (<name> [subjectO paramO extraO])
     Trinary
     (<runtime> subjectO paramO extraO))]

  [text//clip  runtimeT.text//clip]
  [text//index runtimeT.text//index]
  )

## [[IO]]
(def: (io//log messageO)
  Unary
  (lua.or (lua.apply "print" (list messageO))
          runtimeT.unit))

(def: io//error
  Unary
  lua.error)

(def: io//exit
  Unary
  (|>> (list) (lua.apply "os.exit")))

(def: (io//current-time [])
  Nullary
  (|> (lua.apply "os.time" (list))
      (lua.* (lua.int 1,000))))

## [Bundles]
(def: lux-procs
  Bundle
  (|> (dict.new text.Hash<Text>)
      (install "is" (binary lux//is))
      (install "try" (unary lux//try))
      (install "if" (trinary lux//if))
      (install "loop" lux//loop)
      (install "recur" lux//recur)
      ))

(def: bit-procs
  Bundle
  (<| (prefix "bit")
      (|> (dict.new text.Hash<Text>)
          (install "and" (binary bit//and))
          (install "or" (binary bit//or))
          (install "xor" (binary bit//xor))
          (install "left-shift" (binary bit//left-shift))
          (install "logical-right-shift" (binary bit//logical-right-shift))
          (install "arithmetic-right-shift" (binary bit//arithmetic-right-shift))
          )))

(def: int-procs
  Bundle
  (<| (prefix "int")
      (|> (dict.new text.Hash<Text>)
          (install "+" (binary int//add))
          (install "-" (binary int//sub))
          (install "*" (binary int//mul))
          (install "/" (binary int//div))
          (install "%" (binary int//rem))
          (install "=" (binary int//=))
          (install "<" (binary int//<))
          (install "to-frac" (unary int//to-frac))
          (install "char" (unary int//char)))))

(def: frac-procs
  Bundle
  (<| (prefix "frac")
      (|> (dict.new text.Hash<Text>)
          (install "+" (binary frac//add))
          (install "-" (binary frac//sub))
          (install "*" (binary frac//mul))
          (install "/" (binary frac//div))
          (install "%" (binary frac//rem))
          (install "=" (binary frac//=))
          (install "<" (binary frac//<))
          (install "smallest" (nullary frac//smallest))
          (install "min" (nullary frac//min))
          (install "max" (nullary frac//max))
          (install "to-int" (unary frac//to-int))
          (install "encode" (unary frac//encode))
          (install "decode" (unary frac//decode)))))

(def: text-procs
  Bundle
  (<| (prefix "text")
      (|> (dict.new text.Hash<Text>)
          (install "=" (binary text//=))
          (install "<" (binary text//<))
          (install "concat" (binary text//concat))
          (install "index" (trinary text//index))
          (install "size" (unary text//size))
          (install "char" (binary text//char))
          (install "clip" (trinary text//clip))
          )))

(def: io-procs
  Bundle
  (<| (prefix "io")
      (|> (dict.new text.Hash<Text>)
          (install "log" (unary io//log))
          (install "error" (unary io//error))
          (install "exit" (unary io//exit))
          (install "current-time" (nullary io//current-time)))))

(def: #export procedures
  Bundle
  (<| (prefix "lux")
      (|> lux-procs
          (dict.merge bit-procs)
          (dict.merge int-procs)
          (dict.merge frac-procs)
          (dict.merge text-procs)
          (dict.merge io-procs)
          )))