-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtuple.lua
73 lines (56 loc) · 1.78 KB
/
tuple.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- Example of compile-time tuple type
-- that expands to multiple parameters and return values
require 'macro'
macro.do_()
function post_process(file_contents)
local result = file_contents
local function expand_index(a, index)
return a .. "_" .. index
end
local function expand_tuple(a)
return expand_index(a,1), expand_index(a,2), expand_index(a,3)
end
-- NB: We use tuple_ prefix to identify tuple types
-- You could expand this to use a special character like $ if your language server allows it
result = result:gsub("---@param tuple_(%w+) tuple3(%b<>)", function(a, b)
local type = b:sub(2,-2)
local a1, a2, a3 = expand_tuple(a)
local p = "---@param %s " .. type .. "\n"
return p:format(a1) .. p:format(a2) .. p:format(a3):sub(1,-2)
end)
result = result:gsub("tuple3(%b<>)", function(a)
local type = a:sub(2,-2)
return type .. ", " .. type .. ", " .. type
end)
result = result:gsub("tuple_(%w+)(%b[])", function(a,b)
return expand_index(a, b:sub(2,-2))
end)
result = result:gsub("tuple_(%w+)", function(a)
return table.concat({expand_tuple(a)}, ", ")
end)
result = result:gsub("tuple3(%b())", function(a)
return a:sub(2,-2) .. " --[[ tuple ]]"
end)
return result
end
---Mock tuple3 type and constructor
-- used to satisfy lua IDE and type checking
-- but removed at build-time
---@alias tuple3<T> T[]
---@generic T
---@return tuple3<T>
local function tuple3(a,b,c)
return {a, b, c}
end
macro.end_()
---@generic T
---@param tuple_a tuple3<T>
---@param tuple_b tuple3<T>
---@return tuple3<T>
local function vec3_add(tuple_a, tuple_b)
return tuple3(tuple_a[1] + tuple_b[1], tuple_a[2] + tuple_b[2], tuple_a[3] + tuple_b[3])
end
local tuple_x = tuple3(1, 0, 0)
local tuple_y = tuple3(0, 1, 0)
local tuple_sum = vec3_add(tuple_x, tuple_y)
print("sum: ", tuple_sum)