-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchktyp.lua
49 lines (47 loc) · 1.11 KB
/
chktyp.lua
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
local function check(c, v)
-- support Lua <5.3
local mathtype = math.type or function(n)
if type(n) ~= "number" then return type(n) end
return tostring(n):match"^%-?%d+$" and "integer" or "float"
end
local t = {
s = {type, "string"},
b = {type, "boolean"},
n = {type, "number"},
i = {mathtype, "integer"},
t = {type, "table"},
f = {type, "function"},
}
assert(t[c], "type specifier '" .. c .. "' unknown")
return t[c][1](v) == t[c][2]
end
---@param types string
local function chktyp(types, ...)
assert(type(types) == "string")
local j = 1
local c
local function nextprint()
repeat
c = types:sub(j,j)
j = j + 1
until c ~= " "
end
nextprint()
for i = 1, select("#", ...) do
local v = select(i, ...)
local ok = check(c, v)
nextprint()
while c == "|" do
nextprint()
ok = ok or check(c, v)
nextprint()
end
if c == "?" then
ok = ok or (v == nil)
nextprint()
end
assert(ok, "type mismatch")
end
assert(j == #types + 2, "not enough arguments provided based on types string")
end
return chktyp