forked from terralang/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cond.t
31 lines (29 loc) · 814 Bytes
/
cond.t
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
local tunpack = unpack or table.unpack
return macro(function(...)
local args = {...}
local function build(store, condition, expression, ...)
if expression == nil then condition, expression = expression, condition end
local subargs = {...}
if condition ~= nil then
return quote
if [condition] then
[store] = [expression]
else
[build(store, tunpack(subargs))]
end
end
else
return quote
[store] = [expression]
end
end
end
local _, expr1 = ...
local store = symbol(expr1:gettype(), "condres")
return quote
var [store]
[build(store, tunpack(args))]
in
[store]
end
end)