aboutsummaryrefslogtreecommitdiff
path: root/testes/constructs.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-05-17 11:11:44 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-05-17 11:11:44 -0300
commitd9f40e3f6fb61650240c47d548bee69b24b07859 (patch)
treeab01022b3e3bc6bdb800423c97095a9423e0a798 /testes/constructs.lua
parent347d6961ac14213264c7176e3d125c9ba8475b01 (diff)
downloadlua-d9f40e3f6fb61650240c47d548bee69b24b07859.tar.gz
lua-d9f40e3f6fb61650240c47d548bee69b24b07859.tar.bz2
lua-d9f40e3f6fb61650240c47d548bee69b24b07859.zip
First implementation for 'const' variables
A variable can be declared const, which means it cannot be assigned to, with the syntax 'local <const> name = exp'.
Diffstat (limited to 'testes/constructs.lua')
-rw-r--r--testes/constructs.lua61
1 files changed, 59 insertions, 2 deletions
diff --git a/testes/constructs.lua b/testes/constructs.lua
index a83df79e..b91e0979 100644
--- a/testes/constructs.lua
+++ b/testes/constructs.lua
@@ -59,6 +59,41 @@ assert((x>y) and x or y == 2);
59 59
60assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) 60assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891)
61 61
62do -- testing operators with diffent kinds of constants
63 -- operands to consider:
64 -- * fit in register
65 -- * constant doesn't fit in register
66 -- * floats with integral values
67 local operand = {3, 100, 5.0, -10, -5.0, 10000, -10000}
68 local operator = {"+", "-", "*", "/", "//", "%", "^",
69 "&", "|", "^", "<<", ">>",
70 "==", "~=", "<", ">", "<=", ">=",}
71 for _, op in ipairs(operator) do
72 local f = assert(load(string.format([[return function (x,y)
73 return x %s y
74 end]], op)))();
75 for _, o1 in ipairs(operand) do
76 for _, o2 in ipairs(operand) do
77 local gab = f(o1, o2)
78
79 _ENV.XX = o1
80 code = string.format("return XX %s %s", op, o2)
81 res = assert(load(code))()
82 assert(res == gab)
83
84 _ENV.XX = o2
85 local code = string.format("return (%s) %s XX", o1, op)
86 local res = assert(load(code))()
87 assert(res == gab)
88
89 code = string.format("return (%s) %s %s", o1, op, o2)
90 res = assert(load(code))()
91 assert(res == gab)
92 end
93 end
94 end
95end
96
62 97
63-- silly loops 98-- silly loops
64repeat until 1; repeat until true; 99repeat until 1; repeat until true;
@@ -175,6 +210,28 @@ assert(a==1 and b==nil)
175 210
176print'+'; 211print'+';
177 212
213do -- testing constants
214 local <const> prog = [[local <XXX> x = 10]]
215 checkload(prog, "unknown attribute 'XXX'")
216
217 checkload([[local <const> xxx = 20; xxx = 10]],
218 ":1: assignment to const variable 'xxx'")
219
220 checkload([[
221 local xx;
222 local <const> xxx = 20;
223 local yyy;
224 local function foo ()
225 local abc = xx + yyy + xxx;
226 return function () return function () xxx = yyy end end
227 end
228 ]], ":6: assignment to const variable 'xxx'")
229
230 checkload([[
231 local <toclose> x = nil
232 x = io.open()
233 ]], ":2: assignment to const variable 'x'")
234end
178 235
179f = [[ 236f = [[
180return function ( a , b , c , d , e ) 237return function ( a , b , c , d , e )
@@ -245,12 +302,12 @@ print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')')
245 302
246 303
247-- operators with their respective values 304-- operators with their respective values
248local binops = { 305local <const> binops = {
249 {" and ", function (a,b) if not a then return a else return b end end}, 306 {" and ", function (a,b) if not a then return a else return b end end},
250 {" or ", function (a,b) if a then return a else return b end end}, 307 {" or ", function (a,b) if a then return a else return b end end},
251} 308}
252 309
253local cases = {} 310local <const> cases = {}
254 311
255-- creates all combinations of '(cases[i] op cases[n-i])' plus 312-- creates all combinations of '(cases[i] op cases[n-i])' plus
256-- 'not(cases[i] op cases[n-i])' (syntax + value) 313-- 'not(cases[i] op cases[n-i])' (syntax + value)