-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
defer_test.nelua
159 lines (143 loc) · 2.37 KB
/
defer_test.nelua
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
require 'string'
local sb: stringbuilder
local s: string
-- return
local function f(sb: *stringbuilder)
sb:clear()
sb:write'a'
defer sb:write'd' end
defer sb:write'c' end
sb:write'b'
return
end
f(&sb)
assert(sb:view() == 'abcd')
-- do block
local function f(sb: *stringbuilder)
sb:clear()
sb:write'a'
defer sb:write'd' end
do
defer sb:write'b' end
end
sb:write'c'
end
f(&sb)
assert(sb:view() == 'abcd')
-- nested do blocks
local function f(sb: *stringbuilder)
sb:clear()
sb:write'a'
defer sb:write'e' end
do
defer sb:write'c' end
do
defer sb:write'b' end
end
end
sb:write'd'
end
f(&sb)
assert(sb:view() == 'abcde')
-- return in middle nested
local function f(sb: *stringbuilder)
sb:clear()
sb:write'a'
defer sb:write'd' end
if true then
defer sb:write'c' end
if true then
defer sb:write'b' end
end
return
end
end
f(&sb)
assert(sb:view() == 'abcd')
-- middle return
local function f(sb: *stringbuilder)
sb:clear()
sb:write'a'
defer sb:write'd' end
if true then
defer sb:write'c' end
return
if true then
defer sb:write'b' end
end
end
end
f(&sb)
assert(sb:view() == 'acd')
-- while and break
local function f(sb: *stringbuilder)
sb:clear()
sb:write'a'
defer sb:write'c' end
while true do
defer sb:write'b' end
break
end
end
f(&sb)
assert(sb:view() == 'abc')
-- for and continue
local function f(sb: *stringbuilder)
sb:clear()
sb:write'a'
defer sb:write'c' end
for i=1,3 do
defer sb:write'b' end
continue
defer sb:write' ' end
end
end
f(&sb)
assert(sb:view() == 'abbbc')
-- repeat
local function f(sb: *stringbuilder)
sb:clear()
sb:write'a'
defer sb:write'c' end
repeat
defer sb:write'b' end
local x = true
until x
end
f(&sb)
assert(sb:view() == 'abc')
sb:destroy()
do -- issue #46
local n = 0
do
defer n = n+1 end
local r = (do
in 0
end)
n = n + r
end
assert(n == 1)
n = 0
do
defer n = n+1 end
local function x()
return 0
end
local r = x()
end
assert(n == 1)
end
do -- issue #233
local function f(n: integer): integer
defer n = n + 1 end
return n
end
local function g(n: integer, m: integer): (integer, integer)
defer n = n + 1 m = m + 1 end
return n, m
end
assert(f(1) == 1)
local n, m = g(1,2)
assert(n == 1 and m == 2)
end
print 'defer OK!'