1print "testing syntax"
2
3-- testing priorities
4
5assert(2^3^2 == 2^(3^2));
6assert(2^3*4 == (2^3)*4);
7assert(2^-2 == 1/4 and -2^- -2 == - - -4);
8assert(not nil and 2 and not(2>3 or 3<2));
9assert(-3-1-5 == 0+0-9);
10assert(-2^2 == -4 and (-2)^2 == 4 and 2*2-3-1 == 0);
11assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33");
12assert(not(2+1 > 3*1) and "a".."b" > "a");
13
14assert(not ((true or false) and nil))
15assert(      true or false  and nil)
16
17local a,b = 1,nil;
18assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75);
19x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
20x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x);
21
22x,y=1,2;
23assert((x>y) and x or y == 2);
24x,y=2,1;
25assert((x>y) and x or y == 2);
26
27assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891)
28
29
30-- silly loops
31repeat until 1; repeat until true;
32while false do end; while nil do end;
33
34do  -- test old bug (first name could not be an `upvalue')
35 local a; function f(x) x={a=1}; x={x=1}; x={G=1} end
36end
37
38function f (i)
39  if type(i) ~= 'number' then return i,'jojo'; end;
40  if i > 0 then return i, f(i-1); end;
41end
42
43x = {f(3), f(5), f(10);};
44assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1);
45assert(x[nil] == nil)
46x = {f'alo', f'xixi', nil};
47assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil);
48x = {f'alo'..'xixi'};
49assert(x[1] == 'aloxixi')
50x = {f{}}
51assert(x[2] == 'jojo' and type(x[1]) == 'table')
52
53
54local f = function (i)
55  if i < 10 then return 'a';
56  elseif i < 20 then return 'b';
57  elseif i < 30 then return 'c';
58  end;
59end
60
61assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
62
63for i=1,1000 do break; end;
64n=100;
65i=3;
66t = {};
67a=nil
68while not a do
69  a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
70end
71assert(a == n*(n+1)/2 and i==3);
72assert(t[1] and t[n] and not t[0] and not t[n+1])
73
74function f(b)
75  local x = 1;
76  repeat
77    local a;
78    if b==1 then local b=1; x=10; break
79    elseif b==2 then x=20; break;
80    elseif b==3 then x=30;
81    else local a,b,c,d=math.sin(1); x=x+1;
82    end
83  until x>=12;
84  return x;
85end;
86
87assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12)
88
89
90local f = function (i)
91  if i < 10 then return 'a'
92  elseif i < 20 then return 'b'
93  elseif i < 30 then return 'c'
94  else return 8
95  end
96end
97
98assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8)
99
100local a, b = nil, 23
101x = {f(100)*2+3 or a, a or b+2}
102assert(x[1] == 19 and x[2] == 25)
103x = {f=2+3 or a, a = b+2}
104assert(x.f == 5 and x.a == 25)
105
106a={y=1}
107x = {a.y}
108assert(x[1] == 1)
109
110function f(i)
111  while 1 do
112    if i>0 then i=i-1;
113    else return; end;
114  end;
115end;
116
117function g(i)
118  while 1 do
119    if i>0 then i=i-1
120    else return end
121  end
122end
123
124f(10); g(10);
125
126do
127  function f () return 1,2,3; end
128  local a, b, c = f();
129  assert(a==1 and b==2 and c==3)
130  a, b, c = (f());
131  assert(a==1 and b==nil and c==nil)
132end
133
134local a,b = 3 and f();
135assert(a==1 and b==nil)
136
137function g() f(); return; end;
138assert(g() == nil)
139function g() return nil or f() end
140a,b = g()
141assert(a==1 and b==nil)
142
143print'+';
144
145
146f = [[
147return function ( a , b , c , d , e )
148  local x = a >= b or c or ( d and e ) or nil
149  return x
150end , { a = 1 , b = 2 >= 1 , } or { 1 };
151]]
152f = string.gsub(f, "%s+", "\n");   -- force a SETLINE between opcodes
153f,a = loadstring(f)();
154assert(a.a == 1 and a.b)
155
156function g (a,b,c,d,e)
157  if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
158end
159
160function h (a,b,c,d,e)
161  while (a>=b or c or (d and e) or nil) do return 1; end;
162  return 0;
163end;
164
165assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1)
166assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
167assert(f(1,2,'a')
168~=          -- force SETLINE before nil
169nil, "")
170assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
171assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and
172                                   h(1,2,nil,1,'x') == 1)
173assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and
174                                     h(1,2,nil,nil,'x') == 0)
175assert(f(1,2,nil,1,nil) == nil and g(1,2,nil,1,nil) == 0 and
176                                   h(1,2,nil,1,nil) == 0)
177
178assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true)
179x = 2<3 and not 3; assert(x==false)
180x = 2<1 or (2>1 and 'a'); assert(x=='a')
181
182
183do
184  local a; if nil then a=1; else a=2; end;    -- this nil comes as PUSHNIL 2
185  assert(a==2)
186end
187
188function F(a)
189  assert(debug.getinfo(1, "n").name == 'F')
190  return a,2,3
191end
192
193a,b = F(1)~=nil; assert(a == true and b == nil);
194a,b = F(nil)==nil; assert(a == true and b == nil)
195
196----------------------------------------------------------------
197-- creates all combinations of
198-- [not] ([not] arg op [not] (arg op [not] arg ))
199-- and tests each one
200
201function ID(x) return x end
202
203function f(t, i)
204  local b = t.n
205  local res = math.mod(math.floor(i/c), b)+1
206  c = c*b
207  return t[res]
208end
209
210local arg = {" ( 1 < 2 ) ", " ( 1 >= 2 ) ", " F ( ) ", "  nil "; n=4}
211
212local op = {" and ", " or ", " == ", " ~= "; n=4}
213
214local neg = {" ", " not "; n=2}
215
216local i = 0
217repeat
218  c = 1
219  local s = f(neg, i)..'ID('..f(neg, i)..f(arg, i)..f(op, i)..
220            f(neg, i)..'ID('..f(arg, i)..f(op, i)..f(neg, i)..f(arg, i)..'))'
221  local s1 = string.gsub(s, 'ID', '')
222  K,X,NX,WX1,WX2 = nil
223  s = string.format([[
224      local a = %s
225      local b = not %s
226      K = b
227      local xxx;
228      if %s then X = a  else X = b end
229      if %s then NX = b  else NX = a end
230      while %s do WX1 = a; break end
231      while %s do WX2 = a; break end
232      repeat if (%s) then break end; assert(b)  until not(%s)
233  ]], s1, s, s1, s, s1, s, s1, s, s)
234  assert(loadstring(s))()
235  assert(X and not NX and not WX1 == K and not WX2 == K)
236  if math.mod(i,4000) == 0 then print('+') end
237  i = i+1
238until i==c
239
240print'OK'
241