@tilua/compiler 2.3.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,179 @@
1
+ local __NAME__ = {}
2
+
3
+ function __NAME__.find(t, test)
4
+ for i = 1, #t do
5
+ if test(t[i], i) then
6
+ return t[i]
7
+ end
8
+ end
9
+ return nil
10
+ end
11
+
12
+ function __NAME__.findIndex(t, test)
13
+ for i = 1, #t do
14
+ if test(t[i], i) then
15
+ return i
16
+ end
17
+ end
18
+ return nil
19
+ end
20
+
21
+ function __NAME__.filter(t, test)
22
+ local out = {}
23
+ for i = 1, #t do
24
+ if test(t[i], i) then
25
+ out[#out + 1] = t[i]
26
+ end
27
+ end
28
+ return out
29
+ end
30
+
31
+ function __NAME__.map(t, transform)
32
+ local out = {}
33
+ for i = 1, #t do
34
+ out[i] = transform(t[i], i)
35
+ end
36
+ return out
37
+ end
38
+
39
+ function __NAME__.forEach(t, visit)
40
+ for i = 1, #t do
41
+ visit(t[i], i)
42
+ end
43
+ end
44
+
45
+ function __NAME__.some(t, test)
46
+ for i = 1, #t do
47
+ if test(t[i], i) then
48
+ return true
49
+ end
50
+ end
51
+ return false
52
+ end
53
+
54
+ function __NAME__.every(t, test)
55
+ for i = 1, #t do
56
+ if not test(t[i], i) then
57
+ return false
58
+ end
59
+ end
60
+ return true
61
+ end
62
+
63
+ function __NAME__.reduce(t, step, initial)
64
+ local total = initial
65
+ for i = 1, #t do
66
+ total = step(total, t[i], i)
67
+ end
68
+ return total
69
+ end
70
+
71
+ function __NAME__.includes(t, value)
72
+ for i = 1, #t do
73
+ if t[i] == value then
74
+ return true
75
+ end
76
+ end
77
+ return false
78
+ end
79
+
80
+ function __NAME__.indexOf(t, value, start)
81
+ for i = start or 1, #t do
82
+ if t[i] == value then
83
+ return i
84
+ end
85
+ end
86
+ return nil
87
+ end
88
+
89
+ function __NAME__.join(t, separator)
90
+ local out = {}
91
+ for i = 1, #t do
92
+ out[i] = tostring(t[i])
93
+ end
94
+ return table.concat(out, separator or ",")
95
+ end
96
+
97
+ function __NAME__.concat(t, ...)
98
+ local out = {}
99
+ table.move(t, 1, #t, 1, out)
100
+ for i = 1, select("#", ...) do
101
+ local part = select(i, ...)
102
+ table.move(part, 1, #part, #out + 1, out)
103
+ end
104
+ return out
105
+ end
106
+
107
+ -- An inclusive range, as `string.sub` takes: `slice(2, 3)` is the second and
108
+ -- third elements, and a negative counts back from the last.
109
+ function __NAME__.slice(t, start, stop)
110
+ local n = #t
111
+ local from = start or 1
112
+ local to = stop or n
113
+ if from < 0 then from = n + from + 1 end
114
+ if to < 0 then to = n + to + 1 end
115
+ if from < 1 then from = 1 end
116
+ if to > n then to = n end
117
+ local out = {}
118
+ for i = from, to do
119
+ out[#out + 1] = t[i]
120
+ end
121
+ return out
122
+ end
123
+
124
+ -- One level, as JavaScript's `flat()` does by default.
125
+ function __NAME__.flat(t)
126
+ local out = {}
127
+ for i = 1, #t do
128
+ local value = t[i]
129
+ if type(value) == "table" then
130
+ table.move(value, 1, #value, #out + 1, out)
131
+ else
132
+ out[#out + 1] = value
133
+ end
134
+ end
135
+ return out
136
+ end
137
+
138
+ function __NAME__.reverse(t)
139
+ local n = #t
140
+ for i = 1, n // 2 do
141
+ t[i], t[n - i + 1] = t[n - i + 1], t[i]
142
+ end
143
+ return t
144
+ end
145
+
146
+ function __NAME__.sort(t, compare)
147
+ if compare then
148
+ table.sort(t, compare)
149
+ else
150
+ table.sort(t)
151
+ end
152
+ return t
153
+ end
154
+
155
+ function __NAME__.push(t, ...)
156
+ local n = #t
157
+ for i = 1, select("#", ...) do
158
+ t[n + i] = select(i, ...)
159
+ end
160
+ return #t
161
+ end
162
+
163
+ function __NAME__.pop(t)
164
+ local n = #t
165
+ local last = t[n]
166
+ t[n] = nil
167
+ return last
168
+ end
169
+
170
+ function __NAME__.shift(t)
171
+ return table.remove(t, 1)
172
+ end
173
+
174
+ function __NAME__.unshift(t, ...)
175
+ for i = select("#", ...), 1, -1 do
176
+ table.insert(t, 1, (select(i, ...)))
177
+ end
178
+ return #t
179
+ end
@@ -0,0 +1,54 @@
1
+ local __NAME__ = {}
2
+
3
+ -- `known` is the object's keys in the order its type wrote them, handed over
4
+ -- by the lowering; `open` says the type also has an indexer, whose keys
5
+ -- follow the named ones. With no one order to give (`known` is nil), every
6
+ -- key is listed, in the order `pairs` walks them.
7
+ local function keysOf(known, open, t)
8
+ local out = {}
9
+ if known == nil then
10
+ for key in pairs(t) do
11
+ out[#out + 1] = key
12
+ end
13
+ return out
14
+ end
15
+ for i = 1, #known do
16
+ out[i] = known[i]
17
+ end
18
+ if open then
19
+ local listed = {}
20
+ for i = 1, #known do
21
+ listed[known[i]] = true
22
+ end
23
+ for key in pairs(t) do
24
+ if not listed[key] then
25
+ out[#out + 1] = key
26
+ end
27
+ end
28
+ end
29
+ return out
30
+ end
31
+
32
+ function __NAME__.keys(known, open, t)
33
+ return keysOf(known, open, t)
34
+ end
35
+
36
+ -- Indexed, not appended: a missing optional member leaves its place `nil`
37
+ -- rather than moving everything after it up one.
38
+ function __NAME__.values(known, open, t)
39
+ local keys = keysOf(known, open, t)
40
+ local out = {}
41
+ for i = 1, #keys do
42
+ out[i] = t[keys[i]]
43
+ end
44
+ return out
45
+ end
46
+
47
+ function __NAME__.entries(known, open, t)
48
+ local keys = keysOf(known, open, t)
49
+ local out = {}
50
+ for i = 1, #keys do
51
+ out[i] = { keys[i], t[keys[i]] }
52
+ end
53
+ return out
54
+ end
@@ -0,0 +1,119 @@
1
+ local __NAME__ = {}
2
+
3
+ function __NAME__.trim(s)
4
+ return (string.gsub(string.gsub(s, "^%s+", ""), "%s+$", ""))
5
+ end
6
+
7
+ function __NAME__.trimStart(s)
8
+ return (string.gsub(s, "^%s+", ""))
9
+ end
10
+
11
+ function __NAME__.trimEnd(s)
12
+ return (string.gsub(s, "%s+$", ""))
13
+ end
14
+
15
+ function __NAME__.startsWith(s, text)
16
+ return string.sub(s, 1, #text) == text
17
+ end
18
+
19
+ function __NAME__.endsWith(s, text)
20
+ return #text == 0 or string.sub(s, -#text) == text
21
+ end
22
+
23
+ function __NAME__.includes(s, text)
24
+ return string.find(s, text, 1, true) ~= nil
25
+ end
26
+
27
+ function __NAME__.indexOf(s, text, start)
28
+ local at = string.find(s, text, start or 1, true)
29
+ return at
30
+ end
31
+
32
+ function __NAME__.slice(s, start, stop)
33
+ return string.sub(s, start or 1, stop or -1)
34
+ end
35
+
36
+ -- Plain text, not a pattern: `replace("a.b", ".", "-")` changes the dot.
37
+ function __NAME__.replace(s, text, replacement)
38
+ if text == "" then
39
+ return replacement .. s
40
+ end
41
+ local at, to = string.find(s, text, 1, true)
42
+ if not at then
43
+ return s
44
+ end
45
+ return string.sub(s, 1, at - 1) .. replacement .. string.sub(s, to + 1)
46
+ end
47
+
48
+ function __NAME__.replaceAll(s, text, replacement)
49
+ if text == "" then
50
+ return s
51
+ end
52
+ local out = {}
53
+ local at = 1
54
+ while true do
55
+ local from, to = string.find(s, text, at, true)
56
+ if not from then
57
+ break
58
+ end
59
+ out[#out + 1] = string.sub(s, at, from - 1)
60
+ out[#out + 1] = replacement
61
+ at = to + 1
62
+ end
63
+ out[#out + 1] = string.sub(s, at)
64
+ return table.concat(out)
65
+ end
66
+
67
+ function __NAME__.padStart(s, length, padding)
68
+ local pad = padding or " "
69
+ if #s >= length or pad == "" then
70
+ return s
71
+ end
72
+ local filler = string.rep(pad, math.ceil((length - #s) / #pad))
73
+ return string.sub(filler, 1, length - #s) .. s
74
+ end
75
+
76
+ function __NAME__.padEnd(s, length, padding)
77
+ local pad = padding or " "
78
+ if #s >= length or pad == "" then
79
+ return s
80
+ end
81
+ local filler = string.rep(pad, math.ceil((length - #s) / #pad))
82
+ return s .. string.sub(filler, 1, length - #s)
83
+ end
84
+
85
+ -- Lua's own, where Lua answers several values and tilua one: the positions
86
+ -- and captures as an array, or nil; the new string without the count.
87
+ local function values(...)
88
+ if select("#", ...) <= 1 and (...) == nil then
89
+ return nil
90
+ end
91
+ return { ... }
92
+ end
93
+
94
+ function __NAME__.find(s, pattern, init, plain)
95
+ return values(string.find(s, pattern, init, plain))
96
+ end
97
+
98
+ function __NAME__.match(s, pattern, init)
99
+ return values(string.match(s, pattern, init))
100
+ end
101
+
102
+ function __NAME__.gsub(s, pattern, replacement, n)
103
+ return (string.gsub(s, pattern, replacement, n))
104
+ end
105
+
106
+ function __NAME__.byte(s, i, j)
107
+ if j == nil then
108
+ return (string.byte(s, i))
109
+ end
110
+ return { string.byte(s, i, j) }
111
+ end
112
+
113
+ -- An iterator function: each call is one match's captures, as an array.
114
+ function __NAME__.gmatch(s, pattern)
115
+ local step = string.gmatch(s, pattern)
116
+ return function()
117
+ return values(step())
118
+ end
119
+ end