aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/lua/procedure/host.jvm.lux
blob: 2271d354a0c55a715d81a779d41df07da240eb6d (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
(.module:
  lux
  (lux (control [monad #+ do])
       (data [text]
             text/format
             (coll [list "list/" Functor<List>]
                   (dictionary ["dict" unordered #+ Dict])))
       [macro "macro/" Monad<Meta>])
  (luxc ["&" lang]
        (lang ["la" analysis]
              ["ls" synthesis]
              (host [lua #+ Lua Expression Statement])))
  [///]
  (/// [".T" runtime])
  (// ["@" common]))

(do-template [<name> <lua>]
  [(def: (<name> _) @.Nullary <lua>)]

  [lua//nil      "nil"]
  [lua//table    "{}"]
  )

(def: (lua//global proc translate inputs)
  (-> Text @.Proc)
  (case inputs
    (^ (list [_ (#.Text name)]))
    (do macro.Monad<Meta>
      []
      (wrap name))

    _
    (&.throw @.Wrong-Syntax (@.wrong-syntax proc inputs))))

(def: (lua//call proc translate inputs)
  (-> Text @.Proc)
  (case inputs
    (^ (list& functionS argsS+))
    (do macro.Monad<Meta>
      [functionO (translate functionS)
       argsO+ (monad.map @ translate argsS+)]
      (wrap (lua.apply functionO argsO+)))

    _
    (&.throw @.Wrong-Syntax (@.wrong-syntax proc inputs))))

(def: lua-procs
  @.Bundle
  (|> (dict.new text.Hash<Text>)
      (@.install "nil" (@.nullary lua//nil))
      (@.install "table" (@.nullary lua//table))
      (@.install "global" lua//global)
      (@.install "call" lua//call)))

(def: (table//call proc translate inputs)
  (-> Text @.Proc)
  (case inputs
    (^ (list& tableS [_ (#.Text field)] argsS+))
    (do macro.Monad<Meta>
      [tableO (translate tableS)
       argsO+ (monad.map @ translate argsS+)]
      (wrap (lua.method field tableO argsO+)))

    _
    (&.throw @.Wrong-Syntax (@.wrong-syntax proc inputs))))

(def: (table//get [fieldO tableO])
  @.Binary
  (runtimeT.lua//get tableO fieldO))

(def: (table//set [fieldO valueO tableO])
  @.Trinary
  (runtimeT.lua//set tableO fieldO valueO))

(def: table-procs
  @.Bundle
  (<| (@.prefix "table")
      (|> (dict.new text.Hash<Text>)
          (@.install "call" table//call)
          (@.install "get" (@.binary table//get))
          (@.install "set" (@.trinary table//set)))))

(def: #export procedures
  @.Bundle
  (<| (@.prefix "lua")
      (|> lua-procs
          (dict.merge table-procs))))