diff options
Diffstat (limited to '')
50 files changed, 3020 insertions, 0 deletions
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/bisect.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/bisect.lua new file mode 100644 index 0000000..f91e69b --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/bisect.lua | |||
@@ -0,0 +1,27 @@ | |||
1 | -- bisection method for solving non-linear equations | ||
2 | |||
3 | delta=1e-6 -- tolerance | ||
4 | |||
5 | function bisect(f,a,b,fa,fb) | ||
6 | local c=(a+b)/2 | ||
7 | io.write(n," c=",c," a=",a," b=",b,"\n") | ||
8 | if c==a or c==b or math.abs(a-b)<delta then return c,b-a end | ||
9 | n=n+1 | ||
10 | local fc=f(c) | ||
11 | if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end | ||
12 | end | ||
13 | |||
14 | -- find root of f in the inverval [a,b]. needs f(a)*f(b)<0 | ||
15 | function solve(f,a,b) | ||
16 | n=0 | ||
17 | local z,e=bisect(f,a,b,f(a),f(b)) | ||
18 | io.write(string.format("after %d steps, root is %.17g with error %.1e, f=%.1e\n",n,z,e,f(z))) | ||
19 | end | ||
20 | |||
21 | -- our function | ||
22 | function f(x) | ||
23 | return x*x*x-x-1 | ||
24 | end | ||
25 | |||
26 | -- find zero in [1,2] | ||
27 | solve(f,1,2) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/cf.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/cf.lua new file mode 100644 index 0000000..8cda54b --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/cf.lua | |||
@@ -0,0 +1,16 @@ | |||
1 | -- temperature conversion table (celsius to farenheit) | ||
2 | |||
3 | for c0=-20,50-1,10 do | ||
4 | io.write("C ") | ||
5 | for c=c0,c0+10-1 do | ||
6 | io.write(string.format("%3.0f ",c)) | ||
7 | end | ||
8 | io.write("\n") | ||
9 | |||
10 | io.write("F ") | ||
11 | for c=c0,c0+10-1 do | ||
12 | f=(9/5)*c+32 | ||
13 | io.write(string.format("%3.0f ",f)) | ||
14 | end | ||
15 | io.write("\n\n") | ||
16 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/echo.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/echo.lua new file mode 100644 index 0000000..4313439 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/echo.lua | |||
@@ -0,0 +1,5 @@ | |||
1 | -- echo command line arguments | ||
2 | |||
3 | for i=0,table.getn(arg) do | ||
4 | print(i,arg[i]) | ||
5 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/env.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/env.lua new file mode 100644 index 0000000..9e62a57 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/env.lua | |||
@@ -0,0 +1,7 @@ | |||
1 | -- read environment variables as if they were global variables | ||
2 | |||
3 | local f=function (t,i) return os.getenv(i) end | ||
4 | setmetatable(getfenv(),{__index=f}) | ||
5 | |||
6 | -- an example | ||
7 | print(a,USER,PATH) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/factorial.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/factorial.lua new file mode 100644 index 0000000..7c4cf0f --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/factorial.lua | |||
@@ -0,0 +1,32 @@ | |||
1 | -- function closures are powerful | ||
2 | |||
3 | -- traditional fixed-point operator from functional programming | ||
4 | Y = function (g) | ||
5 | local a = function (f) return f(f) end | ||
6 | return a(function (f) | ||
7 | return g(function (x) | ||
8 | local c=f(f) | ||
9 | return c(x) | ||
10 | end) | ||
11 | end) | ||
12 | end | ||
13 | |||
14 | |||
15 | -- factorial without recursion | ||
16 | F = function (f) | ||
17 | return function (n) | ||
18 | if n == 0 then return 1 | ||
19 | else return n*f(n-1) end | ||
20 | end | ||
21 | end | ||
22 | |||
23 | factorial = Y(F) -- factorial is the fixed point of F | ||
24 | |||
25 | -- now test it | ||
26 | function test(x) | ||
27 | io.write(x,"! = ",factorial(x),"\n") | ||
28 | end | ||
29 | |||
30 | for n=0,16 do | ||
31 | test(n) | ||
32 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/fib.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/fib.lua new file mode 100644 index 0000000..97a921b --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/fib.lua | |||
@@ -0,0 +1,40 @@ | |||
1 | -- fibonacci function with cache | ||
2 | |||
3 | -- very inefficient fibonacci function | ||
4 | function fib(n) | ||
5 | N=N+1 | ||
6 | if n<2 then | ||
7 | return n | ||
8 | else | ||
9 | return fib(n-1)+fib(n-2) | ||
10 | end | ||
11 | end | ||
12 | |||
13 | -- a general-purpose value cache | ||
14 | function cache(f) | ||
15 | local c={} | ||
16 | return function (x) | ||
17 | local y=c[x] | ||
18 | if not y then | ||
19 | y=f(x) | ||
20 | c[x]=y | ||
21 | end | ||
22 | return y | ||
23 | end | ||
24 | end | ||
25 | |||
26 | -- run and time it | ||
27 | function test(s,f) | ||
28 | N=0 | ||
29 | local c=os.clock() | ||
30 | local v=f(n) | ||
31 | local t=os.clock()-c | ||
32 | print(s,n,v,t,N) | ||
33 | end | ||
34 | |||
35 | n=arg[1] or 24 -- for other values, do lua fib.lua XX | ||
36 | n=tonumber(n) | ||
37 | print("","n","value","time","evals") | ||
38 | test("plain",fib) | ||
39 | fib=cache(fib) | ||
40 | test("cached",fib) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/fibfor.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/fibfor.lua new file mode 100644 index 0000000..19bb34b --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/fibfor.lua | |||
@@ -0,0 +1,13 @@ | |||
1 | -- example of for with generator functions | ||
2 | |||
3 | function generatefib (n) | ||
4 | return coroutine.wrap(function () | ||
5 | local a,b = 1, 1 | ||
6 | while a <= n do | ||
7 | coroutine.yield(a) | ||
8 | a, b = b, a+b | ||
9 | end | ||
10 | end, n) | ||
11 | end | ||
12 | |||
13 | for i in generatefib(1000) do print(i) end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/globals.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/globals.lua new file mode 100644 index 0000000..d4c20e1 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/globals.lua | |||
@@ -0,0 +1,13 @@ | |||
1 | -- reads luac listings and reports global variable usage | ||
2 | -- lines where a global is written to are marked with "*" | ||
3 | -- typical usage: luac -p -l file.lua | lua globals.lua | sort | lua table.lua | ||
4 | |||
5 | while 1 do | ||
6 | local s=io.read() | ||
7 | if s==nil then break end | ||
8 | local ok,_,l,op,g=string.find(s,"%[%-?(%d*)%]%s*([GS])ETGLOBAL.-;%s+(.*)$") | ||
9 | if ok then | ||
10 | if op=="S" then op="*" else op="" end | ||
11 | io.write(g,"\t",l,op,"\n") | ||
12 | end | ||
13 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/hello.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/hello.lua new file mode 100644 index 0000000..0925498 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/hello.lua | |||
@@ -0,0 +1,3 @@ | |||
1 | -- the first program in every language | ||
2 | |||
3 | io.write("Hello world, from ",_VERSION,"!\n") | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/life.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/life.lua new file mode 100644 index 0000000..911d9fe --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/life.lua | |||
@@ -0,0 +1,111 @@ | |||
1 | -- life.lua | ||
2 | -- original by Dave Bollinger <DBollinger@compuserve.com> posted to lua-l | ||
3 | -- modified to use ANSI terminal escape sequences | ||
4 | -- modified to use for instead of while | ||
5 | |||
6 | local write=io.write | ||
7 | |||
8 | ALIVE="¥" DEAD="þ" | ||
9 | ALIVE="O" DEAD="-" | ||
10 | |||
11 | function delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary | ||
12 | for i=1,10000 do end | ||
13 | -- local i=os.clock()+1 while(os.clock()<i) do end | ||
14 | end | ||
15 | |||
16 | function ARRAY2D(w,h) | ||
17 | local t = {w=w,h=h} | ||
18 | for y=1,h do | ||
19 | t[y] = {} | ||
20 | for x=1,w do | ||
21 | t[y][x]=0 | ||
22 | end | ||
23 | end | ||
24 | return t | ||
25 | end | ||
26 | |||
27 | _CELLS = {} | ||
28 | |||
29 | -- give birth to a "shape" within the cell array | ||
30 | function _CELLS:spawn(shape,left,top) | ||
31 | for y=0,shape.h-1 do | ||
32 | for x=0,shape.w-1 do | ||
33 | self[top+y][left+x] = shape[y*shape.w+x+1] | ||
34 | end | ||
35 | end | ||
36 | end | ||
37 | |||
38 | -- run the CA and produce the next generation | ||
39 | function _CELLS:evolve(next) | ||
40 | local ym1,y,yp1,yi=self.h-1,self.h,1,self.h | ||
41 | while yi > 0 do | ||
42 | local xm1,x,xp1,xi=self.w-1,self.w,1,self.w | ||
43 | while xi > 0 do | ||
44 | local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] + | ||
45 | self[y][xm1] + self[y][xp1] + | ||
46 | self[yp1][xm1] + self[yp1][x] + self[yp1][xp1] | ||
47 | next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0 | ||
48 | xm1,x,xp1,xi = x,xp1,xp1+1,xi-1 | ||
49 | end | ||
50 | ym1,y,yp1,yi = y,yp1,yp1+1,yi-1 | ||
51 | end | ||
52 | end | ||
53 | |||
54 | -- output the array to screen | ||
55 | function _CELLS:draw() | ||
56 | local out="" -- accumulate to reduce flicker | ||
57 | for y=1,self.h do | ||
58 | for x=1,self.w do | ||
59 | out=out..(((self[y][x]>0) and ALIVE) or DEAD) | ||
60 | end | ||
61 | out=out.."\n" | ||
62 | end | ||
63 | write(out) | ||
64 | end | ||
65 | |||
66 | -- constructor | ||
67 | function CELLS(w,h) | ||
68 | local c = ARRAY2D(w,h) | ||
69 | c.spawn = _CELLS.spawn | ||
70 | c.evolve = _CELLS.evolve | ||
71 | c.draw = _CELLS.draw | ||
72 | return c | ||
73 | end | ||
74 | |||
75 | -- | ||
76 | -- shapes suitable for use with spawn() above | ||
77 | -- | ||
78 | HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 } | ||
79 | GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 } | ||
80 | EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 } | ||
81 | FISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 } | ||
82 | BUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 } | ||
83 | |||
84 | -- the main routine | ||
85 | function LIFE(w,h) | ||
86 | -- create two arrays | ||
87 | local thisgen = CELLS(w,h) | ||
88 | local nextgen = CELLS(w,h) | ||
89 | |||
90 | -- create some life | ||
91 | -- about 1000 generations of fun, then a glider steady-state | ||
92 | thisgen:spawn(GLIDER,5,4) | ||
93 | thisgen:spawn(EXPLODE,25,10) | ||
94 | thisgen:spawn(FISH,4,12) | ||
95 | |||
96 | -- run until break | ||
97 | local gen=1 | ||
98 | write("\027[2J") -- ANSI clear screen | ||
99 | while 1 do | ||
100 | thisgen:evolve(nextgen) | ||
101 | thisgen,nextgen = nextgen,thisgen | ||
102 | write("\027[H") -- ANSI home cursor | ||
103 | thisgen:draw() | ||
104 | write("Life - generation ",gen,"\n") | ||
105 | gen=gen+1 | ||
106 | if gen>2000 then break end | ||
107 | --delay() -- no delay | ||
108 | end | ||
109 | end | ||
110 | |||
111 | LIFE(40,20) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/luac.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/luac.lua new file mode 100644 index 0000000..b009ae9 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/luac.lua | |||
@@ -0,0 +1,7 @@ | |||
1 | -- bare-bones luac in Lua | ||
2 | -- usage: lua luac.lua file.lua | ||
3 | |||
4 | assert(arg[1]~=nil and arg[2]==nil,"usage: lua luac.lua file.lua") | ||
5 | f=assert(io.open("luac.out","wb")) | ||
6 | f:write(string.dump(assert(loadfile(arg[1])))) | ||
7 | io.close(f) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/printf.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/printf.lua new file mode 100644 index 0000000..66dfda6 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/printf.lua | |||
@@ -0,0 +1,7 @@ | |||
1 | -- an implementation of printf | ||
2 | |||
3 | function printf(...) | ||
4 | io.write(string.format(unpack(arg))) | ||
5 | end | ||
6 | |||
7 | printf("Hello %s from %s on %s\n",os.getenv"USER" or "there",_VERSION,os.date()) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/readonly.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/readonly.lua new file mode 100644 index 0000000..85c0b4e --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/readonly.lua | |||
@@ -0,0 +1,12 @@ | |||
1 | -- make global variables readonly | ||
2 | |||
3 | local f=function (t,i) error("cannot redefine global variable `"..i.."'",2) end | ||
4 | local g={} | ||
5 | local G=getfenv() | ||
6 | setmetatable(g,{__index=G,__newindex=f}) | ||
7 | setfenv(1,g) | ||
8 | |||
9 | -- an example | ||
10 | rawset(g,"x",3) | ||
11 | x=2 | ||
12 | y=1 -- cannot redefine `y' | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/sieve.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/sieve.lua new file mode 100644 index 0000000..0871bb2 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/sieve.lua | |||
@@ -0,0 +1,29 @@ | |||
1 | -- the sieve of of Eratosthenes programmed with coroutines | ||
2 | -- typical usage: lua -e N=1000 sieve.lua | column | ||
3 | |||
4 | -- generate all the numbers from 2 to n | ||
5 | function gen (n) | ||
6 | return coroutine.wrap(function () | ||
7 | for i=2,n do coroutine.yield(i) end | ||
8 | end) | ||
9 | end | ||
10 | |||
11 | -- filter the numbers generated by `g', removing multiples of `p' | ||
12 | function filter (p, g) | ||
13 | return coroutine.wrap(function () | ||
14 | while 1 do | ||
15 | local n = g() | ||
16 | if n == nil then return end | ||
17 | if math.mod(n, p) ~= 0 then coroutine.yield(n) end | ||
18 | end | ||
19 | end) | ||
20 | end | ||
21 | |||
22 | N=N or 1000 -- from command line | ||
23 | x = gen(N) -- generate primes up to N | ||
24 | while 1 do | ||
25 | local n = x() -- pick a number until done | ||
26 | if n == nil then break end | ||
27 | print(n) -- must be a prime number | ||
28 | x = filter(n, x) -- now remove its multiples | ||
29 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/sort.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/sort.lua new file mode 100644 index 0000000..0bcb15f --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/sort.lua | |||
@@ -0,0 +1,66 @@ | |||
1 | -- two implementations of a sort function | ||
2 | -- this is an example only. Lua has now a built-in function "sort" | ||
3 | |||
4 | -- extracted from Programming Pearls, page 110 | ||
5 | function qsort(x,l,u,f) | ||
6 | if l<u then | ||
7 | local m=math.random(u-(l-1))+l-1 -- choose a random pivot in range l..u | ||
8 | x[l],x[m]=x[m],x[l] -- swap pivot to first position | ||
9 | local t=x[l] -- pivot value | ||
10 | m=l | ||
11 | local i=l+1 | ||
12 | while i<=u do | ||
13 | -- invariant: x[l+1..m] < t <= x[m+1..i-1] | ||
14 | if f(x[i],t) then | ||
15 | m=m+1 | ||
16 | x[m],x[i]=x[i],x[m] -- swap x[i] and x[m] | ||
17 | end | ||
18 | i=i+1 | ||
19 | end | ||
20 | x[l],x[m]=x[m],x[l] -- swap pivot to a valid place | ||
21 | -- x[l+1..m-1] < x[m] <= x[m+1..u] | ||
22 | qsort(x,l,m-1,f) | ||
23 | qsort(x,m+1,u,f) | ||
24 | end | ||
25 | end | ||
26 | |||
27 | function selectionsort(x,n,f) | ||
28 | local i=1 | ||
29 | while i<=n do | ||
30 | local m,j=i,i+1 | ||
31 | while j<=n do | ||
32 | if f(x[j],x[m]) then m=j end | ||
33 | j=j+1 | ||
34 | end | ||
35 | x[i],x[m]=x[m],x[i] -- swap x[i] and x[m] | ||
36 | i=i+1 | ||
37 | end | ||
38 | end | ||
39 | |||
40 | function show(m,x) | ||
41 | io.write(m,"\n\t") | ||
42 | local i=1 | ||
43 | while x[i] do | ||
44 | io.write(x[i]) | ||
45 | i=i+1 | ||
46 | if x[i] then io.write(",") end | ||
47 | end | ||
48 | io.write("\n") | ||
49 | end | ||
50 | |||
51 | function testsorts(x) | ||
52 | local n=1 | ||
53 | while x[n] do n=n+1 end; n=n-1 -- count elements | ||
54 | show("original",x) | ||
55 | qsort(x,1,n,function (x,y) return x<y end) | ||
56 | show("after quicksort",x) | ||
57 | selectionsort(x,n,function (x,y) return x>y end) | ||
58 | show("after reverse selection sort",x) | ||
59 | qsort(x,1,n,function (x,y) return x<y end) | ||
60 | show("after quicksort again",x) | ||
61 | end | ||
62 | |||
63 | -- array to be sorted | ||
64 | x={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"} | ||
65 | |||
66 | testsorts(x) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/table.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/table.lua new file mode 100644 index 0000000..235089c --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/table.lua | |||
@@ -0,0 +1,12 @@ | |||
1 | -- make table, grouping all data for the same item | ||
2 | -- input is 2 columns (item, data) | ||
3 | |||
4 | local A | ||
5 | while 1 do | ||
6 | local l=io.read() | ||
7 | if l==nil then break end | ||
8 | local _,_,a,b=string.find(l,'"?([_%w]+)"?%s*(.*)$') | ||
9 | if a~=A then A=a io.write("\n",a,":") end | ||
10 | io.write(" ",b) | ||
11 | end | ||
12 | io.write("\n") | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/trace-calls.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/trace-calls.lua new file mode 100644 index 0000000..63c8b8f --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/trace-calls.lua | |||
@@ -0,0 +1,32 @@ | |||
1 | -- trace calls | ||
2 | -- example: lua -ltrace-calls.lua bisect.lua | ||
3 | |||
4 | local level=0 | ||
5 | |||
6 | function hook(event) | ||
7 | local t=debug.getinfo(3) | ||
8 | io.write(level," >>> ",string.rep(" ",level)) | ||
9 | if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end | ||
10 | t=debug.getinfo(2) | ||
11 | if event=="call" then | ||
12 | level=level+1 | ||
13 | else | ||
14 | level=level-1 if level<0 then level=0 end | ||
15 | end | ||
16 | if t.what=="main" then | ||
17 | if event=="call" then | ||
18 | io.write("begin ",t.short_src) | ||
19 | else | ||
20 | io.write("end ",t.short_src) | ||
21 | end | ||
22 | elseif t.what=="Lua" then | ||
23 | -- table.foreach(t,print) | ||
24 | io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">") | ||
25 | else | ||
26 | io.write(event," ",t.name or "(C)"," [",t.what,"] ") | ||
27 | end | ||
28 | io.write("\n") | ||
29 | end | ||
30 | |||
31 | debug.sethook(hook,"cr") | ||
32 | level=0 | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/trace-globals.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/trace-globals.lua new file mode 100644 index 0000000..295e670 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/trace-globals.lua | |||
@@ -0,0 +1,38 @@ | |||
1 | -- trace assigments to global variables | ||
2 | |||
3 | do | ||
4 | -- a tostring that quotes strings. note the use of the original tostring. | ||
5 | local _tostring=tostring | ||
6 | local tostring=function(a) | ||
7 | if type(a)=="string" then | ||
8 | return string.format("%q",a) | ||
9 | else | ||
10 | return _tostring(a) | ||
11 | end | ||
12 | end | ||
13 | |||
14 | local log=function (name,old,new) | ||
15 | local t=debug.getinfo(3,"Sl") | ||
16 | local line=t.currentline | ||
17 | io.write(t.short_src) | ||
18 | if line>=0 then io.write(":",line) end | ||
19 | io.write(": ",name," is now ",tostring(new)," (was ",tostring(old),")","\n") | ||
20 | end | ||
21 | |||
22 | local g={} | ||
23 | local set=function (t,name,value) | ||
24 | log(name,g[name],value) | ||
25 | g[name]=value | ||
26 | end | ||
27 | setmetatable(getfenv(),{__index=g,__newindex=set}) | ||
28 | end | ||
29 | |||
30 | -- an example | ||
31 | |||
32 | a=1 | ||
33 | b=2 | ||
34 | a=10 | ||
35 | b=20 | ||
36 | b=nil | ||
37 | b=200 | ||
38 | print(a,b,c) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/undefined.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/undefined.lua new file mode 100644 index 0000000..efe5f24 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/undefined.lua | |||
@@ -0,0 +1,9 @@ | |||
1 | -- catch "undefined" global variables | ||
2 | |||
3 | local f=function (t,i) error("undefined global variable `"..i.."'",2) end | ||
4 | setmetatable(getfenv(),{__index=f}) | ||
5 | |||
6 | -- an example | ||
7 | a=1 | ||
8 | c=3 | ||
9 | print(a,b,c) -- `b' is undefined | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/xd.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/xd.lua new file mode 100644 index 0000000..32331dc --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/xd.lua | |||
@@ -0,0 +1,14 @@ | |||
1 | -- hex dump | ||
2 | -- usage: lua xd.lua < file | ||
3 | |||
4 | local offset=0 | ||
5 | |||
6 | while 1 do | ||
7 | local s=io.read(16) | ||
8 | if s==nil then return end | ||
9 | io.write(string.format("%08X ",offset)) | ||
10 | string.gsub(s,"(.)",function (c) io.write(string.format("%02X ",string.byte(c))) end) | ||
11 | io.write(string.rep(" ",3*(16-string.len(s)))) | ||
12 | io.write(" ",string.gsub(s,"%c","."),"\n") | ||
13 | offset=offset+16 | ||
14 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua new file mode 100644 index 0000000..f91e69b --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua | |||
@@ -0,0 +1,27 @@ | |||
1 | -- bisection method for solving non-linear equations | ||
2 | |||
3 | delta=1e-6 -- tolerance | ||
4 | |||
5 | function bisect(f,a,b,fa,fb) | ||
6 | local c=(a+b)/2 | ||
7 | io.write(n," c=",c," a=",a," b=",b,"\n") | ||
8 | if c==a or c==b or math.abs(a-b)<delta then return c,b-a end | ||
9 | n=n+1 | ||
10 | local fc=f(c) | ||
11 | if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end | ||
12 | end | ||
13 | |||
14 | -- find root of f in the inverval [a,b]. needs f(a)*f(b)<0 | ||
15 | function solve(f,a,b) | ||
16 | n=0 | ||
17 | local z,e=bisect(f,a,b,f(a),f(b)) | ||
18 | io.write(string.format("after %d steps, root is %.17g with error %.1e, f=%.1e\n",n,z,e,f(z))) | ||
19 | end | ||
20 | |||
21 | -- our function | ||
22 | function f(x) | ||
23 | return x*x*x-x-1 | ||
24 | end | ||
25 | |||
26 | -- find zero in [1,2] | ||
27 | solve(f,1,2) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/cf.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/cf.lua new file mode 100644 index 0000000..8cda54b --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/cf.lua | |||
@@ -0,0 +1,16 @@ | |||
1 | -- temperature conversion table (celsius to farenheit) | ||
2 | |||
3 | for c0=-20,50-1,10 do | ||
4 | io.write("C ") | ||
5 | for c=c0,c0+10-1 do | ||
6 | io.write(string.format("%3.0f ",c)) | ||
7 | end | ||
8 | io.write("\n") | ||
9 | |||
10 | io.write("F ") | ||
11 | for c=c0,c0+10-1 do | ||
12 | f=(9/5)*c+32 | ||
13 | io.write(string.format("%3.0f ",f)) | ||
14 | end | ||
15 | io.write("\n\n") | ||
16 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/echo.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/echo.lua new file mode 100644 index 0000000..4313439 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/echo.lua | |||
@@ -0,0 +1,5 @@ | |||
1 | -- echo command line arguments | ||
2 | |||
3 | for i=0,table.getn(arg) do | ||
4 | print(i,arg[i]) | ||
5 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/env.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/env.lua new file mode 100644 index 0000000..9e62a57 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/env.lua | |||
@@ -0,0 +1,7 @@ | |||
1 | -- read environment variables as if they were global variables | ||
2 | |||
3 | local f=function (t,i) return os.getenv(i) end | ||
4 | setmetatable(getfenv(),{__index=f}) | ||
5 | |||
6 | -- an example | ||
7 | print(a,USER,PATH) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/factorial.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/factorial.lua new file mode 100644 index 0000000..7c4cf0f --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/factorial.lua | |||
@@ -0,0 +1,32 @@ | |||
1 | -- function closures are powerful | ||
2 | |||
3 | -- traditional fixed-point operator from functional programming | ||
4 | Y = function (g) | ||
5 | local a = function (f) return f(f) end | ||
6 | return a(function (f) | ||
7 | return g(function (x) | ||
8 | local c=f(f) | ||
9 | return c(x) | ||
10 | end) | ||
11 | end) | ||
12 | end | ||
13 | |||
14 | |||
15 | -- factorial without recursion | ||
16 | F = function (f) | ||
17 | return function (n) | ||
18 | if n == 0 then return 1 | ||
19 | else return n*f(n-1) end | ||
20 | end | ||
21 | end | ||
22 | |||
23 | factorial = Y(F) -- factorial is the fixed point of F | ||
24 | |||
25 | -- now test it | ||
26 | function test(x) | ||
27 | io.write(x,"! = ",factorial(x),"\n") | ||
28 | end | ||
29 | |||
30 | for n=0,16 do | ||
31 | test(n) | ||
32 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/fib.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/fib.lua new file mode 100644 index 0000000..97a921b --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/fib.lua | |||
@@ -0,0 +1,40 @@ | |||
1 | -- fibonacci function with cache | ||
2 | |||
3 | -- very inefficient fibonacci function | ||
4 | function fib(n) | ||
5 | N=N+1 | ||
6 | if n<2 then | ||
7 | return n | ||
8 | else | ||
9 | return fib(n-1)+fib(n-2) | ||
10 | end | ||
11 | end | ||
12 | |||
13 | -- a general-purpose value cache | ||
14 | function cache(f) | ||
15 | local c={} | ||
16 | return function (x) | ||
17 | local y=c[x] | ||
18 | if not y then | ||
19 | y=f(x) | ||
20 | c[x]=y | ||
21 | end | ||
22 | return y | ||
23 | end | ||
24 | end | ||
25 | |||
26 | -- run and time it | ||
27 | function test(s,f) | ||
28 | N=0 | ||
29 | local c=os.clock() | ||
30 | local v=f(n) | ||
31 | local t=os.clock()-c | ||
32 | print(s,n,v,t,N) | ||
33 | end | ||
34 | |||
35 | n=arg[1] or 24 -- for other values, do lua fib.lua XX | ||
36 | n=tonumber(n) | ||
37 | print("","n","value","time","evals") | ||
38 | test("plain",fib) | ||
39 | fib=cache(fib) | ||
40 | test("cached",fib) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/fibfor.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/fibfor.lua new file mode 100644 index 0000000..8bbba39 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/fibfor.lua | |||
@@ -0,0 +1,13 @@ | |||
1 | -- example of for with generator functions | ||
2 | |||
3 | function generatefib (n) | ||
4 | return coroutine.wrap(function () | ||
5 | local a,b = 1, 1 | ||
6 | while a <= n do | ||
7 | coroutine.yield(a) | ||
8 | a, b = b, a+b | ||
9 | end | ||
10 | end) | ||
11 | end | ||
12 | |||
13 | for i in generatefib(1000) do print(i) end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/globals.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/globals.lua new file mode 100644 index 0000000..d4c20e1 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/globals.lua | |||
@@ -0,0 +1,13 @@ | |||
1 | -- reads luac listings and reports global variable usage | ||
2 | -- lines where a global is written to are marked with "*" | ||
3 | -- typical usage: luac -p -l file.lua | lua globals.lua | sort | lua table.lua | ||
4 | |||
5 | while 1 do | ||
6 | local s=io.read() | ||
7 | if s==nil then break end | ||
8 | local ok,_,l,op,g=string.find(s,"%[%-?(%d*)%]%s*([GS])ETGLOBAL.-;%s+(.*)$") | ||
9 | if ok then | ||
10 | if op=="S" then op="*" else op="" end | ||
11 | io.write(g,"\t",l,op,"\n") | ||
12 | end | ||
13 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/hello.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/hello.lua new file mode 100644 index 0000000..0925498 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/hello.lua | |||
@@ -0,0 +1,3 @@ | |||
1 | -- the first program in every language | ||
2 | |||
3 | io.write("Hello world, from ",_VERSION,"!\n") | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/life.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/life.lua new file mode 100644 index 0000000..911d9fe --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/life.lua | |||
@@ -0,0 +1,111 @@ | |||
1 | -- life.lua | ||
2 | -- original by Dave Bollinger <DBollinger@compuserve.com> posted to lua-l | ||
3 | -- modified to use ANSI terminal escape sequences | ||
4 | -- modified to use for instead of while | ||
5 | |||
6 | local write=io.write | ||
7 | |||
8 | ALIVE="¥" DEAD="þ" | ||
9 | ALIVE="O" DEAD="-" | ||
10 | |||
11 | function delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary | ||
12 | for i=1,10000 do end | ||
13 | -- local i=os.clock()+1 while(os.clock()<i) do end | ||
14 | end | ||
15 | |||
16 | function ARRAY2D(w,h) | ||
17 | local t = {w=w,h=h} | ||
18 | for y=1,h do | ||
19 | t[y] = {} | ||
20 | for x=1,w do | ||
21 | t[y][x]=0 | ||
22 | end | ||
23 | end | ||
24 | return t | ||
25 | end | ||
26 | |||
27 | _CELLS = {} | ||
28 | |||
29 | -- give birth to a "shape" within the cell array | ||
30 | function _CELLS:spawn(shape,left,top) | ||
31 | for y=0,shape.h-1 do | ||
32 | for x=0,shape.w-1 do | ||
33 | self[top+y][left+x] = shape[y*shape.w+x+1] | ||
34 | end | ||
35 | end | ||
36 | end | ||
37 | |||
38 | -- run the CA and produce the next generation | ||
39 | function _CELLS:evolve(next) | ||
40 | local ym1,y,yp1,yi=self.h-1,self.h,1,self.h | ||
41 | while yi > 0 do | ||
42 | local xm1,x,xp1,xi=self.w-1,self.w,1,self.w | ||
43 | while xi > 0 do | ||
44 | local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] + | ||
45 | self[y][xm1] + self[y][xp1] + | ||
46 | self[yp1][xm1] + self[yp1][x] + self[yp1][xp1] | ||
47 | next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0 | ||
48 | xm1,x,xp1,xi = x,xp1,xp1+1,xi-1 | ||
49 | end | ||
50 | ym1,y,yp1,yi = y,yp1,yp1+1,yi-1 | ||
51 | end | ||
52 | end | ||
53 | |||
54 | -- output the array to screen | ||
55 | function _CELLS:draw() | ||
56 | local out="" -- accumulate to reduce flicker | ||
57 | for y=1,self.h do | ||
58 | for x=1,self.w do | ||
59 | out=out..(((self[y][x]>0) and ALIVE) or DEAD) | ||
60 | end | ||
61 | out=out.."\n" | ||
62 | end | ||
63 | write(out) | ||
64 | end | ||
65 | |||
66 | -- constructor | ||
67 | function CELLS(w,h) | ||
68 | local c = ARRAY2D(w,h) | ||
69 | c.spawn = _CELLS.spawn | ||
70 | c.evolve = _CELLS.evolve | ||
71 | c.draw = _CELLS.draw | ||
72 | return c | ||
73 | end | ||
74 | |||
75 | -- | ||
76 | -- shapes suitable for use with spawn() above | ||
77 | -- | ||
78 | HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 } | ||
79 | GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 } | ||
80 | EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 } | ||
81 | FISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 } | ||
82 | BUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 } | ||
83 | |||
84 | -- the main routine | ||
85 | function LIFE(w,h) | ||
86 | -- create two arrays | ||
87 | local thisgen = CELLS(w,h) | ||
88 | local nextgen = CELLS(w,h) | ||
89 | |||
90 | -- create some life | ||
91 | -- about 1000 generations of fun, then a glider steady-state | ||
92 | thisgen:spawn(GLIDER,5,4) | ||
93 | thisgen:spawn(EXPLODE,25,10) | ||
94 | thisgen:spawn(FISH,4,12) | ||
95 | |||
96 | -- run until break | ||
97 | local gen=1 | ||
98 | write("\027[2J") -- ANSI clear screen | ||
99 | while 1 do | ||
100 | thisgen:evolve(nextgen) | ||
101 | thisgen,nextgen = nextgen,thisgen | ||
102 | write("\027[H") -- ANSI home cursor | ||
103 | thisgen:draw() | ||
104 | write("Life - generation ",gen,"\n") | ||
105 | gen=gen+1 | ||
106 | if gen>2000 then break end | ||
107 | --delay() -- no delay | ||
108 | end | ||
109 | end | ||
110 | |||
111 | LIFE(40,20) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/luac.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/luac.lua new file mode 100644 index 0000000..96a0a97 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/luac.lua | |||
@@ -0,0 +1,7 @@ | |||
1 | -- bare-bones luac in Lua | ||
2 | -- usage: lua luac.lua file.lua | ||
3 | |||
4 | assert(arg[1]~=nil and arg[2]==nil,"usage: lua luac.lua file.lua") | ||
5 | f=assert(io.open("luac.out","wb")) | ||
6 | assert(f:write(string.dump(assert(loadfile(arg[1]))))) | ||
7 | assert(f:close()) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/printf.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/printf.lua new file mode 100644 index 0000000..58c63ff --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/printf.lua | |||
@@ -0,0 +1,7 @@ | |||
1 | -- an implementation of printf | ||
2 | |||
3 | function printf(...) | ||
4 | io.write(string.format(...)) | ||
5 | end | ||
6 | |||
7 | printf("Hello %s from %s on %s\n",os.getenv"USER" or "there",_VERSION,os.date()) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/readonly.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/readonly.lua new file mode 100644 index 0000000..85c0b4e --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/readonly.lua | |||
@@ -0,0 +1,12 @@ | |||
1 | -- make global variables readonly | ||
2 | |||
3 | local f=function (t,i) error("cannot redefine global variable `"..i.."'",2) end | ||
4 | local g={} | ||
5 | local G=getfenv() | ||
6 | setmetatable(g,{__index=G,__newindex=f}) | ||
7 | setfenv(1,g) | ||
8 | |||
9 | -- an example | ||
10 | rawset(g,"x",3) | ||
11 | x=2 | ||
12 | y=1 -- cannot redefine `y' | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/sieve.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/sieve.lua new file mode 100644 index 0000000..0871bb2 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/sieve.lua | |||
@@ -0,0 +1,29 @@ | |||
1 | -- the sieve of of Eratosthenes programmed with coroutines | ||
2 | -- typical usage: lua -e N=1000 sieve.lua | column | ||
3 | |||
4 | -- generate all the numbers from 2 to n | ||
5 | function gen (n) | ||
6 | return coroutine.wrap(function () | ||
7 | for i=2,n do coroutine.yield(i) end | ||
8 | end) | ||
9 | end | ||
10 | |||
11 | -- filter the numbers generated by `g', removing multiples of `p' | ||
12 | function filter (p, g) | ||
13 | return coroutine.wrap(function () | ||
14 | while 1 do | ||
15 | local n = g() | ||
16 | if n == nil then return end | ||
17 | if math.mod(n, p) ~= 0 then coroutine.yield(n) end | ||
18 | end | ||
19 | end) | ||
20 | end | ||
21 | |||
22 | N=N or 1000 -- from command line | ||
23 | x = gen(N) -- generate primes up to N | ||
24 | while 1 do | ||
25 | local n = x() -- pick a number until done | ||
26 | if n == nil then break end | ||
27 | print(n) -- must be a prime number | ||
28 | x = filter(n, x) -- now remove its multiples | ||
29 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/sort.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/sort.lua new file mode 100644 index 0000000..0bcb15f --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/sort.lua | |||
@@ -0,0 +1,66 @@ | |||
1 | -- two implementations of a sort function | ||
2 | -- this is an example only. Lua has now a built-in function "sort" | ||
3 | |||
4 | -- extracted from Programming Pearls, page 110 | ||
5 | function qsort(x,l,u,f) | ||
6 | if l<u then | ||
7 | local m=math.random(u-(l-1))+l-1 -- choose a random pivot in range l..u | ||
8 | x[l],x[m]=x[m],x[l] -- swap pivot to first position | ||
9 | local t=x[l] -- pivot value | ||
10 | m=l | ||
11 | local i=l+1 | ||
12 | while i<=u do | ||
13 | -- invariant: x[l+1..m] < t <= x[m+1..i-1] | ||
14 | if f(x[i],t) then | ||
15 | m=m+1 | ||
16 | x[m],x[i]=x[i],x[m] -- swap x[i] and x[m] | ||
17 | end | ||
18 | i=i+1 | ||
19 | end | ||
20 | x[l],x[m]=x[m],x[l] -- swap pivot to a valid place | ||
21 | -- x[l+1..m-1] < x[m] <= x[m+1..u] | ||
22 | qsort(x,l,m-1,f) | ||
23 | qsort(x,m+1,u,f) | ||
24 | end | ||
25 | end | ||
26 | |||
27 | function selectionsort(x,n,f) | ||
28 | local i=1 | ||
29 | while i<=n do | ||
30 | local m,j=i,i+1 | ||
31 | while j<=n do | ||
32 | if f(x[j],x[m]) then m=j end | ||
33 | j=j+1 | ||
34 | end | ||
35 | x[i],x[m]=x[m],x[i] -- swap x[i] and x[m] | ||
36 | i=i+1 | ||
37 | end | ||
38 | end | ||
39 | |||
40 | function show(m,x) | ||
41 | io.write(m,"\n\t") | ||
42 | local i=1 | ||
43 | while x[i] do | ||
44 | io.write(x[i]) | ||
45 | i=i+1 | ||
46 | if x[i] then io.write(",") end | ||
47 | end | ||
48 | io.write("\n") | ||
49 | end | ||
50 | |||
51 | function testsorts(x) | ||
52 | local n=1 | ||
53 | while x[n] do n=n+1 end; n=n-1 -- count elements | ||
54 | show("original",x) | ||
55 | qsort(x,1,n,function (x,y) return x<y end) | ||
56 | show("after quicksort",x) | ||
57 | selectionsort(x,n,function (x,y) return x>y end) | ||
58 | show("after reverse selection sort",x) | ||
59 | qsort(x,1,n,function (x,y) return x<y end) | ||
60 | show("after quicksort again",x) | ||
61 | end | ||
62 | |||
63 | -- array to be sorted | ||
64 | x={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"} | ||
65 | |||
66 | testsorts(x) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/table.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/table.lua new file mode 100644 index 0000000..235089c --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/table.lua | |||
@@ -0,0 +1,12 @@ | |||
1 | -- make table, grouping all data for the same item | ||
2 | -- input is 2 columns (item, data) | ||
3 | |||
4 | local A | ||
5 | while 1 do | ||
6 | local l=io.read() | ||
7 | if l==nil then break end | ||
8 | local _,_,a,b=string.find(l,'"?([_%w]+)"?%s*(.*)$') | ||
9 | if a~=A then A=a io.write("\n",a,":") end | ||
10 | io.write(" ",b) | ||
11 | end | ||
12 | io.write("\n") | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/trace-calls.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/trace-calls.lua new file mode 100644 index 0000000..6d7a7b3 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/trace-calls.lua | |||
@@ -0,0 +1,32 @@ | |||
1 | -- trace calls | ||
2 | -- example: lua -ltrace-calls bisect.lua | ||
3 | |||
4 | local level=0 | ||
5 | |||
6 | local function hook(event) | ||
7 | local t=debug.getinfo(3) | ||
8 | io.write(level," >>> ",string.rep(" ",level)) | ||
9 | if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end | ||
10 | t=debug.getinfo(2) | ||
11 | if event=="call" then | ||
12 | level=level+1 | ||
13 | else | ||
14 | level=level-1 if level<0 then level=0 end | ||
15 | end | ||
16 | if t.what=="main" then | ||
17 | if event=="call" then | ||
18 | io.write("begin ",t.short_src) | ||
19 | else | ||
20 | io.write("end ",t.short_src) | ||
21 | end | ||
22 | elseif t.what=="Lua" then | ||
23 | -- table.foreach(t,print) | ||
24 | io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">") | ||
25 | else | ||
26 | io.write(event," ",t.name or "(C)"," [",t.what,"] ") | ||
27 | end | ||
28 | io.write("\n") | ||
29 | end | ||
30 | |||
31 | debug.sethook(hook,"cr") | ||
32 | level=0 | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/trace-globals.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/trace-globals.lua new file mode 100644 index 0000000..295e670 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/trace-globals.lua | |||
@@ -0,0 +1,38 @@ | |||
1 | -- trace assigments to global variables | ||
2 | |||
3 | do | ||
4 | -- a tostring that quotes strings. note the use of the original tostring. | ||
5 | local _tostring=tostring | ||
6 | local tostring=function(a) | ||
7 | if type(a)=="string" then | ||
8 | return string.format("%q",a) | ||
9 | else | ||
10 | return _tostring(a) | ||
11 | end | ||
12 | end | ||
13 | |||
14 | local log=function (name,old,new) | ||
15 | local t=debug.getinfo(3,"Sl") | ||
16 | local line=t.currentline | ||
17 | io.write(t.short_src) | ||
18 | if line>=0 then io.write(":",line) end | ||
19 | io.write(": ",name," is now ",tostring(new)," (was ",tostring(old),")","\n") | ||
20 | end | ||
21 | |||
22 | local g={} | ||
23 | local set=function (t,name,value) | ||
24 | log(name,g[name],value) | ||
25 | g[name]=value | ||
26 | end | ||
27 | setmetatable(getfenv(),{__index=g,__newindex=set}) | ||
28 | end | ||
29 | |||
30 | -- an example | ||
31 | |||
32 | a=1 | ||
33 | b=2 | ||
34 | a=10 | ||
35 | b=20 | ||
36 | b=nil | ||
37 | b=200 | ||
38 | print(a,b,c) | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/xd.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/xd.lua new file mode 100644 index 0000000..ebc3eff --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/xd.lua | |||
@@ -0,0 +1,14 @@ | |||
1 | -- hex dump | ||
2 | -- usage: lua xd.lua < file | ||
3 | |||
4 | local offset=0 | ||
5 | while true do | ||
6 | local s=io.read(16) | ||
7 | if s==nil then return end | ||
8 | io.write(string.format("%08X ",offset)) | ||
9 | string.gsub(s,"(.)", | ||
10 | function (c) io.write(string.format("%02X ",string.byte(c))) end) | ||
11 | io.write(string.rep(" ",3*(16-string.len(s)))) | ||
12 | io.write(" ",string.gsub(s,"%c","."),"\n") | ||
13 | offset=offset+16 | ||
14 | end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/README b/LuaSL/testLua/yueliang-0.4.1/test_lua/README new file mode 100644 index 0000000..07d9eb2 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/README | |||
@@ -0,0 +1,65 @@ | |||
1 | test_scripts-5.*.lua | ||
2 | ---------------------- | ||
3 | |||
4 | The scripts test_scripts*.lua are for exercising the frontends. Such | ||
5 | testing is non-exhaustive, but useful all the same. | ||
6 | |||
7 | The files in the 5.0 directory are the sample scripts from the Lua 5.0.x | ||
8 | test directory. Ditto for the 5.1 directory. See the COPYRIGHT_Lua5 for | ||
9 | the copyright notice. | ||
10 | |||
11 | For example, to run the 5.0.x script tester: | ||
12 | |||
13 | >lua test_scripts-5.0.lua | ||
14 | |||
15 | Or, if you have both Lua 5.0.x and Lua 5.1.x, you can prepare two | ||
16 | binaries and run them like this: | ||
17 | |||
18 | >lua5.0 test_scripts-5.0.lua | ||
19 | >lua5.1 test_scripts-5.1.lua | ||
20 | |||
21 | If the compilation result is exact, "CORRECT" is printed, otherwise both | ||
22 | binary chunks are written to the current directory as bc1.out and | ||
23 | bc2.out. You can use a disassembly tool like ChunkSpy to generate | ||
24 | listings for both files, then they can be compared with a visual diff | ||
25 | tool. ChunkSpy 0.9.8 supports 5.0.x and 5.1.x. | ||
26 | |||
27 | For testing additional files, add the argument "ALL" like this: | ||
28 | |||
29 | >lua test_scripts-5.0.lua ALL | ||
30 | |||
31 | This will pull in additional personal script files for testing. But in | ||
32 | order to do so, you'd have to adjust the files files-other-*.txt. | ||
33 | |||
34 | Current status: | ||
35 | |||
36 | Frontend version File set Result | ||
37 | --------------------------------------------------------------------- | ||
38 | Yueliang 5.0.3 files-lua-5.0.txt ALL CORRECT | ||
39 | files-yueliang-5.0.txt ALL CORRECT | ||
40 | Yueliang 5.1.2 files-lua-5.1.txt ALL CORRECT | ||
41 | files-yueliang-5.1.txt ALL CORRECT | ||
42 | --------------------------------------------------------------------- | ||
43 | |||
44 | test_parser-5.x.lua | ||
45 | ------------------- | ||
46 | |||
47 | The script files test_parser-5.0.lua and test_parser-5.1.lua contains | ||
48 | many test cases for both parsers. There are a lot of failure cases as | ||
49 | well as pass cases in order to exercise the parsers well, though the | ||
50 | test list is not exhaustive. | ||
51 | |||
52 | test_parser-5.0.lua 503 test cases | ||
53 | test_parser-5.1.lua 524 test cases | ||
54 | |||
55 | For the actual test scripts, see test_lparser2.lua in the respective | ||
56 | test directories of each front end. The 5.0.x front end of Yueliang | ||
57 | passed all the tests in test_parser-5.0.lua without any failures, while | ||
58 | testing of the 5.1.x front end using test_parser-5.1.lua led to one | ||
59 | bug found and fixed. | ||
60 | |||
61 | For Lua 5.0.2, see Yueliang 0.1.3, which was the last release of Lua | ||
62 | 5.0.2 material. | ||
63 | |||
64 | For Lua 5.1.1, see Yueliang 0.2.1, which was the last release of Lua | ||
65 | 5.1.1 material. | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/files-lua-5.0.txt b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-lua-5.0.txt new file mode 100644 index 0000000..d6b0c3c --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-lua-5.0.txt | |||
@@ -0,0 +1,21 @@ | |||
1 | # these are the sample scripts found in Lua 5.0.x | ||
2 | 5.0/bisect.lua | ||
3 | 5.0/cf.lua | ||
4 | 5.0/echo.lua | ||
5 | 5.0/env.lua | ||
6 | 5.0/factorial.lua | ||
7 | 5.0/fib.lua | ||
8 | 5.0/fibfor.lua | ||
9 | 5.0/globals.lua | ||
10 | 5.0/hello.lua | ||
11 | 5.0/life.lua | ||
12 | 5.0/luac.lua | ||
13 | 5.0/printf.lua | ||
14 | 5.0/readonly.lua | ||
15 | 5.0/sieve.lua | ||
16 | 5.0/sort.lua | ||
17 | 5.0/table.lua | ||
18 | 5.0/trace-calls.lua | ||
19 | 5.0/trace-globals.lua | ||
20 | 5.0/undefined.lua | ||
21 | 5.0/xd.lua | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/files-lua-5.1.txt b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-lua-5.1.txt new file mode 100644 index 0000000..a11d670 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-lua-5.1.txt | |||
@@ -0,0 +1,20 @@ | |||
1 | # these are the sample scripts found in Lua 5.1.x | ||
2 | 5.1/bisect.lua | ||
3 | 5.1/cf.lua | ||
4 | 5.1/echo.lua | ||
5 | 5.1/env.lua | ||
6 | 5.1/factorial.lua | ||
7 | 5.1/fib.lua | ||
8 | 5.1/fibfor.lua | ||
9 | 5.1/globals.lua | ||
10 | 5.1/hello.lua | ||
11 | 5.1/life.lua | ||
12 | 5.1/luac.lua | ||
13 | 5.1/printf.lua | ||
14 | 5.1/readonly.lua | ||
15 | 5.1/sieve.lua | ||
16 | 5.1/sort.lua | ||
17 | 5.1/table.lua | ||
18 | 5.1/trace-calls.lua | ||
19 | 5.1/trace-globals.lua | ||
20 | 5.1/xd.lua | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/files-other-5.0.txt b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-other-5.0.txt new file mode 100644 index 0000000..a661bc5 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-other-5.0.txt | |||
@@ -0,0 +1,21 @@ | |||
1 | # these are other 5.0.x scripts found in my project directories | ||
2 | # WARNING currently out of date... | ||
3 | |||
4 | ../../SciTELuaExporters/SciTE_ExportBase.lua | ||
5 | ../../SciTELuaExporters/SciTE_ExporterABW.lua | ||
6 | ../../SciTELuaExporters/SciTE_ExporterHTML.lua | ||
7 | ../../SciTELuaExporters/SciTE_ExporterHTMLPlus.lua | ||
8 | ../../SciTELuaExporters/SciTE_ExporterODT.lua | ||
9 | ../../SciTELuaExporters/SciTE_ExporterPDF.lua | ||
10 | ../../SciTELuaExporters/SciTE_ExporterPDFPlus.lua | ||
11 | ../../SciTELuaExporters/SciTE_ExporterRTF.lua | ||
12 | ../../SciTELuaExporters/SciTE_ExporterSXW.lua | ||
13 | ../../SciTELuaExporters/SciTE_ExporterTeX.lua | ||
14 | ../../SciTELuaExporters/SciTE_ExporterTemplate.lua | ||
15 | ../../SciTELuaExporters/SciTE_ExporterXML.lua | ||
16 | ../../LuaSrcDiet/LuaSrcDiet.lua | ||
17 | ../../LuaSrcDiet/LuaSrcDiet_.lua | ||
18 | ../../ChunkBake/ChunkBake.lua | ||
19 | ../../ChunkBake/misc/TestRig.lua | ||
20 | ../../ChunkSpy/5.0.2/ChunkSpy.lua | ||
21 | ../../ChunkSpy/5.1-work4/ChunkSpy.lua | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/files-other-5.1.txt b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-other-5.1.txt new file mode 100644 index 0000000..fa5827d --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-other-5.1.txt | |||
@@ -0,0 +1,3 @@ | |||
1 | # these are other 5.1.x scripts found in my project directories | ||
2 | # WARNING currently out of date... | ||
3 | |||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/files-yueliang-5.0.txt b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-yueliang-5.0.txt new file mode 100644 index 0000000..72e1da9 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-yueliang-5.0.txt | |||
@@ -0,0 +1,34 @@ | |||
1 | # these are yueliang files for 5.0.x | ||
2 | |||
3 | test_scripts-5.0.lua | ||
4 | |||
5 | ../orig-5.0.3/lzio.lua | ||
6 | ../orig-5.0.3/ldump.lua | ||
7 | ../orig-5.0.3/lopcodes.lua | ||
8 | ../orig-5.0.3/llex.lua | ||
9 | ../orig-5.0.3/lparser.lua | ||
10 | ../orig-5.0.3/lcode.lua | ||
11 | ../orig-5.0.3/luac.lua | ||
12 | |||
13 | ../orig-5.0.3/test/test_lzio.lua | ||
14 | ../orig-5.0.3/test/test_ldump.lua | ||
15 | ../orig-5.0.3/test/test_llex.lua | ||
16 | ../orig-5.0.3/test/test_lparser.lua | ||
17 | ../orig-5.0.3/test/test_number.lua | ||
18 | ../orig-5.0.3/test/bench_llex.lua | ||
19 | ../orig-5.0.3/tools/call_graph.lua | ||
20 | ../orig-5.0.3/tools/sample_expr.lua | ||
21 | |||
22 | ../nat-5.0.3/lzio_mk2.lua | ||
23 | ../nat-5.0.3/lzio_mk4.lua | ||
24 | ../nat-5.0.3/llex_mk2.lua | ||
25 | ../nat-5.0.3/llex_mk3.lua | ||
26 | ../nat-5.0.3/llex_mk4.lua | ||
27 | |||
28 | ../nat-5.0.3/test/test_lzio_mk2.lua | ||
29 | ../nat-5.0.3/test/test_llex_mk2.lua | ||
30 | ../nat-5.0.3/test/test_llex_mk3.lua | ||
31 | ../nat-5.0.3/test/test_llex_mk4.lua | ||
32 | ../nat-5.0.3/test/bench_llex_mk2.lua | ||
33 | ../nat-5.0.3/test/bench_llex_mk3.lua | ||
34 | ../nat-5.0.3/test/bench_llex_mk4.lua | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/files-yueliang-5.1.txt b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-yueliang-5.1.txt new file mode 100644 index 0000000..6a112d9 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/files-yueliang-5.1.txt | |||
@@ -0,0 +1,19 @@ | |||
1 | # these are yueliang files for 5.1.x | ||
2 | |||
3 | test_scripts-5.1.lua | ||
4 | |||
5 | ../orig-5.1.3/lzio.lua | ||
6 | ../orig-5.1.3/ldump.lua | ||
7 | ../orig-5.1.3/lopcodes.lua | ||
8 | ../orig-5.1.3/llex.lua | ||
9 | ../orig-5.1.3/lparser.lua | ||
10 | ../orig-5.1.3/lcode.lua | ||
11 | ../orig-5.1.3/luac.lua | ||
12 | |||
13 | ../orig-5.1.3/test/test_lzio.lua | ||
14 | ../orig-5.1.3/test/test_ldump.lua | ||
15 | ../orig-5.1.3/test/test_llex.lua | ||
16 | ../orig-5.1.3/test/test_lparser.lua | ||
17 | ../orig-5.1.3/test/test_number.lua | ||
18 | ../orig-5.1.3/test/bench_llex.lua | ||
19 | |||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/test_parser-5.0.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/test_parser-5.0.lua new file mode 100644 index 0000000..b563ed3 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/test_parser-5.0.lua | |||
@@ -0,0 +1,761 @@ | |||
1 | --[[-------------------------------------------------------------------- | ||
2 | |||
3 | test_parser-5.0.lua | ||
4 | Lua 5.0.x parser test cases | ||
5 | This file is part of Yueliang. | ||
6 | |||
7 | Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net> | ||
8 | The COPYRIGHT file describes the conditions | ||
9 | under which this software may be distributed. | ||
10 | |||
11 | See the ChangeLog for more information. | ||
12 | |||
13 | ----------------------------------------------------------------------]] | ||
14 | |||
15 | --[[-------------------------------------------------------------------- | ||
16 | -- Notes: | ||
17 | -- * there is no intention of properly specifying a grammar; the notes | ||
18 | -- are meant to reflect the structure of lparser so that it is easy | ||
19 | -- to locate and modify lparser if so desired | ||
20 | -- * some test may have invalid expressions but will compile, this | ||
21 | -- is because the chunk hasn't been executed yet | ||
22 | ----------------------------------------------------------------------]] | ||
23 | |||
24 | --[[-------------------------------------------------------------------- | ||
25 | -- lparser parsing structure, the Lua 5.0.x version... | ||
26 | ----------------------------------------------------------------------]] | ||
27 | -------------------------------------------------------------------- | ||
28 | -- chunk -> { stat [';'] } | ||
29 | -------------------------------------------------------------------- | ||
30 | -- stat -> DO block END | ||
31 | -- block -> chunk | ||
32 | -------------------------------------------------------------------- | ||
33 | -- stat -> breakstat | ||
34 | -- breakstat -> BREAK | ||
35 | -- * must have a loop to break | ||
36 | -- * must be last statement in a block | ||
37 | -------------------------------------------------------------------- | ||
38 | -- stat -> retstat | ||
39 | -- retstat -> RETURN explist | ||
40 | -- * must be last statement in a block | ||
41 | -------------------------------------------------------------------- | ||
42 | -- stat -> exprstat | ||
43 | -- exprstat -> primaryexp | ||
44 | -- (-> func | assignment) | ||
45 | -- * if LHS is VCALL then func, otherwise assignment | ||
46 | -- * for func, LHS is VCALL if funcargs in expression | ||
47 | -------------------------------------------------------------------- | ||
48 | -- stat -> funcstat | ||
49 | -- funcstat -> FUNCTION funcname body | ||
50 | -- funcname -> NAME {'.' NAME} [':' NAME] | ||
51 | -------------------------------------------------------------------- | ||
52 | -- body -> '(' parlist ')' chunk END | ||
53 | -- parlist -> [ param { ',' param } ] | ||
54 | -- param -> NAME | ||
55 | -- * DOTS must be the last parameter in a parlist | ||
56 | -------------------------------------------------------------------- | ||
57 | -- stat -> LOCAL localstat | ||
58 | -- LOCAL localstat -> LOCAL NAME {',' NAME} ['=' explist1] | ||
59 | -- explist1 -> expr { ',' expr } | ||
60 | -------------------------------------------------------------------- | ||
61 | -- stat -> LOCAL FUNCTION localfunc | ||
62 | -- LOCAL FUNCTION localfunc -> LOCAL FUNCTION NAME body | ||
63 | -------------------------------------------------------------------- | ||
64 | -- stat -> ifstat | ||
65 | -- ifstat -> IF cond THEN block | ||
66 | -- {ELSEIF cond THEN block} | ||
67 | -- [ELSE block] END | ||
68 | -- block -> chunk | ||
69 | -- cond -> expr | ||
70 | -------------------------------------------------------------------- | ||
71 | -- stat -> forstat | ||
72 | -- forstat -> fornum | forlist | ||
73 | -- forlist -> NAME {,NAME} IN explist1 forbody END | ||
74 | -- fornum -> NAME = exp1,exp1[,exp1] forbody END | ||
75 | -- forbody -> DO block | ||
76 | -- block -> chunk | ||
77 | -------------------------------------------------------------------- | ||
78 | -- stat -> whilestat | ||
79 | -- whilestat -> WHILE cond DO block END | ||
80 | -- block -> chunk | ||
81 | -- cond -> expr | ||
82 | -------------------------------------------------------------------- | ||
83 | -- stat -> repeatstat | ||
84 | -- repeatstat -> REPEAT block UNTIL cond | ||
85 | -- block -> chunk | ||
86 | -- cond -> expr | ||
87 | -------------------------------------------------------------------- | ||
88 | -- assignment -> ',' primaryexp assignment | ||
89 | -- | '=' explist1 | ||
90 | -- explist1 -> expr { ',' expr } | ||
91 | -- * for assignment, LHS must be LOCAL, UPVAL, GLOBAL or INDEXED | ||
92 | -------------------------------------------------------------------- | ||
93 | -- primaryexp -> prefixexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } | ||
94 | -- prefixexp -> NAME | '(' expr ')' | ||
95 | -- funcargs -> '(' [ explist1 ] ')' | constructor | STRING | ||
96 | -- * funcargs turn an expr into a function call | ||
97 | -------------------------------------------------------------------- | ||
98 | -- expr -> subexpr | ||
99 | -- subexpr -> (UNOPR subexpr | simpleexp) { BINOPR subexpr } | ||
100 | -- simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ||
101 | -- | constructor | FUNCTION body | primaryexp | ||
102 | -------------------------------------------------------------------- | ||
103 | -- constructor -> '{' [ field { fieldsep field } [ fieldsep ] ] '}' | ||
104 | -- fieldsep -> ',' | ';' | ||
105 | -- field -> recfield | listfield | ||
106 | -- recfield -> ( NAME | '[' exp1 ']' ) = exp1 | ||
107 | -- listfield -> expr | ||
108 | -------------------------------------------------------------------- | ||
109 | |||
110 | --[[-------------------------------------------------------------------- | ||
111 | -- parser test cases, Lua 5.0.x | ||
112 | -- * uncomment string delimiter to enable syntax highlighting... | ||
113 | -- * anything that matches "^%s*%-%-" is a comment | ||
114 | -- * headings to display are "^%s*TESTS:%s*(.*)$" | ||
115 | -- * FAIL test cases should match "%s*%-%-%s*FAIL%s*$" | ||
116 | ----------------------------------------------------------------------]] | ||
117 | tests_source = [[ | ||
118 | -------------------------------------------------------------------- | ||
119 | TESTS: empty chunks | ||
120 | -------------------------------------------------------------------- | ||
121 | -- chunk -> { stat [';'] } | ||
122 | -------------------------------------------------------------------- | ||
123 | |||
124 | ; -- FAIL | ||
125 | -------------------------------------------------------------------- | ||
126 | TESTS: optional semicolon, simple local statements | ||
127 | -------------------------------------------------------------------- | ||
128 | -- stat -> LOCAL localstat | ||
129 | -- LOCAL localstat -> LOCAL NAME {',' NAME} ['=' explist1] | ||
130 | -- explist1 -> expr { ',' expr } | ||
131 | -------------------------------------------------------------------- | ||
132 | local -- FAIL | ||
133 | local; -- FAIL | ||
134 | local = -- FAIL | ||
135 | local end -- FAIL | ||
136 | local a | ||
137 | local a; | ||
138 | local a, b, c | ||
139 | local a; local b local c; | ||
140 | local a = 1 | ||
141 | local a local b = a | ||
142 | local a, b = 1, 2 | ||
143 | local a, b, c = 1, 2, 3 | ||
144 | local a, b, c = 1 | ||
145 | local a = 1, 2, 3 | ||
146 | local a, local -- FAIL | ||
147 | local 1 -- FAIL | ||
148 | local "foo" -- FAIL | ||
149 | local a = local -- FAIL | ||
150 | local a, b, = -- FAIL | ||
151 | local a, b = 1, local -- FAIL | ||
152 | local a, b = , local -- FAIL | ||
153 | -------------------------------------------------------------------- | ||
154 | TESTS: simple DO blocks | ||
155 | -------------------------------------------------------------------- | ||
156 | -- stat -> DO block END | ||
157 | -- block -> chunk | ||
158 | -------------------------------------------------------------------- | ||
159 | do -- FAIL | ||
160 | end -- FAIL | ||
161 | do end | ||
162 | do ; end -- FAIL | ||
163 | do 1 end -- FAIL | ||
164 | do "foo" end -- FAIL | ||
165 | do local a, b end | ||
166 | do local a local b end | ||
167 | do local a; local b; end | ||
168 | do local a = 1 end | ||
169 | do do end end | ||
170 | do do end; end | ||
171 | do do do end end end | ||
172 | do do do end; end; end | ||
173 | do do do return end end end | ||
174 | do end do -- FAIL | ||
175 | do end end -- FAIL | ||
176 | do return end | ||
177 | do return return end -- FAIL | ||
178 | do break end -- FAIL | ||
179 | -------------------------------------------------------------------- | ||
180 | TESTS: simple WHILE loops | ||
181 | -------------------------------------------------------------------- | ||
182 | -- stat -> whilestat | ||
183 | -- whilestat -> WHILE cond DO block END | ||
184 | -- block -> chunk | ||
185 | -- cond -> expr | ||
186 | -------------------------------------------------------------------- | ||
187 | while -- FAIL | ||
188 | while do -- FAIL | ||
189 | while = -- FAIL | ||
190 | while 1 do -- FAIL | ||
191 | while 1 do end | ||
192 | while 1 do local a end | ||
193 | while 1 do local a local b end | ||
194 | while 1 do local a; local b; end | ||
195 | while 1 do 2 end -- FAIL | ||
196 | while 1 do "foo" end -- FAIL | ||
197 | while true do end | ||
198 | while 1 do ; end -- FAIL | ||
199 | while 1 do while -- FAIL | ||
200 | while 1 end -- FAIL | ||
201 | while 1 2 do -- FAIL | ||
202 | while 1 = 2 do -- FAIL | ||
203 | while 1 do return end | ||
204 | while 1 do return return end -- FAIL | ||
205 | while 1 do do end end | ||
206 | while 1 do do return end end | ||
207 | while 1 do break end | ||
208 | while 1 do break break end -- FAIL | ||
209 | while 1 do do break end end | ||
210 | -------------------------------------------------------------------- | ||
211 | TESTS: simple REPEAT loops | ||
212 | -------------------------------------------------------------------- | ||
213 | -- stat -> repeatstat | ||
214 | -- repeatstat -> REPEAT block UNTIL cond | ||
215 | -- block -> chunk | ||
216 | -- cond -> expr | ||
217 | -------------------------------------------------------------------- | ||
218 | repeat -- FAIL | ||
219 | repeat until -- FAIL | ||
220 | repeat until 0 | ||
221 | repeat until false | ||
222 | repeat until local -- FAIL | ||
223 | repeat end -- FAIL | ||
224 | repeat 1 -- FAIL | ||
225 | repeat = -- FAIL | ||
226 | repeat local a until 1 | ||
227 | repeat local a local b until 0 | ||
228 | repeat local a; local b; until 0 | ||
229 | repeat ; until 1 -- FAIL | ||
230 | repeat 2 until 1 -- FAIL | ||
231 | repeat "foo" until 1 -- FAIL | ||
232 | repeat return until 0 | ||
233 | repeat return return until 0 -- FAIL | ||
234 | repeat break until 0 | ||
235 | repeat break break until 0 -- FAIL | ||
236 | repeat do end until 0 | ||
237 | repeat do return end until 0 | ||
238 | repeat do break end until 0 | ||
239 | -------------------------------------------------------------------- | ||
240 | TESTS: simple FOR loops | ||
241 | -------------------------------------------------------------------- | ||
242 | -- stat -> forstat | ||
243 | -- forstat -> fornum | forlist | ||
244 | -- forlist -> NAME {,NAME} IN explist1 forbody END | ||
245 | -- fornum -> NAME = exp1,exp1[,exp1] forbody END | ||
246 | -- forbody -> DO block | ||
247 | -- block -> chunk | ||
248 | -------------------------------------------------------------------- | ||
249 | for -- FAIL | ||
250 | for do -- FAIL | ||
251 | for end -- FAIL | ||
252 | for 1 -- FAIL | ||
253 | for a -- FAIL | ||
254 | for true -- FAIL | ||
255 | for a, in -- FAIL | ||
256 | for a in -- FAIL | ||
257 | for a do -- FAIL | ||
258 | for a in do -- FAIL | ||
259 | for a in b do -- FAIL | ||
260 | for a in b end -- FAIL | ||
261 | for a in b, do -- FAIL | ||
262 | for a in b do end | ||
263 | for a in b do local a local b end | ||
264 | for a in b do local a; local b; end | ||
265 | for a in b do 1 end -- FAIL | ||
266 | for a in b do "foo" end -- FAIL | ||
267 | for a b in -- FAIL | ||
268 | for a, b, c in p do end | ||
269 | for a, b, c in p, q, r do end | ||
270 | for a in 1 do end | ||
271 | for a in true do end | ||
272 | for a in "foo" do end | ||
273 | for a in b do break end | ||
274 | for a in b do break break end -- FAIL | ||
275 | for a in b do return end | ||
276 | for a in b do return return end -- FAIL | ||
277 | for a in b do do end end | ||
278 | for a in b do do break end end | ||
279 | for a in b do do return end end | ||
280 | for = -- FAIL | ||
281 | for a = -- FAIL | ||
282 | for a, b = -- FAIL | ||
283 | for a = do -- FAIL | ||
284 | for a = 1, do -- FAIL | ||
285 | for a = p, q, do -- FAIL | ||
286 | for a = p q do -- FAIL | ||
287 | for a = b do end -- FAIL | ||
288 | for a = 1, 2, 3, 4 do end -- FAIL | ||
289 | for a = p, q do end | ||
290 | for a = 1, 2 do end | ||
291 | for a = 1, 2 do local a local b end | ||
292 | for a = 1, 2 do local a; local b; end | ||
293 | for a = 1, 2 do 3 end -- FAIL | ||
294 | for a = 1, 2 do "foo" end -- FAIL | ||
295 | for a = p, q, r do end | ||
296 | for a = 1, 2, 3 do end | ||
297 | for a = p, q do break end | ||
298 | for a = p, q do break break end -- FAIL | ||
299 | for a = 1, 2 do return end | ||
300 | for a = 1, 2 do return return end -- FAIL | ||
301 | for a = p, q do do end end | ||
302 | for a = p, q do do break end end | ||
303 | for a = p, q do do return end end | ||
304 | -------------------------------------------------------------------- | ||
305 | TESTS: break statement | ||
306 | -------------------------------------------------------------------- | ||
307 | -- stat -> breakstat | ||
308 | -- breakstat -> BREAK | ||
309 | -- * must have a loop to break | ||
310 | -- * must be last statement in a block | ||
311 | -------------------------------------------------------------------- | ||
312 | break -- FAIL | ||
313 | -------------------------------------------------------------------- | ||
314 | TESTS: return statement | ||
315 | -------------------------------------------------------------------- | ||
316 | -- stat -> retstat | ||
317 | -- retstat -> RETURN explist | ||
318 | -- * must be last statement in a block | ||
319 | -------------------------------------------------------------------- | ||
320 | return | ||
321 | return; | ||
322 | return return -- FAIL | ||
323 | return 1 | ||
324 | return local -- FAIL | ||
325 | return "foo" | ||
326 | return 1, -- FAIL | ||
327 | return 1,2,3 | ||
328 | return a,b,c,d | ||
329 | return 1,2; | ||
330 | -------------------------------------------------------------------- | ||
331 | TESTS: conditional statements | ||
332 | -------------------------------------------------------------------- | ||
333 | -- stat -> ifstat | ||
334 | -- ifstat -> IF cond THEN block | ||
335 | -- {ELSEIF cond THEN block} | ||
336 | -- [ELSE block] END | ||
337 | -- block -> chunk | ||
338 | -- cond -> expr | ||
339 | -------------------------------------------------------------------- | ||
340 | if -- FAIL | ||
341 | elseif -- FAIL | ||
342 | else -- FAIL | ||
343 | then -- FAIL | ||
344 | if then -- FAIL | ||
345 | if 1 -- FAIL | ||
346 | if 1 then -- FAIL | ||
347 | if 1 else -- FAIL | ||
348 | if 1 then else -- FAIL | ||
349 | if 1 then elseif -- FAIL | ||
350 | if 1 then end | ||
351 | if 1 then local a end | ||
352 | if 1 then local a local b end | ||
353 | if 1 then local a; local b; end | ||
354 | if 1 then else end | ||
355 | if 1 then local a else local b end | ||
356 | if 1 then local a; else local b; end | ||
357 | if 1 then elseif 2 -- FAIL | ||
358 | if 1 then elseif 2 then -- FAIL | ||
359 | if 1 then elseif 2 then end | ||
360 | if 1 then local a elseif 2 then local b end | ||
361 | if 1 then local a; elseif 2 then local b; end | ||
362 | if 1 then elseif 2 then else end | ||
363 | if 1 then else if 2 then end end | ||
364 | if 1 then else if 2 then end -- FAIL | ||
365 | if 1 then break end -- FAIL | ||
366 | if 1 then return end | ||
367 | if 1 then return return end -- FAIL | ||
368 | if 1 then end; if 1 then end; | ||
369 | -------------------------------------------------------------------- | ||
370 | TESTS: function statements | ||
371 | -------------------------------------------------------------------- | ||
372 | -- stat -> funcstat | ||
373 | -- funcstat -> FUNCTION funcname body | ||
374 | -- funcname -> NAME {'.' NAME} [':' NAME] | ||
375 | -------------------------------------------------------------------- | ||
376 | -- body -> '(' parlist ')' chunk END | ||
377 | -- parlist -> [ param { ',' param } ] | ||
378 | -- param -> NAME | ||
379 | -- * DOTS must be the last parameter in a parlist | ||
380 | -------------------------------------------------------------------- | ||
381 | function -- FAIL | ||
382 | function 1 -- FAIL | ||
383 | function end -- FAIL | ||
384 | function a -- FAIL | ||
385 | function a end -- FAIL | ||
386 | function a( end -- FAIL | ||
387 | function a() end | ||
388 | function a(1 -- FAIL | ||
389 | function a("foo" -- FAIL | ||
390 | function a(p -- FAIL | ||
391 | function a(p,) -- FAIL | ||
392 | function a(p q -- FAIL | ||
393 | function a(p) end | ||
394 | function a(p,q,) end -- FAIL | ||
395 | function a(p,q,r) end | ||
396 | function a(p,q,1 -- FAIL | ||
397 | function a(p) do -- FAIL | ||
398 | function a(p) 1 end -- FAIL | ||
399 | function a(p) return end | ||
400 | function a(p) break end -- FAIL | ||
401 | function a(p) return return end -- FAIL | ||
402 | function a(p) do end end | ||
403 | function a.( -- FAIL | ||
404 | function a.1 -- FAIL | ||
405 | function a.b() end | ||
406 | function a.b, -- FAIL | ||
407 | function a.b.( -- FAIL | ||
408 | function a.b.c.d() end | ||
409 | function a: -- FAIL | ||
410 | function a:1 -- FAIL | ||
411 | function a:b() end | ||
412 | function a:b: -- FAIL | ||
413 | function a:b. -- FAIL | ||
414 | function a.b.c:d() end | ||
415 | function a(...) end | ||
416 | function a(..., -- FAIL | ||
417 | function a(p,...) end | ||
418 | function a(p,q,r,...) end | ||
419 | function a() local a local b end | ||
420 | function a() local a; local b; end | ||
421 | function a() end; function a() end; | ||
422 | -------------------------------------------------------------------- | ||
423 | TESTS: local function statements | ||
424 | -------------------------------------------------------------------- | ||
425 | -- stat -> LOCAL FUNCTION localfunc | ||
426 | -- LOCAL FUNCTION localfunc -> LOCAL FUNCTION NAME body | ||
427 | -------------------------------------------------------------------- | ||
428 | local function -- FAIL | ||
429 | local function 1 -- FAIL | ||
430 | local function end -- FAIL | ||
431 | local function a -- FAIL | ||
432 | local function a end -- FAIL | ||
433 | local function a( end -- FAIL | ||
434 | local function a() end | ||
435 | local function a(1 -- FAIL | ||
436 | local function a("foo" -- FAIL | ||
437 | local function a(p -- FAIL | ||
438 | local function a(p,) -- FAIL | ||
439 | local function a(p q -- FAIL | ||
440 | local function a(p) end | ||
441 | local function a(p,q,) end -- FAIL | ||
442 | local function a(p,q,r) end | ||
443 | local function a(p,q,1 -- FAIL | ||
444 | local function a(p) do -- FAIL | ||
445 | local function a(p) 1 end -- FAIL | ||
446 | local function a(p) return end | ||
447 | local function a(p) break end -- FAIL | ||
448 | local function a(p) return return end -- FAIL | ||
449 | local function a(p) do end end | ||
450 | local function a. -- FAIL | ||
451 | local function a: -- FAIL | ||
452 | local function a(...) end | ||
453 | local function a(..., -- FAIL | ||
454 | local function a(p,...) end | ||
455 | local function a(p,q,r,...) end | ||
456 | local function a() local a local b end | ||
457 | local function a() local a; local b; end | ||
458 | local function a() end; local function a() end; | ||
459 | -------------------------------------------------------------------- | ||
460 | -- stat -> exprstat | ||
461 | -- exprstat -> primaryexp | ||
462 | -- (-> func | assignment) | ||
463 | -- * if LHS is VCALL then func, otherwise assignment | ||
464 | -- * for func, LHS is VCALL if funcargs in expression | ||
465 | -------------------------------------------------------------------- | ||
466 | TESTS: assignments | ||
467 | -------------------------------------------------------------------- | ||
468 | -- assignment -> ',' primaryexp assignment | ||
469 | -- | '=' explist1 | ||
470 | -- explist1 -> expr { ',' expr } | ||
471 | -- * for assignment, LHS must be LOCAL, UPVAL, GLOBAL or INDEXED | ||
472 | -------------------------------------------------------------------- | ||
473 | a -- FAIL | ||
474 | a, -- FAIL | ||
475 | a,b,c -- FAIL | ||
476 | a,b = -- FAIL | ||
477 | a = 1 | ||
478 | a = 1,2,3 | ||
479 | a,b,c = 1 | ||
480 | a,b,c = 1,2,3 | ||
481 | a.b = 1 | ||
482 | a.b.c = 1 | ||
483 | a[b] = 1 | ||
484 | a[b][c] = 1 | ||
485 | a.b[c] = 1 | ||
486 | a[b].c = 1 | ||
487 | 0 = -- FAIL | ||
488 | "foo" = -- FAIL | ||
489 | true = -- FAIL | ||
490 | (a) = -- FAIL | ||
491 | {} = -- FAIL | ||
492 | a:b() = -- FAIL | ||
493 | a() = -- FAIL | ||
494 | a.b:c() = -- FAIL | ||
495 | a[b]() = -- FAIL | ||
496 | a = a b -- FAIL | ||
497 | a = 1 2 -- FAIL | ||
498 | a = a = 1 -- FAIL | ||
499 | -------------------------------------------------------------------- | ||
500 | TESTS: function calls | ||
501 | -------------------------------------------------------------------- | ||
502 | -- primaryexp -> prefixexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } | ||
503 | -- prefixexp -> NAME | '(' expr ')' | ||
504 | -- funcargs -> '(' [ explist1 ] ')' | constructor | STRING | ||
505 | -- * funcargs turn an expr into a function call | ||
506 | -------------------------------------------------------------------- | ||
507 | a( -- FAIL | ||
508 | a() | ||
509 | a(1) | ||
510 | a(1,) -- FAIL | ||
511 | a(1,2,3) | ||
512 | 1() -- FAIL | ||
513 | a()() | ||
514 | a.b() | ||
515 | a[b]() | ||
516 | a.1 -- FAIL | ||
517 | a.b -- FAIL | ||
518 | a[b] -- FAIL | ||
519 | a.b.( -- FAIL | ||
520 | a.b.c() | ||
521 | a[b][c]() | ||
522 | a[b].c() | ||
523 | a.b[c]() | ||
524 | a:b() | ||
525 | a:b -- FAIL | ||
526 | a:1 -- FAIL | ||
527 | a.b:c() | ||
528 | a[b]:c() | ||
529 | a:b: -- FAIL | ||
530 | a:b():c() | ||
531 | a:b().c[d]:e() | ||
532 | a:b()[c].d:e() | ||
533 | (a)() | ||
534 | ()() -- FAIL | ||
535 | (1)() | ||
536 | ("foo")() | ||
537 | (true)() | ||
538 | (a)()() | ||
539 | (a.b)() | ||
540 | (a[b])() | ||
541 | (a).b() | ||
542 | (a)[b]() | ||
543 | (a):b() | ||
544 | (a).b[c]:d() | ||
545 | (a)[b].c:d() | ||
546 | (a):b():c() | ||
547 | (a):b().c[d]:e() | ||
548 | (a):b()[c].d:e() | ||
549 | -------------------------------------------------------------------- | ||
550 | TESTS: more function calls | ||
551 | -------------------------------------------------------------------- | ||
552 | a"foo" | ||
553 | a[[foo]] | ||
554 | a.b"foo" | ||
555 | a[b]"foo" | ||
556 | a:b"foo" | ||
557 | a{} | ||
558 | a.b{} | ||
559 | a[b]{} | ||
560 | a:b{} | ||
561 | a()"foo" | ||
562 | a"foo"() | ||
563 | a"foo".b() | ||
564 | a"foo"[b]() | ||
565 | a"foo":c() | ||
566 | a"foo""bar" | ||
567 | a"foo"{} | ||
568 | (a):b"foo".c[d]:e"bar" | ||
569 | (a):b"foo"[c].d:e"bar" | ||
570 | a(){} | ||
571 | a{}() | ||
572 | a{}.b() | ||
573 | a{}[b]() | ||
574 | a{}:c() | ||
575 | a{}"foo" | ||
576 | a{}{} | ||
577 | (a):b{}.c[d]:e{} | ||
578 | (a):b{}[c].d:e{} | ||
579 | -------------------------------------------------------------------- | ||
580 | TESTS: simple expressions | ||
581 | -------------------------------------------------------------------- | ||
582 | -- simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ||
583 | -- | constructor | FUNCTION body | primaryexp | ||
584 | -------------------------------------------------------------------- | ||
585 | a = -- FAIL | ||
586 | a = a | ||
587 | a = nil | ||
588 | a = false | ||
589 | a = 1 | ||
590 | a = "foo" | ||
591 | a = [[foo]] | ||
592 | a = {} | ||
593 | a = (a) | ||
594 | a = (nil) | ||
595 | a = (true) | ||
596 | a = (1) | ||
597 | a = ("foo") | ||
598 | a = ([[foo]]) | ||
599 | a = ({}) | ||
600 | a = a.b | ||
601 | a = a.b. -- FAIL | ||
602 | a = a.b.c | ||
603 | a = a:b -- FAIL | ||
604 | a = a[b] | ||
605 | a = a[1] | ||
606 | a = a["foo"] | ||
607 | a = a[b][c] | ||
608 | a = a.b[c] | ||
609 | a = a[b].c | ||
610 | a = (a)[b] | ||
611 | a = (a).c | ||
612 | a = () -- FAIL | ||
613 | a = a() | ||
614 | a = a.b() | ||
615 | a = a[b]() | ||
616 | a = a:b() | ||
617 | a = (a)() | ||
618 | a = (a).b() | ||
619 | a = (a)[b]() | ||
620 | a = (a):b() | ||
621 | a = a"foo" | ||
622 | a = a{} | ||
623 | a = function -- FAIL | ||
624 | a = function 1 -- FAIL | ||
625 | a = function a -- FAIL | ||
626 | a = function end -- FAIL | ||
627 | a = function( -- FAIL | ||
628 | a = function() end | ||
629 | a = function(1 -- FAIL | ||
630 | a = function(p) end | ||
631 | a = function(p,) -- FAIL | ||
632 | a = function(p q -- FAIL | ||
633 | a = function(p,q,r) end | ||
634 | a = function(p,q,1 -- FAIL | ||
635 | a = function(...) end | ||
636 | a = function(..., -- FAIL | ||
637 | a = function(p,...) end | ||
638 | a = function(p,q,r,...) end | ||
639 | -------------------------------------------------------------------- | ||
640 | TESTS: operators | ||
641 | -------------------------------------------------------------------- | ||
642 | -- expr -> subexpr | ||
643 | -- subexpr -> (UNOPR subexpr | simpleexp) { BINOPR subexpr } | ||
644 | -- simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ||
645 | -- | constructor | FUNCTION body | primaryexp | ||
646 | -------------------------------------------------------------------- | ||
647 | a = -10 | ||
648 | a = -"foo" | ||
649 | a = -a | ||
650 | a = -nil | ||
651 | a = -true | ||
652 | a = -{} | ||
653 | a = -function() end | ||
654 | a = -a() | ||
655 | a = -(a) | ||
656 | a = - -- FAIL | ||
657 | a = not 10 | ||
658 | a = not "foo" | ||
659 | a = not a | ||
660 | a = not nil | ||
661 | a = not true | ||
662 | a = not {} | ||
663 | a = not function() end | ||
664 | a = not a() | ||
665 | a = not (a) | ||
666 | a = not -- FAIL | ||
667 | a = 1 + 2; a = 1 - 2 | ||
668 | a = 1 * 2; a = 1 / 2 | ||
669 | a = 1 ^ 2; a = 1 .. 2 | ||
670 | a = 1 + -- FAIL | ||
671 | a = 1 .. -- FAIL | ||
672 | a = 1 * / -- FAIL | ||
673 | a = 1 + -2; a = 1 - -2 | ||
674 | a = 1 * - -- FAIL | ||
675 | a = 1 * not 2; a = 1 / not 2 | ||
676 | a = 1 / not -- FAIL | ||
677 | a = 1 + 2 - 3 * 4 / 5 ^ 6 | ||
678 | a = ((1 + 2) - 3) * (4 / (5 ^ 6)) | ||
679 | a = (1 + (2 - (3 * (4 / (5 ^ ((6))))))) | ||
680 | a = ((1 -- FAIL | ||
681 | a = ((1 + 2) -- FAIL | ||
682 | a = 1) -- FAIL | ||
683 | a = a + b - c | ||
684 | a = "foo" + "bar" | ||
685 | a = "foo".."bar".."baz" | ||
686 | a = true + false - nil | ||
687 | a = {} * {} | ||
688 | a = function() end / function() end | ||
689 | a = a() ^ b() | ||
690 | -------------------------------------------------------------------- | ||
691 | TESTS: more operators | ||
692 | -------------------------------------------------------------------- | ||
693 | a = 1 == 2; a = 1 ~= 2 | ||
694 | a = 1 < 2; a = 1 <= 2 | ||
695 | a = 1 > 2; a = 1 >= 2 | ||
696 | a = 1 < 2 < 3 | ||
697 | a = 1 >= 2 >= 3 | ||
698 | a = 1 == -- FAIL | ||
699 | a = ~= 2 -- FAIL | ||
700 | a = "foo" == "bar" | ||
701 | a = "foo" > "bar" | ||
702 | a = a ~= b | ||
703 | a = true == false | ||
704 | a = 1 and 2; a = 1 or 2 | ||
705 | a = 1 and -- FAIL | ||
706 | a = or 1 -- FAIL | ||
707 | a = 1 and 2 and 3 | ||
708 | a = 1 or 2 or 3 | ||
709 | a = 1 and 2 or 3 | ||
710 | a = a and b or c | ||
711 | a = a() and (b)() or c.d | ||
712 | a = "foo" and "bar" | ||
713 | a = true or false | ||
714 | a = {} and {} or {} | ||
715 | a = (1) and ("foo") or (nil) | ||
716 | a = function() end == function() end | ||
717 | a = function() end or function() end | ||
718 | -------------------------------------------------------------------- | ||
719 | TESTS: constructors | ||
720 | -------------------------------------------------------------------- | ||
721 | -- constructor -> '{' [ field { fieldsep field } [ fieldsep ] ] '}' | ||
722 | -- fieldsep -> ',' | ';' | ||
723 | -- field -> recfield | listfield | ||
724 | -- recfield -> ( NAME | '[' exp1 ']' ) = exp1 | ||
725 | -- listfield -> expr | ||
726 | -------------------------------------------------------------------- | ||
727 | a = { -- FAIL | ||
728 | a = {} | ||
729 | a = {,} -- FAIL | ||
730 | a = {;} | ||
731 | a = {,,} -- FAIL | ||
732 | a = {;;} -- FAIL | ||
733 | a = {{ -- FAIL | ||
734 | a = {{{}}} | ||
735 | a = {{},{},{{}},} | ||
736 | a = { 1 } | ||
737 | a = { 1, } | ||
738 | a = { 1; } | ||
739 | a = { 1, 2 } | ||
740 | a = { a, b, c, } | ||
741 | a = { true; false, nil; } | ||
742 | a = { a.b, a[b]; a:c(), } | ||
743 | a = { 1 + 2, a > b, "a" or "b" } | ||
744 | a = { a=1, } | ||
745 | a = { a=1, b="foo", c=nil } | ||
746 | a = { a -- FAIL | ||
747 | a = { a= -- FAIL | ||
748 | a = { a=, -- FAIL | ||
749 | a = { a=; -- FAIL | ||
750 | a = { 1, a="foo" -- FAIL | ||
751 | a = { 1, a="foo"; b={}, d=true; } | ||
752 | a = { [ -- FAIL | ||
753 | a = { [1 -- FAIL | ||
754 | a = { [1] -- FAIL | ||
755 | a = { [a]= -- FAIL | ||
756 | a = { ["foo"]="bar" } | ||
757 | a = { [1]=a, [2]=b, } | ||
758 | a = { true, a=1; ["foo"]="bar", } | ||
759 | ]] | ||
760 | |||
761 | -- end of script | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/test_parser-5.1.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/test_parser-5.1.lua new file mode 100644 index 0000000..0809652 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/test_parser-5.1.lua | |||
@@ -0,0 +1,783 @@ | |||
1 | --[[-------------------------------------------------------------------- | ||
2 | |||
3 | test_parser-5.1.lua | ||
4 | Lua 5.1.x parser test cases | ||
5 | This file is part of Yueliang. | ||
6 | |||
7 | Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net> | ||
8 | The COPYRIGHT file describes the conditions | ||
9 | under which this software may be distributed. | ||
10 | |||
11 | See the ChangeLog for more information. | ||
12 | |||
13 | ----------------------------------------------------------------------]] | ||
14 | |||
15 | --[[-------------------------------------------------------------------- | ||
16 | -- Notes: | ||
17 | -- * there is no intention of properly specifying a grammar; the notes | ||
18 | -- are meant to reflect the structure of lparser so that it is easy | ||
19 | -- to locate and modify lparser if so desired | ||
20 | -- * some test may have invalid expressions but will compile, this | ||
21 | -- is because the chunk hasn't been executed yet | ||
22 | -- | ||
23 | -- Changed in 5.1.x: | ||
24 | -- * added ..., %, # cases | ||
25 | ----------------------------------------------------------------------]] | ||
26 | |||
27 | --[[-------------------------------------------------------------------- | ||
28 | -- lparser parsing structure, the Lua 5.1.x version... | ||
29 | ----------------------------------------------------------------------]] | ||
30 | -------------------------------------------------------------------- | ||
31 | -- chunk -> { stat [';'] } | ||
32 | -------------------------------------------------------------------- | ||
33 | -- stat -> DO block END | ||
34 | -- block -> chunk | ||
35 | -------------------------------------------------------------------- | ||
36 | -- stat -> breakstat | ||
37 | -- breakstat -> BREAK | ||
38 | -- * must have a loop to break | ||
39 | -- * must be last statement in a block | ||
40 | -------------------------------------------------------------------- | ||
41 | -- stat -> retstat | ||
42 | -- retstat -> RETURN explist | ||
43 | -- * must be last statement in a block | ||
44 | -------------------------------------------------------------------- | ||
45 | -- stat -> exprstat | ||
46 | -- exprstat -> primaryexp | ||
47 | -- (-> func | assignment) | ||
48 | -- * if LHS is VCALL then func, otherwise assignment | ||
49 | -- * for func, LHS is VCALL if funcargs in expression | ||
50 | -------------------------------------------------------------------- | ||
51 | -- stat -> funcstat | ||
52 | -- funcstat -> FUNCTION funcname body | ||
53 | -- funcname -> NAME {'.' NAME} [':' NAME] | ||
54 | -------------------------------------------------------------------- | ||
55 | -- body -> '(' parlist ')' chunk END | ||
56 | -- parlist -> [ param { ',' param } ] | ||
57 | -- param -> NAME | ||
58 | -- * DOTS must be the last parameter in a parlist | ||
59 | -------------------------------------------------------------------- | ||
60 | -- stat -> LOCAL localstat | ||
61 | -- LOCAL localstat -> LOCAL NAME {',' NAME} ['=' explist1] | ||
62 | -- explist1 -> expr { ',' expr } | ||
63 | -------------------------------------------------------------------- | ||
64 | -- stat -> LOCAL FUNCTION localfunc | ||
65 | -- LOCAL FUNCTION localfunc -> LOCAL FUNCTION NAME body | ||
66 | -------------------------------------------------------------------- | ||
67 | -- stat -> ifstat | ||
68 | -- ifstat -> IF cond THEN block | ||
69 | -- {ELSEIF cond THEN block} | ||
70 | -- [ELSE block] END | ||
71 | -- block -> chunk | ||
72 | -- cond -> expr | ||
73 | -------------------------------------------------------------------- | ||
74 | -- stat -> forstat | ||
75 | -- forstat -> fornum | forlist | ||
76 | -- forlist -> NAME {,NAME} IN explist1 forbody END | ||
77 | -- fornum -> NAME = exp1,exp1[,exp1] forbody END | ||
78 | -- forbody -> DO block | ||
79 | -- block -> chunk | ||
80 | -------------------------------------------------------------------- | ||
81 | -- stat -> whilestat | ||
82 | -- whilestat -> WHILE cond DO block END | ||
83 | -- block -> chunk | ||
84 | -- cond -> expr | ||
85 | -------------------------------------------------------------------- | ||
86 | -- stat -> repeatstat | ||
87 | -- repeatstat -> REPEAT chunk UNTIL cond | ||
88 | -- cond -> expr | ||
89 | -------------------------------------------------------------------- | ||
90 | -- assignment -> ',' primaryexp assignment | ||
91 | -- | '=' explist1 | ||
92 | -- explist1 -> expr { ',' expr } | ||
93 | -- * for assignment, LHS must be LOCAL, UPVAL, GLOBAL or INDEXED | ||
94 | -------------------------------------------------------------------- | ||
95 | -- primaryexp -> prefixexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } | ||
96 | -- prefixexp -> NAME | '(' expr ')' | ||
97 | -- funcargs -> '(' [ explist1 ] ')' | constructor | STRING | ||
98 | -- * funcargs turn an expr into a function call | ||
99 | -------------------------------------------------------------------- | ||
100 | -- expr -> subexpr | ||
101 | -- subexpr -> (UNOPR subexpr | simpleexp) { BINOPR subexpr } | ||
102 | -- simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... | ||
103 | -- | constructor | FUNCTION body | primaryexp | ||
104 | -------------------------------------------------------------------- | ||
105 | -- constructor -> '{' [ field { fieldsep field } [ fieldsep ] ] '}' | ||
106 | -- fieldsep -> ',' | ';' | ||
107 | -- field -> recfield | listfield | ||
108 | -- recfield -> ( NAME | '[' exp1 ']' ) = exp1 | ||
109 | -- listfield -> expr | ||
110 | -------------------------------------------------------------------- | ||
111 | |||
112 | --[[-------------------------------------------------------------------- | ||
113 | -- parser test cases, Lua 5.1.x | ||
114 | -- * uncomment string delimiter to enable syntax highlighting... | ||
115 | -- * anything that matches "^%s*%-%-" is a comment | ||
116 | -- * headings to display are "^%s*TESTS:%s*(.*)$" | ||
117 | -- * FAIL test cases should match "%s*%-%-%s*FAIL%s*$" | ||
118 | ----------------------------------------------------------------------]] | ||
119 | tests_source = [=[ | ||
120 | -------------------------------------------------------------------- | ||
121 | TESTS: empty chunks | ||
122 | -------------------------------------------------------------------- | ||
123 | -- chunk -> { stat [';'] } | ||
124 | -------------------------------------------------------------------- | ||
125 | |||
126 | ; -- FAIL | ||
127 | -------------------------------------------------------------------- | ||
128 | TESTS: optional semicolon, simple local statements | ||
129 | -------------------------------------------------------------------- | ||
130 | -- stat -> LOCAL localstat | ||
131 | -- LOCAL localstat -> LOCAL NAME {',' NAME} ['=' explist1] | ||
132 | -- explist1 -> expr { ',' expr } | ||
133 | -------------------------------------------------------------------- | ||
134 | local -- FAIL | ||
135 | local; -- FAIL | ||
136 | local = -- FAIL | ||
137 | local end -- FAIL | ||
138 | local a | ||
139 | local a; | ||
140 | local a, b, c | ||
141 | local a; local b local c; | ||
142 | local a = 1 | ||
143 | local a local b = a | ||
144 | local a, b = 1, 2 | ||
145 | local a, b, c = 1, 2, 3 | ||
146 | local a, b, c = 1 | ||
147 | local a = 1, 2, 3 | ||
148 | local a, local -- FAIL | ||
149 | local 1 -- FAIL | ||
150 | local "foo" -- FAIL | ||
151 | local a = local -- FAIL | ||
152 | local a, b, = -- FAIL | ||
153 | local a, b = 1, local -- FAIL | ||
154 | local a, b = , local -- FAIL | ||
155 | -------------------------------------------------------------------- | ||
156 | TESTS: simple DO blocks | ||
157 | -------------------------------------------------------------------- | ||
158 | -- stat -> DO block END | ||
159 | -- block -> chunk | ||
160 | -------------------------------------------------------------------- | ||
161 | do -- FAIL | ||
162 | end -- FAIL | ||
163 | do end | ||
164 | do ; end -- FAIL | ||
165 | do 1 end -- FAIL | ||
166 | do "foo" end -- FAIL | ||
167 | do local a, b end | ||
168 | do local a local b end | ||
169 | do local a; local b; end | ||
170 | do local a = 1 end | ||
171 | do do end end | ||
172 | do do end; end | ||
173 | do do do end end end | ||
174 | do do do end; end; end | ||
175 | do do do return end end end | ||
176 | do end do -- FAIL | ||
177 | do end end -- FAIL | ||
178 | do return end | ||
179 | do return return end -- FAIL | ||
180 | do break end -- FAIL | ||
181 | -------------------------------------------------------------------- | ||
182 | TESTS: simple WHILE loops | ||
183 | -------------------------------------------------------------------- | ||
184 | -- stat -> whilestat | ||
185 | -- whilestat -> WHILE cond DO block END | ||
186 | -- block -> chunk | ||
187 | -- cond -> expr | ||
188 | -------------------------------------------------------------------- | ||
189 | while -- FAIL | ||
190 | while do -- FAIL | ||
191 | while = -- FAIL | ||
192 | while 1 do -- FAIL | ||
193 | while 1 do end | ||
194 | while 1 do local a end | ||
195 | while 1 do local a local b end | ||
196 | while 1 do local a; local b; end | ||
197 | while 1 do 2 end -- FAIL | ||
198 | while 1 do "foo" end -- FAIL | ||
199 | while true do end | ||
200 | while 1 do ; end -- FAIL | ||
201 | while 1 do while -- FAIL | ||
202 | while 1 end -- FAIL | ||
203 | while 1 2 do -- FAIL | ||
204 | while 1 = 2 do -- FAIL | ||
205 | while 1 do return end | ||
206 | while 1 do return return end -- FAIL | ||
207 | while 1 do do end end | ||
208 | while 1 do do return end end | ||
209 | while 1 do break end | ||
210 | while 1 do break break end -- FAIL | ||
211 | while 1 do do break end end | ||
212 | -------------------------------------------------------------------- | ||
213 | TESTS: simple REPEAT loops | ||
214 | -------------------------------------------------------------------- | ||
215 | -- stat -> repeatstat | ||
216 | -- repeatstat -> REPEAT chunk UNTIL cond | ||
217 | -- cond -> expr | ||
218 | -------------------------------------------------------------------- | ||
219 | repeat -- FAIL | ||
220 | repeat until -- FAIL | ||
221 | repeat until 0 | ||
222 | repeat until false | ||
223 | repeat until local -- FAIL | ||
224 | repeat end -- FAIL | ||
225 | repeat 1 -- FAIL | ||
226 | repeat = -- FAIL | ||
227 | repeat local a until 1 | ||
228 | repeat local a local b until 0 | ||
229 | repeat local a; local b; until 0 | ||
230 | repeat ; until 1 -- FAIL | ||
231 | repeat 2 until 1 -- FAIL | ||
232 | repeat "foo" until 1 -- FAIL | ||
233 | repeat return until 0 | ||
234 | repeat return return until 0 -- FAIL | ||
235 | repeat break until 0 | ||
236 | repeat break break until 0 -- FAIL | ||
237 | repeat do end until 0 | ||
238 | repeat do return end until 0 | ||
239 | repeat do break end until 0 | ||
240 | -------------------------------------------------------------------- | ||
241 | TESTS: simple FOR loops | ||
242 | -------------------------------------------------------------------- | ||
243 | -- stat -> forstat | ||
244 | -- forstat -> fornum | forlist | ||
245 | -- forlist -> NAME {,NAME} IN explist1 forbody END | ||
246 | -- fornum -> NAME = exp1,exp1[,exp1] forbody END | ||
247 | -- forbody -> DO block | ||
248 | -- block -> chunk | ||
249 | -------------------------------------------------------------------- | ||
250 | for -- FAIL | ||
251 | for do -- FAIL | ||
252 | for end -- FAIL | ||
253 | for 1 -- FAIL | ||
254 | for a -- FAIL | ||
255 | for true -- FAIL | ||
256 | for a, in -- FAIL | ||
257 | for a in -- FAIL | ||
258 | for a do -- FAIL | ||
259 | for a in do -- FAIL | ||
260 | for a in b do -- FAIL | ||
261 | for a in b end -- FAIL | ||
262 | for a in b, do -- FAIL | ||
263 | for a in b do end | ||
264 | for a in b do local a local b end | ||
265 | for a in b do local a; local b; end | ||
266 | for a in b do 1 end -- FAIL | ||
267 | for a in b do "foo" end -- FAIL | ||
268 | for a b in -- FAIL | ||
269 | for a, b, c in p do end | ||
270 | for a, b, c in p, q, r do end | ||
271 | for a in 1 do end | ||
272 | for a in true do end | ||
273 | for a in "foo" do end | ||
274 | for a in b do break end | ||
275 | for a in b do break break end -- FAIL | ||
276 | for a in b do return end | ||
277 | for a in b do return return end -- FAIL | ||
278 | for a in b do do end end | ||
279 | for a in b do do break end end | ||
280 | for a in b do do return end end | ||
281 | for = -- FAIL | ||
282 | for a = -- FAIL | ||
283 | for a, b = -- FAIL | ||
284 | for a = do -- FAIL | ||
285 | for a = 1, do -- FAIL | ||
286 | for a = p, q, do -- FAIL | ||
287 | for a = p q do -- FAIL | ||
288 | for a = b do end -- FAIL | ||
289 | for a = 1, 2, 3, 4 do end -- FAIL | ||
290 | for a = p, q do end | ||
291 | for a = 1, 2 do end | ||
292 | for a = 1, 2 do local a local b end | ||
293 | for a = 1, 2 do local a; local b; end | ||
294 | for a = 1, 2 do 3 end -- FAIL | ||
295 | for a = 1, 2 do "foo" end -- FAIL | ||
296 | for a = p, q, r do end | ||
297 | for a = 1, 2, 3 do end | ||
298 | for a = p, q do break end | ||
299 | for a = p, q do break break end -- FAIL | ||
300 | for a = 1, 2 do return end | ||
301 | for a = 1, 2 do return return end -- FAIL | ||
302 | for a = p, q do do end end | ||
303 | for a = p, q do do break end end | ||
304 | for a = p, q do do return end end | ||
305 | -------------------------------------------------------------------- | ||
306 | TESTS: break statement | ||
307 | -------------------------------------------------------------------- | ||
308 | -- stat -> breakstat | ||
309 | -- breakstat -> BREAK | ||
310 | -- * must have a loop to break | ||
311 | -- * must be last statement in a block | ||
312 | -------------------------------------------------------------------- | ||
313 | break -- FAIL | ||
314 | -------------------------------------------------------------------- | ||
315 | TESTS: return statement | ||
316 | -------------------------------------------------------------------- | ||
317 | -- stat -> retstat | ||
318 | -- retstat -> RETURN explist | ||
319 | -- * must be last statement in a block | ||
320 | -------------------------------------------------------------------- | ||
321 | return | ||
322 | return; | ||
323 | return return -- FAIL | ||
324 | return 1 | ||
325 | return local -- FAIL | ||
326 | return "foo" | ||
327 | return 1, -- FAIL | ||
328 | return 1,2,3 | ||
329 | return a,b,c,d | ||
330 | return 1,2; | ||
331 | return ... | ||
332 | return 1,a,... | ||
333 | -------------------------------------------------------------------- | ||
334 | TESTS: conditional statements | ||
335 | -------------------------------------------------------------------- | ||
336 | -- stat -> ifstat | ||
337 | -- ifstat -> IF cond THEN block | ||
338 | -- {ELSEIF cond THEN block} | ||
339 | -- [ELSE block] END | ||
340 | -- block -> chunk | ||
341 | -- cond -> expr | ||
342 | -------------------------------------------------------------------- | ||
343 | if -- FAIL | ||
344 | elseif -- FAIL | ||
345 | else -- FAIL | ||
346 | then -- FAIL | ||
347 | if then -- FAIL | ||
348 | if 1 -- FAIL | ||
349 | if 1 then -- FAIL | ||
350 | if 1 else -- FAIL | ||
351 | if 1 then else -- FAIL | ||
352 | if 1 then elseif -- FAIL | ||
353 | if 1 then end | ||
354 | if 1 then local a end | ||
355 | if 1 then local a local b end | ||
356 | if 1 then local a; local b; end | ||
357 | if 1 then else end | ||
358 | if 1 then local a else local b end | ||
359 | if 1 then local a; else local b; end | ||
360 | if 1 then elseif 2 -- FAIL | ||
361 | if 1 then elseif 2 then -- FAIL | ||
362 | if 1 then elseif 2 then end | ||
363 | if 1 then local a elseif 2 then local b end | ||
364 | if 1 then local a; elseif 2 then local b; end | ||
365 | if 1 then elseif 2 then else end | ||
366 | if 1 then else if 2 then end end | ||
367 | if 1 then else if 2 then end -- FAIL | ||
368 | if 1 then break end -- FAIL | ||
369 | if 1 then return end | ||
370 | if 1 then return return end -- FAIL | ||
371 | if 1 then end; if 1 then end; | ||
372 | -------------------------------------------------------------------- | ||
373 | TESTS: function statements | ||
374 | -------------------------------------------------------------------- | ||
375 | -- stat -> funcstat | ||
376 | -- funcstat -> FUNCTION funcname body | ||
377 | -- funcname -> NAME {'.' NAME} [':' NAME] | ||
378 | -------------------------------------------------------------------- | ||
379 | -- body -> '(' parlist ')' chunk END | ||
380 | -- parlist -> [ param { ',' param } ] | ||
381 | -- param -> NAME | ||
382 | -- * DOTS must be the last parameter in a parlist | ||
383 | -------------------------------------------------------------------- | ||
384 | function -- FAIL | ||
385 | function 1 -- FAIL | ||
386 | function end -- FAIL | ||
387 | function a -- FAIL | ||
388 | function a end -- FAIL | ||
389 | function a( end -- FAIL | ||
390 | function a() end | ||
391 | function a(1 -- FAIL | ||
392 | function a("foo" -- FAIL | ||
393 | function a(p -- FAIL | ||
394 | function a(p,) -- FAIL | ||
395 | function a(p q -- FAIL | ||
396 | function a(p) end | ||
397 | function a(p,q,) end -- FAIL | ||
398 | function a(p,q,r) end | ||
399 | function a(p,q,1 -- FAIL | ||
400 | function a(p) do -- FAIL | ||
401 | function a(p) 1 end -- FAIL | ||
402 | function a(p) return end | ||
403 | function a(p) break end -- FAIL | ||
404 | function a(p) return return end -- FAIL | ||
405 | function a(p) do end end | ||
406 | function a.( -- FAIL | ||
407 | function a.1 -- FAIL | ||
408 | function a.b() end | ||
409 | function a.b, -- FAIL | ||
410 | function a.b.( -- FAIL | ||
411 | function a.b.c.d() end | ||
412 | function a: -- FAIL | ||
413 | function a:1 -- FAIL | ||
414 | function a:b() end | ||
415 | function a:b: -- FAIL | ||
416 | function a:b. -- FAIL | ||
417 | function a.b.c:d() end | ||
418 | function a(...) end | ||
419 | function a(..., -- FAIL | ||
420 | function a(p,...) end | ||
421 | function a(p,q,r,...) end | ||
422 | function a() local a local b end | ||
423 | function a() local a; local b; end | ||
424 | function a() end; function a() end; | ||
425 | -------------------------------------------------------------------- | ||
426 | TESTS: local function statements | ||
427 | -------------------------------------------------------------------- | ||
428 | -- stat -> LOCAL FUNCTION localfunc | ||
429 | -- LOCAL FUNCTION localfunc -> LOCAL FUNCTION NAME body | ||
430 | -------------------------------------------------------------------- | ||
431 | local function -- FAIL | ||
432 | local function 1 -- FAIL | ||
433 | local function end -- FAIL | ||
434 | local function a -- FAIL | ||
435 | local function a end -- FAIL | ||
436 | local function a( end -- FAIL | ||
437 | local function a() end | ||
438 | local function a(1 -- FAIL | ||
439 | local function a("foo" -- FAIL | ||
440 | local function a(p -- FAIL | ||
441 | local function a(p,) -- FAIL | ||
442 | local function a(p q -- FAIL | ||
443 | local function a(p) end | ||
444 | local function a(p,q,) end -- FAIL | ||
445 | local function a(p,q,r) end | ||
446 | local function a(p,q,1 -- FAIL | ||
447 | local function a(p) do -- FAIL | ||
448 | local function a(p) 1 end -- FAIL | ||
449 | local function a(p) return end | ||
450 | local function a(p) break end -- FAIL | ||
451 | local function a(p) return return end -- FAIL | ||
452 | local function a(p) do end end | ||
453 | local function a. -- FAIL | ||
454 | local function a: -- FAIL | ||
455 | local function a(...) end | ||
456 | local function a(..., -- FAIL | ||
457 | local function a(p,...) end | ||
458 | local function a(p,q,r,...) end | ||
459 | local function a() local a local b end | ||
460 | local function a() local a; local b; end | ||
461 | local function a() end; local function a() end; | ||
462 | -------------------------------------------------------------------- | ||
463 | -- stat -> exprstat | ||
464 | -- exprstat -> primaryexp | ||
465 | -- (-> func | assignment) | ||
466 | -- * if LHS is VCALL then func, otherwise assignment | ||
467 | -- * for func, LHS is VCALL if funcargs in expression | ||
468 | -------------------------------------------------------------------- | ||
469 | TESTS: assignments | ||
470 | -------------------------------------------------------------------- | ||
471 | -- assignment -> ',' primaryexp assignment | ||
472 | -- | '=' explist1 | ||
473 | -- explist1 -> expr { ',' expr } | ||
474 | -- * for assignment, LHS must be LOCAL, UPVAL, GLOBAL or INDEXED | ||
475 | -------------------------------------------------------------------- | ||
476 | a -- FAIL | ||
477 | a, -- FAIL | ||
478 | a,b,c -- FAIL | ||
479 | a,b = -- FAIL | ||
480 | a = 1 | ||
481 | a = 1,2,3 | ||
482 | a,b,c = 1 | ||
483 | a,b,c = 1,2,3 | ||
484 | a.b = 1 | ||
485 | a.b.c = 1 | ||
486 | a[b] = 1 | ||
487 | a[b][c] = 1 | ||
488 | a.b[c] = 1 | ||
489 | a[b].c = 1 | ||
490 | 0 = -- FAIL | ||
491 | "foo" = -- FAIL | ||
492 | true = -- FAIL | ||
493 | (a) = -- FAIL | ||
494 | {} = -- FAIL | ||
495 | a:b() = -- FAIL | ||
496 | a() = -- FAIL | ||
497 | a.b:c() = -- FAIL | ||
498 | a[b]() = -- FAIL | ||
499 | a = a b -- FAIL | ||
500 | a = 1 2 -- FAIL | ||
501 | a = a = 1 -- FAIL | ||
502 | -------------------------------------------------------------------- | ||
503 | TESTS: function calls | ||
504 | -------------------------------------------------------------------- | ||
505 | -- primaryexp -> prefixexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } | ||
506 | -- prefixexp -> NAME | '(' expr ')' | ||
507 | -- funcargs -> '(' [ explist1 ] ')' | constructor | STRING | ||
508 | -- * funcargs turn an expr into a function call | ||
509 | -------------------------------------------------------------------- | ||
510 | a( -- FAIL | ||
511 | a() | ||
512 | a(1) | ||
513 | a(1,) -- FAIL | ||
514 | a(1,2,3) | ||
515 | 1() -- FAIL | ||
516 | a()() | ||
517 | a.b() | ||
518 | a[b]() | ||
519 | a.1 -- FAIL | ||
520 | a.b -- FAIL | ||
521 | a[b] -- FAIL | ||
522 | a.b.( -- FAIL | ||
523 | a.b.c() | ||
524 | a[b][c]() | ||
525 | a[b].c() | ||
526 | a.b[c]() | ||
527 | a:b() | ||
528 | a:b -- FAIL | ||
529 | a:1 -- FAIL | ||
530 | a.b:c() | ||
531 | a[b]:c() | ||
532 | a:b: -- FAIL | ||
533 | a:b():c() | ||
534 | a:b().c[d]:e() | ||
535 | a:b()[c].d:e() | ||
536 | (a)() | ||
537 | ()() -- FAIL | ||
538 | (1)() | ||
539 | ("foo")() | ||
540 | (true)() | ||
541 | (a)()() | ||
542 | (a.b)() | ||
543 | (a[b])() | ||
544 | (a).b() | ||
545 | (a)[b]() | ||
546 | (a):b() | ||
547 | (a).b[c]:d() | ||
548 | (a)[b].c:d() | ||
549 | (a):b():c() | ||
550 | (a):b().c[d]:e() | ||
551 | (a):b()[c].d:e() | ||
552 | -------------------------------------------------------------------- | ||
553 | TESTS: more function calls | ||
554 | -------------------------------------------------------------------- | ||
555 | a"foo" | ||
556 | a[[foo]] | ||
557 | a.b"foo" | ||
558 | a[b]"foo" | ||
559 | a:b"foo" | ||
560 | a{} | ||
561 | a.b{} | ||
562 | a[b]{} | ||
563 | a:b{} | ||
564 | a()"foo" | ||
565 | a"foo"() | ||
566 | a"foo".b() | ||
567 | a"foo"[b]() | ||
568 | a"foo":c() | ||
569 | a"foo""bar" | ||
570 | a"foo"{} | ||
571 | (a):b"foo".c[d]:e"bar" | ||
572 | (a):b"foo"[c].d:e"bar" | ||
573 | a(){} | ||
574 | a{}() | ||
575 | a{}.b() | ||
576 | a{}[b]() | ||
577 | a{}:c() | ||
578 | a{}"foo" | ||
579 | a{}{} | ||
580 | (a):b{}.c[d]:e{} | ||
581 | (a):b{}[c].d:e{} | ||
582 | -------------------------------------------------------------------- | ||
583 | TESTS: simple expressions | ||
584 | -------------------------------------------------------------------- | ||
585 | -- simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... | ||
586 | -- | constructor | FUNCTION body | primaryexp | ||
587 | -------------------------------------------------------------------- | ||
588 | a = -- FAIL | ||
589 | a = a | ||
590 | a = nil | ||
591 | a = false | ||
592 | a = 1 | ||
593 | a = "foo" | ||
594 | a = [[foo]] | ||
595 | a = {} | ||
596 | a = (a) | ||
597 | a = (nil) | ||
598 | a = (true) | ||
599 | a = (1) | ||
600 | a = ("foo") | ||
601 | a = ([[foo]]) | ||
602 | a = ({}) | ||
603 | a = a.b | ||
604 | a = a.b. -- FAIL | ||
605 | a = a.b.c | ||
606 | a = a:b -- FAIL | ||
607 | a = a[b] | ||
608 | a = a[1] | ||
609 | a = a["foo"] | ||
610 | a = a[b][c] | ||
611 | a = a.b[c] | ||
612 | a = a[b].c | ||
613 | a = (a)[b] | ||
614 | a = (a).c | ||
615 | a = () -- FAIL | ||
616 | a = a() | ||
617 | a = a.b() | ||
618 | a = a[b]() | ||
619 | a = a:b() | ||
620 | a = (a)() | ||
621 | a = (a).b() | ||
622 | a = (a)[b]() | ||
623 | a = (a):b() | ||
624 | a = a"foo" | ||
625 | a = a{} | ||
626 | a = function -- FAIL | ||
627 | a = function 1 -- FAIL | ||
628 | a = function a -- FAIL | ||
629 | a = function end -- FAIL | ||
630 | a = function( -- FAIL | ||
631 | a = function() end | ||
632 | a = function(1 -- FAIL | ||
633 | a = function(p) end | ||
634 | a = function(p,) -- FAIL | ||
635 | a = function(p q -- FAIL | ||
636 | a = function(p,q,r) end | ||
637 | a = function(p,q,1 -- FAIL | ||
638 | a = function(...) end | ||
639 | a = function(..., -- FAIL | ||
640 | a = function(p,...) end | ||
641 | a = function(p,q,r,...) end | ||
642 | a = ... | ||
643 | a = a, b, ... | ||
644 | a = (...) | ||
645 | a = ..., 1, 2 | ||
646 | a = function() return ... end -- FAIL | ||
647 | -------------------------------------------------------------------- | ||
648 | TESTS: operators | ||
649 | -------------------------------------------------------------------- | ||
650 | -- expr -> subexpr | ||
651 | -- subexpr -> (UNOPR subexpr | simpleexp) { BINOPR subexpr } | ||
652 | -- simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... | ||
653 | -- | constructor | FUNCTION body | primaryexp | ||
654 | -------------------------------------------------------------------- | ||
655 | a = -10 | ||
656 | a = -"foo" | ||
657 | a = -a | ||
658 | a = -nil | ||
659 | a = -true | ||
660 | a = -{} | ||
661 | a = -function() end | ||
662 | a = -a() | ||
663 | a = -(a) | ||
664 | a = - -- FAIL | ||
665 | a = not 10 | ||
666 | a = not "foo" | ||
667 | a = not a | ||
668 | a = not nil | ||
669 | a = not true | ||
670 | a = not {} | ||
671 | a = not function() end | ||
672 | a = not a() | ||
673 | a = not (a) | ||
674 | a = not -- FAIL | ||
675 | a = #10 | ||
676 | a = #"foo" | ||
677 | a = #a | ||
678 | a = #nil | ||
679 | a = #true | ||
680 | a = #{} | ||
681 | a = #function() end | ||
682 | a = #a() | ||
683 | a = #(a) | ||
684 | a = # -- FAIL | ||
685 | a = 1 + 2; a = 1 - 2 | ||
686 | a = 1 * 2; a = 1 / 2 | ||
687 | a = 1 ^ 2; a = 1 % 2 | ||
688 | a = 1 .. 2 | ||
689 | a = 1 + -- FAIL | ||
690 | a = 1 .. -- FAIL | ||
691 | a = 1 * / -- FAIL | ||
692 | a = 1 + -2; a = 1 - -2 | ||
693 | a = 1 * - -- FAIL | ||
694 | a = 1 * not 2; a = 1 / not 2 | ||
695 | a = 1 / not -- FAIL | ||
696 | a = 1 * #"foo"; a = 1 / #"foo" | ||
697 | a = 1 / # -- FAIL | ||
698 | a = 1 + 2 - 3 * 4 / 5 % 6 ^ 7 | ||
699 | a = ((1 + 2) - 3) * (4 / (5 % 6 ^ 7)) | ||
700 | a = (1 + (2 - (3 * (4 / (5 % 6 ^ ((7))))))) | ||
701 | a = ((1 -- FAIL | ||
702 | a = ((1 + 2) -- FAIL | ||
703 | a = 1) -- FAIL | ||
704 | a = a + b - c | ||
705 | a = "foo" + "bar" | ||
706 | a = "foo".."bar".."baz" | ||
707 | a = true + false - nil | ||
708 | a = {} * {} | ||
709 | a = function() end / function() end | ||
710 | a = a() ^ b() | ||
711 | a = ... % ... | ||
712 | -------------------------------------------------------------------- | ||
713 | TESTS: more operators | ||
714 | -------------------------------------------------------------------- | ||
715 | a = 1 == 2; a = 1 ~= 2 | ||
716 | a = 1 < 2; a = 1 <= 2 | ||
717 | a = 1 > 2; a = 1 >= 2 | ||
718 | a = 1 < 2 < 3 | ||
719 | a = 1 >= 2 >= 3 | ||
720 | a = 1 == -- FAIL | ||
721 | a = ~= 2 -- FAIL | ||
722 | a = "foo" == "bar" | ||
723 | a = "foo" > "bar" | ||
724 | a = a ~= b | ||
725 | a = true == false | ||
726 | a = 1 and 2; a = 1 or 2 | ||
727 | a = 1 and -- FAIL | ||
728 | a = or 1 -- FAIL | ||
729 | a = 1 and 2 and 3 | ||
730 | a = 1 or 2 or 3 | ||
731 | a = 1 and 2 or 3 | ||
732 | a = a and b or c | ||
733 | a = a() and (b)() or c.d | ||
734 | a = "foo" and "bar" | ||
735 | a = true or false | ||
736 | a = {} and {} or {} | ||
737 | a = (1) and ("foo") or (nil) | ||
738 | a = function() end == function() end | ||
739 | a = function() end or function() end | ||
740 | -------------------------------------------------------------------- | ||
741 | TESTS: constructors | ||
742 | -------------------------------------------------------------------- | ||
743 | -- constructor -> '{' [ field { fieldsep field } [ fieldsep ] ] '}' | ||
744 | -- fieldsep -> ',' | ';' | ||
745 | -- field -> recfield | listfield | ||
746 | -- recfield -> ( NAME | '[' exp1 ']' ) = exp1 | ||
747 | -- listfield -> expr | ||
748 | -------------------------------------------------------------------- | ||
749 | a = { -- FAIL | ||
750 | a = {} | ||
751 | a = {,} -- FAIL | ||
752 | a = {;} -- FAIL | ||
753 | a = {,,} -- FAIL | ||
754 | a = {;;} -- FAIL | ||
755 | a = {{ -- FAIL | ||
756 | a = {{{}}} | ||
757 | a = {{},{},{{}},} | ||
758 | a = { 1 } | ||
759 | a = { 1, } | ||
760 | a = { 1; } | ||
761 | a = { 1, 2 } | ||
762 | a = { a, b, c, } | ||
763 | a = { true; false, nil; } | ||
764 | a = { a.b, a[b]; a:c(), } | ||
765 | a = { 1 + 2, a > b, "a" or "b" } | ||
766 | a = { a=1, } | ||
767 | a = { a=1, b="foo", c=nil } | ||
768 | a = { a -- FAIL | ||
769 | a = { a= -- FAIL | ||
770 | a = { a=, -- FAIL | ||
771 | a = { a=; -- FAIL | ||
772 | a = { 1, a="foo" -- FAIL | ||
773 | a = { 1, a="foo"; b={}, d=true; } | ||
774 | a = { [ -- FAIL | ||
775 | a = { [1 -- FAIL | ||
776 | a = { [1] -- FAIL | ||
777 | a = { [a]= -- FAIL | ||
778 | a = { ["foo"]="bar" } | ||
779 | a = { [1]=a, [2]=b, } | ||
780 | a = { true, a=1; ["foo"]="bar", } | ||
781 | ]=] | ||
782 | |||
783 | -- end of script | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/test_scripts-5.0.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/test_scripts-5.0.lua new file mode 100644 index 0000000..53a3ae3 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/test_scripts-5.0.lua | |||
@@ -0,0 +1,158 @@ | |||
1 | --[[-------------------------------------------------------------------- | ||
2 | |||
3 | test_scripts-5.0.lua | ||
4 | Compile and compare Lua files | ||
5 | This file is part of Yueliang. | ||
6 | |||
7 | Copyright (c) 2005-2007 Kein-Hong Man <khman@users.sf.net> | ||
8 | The COPYRIGHT file describes the conditions | ||
9 | under which this software may be distributed. | ||
10 | |||
11 | See the ChangeLog for more information. | ||
12 | |||
13 | ----------------------------------------------------------------------]] | ||
14 | |||
15 | --[[-------------------------------------------------------------------- | ||
16 | -- NOTE | ||
17 | -- * use the argument ALL to pull in additional personal Lua scripts | ||
18 | -- for testing, e.g. lua test_scripts.lua ALL | ||
19 | ----------------------------------------------------------------------]] | ||
20 | |||
21 | ------------------------------------------------------------------------ | ||
22 | -- reads in a list of files to crunch from a text file | ||
23 | ------------------------------------------------------------------------ | ||
24 | |||
25 | local function loadlist(fname, flist) | ||
26 | local inf = io.open(fname, "r") | ||
27 | if not inf then error("cannot open "..fname.." for reading") end | ||
28 | while true do | ||
29 | local d = inf:read("*l") | ||
30 | if not d then break end | ||
31 | if string.find(d, "^%s*$") or string.find(d, "^#") then | ||
32 | -- comments and empty lines are ignored | ||
33 | else | ||
34 | table.insert(flist, d) | ||
35 | end | ||
36 | end | ||
37 | inf:close() | ||
38 | end | ||
39 | |||
40 | ------------------------------------------------------------------------ | ||
41 | -- read in list of files to test | ||
42 | ------------------------------------------------------------------------ | ||
43 | |||
44 | local files = {} | ||
45 | |||
46 | loadlist("files-lua-5.0.txt", files) | ||
47 | loadlist("files-yueliang-5.0.txt", files) | ||
48 | |||
49 | -- pull in personal scripts to test if user specifies "ALL" | ||
50 | if arg[1] == "ALL" then | ||
51 | loadlist("files-other-5.0.txt", files) | ||
52 | end | ||
53 | |||
54 | -- if you want to specify files in this script itself (not recommended) | ||
55 | -- you can add them using the following | ||
56 | --[[ | ||
57 | for v in string.gfind([[ | ||
58 | ]], "[^%s]+") do | ||
59 | table.insert(files, v) | ||
60 | end | ||
61 | --]] | ||
62 | |||
63 | total = 0 -- sum of sizes | ||
64 | fsize = 0 -- current file size | ||
65 | |||
66 | ------------------------------------------------------------------------ | ||
67 | -- initialize | ||
68 | ------------------------------------------------------------------------ | ||
69 | |||
70 | require("../orig-5.0.3/lzio") | ||
71 | require("../orig-5.0.3/llex") | ||
72 | require("../orig-5.0.3/lopcodes") | ||
73 | require("../orig-5.0.3/ldump") | ||
74 | require("../orig-5.0.3/lcode") | ||
75 | require("../orig-5.0.3/lparser") | ||
76 | |||
77 | function lua_assert(test) | ||
78 | if not test then error("assertion failed!") end | ||
79 | end | ||
80 | |||
81 | luaX:init() | ||
82 | |||
83 | io.stdout:write("\n\n") | ||
84 | |||
85 | ------------------------------------------------------------------------ | ||
86 | -- * basic comparison for now; do it properly later | ||
87 | ------------------------------------------------------------------------ | ||
88 | |||
89 | local LuaState = {} | ||
90 | |||
91 | ------------------------------------------------------------------------ | ||
92 | -- dump binary chunks to a file if something goes wrong | ||
93 | ------------------------------------------------------------------------ | ||
94 | local function Dump(data, filename) | ||
95 | h = io.open(filename, "wb") | ||
96 | if not h then error("failed to open "..filename.." for writing") end | ||
97 | h:write(data) | ||
98 | h:close() | ||
99 | end | ||
100 | |||
101 | ------------------------------------------------------------------------ | ||
102 | -- create custom chunk reader (sums up file sizes) | ||
103 | ------------------------------------------------------------------------ | ||
104 | function make_getF(filename) | ||
105 | local h = io.open(filename, "r") | ||
106 | if not h then return nil end | ||
107 | fsize = h:seek("end") | ||
108 | h:seek("set") | ||
109 | total = total + fsize | ||
110 | return function() -- chunk reader anonymous function here | ||
111 | if not h then return nil end | ||
112 | local buff = h:read(512) | ||
113 | if not buff then h:close() end | ||
114 | return buff | ||
115 | end | ||
116 | end | ||
117 | |||
118 | ------------------------------------------------------------------------ | ||
119 | -- attempt to compile Lua source files | ||
120 | ------------------------------------------------------------------------ | ||
121 | for i, filename in ipairs(files) do | ||
122 | -- compile a source file | ||
123 | local zio = luaZ:init(make_getF(filename), nil, "@"..filename) | ||
124 | if not zio then | ||
125 | error("could not initialize zio stream for "..filename) | ||
126 | end | ||
127 | io.stdout:write(filename.."("..fsize.."): ") | ||
128 | local Func = luaY:parser(LuaState, zio, nil) | ||
129 | local Writer, Buff = luaU:make_setS() | ||
130 | luaU:dump(LuaState, Func, Writer, Buff) | ||
131 | local bc1 = Buff.data -- Yueliang's output | ||
132 | |||
133 | local f = loadfile(filename) | ||
134 | local bc2 = string.dump(f) -- Lua's output | ||
135 | |||
136 | -- compare outputs | ||
137 | if string.len(bc1) ~= string.len(bc2) then | ||
138 | Dump(bc1, "bc1.out") | ||
139 | Dump(bc2, "bc2.out") | ||
140 | error("binary chunk sizes different") | ||
141 | elseif bc1 ~= bc2 then | ||
142 | Dump(bc1, "bc1.out") | ||
143 | Dump(bc2, "bc2.out") | ||
144 | error("binary chunks different") | ||
145 | else | ||
146 | io.stdout:write("CORRECT\n") | ||
147 | end | ||
148 | local x, y = gcinfo() | ||
149 | -- memory usage isn't really a problem for the straight port | ||
150 | -- string handling in Lua that follows the original C closely is more | ||
151 | -- of a problem, but this will be fixed elsewhere, not in this tree | ||
152 | --io.stdout:write("gcinfo: "..x.." "..y.."\n") | ||
153 | end | ||
154 | |||
155 | -- summaries here | ||
156 | io.stdout:write("\nTotal file sizes: "..total.."\n") | ||
157 | |||
158 | -- end | ||
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/test_scripts-5.1.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/test_scripts-5.1.lua new file mode 100644 index 0000000..4d84055 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/test_scripts-5.1.lua | |||
@@ -0,0 +1,158 @@ | |||
1 | --[[-------------------------------------------------------------------- | ||
2 | |||
3 | test_scripts-5.1.lua | ||
4 | Compile and compare Lua files | ||
5 | This file is part of Yueliang. | ||
6 | |||
7 | Copyright (c) 2005-2007 Kein-Hong Man <khman@users.sf.net> | ||
8 | The COPYRIGHT file describes the conditions | ||
9 | under which this software may be distributed. | ||
10 | |||
11 | See the ChangeLog for more information. | ||
12 | |||
13 | ----------------------------------------------------------------------]] | ||
14 | |||
15 | --[[-------------------------------------------------------------------- | ||
16 | -- NOTE | ||
17 | -- * use the argument ALL to pull in additional personal Lua scripts | ||
18 | -- for testing, e.g. lua test_scripts.lua ALL | ||
19 | ----------------------------------------------------------------------]] | ||
20 | |||
21 | ------------------------------------------------------------------------ | ||
22 | -- reads in a list of files to crunch from a text file | ||
23 | ------------------------------------------------------------------------ | ||
24 | |||
25 | local function loadlist(fname, flist) | ||
26 | local inf = io.open(fname, "r") | ||
27 | if not inf then error("cannot open "..fname.." for reading") end | ||
28 | while true do | ||
29 | local d = inf:read("*l") | ||
30 | if not d then break end | ||
31 | if string.match(d, "^%s*$") or string.match(d, "^#") then | ||
32 | -- comments and empty lines are ignored | ||
33 | else | ||
34 | flist[#flist+1] = d | ||
35 | end | ||
36 | end | ||
37 | inf:close() | ||
38 | end | ||
39 | |||
40 | ------------------------------------------------------------------------ | ||
41 | -- read in list of files to test | ||
42 | ------------------------------------------------------------------------ | ||
43 | |||
44 | local files = {} | ||
45 | |||
46 | loadlist("files-lua-5.1.txt", files) | ||
47 | loadlist("files-yueliang-5.1.txt", files) | ||
48 | |||
49 | -- pull in personal scripts to test if user specifies "ALL" | ||
50 | if arg[1] == "ALL" then | ||
51 | loadlist("files-other-5.1.txt", files) | ||
52 | end | ||
53 | |||
54 | -- if you want to specify files in this script itself (not recommended) | ||
55 | -- you can add them using the following | ||
56 | --[=[ | ||
57 | for v in string.gmatch([[ | ||
58 | ]], "[^%s]+") do | ||
59 | table.insert(files, v) | ||
60 | end | ||
61 | --]=] | ||
62 | |||
63 | total = 0 -- sum of sizes | ||
64 | fsize = 0 -- current file size | ||
65 | |||
66 | ------------------------------------------------------------------------ | ||
67 | -- initialize | ||
68 | ------------------------------------------------------------------------ | ||
69 | |||
70 | dofile("../orig-5.1.2/lzio.lua") | ||
71 | dofile("../orig-5.1.2/llex.lua") | ||
72 | dofile("../orig-5.1.2/lopcodes.lua") | ||
73 | dofile("../orig-5.1.2/ldump.lua") | ||
74 | dofile("../orig-5.1.2/lcode.lua") | ||
75 | dofile("../orig-5.1.2/lparser.lua") | ||
76 | |||
77 | function lua_assert(test) | ||
78 | if not test then error("assertion failed!") end | ||
79 | end | ||
80 | |||
81 | luaX:init() | ||
82 | |||
83 | io.stdout:write("\n\n") | ||
84 | |||
85 | ------------------------------------------------------------------------ | ||
86 | -- * basic comparison for now; do it properly later | ||
87 | ------------------------------------------------------------------------ | ||
88 | |||
89 | local LuaState = {} | ||
90 | |||
91 | ------------------------------------------------------------------------ | ||
92 | -- dump binary chunks to a file if something goes wrong | ||
93 | ------------------------------------------------------------------------ | ||
94 | local function Dump(data, filename) | ||
95 | h = io.open(filename, "wb") | ||
96 | if not h then error("failed to open "..filename.." for writing") end | ||
97 | h:write(data) | ||
98 | h:close() | ||
99 | end | ||
100 | |||
101 | ------------------------------------------------------------------------ | ||
102 | -- create custom chunk reader (sums up file sizes) | ||
103 | ------------------------------------------------------------------------ | ||
104 | function make_getF(filename) | ||
105 | local h = io.open(filename, "r") | ||
106 | if not h then return nil end | ||
107 | fsize = h:seek("end") | ||
108 | h:seek("set") | ||
109 | total = total + fsize | ||
110 | return function() -- chunk reader anonymous function here | ||
111 | if not h then return nil end | ||
112 | local buff = h:read(512) | ||
113 | if not buff then h:close() end | ||
114 | return buff | ||
115 | end | ||
116 | end | ||
117 | |||
118 | ------------------------------------------------------------------------ | ||
119 | -- attempt to compile Lua source files | ||
120 | ------------------------------------------------------------------------ | ||
121 | for i, filename in ipairs(files) do | ||
122 | -- compile a source file | ||
123 | local zio = luaZ:init(make_getF(filename), nil) | ||
124 | if not zio then | ||
125 | error("could not initialize zio stream for "..filename) | ||
126 | end | ||
127 | io.stdout:write(filename.."("..fsize.."): ") | ||
128 | local Func = luaY:parser(LuaState, zio, nil, "@"..filename) | ||
129 | local Writer, Buff = luaU:make_setS() | ||
130 | luaU:dump(LuaState, Func, Writer, Buff) | ||
131 | local bc1 = Buff.data -- Yueliang's output | ||
132 | |||
133 | local f = loadfile(filename) | ||
134 | local bc2 = string.dump(f) -- Lua's output | ||
135 | |||
136 | -- compare outputs | ||
137 | if #bc1 ~= #bc2 then | ||
138 | Dump(bc1, "bc1.out") | ||
139 | Dump(bc2, "bc2.out") | ||
140 | error("binary chunk sizes different") | ||
141 | elseif bc1 ~= bc2 then | ||
142 | Dump(bc1, "bc1.out") | ||
143 | Dump(bc2, "bc2.out") | ||
144 | error("binary chunks different") | ||
145 | else | ||
146 | io.stdout:write("CORRECT\n") | ||
147 | end | ||
148 | local x = collectgarbage("count") | ||
149 | -- memory usage isn't really a problem for the straight port | ||
150 | -- string handling in Lua that follows the original C closely is more | ||
151 | -- of a problem, but this will be fixed elsewhere, not in this tree | ||
152 | --io.stdout:write("collectgarbage: "..x.."\n") | ||
153 | end | ||
154 | |||
155 | -- summaries here | ||
156 | io.stdout:write("\nTotal file sizes: "..total.."\n") | ||
157 | |||
158 | -- end | ||