From 2d1df4714e2736dbde7855ddcd76b4c1de822fa5 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Mon, 23 Jan 2012 21:58:02 +1000 Subject: Added a big bunch of example lua scripts for testing the speed of lua compiling. --- .../testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua | 27 +++++ LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/cf.lua | 16 +++ LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/echo.lua | 5 + LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/env.lua | 7 ++ .../yueliang-0.4.1/test_lua/5.1/factorial.lua | 32 ++++++ LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/fib.lua | 40 ++++++++ .../testLua/yueliang-0.4.1/test_lua/5.1/fibfor.lua | 13 +++ .../yueliang-0.4.1/test_lua/5.1/globals.lua | 13 +++ .../testLua/yueliang-0.4.1/test_lua/5.1/hello.lua | 3 + LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/life.lua | 111 +++++++++++++++++++++ LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/luac.lua | 7 ++ .../testLua/yueliang-0.4.1/test_lua/5.1/printf.lua | 7 ++ .../yueliang-0.4.1/test_lua/5.1/readonly.lua | 12 +++ .../testLua/yueliang-0.4.1/test_lua/5.1/sieve.lua | 29 ++++++ LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/sort.lua | 66 ++++++++++++ .../testLua/yueliang-0.4.1/test_lua/5.1/table.lua | 12 +++ .../yueliang-0.4.1/test_lua/5.1/trace-calls.lua | 32 ++++++ .../yueliang-0.4.1/test_lua/5.1/trace-globals.lua | 38 +++++++ LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/xd.lua | 14 +++ 19 files changed, 484 insertions(+) create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/cf.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/echo.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/env.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/factorial.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/fib.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/fibfor.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/globals.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/hello.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/life.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/luac.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/printf.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/readonly.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/sieve.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/sort.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/table.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/trace-calls.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/trace-globals.lua create mode 100644 LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/xd.lua (limited to 'LuaSL/testLua/yueliang-0.4.1/test_lua/5.1') 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 @@ +-- bisection method for solving non-linear equations + +delta=1e-6 -- tolerance + +function bisect(f,a,b,fa,fb) + local c=(a+b)/2 + io.write(n," c=",c," a=",a," b=",b,"\n") + if c==a or c==b or math.abs(a-b) posted to lua-l +-- modified to use ANSI terminal escape sequences +-- modified to use for instead of while + +local write=io.write + +ALIVE="¥" DEAD="þ" +ALIVE="O" DEAD="-" + +function delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary + for i=1,10000 do end + -- local i=os.clock()+1 while(os.clock() 0 do + local xm1,x,xp1,xi=self.w-1,self.w,1,self.w + while xi > 0 do + local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] + + self[y][xm1] + self[y][xp1] + + self[yp1][xm1] + self[yp1][x] + self[yp1][xp1] + next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0 + xm1,x,xp1,xi = x,xp1,xp1+1,xi-1 + end + ym1,y,yp1,yi = y,yp1,yp1+1,yi-1 + end +end + +-- output the array to screen +function _CELLS:draw() + local out="" -- accumulate to reduce flicker + for y=1,self.h do + for x=1,self.w do + out=out..(((self[y][x]>0) and ALIVE) or DEAD) + end + out=out.."\n" + end + write(out) +end + +-- constructor +function CELLS(w,h) + local c = ARRAY2D(w,h) + c.spawn = _CELLS.spawn + c.evolve = _CELLS.evolve + c.draw = _CELLS.draw + return c +end + +-- +-- shapes suitable for use with spawn() above +-- +HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 } +GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 } +EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 } +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 } +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 } + +-- the main routine +function LIFE(w,h) + -- create two arrays + local thisgen = CELLS(w,h) + local nextgen = CELLS(w,h) + + -- create some life + -- about 1000 generations of fun, then a glider steady-state + thisgen:spawn(GLIDER,5,4) + thisgen:spawn(EXPLODE,25,10) + thisgen:spawn(FISH,4,12) + + -- run until break + local gen=1 + write("\027[2J") -- ANSI clear screen + while 1 do + thisgen:evolve(nextgen) + thisgen,nextgen = nextgen,thisgen + write("\027[H") -- ANSI home cursor + thisgen:draw() + write("Life - generation ",gen,"\n") + gen=gen+1 + if gen>2000 then break end + --delay() -- no delay + end +end + +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 @@ +-- bare-bones luac in Lua +-- usage: lua luac.lua file.lua + +assert(arg[1]~=nil and arg[2]==nil,"usage: lua luac.lua file.lua") +f=assert(io.open("luac.out","wb")) +assert(f:write(string.dump(assert(loadfile(arg[1]))))) +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 @@ +-- an implementation of printf + +function printf(...) + io.write(string.format(...)) +end + +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 @@ +-- make global variables readonly + +local f=function (t,i) error("cannot redefine global variable `"..i.."'",2) end +local g={} +local G=getfenv() +setmetatable(g,{__index=G,__newindex=f}) +setfenv(1,g) + +-- an example +rawset(g,"x",3) +x=2 +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 @@ +-- the sieve of of Eratosthenes programmed with coroutines +-- typical usage: lua -e N=1000 sieve.lua | column + +-- generate all the numbers from 2 to n +function gen (n) + return coroutine.wrap(function () + for i=2,n do coroutine.yield(i) end + end) +end + +-- filter the numbers generated by `g', removing multiples of `p' +function filter (p, g) + return coroutine.wrap(function () + while 1 do + local n = g() + if n == nil then return end + if math.mod(n, p) ~= 0 then coroutine.yield(n) end + end + end) +end + +N=N or 1000 -- from command line +x = gen(N) -- generate primes up to N +while 1 do + local n = x() -- pick a number until done + if n == nil then break end + print(n) -- must be a prime number + x = filter(n, x) -- now remove its multiples +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 @@ +-- two implementations of a sort function +-- this is an example only. Lua has now a built-in function "sort" + +-- extracted from Programming Pearls, page 110 +function qsort(x,l,u,f) + if ly end) + show("after reverse selection sort",x) + qsort(x,1,n,function (x,y) return x>> ",string.rep(" ",level)) + if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end + t=debug.getinfo(2) + if event=="call" then + level=level+1 + else + level=level-1 if level<0 then level=0 end + end + if t.what=="main" then + if event=="call" then + io.write("begin ",t.short_src) + else + io.write("end ",t.short_src) + end + elseif t.what=="Lua" then +-- table.foreach(t,print) + io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">") + else + io.write(event," ",t.name or "(C)"," [",t.what,"] ") + end + io.write("\n") +end + +debug.sethook(hook,"cr") +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 @@ +-- trace assigments to global variables + +do + -- a tostring that quotes strings. note the use of the original tostring. + local _tostring=tostring + local tostring=function(a) + if type(a)=="string" then + return string.format("%q",a) + else + return _tostring(a) + end + end + + local log=function (name,old,new) + local t=debug.getinfo(3,"Sl") + local line=t.currentline + io.write(t.short_src) + if line>=0 then io.write(":",line) end + io.write(": ",name," is now ",tostring(new)," (was ",tostring(old),")","\n") + end + + local g={} + local set=function (t,name,value) + log(name,g[name],value) + g[name]=value + end + setmetatable(getfenv(),{__index=g,__newindex=set}) +end + +-- an example + +a=1 +b=2 +a=10 +b=20 +b=nil +b=200 +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 @@ +-- hex dump +-- usage: lua xd.lua < file + +local offset=0 +while true do + local s=io.read(16) + if s==nil then return end + io.write(string.format("%08X ",offset)) + string.gsub(s,"(.)", + function (c) io.write(string.format("%02X ",string.byte(c))) end) + io.write(string.rep(" ",3*(16-string.len(s)))) + io.write(" ",string.gsub(s,"%c","."),"\n") + offset=offset+16 +end -- cgit v1.1