aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/js/procedure/host.jvm.lux
blob: 00c2429a452f419440a7f909cd64c43ce3200ae8 (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
(.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]))
  [///]
  (/// [".T" runtime])
  (// ["@" common]))

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

  [js//null      "null"]
  [js//undefined "undefined"]
  [js//object    "{}"]
  )

(def: (js//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: (js//call proc translate inputs)
  (-> Text @.Proc)
  (case inputs
    (^ (list& functionS argsS+))
    (do macro.Monad<Meta>
      [functionJS (translate functionS)
       argsJS+ (monad.map @ translate argsS+)]
      (wrap (format "(" functionJS ")("
                    (text.join-with "," argsJS+)
                    ")")))

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

(def: js-procs
  @.Bundle
  (|> (dict.new text.Hash<Text>)
      (@.install "null" (@.nullary js//null))
      (@.install "undefined" (@.nullary js//undefined))
      (@.install "object" (@.nullary js//object))
      (@.install "global" js//global)
      (@.install "call" js//call)))

(def: (object//new proc translate inputs)
  (-> Text @.Proc)
  (case inputs
    (^ (list& constructorS argsS+))
    (do macro.Monad<Meta>
      [constructorJS (translate constructorS)
       argsJS+ (monad.map @ translate argsS+)]
      (wrap (format "new (" constructorJS ")("
                    (text.join-with "," argsJS+)
                    ")")))

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

(def: (object//call proc translate inputs)
  (-> Text @.Proc)
  (case inputs
    (^ (list& objectS fieldS argsS+))
    (do macro.Monad<Meta>
      [objectJS (translate objectS)
       fieldJS (translate fieldS)
       argsJS+ (monad.map @ translate argsS+)]
      (wrap (format runtimeT.js//call
                    "(" objectJS
                    "," fieldJS
                    "," "[" (text.join-with "," argsJS+) "]"
                    ")")))

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

(def: (object//get [fieldJS objectJS])
  @.Binary
  (format runtimeT.js//get "(" objectJS "," fieldJS ")"))

(def: (object//set [fieldJS valueJS objectJS])
  @.Trinary
  (format runtimeT.js//set "(" objectJS "," fieldJS "," valueJS ")"))

(def: (object//delete [fieldJS objectJS])
  @.Binary
  (format runtimeT.js//delete "(" objectJS "," fieldJS ")"))

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

(def: (array//literal elementsJS+)
  @.Variadic
  (format "[" (text.join-with "," elementsJS+) "]"))

(def: (array//read [indexJS arrayJS])
  @.Binary
  (format runtimeT.array//get "(" arrayJS "," indexJS ")"))

(def: (array//write [indexJS valueJS arrayJS])
  @.Trinary
  (format runtimeT.array//put "(" arrayJS "," indexJS "," valueJS ")"))

(def: (array//delete [indexJS arrayJS])
  @.Binary
  (format runtimeT.array//remove "(" arrayJS "," indexJS ")"))

(def: (array//length arrayJS)
  @.Unary
  (format arrayJS ".length"))

(def: array-procs
  @.Bundle
  (<| (@.prefix "array")
      (|> (dict.new text.Hash<Text>)
          (@.install "literal" (@.variadic array//literal))
          (@.install "read" (@.binary array//read))
          (@.install "write" (@.trinary array//write))
          (@.install "delete" (@.binary array//delete))
          (@.install "length" (@.unary array//length))
          )))

(def: #export procedures
  @.Bundle
  (<| (@.prefix "js")
      (|> js-procs
          (dict.merge object-procs)
          (dict.merge array-procs)
          )))