aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/host/lua.lux
blob: ec2fa8928455e7df5325b9aaf9cd0a1a4ad3136d (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
(.module:
  [lux #- not or and function]
  (lux (data [text]
             text/format
             (coll [list "list/" Functor<List> Fold<List>]))))

(type: #export Lua Text)

(type: #export Expression Lua)

(type: #export Statement Lua)

(def: #export nil
  Expression
  "nil")

(def: #export bool
  (-> Bit Expression)
  %b)

(def: #export int
  (-> Int Expression)
  %i)

(def: #export float
  (-> Frac Expression)
  %f)

(def: #export (string value)
  (-> Text Expression)
  (%t value))

(def: #export (array elements)
  (-> (List Expression) Expression)
  (format "{" (text.join-with "," elements) "}"))

(def: #export (nth idx array)
  (-> Expression Expression Expression)
  (format "(" array ")[" idx "]"))

(def: #export (length array)
  (-> Expression Expression)
  (format "#(" array ")"))

(def: #export (apply func args)
  (-> Expression (List Expression) Expression)
  (format func "(" (text.join-with "," args) ")"))

(def: #export (method field table args)
  (-> Text Expression (List Expression) Expression)
  (format table ":" field "(" (text.join-with "," args) ")"))

(def: #export (local! name value)
  (-> Text (Maybe Expression) Statement)
  (case value
    #.None
    (format "local " name ";")
    
    (#.Some value)
    (format "local " name " = " value ";")))

(def: #export (global! name value)
  (-> Text (Maybe Expression) Statement)
  (case value
    #.None
    (format name ";")
    
    (#.Some value)
    (format name " = " value ";")))

(def: #export (set! name value)
  (-> Text Expression Statement)
  (format name " = " value ";"))

(def: #export (if! test then! else!)
  (-> Expression Statement Statement Statement)
  (format "if " test
          " then " then!
          " else " else!
          " end;"))

(def: #export (when! test then!)
  (-> Expression Statement Statement)
  (format "if " test
          " then " then!
          " end;"))

(def: #export (cond! clauses else!)
  (-> (List [Expression Statement]) Statement Statement)
  (list/fold (.function (_ [test then!] next!)
               (if! test then! next!))
             else!
             (list.reverse clauses)))

(def: #export (block! statements)
  (-> (List Statement) Statement)
  (text.join-with " " statements))

(def: #export (while! test body)
  (-> Expression Statement Statement)
  (format "while " test " do " body " end;"))

(def: #export (for-in! vars source body)
  (-> (List Text) Expression Statement Statement)
  (format "for " (text.join-with "," vars) " in " source
          " do " body " end;"))

(def: #export (for-step! var from to step body)
  (-> Text Expression Expression Expression Statement
      Statement)
  (format "for " var " = " from ", " to ", " step
          " do " body " end;"))

(def: #export (error message)
  (-> Expression Expression)
  (apply "error" (list message)))

(def: #export (return! value)
  (-> Expression Statement)
  (format "return " value ";"))

(def: #export (function args body)
  (-> (List Text) Statement Expression)
  (format "("
          (format "function " (format "(" (text.join-with ", " args) ")")
                  " "
                  body
                  " end")
          ")"))

(def: #export (function! name args body)
  (-> Text (List Text) Statement Expression)
  (format "function " name (format "(" (text.join-with ", " args) ")")
          " "
          body
          " end;"))

(def: #export (table fields)
  (-> (List [Text Expression]) Expression)
  (format "{"
          (|> fields
              (list/map (.function (_ [key val])
                          (format key " = " val)))
              (text.join-with ", "))
          "}"))

(do-template [<name> <op>]
  [(def: #export (<name> param subject)
     (-> Expression Expression Expression)
     (format "(" subject " " <op> " " param ")"))]

  [=       "=="]
  [<       "<"]
  [<=      "<="]
  [>       ">"]
  [>=      ">="]
  [+       "+"]
  [-       "-"]
  [*       "*"]
  [/       "/"]
  [//      "//"]
  [%       "%"]
  )

(do-template [<name> <op>]
  [(def: #export (<name> param subject)
     (-> Expression Expression Expression)
     (format "(" param " " <op> " " subject ")"))]

  [or      "or"]
  [and     "and"]
  [bit-or  "|"]
  [bit-and "&"]
  [bit-xor "~"]
  )

(do-template [<name> <op>]
  [(def: #export (<name> param subject)
     (-> Expression Expression Expression)
     (format "(" subject " " <op> " " param ")"))]

  [bit-shl "<<"]
  [bit-shr ">>"]
  )

(def: #export (not subject)
  (-> Expression Expression)
  (format "(not " subject ")"))