此页面包含 1995 年 11 月在滑铁卢大学的软件工程实验室中进行的有关 Lua 的演讲幻灯片。当时,Lua 的当前版本是2.4,因此这些幻灯片有些过时:例如,3.0 中的回退被标记方法取代,5.0 中的回退被元表取代,4.0 中的 C API 发生了变化。尽管如此,要点仍然有效。
Luiz Henrique de Figueiredo、Roberto Ierusalimschy、Waldemar Celes 著
colors = {"red", "green", "blue", "black"} colors = {} colors[1]="red" colors[2]="green" colors[3]="blue" colors[4]="black"
w = {x=200, y=300, color="blue"} w = {} w.x=200 w.y=300 w.color="blue"
w = Window{x=200, y=300, color="blue"}
i,v = nextvar(nil) while i do ... i,v = nextvar(i) end
i,v = next(t,nil) while i do ... i,v = next(t,i) end
function save(i,v) local t=type(v) write(i..'=') if t=='nil' then write('nil') elseif t=='number' then write(v) elseif t=='string' then write('"'..v..'"') elseif t=='table' then write_record(v) end end function write_record(t) local i,v=next(t,nil) write('{') while i do save(i,v) i,v=next(t,i) if i then write(',') end end write('}') end writeto("state") -- save env to file i,v=nextvar(nil) while i do save(i,v) i,v=nextvar(i) end dofile("state") -- restore env from file
function x:f(a) function dummy(self,a) ... ... end end x.f = dummy x:f(b) x.f(x,b)
function Index(t,f) if f=="parent" then return oldIndex(t,f) end local p=t.parent if type(p)=="table" then return p[f] else return oldIndex(t,f) end end oldIndex=setfallback("index", Index) a=Window{x=100, y=200, color="red"} b=Window{x=300, y=400, parent=a} b.color == "red"
function Overload(a,b,op) local i=op.."("..a.name..","..b.name..")" if T[i]==nil then n=n+1 T[i]=create("t"..n) write(T[i].name..'='..i.."\n") end return T[i] end function create(v) local t={name=v} setglobal(v,t) return t end setfallback("arith",Overload) create("a") create("b") create("c") n=0 T={} E=(a*a+b*b)*(a*a-b*b)/(a*a+b*b+c)+(a*(b*b)*c) t1=mul(a,a) t2=mul(b,b) t3=add(t1,t2) t4=sub(t1,t2) t5=mul(t3,t4) t6=add(t3,c) t7=div(t5,t6) t8=mul(a,t2) t9=mul(t8,c) t10=add(t7,t9)全局公共子表达式识别!
0 PUSHGLOBAL "b" 3 PUSHGLOBAL "f" 6 PUSHGLOBAL "c" 9 CALLFUNC 1,1 12 ADD 13 STOREGLOBAL "a"
lua_Object lua_setfallback (char *name, lua_CFunction fallback); void lua_error (char *s); int lua_dofile (char *filename); int lua_dostring (char *string); int lua_callfunction (lua_Object function); int lua_call (char *funcname); void lua_beginblock (void); void lua_endblock (void); lua_Object lua_getparam (int number); #define lua_getresult(_) lua_getparam(_) float lua_getnumber (lua_Object object); char *lua_getstring (lua_Object object); lua_CFunction lua_getcfunction (lua_Object object); void *lua_getuserdata (lua_Object object); void lua_pushnil (void); void lua_pushnumber (float n); void lua_pushstring (char *s); void lua_pushliteral (char *s); void lua_pushcfunction (lua_CFunction fn); void lua_pushusertag (void *u, int tag); void lua_pushobject (lua_Object object); lua_Object lua_getglobal (char *name); void lua_storeglobal (char *name); void lua_storesubscript (void); lua_Object lua_getsubscript (void); int lua_type (lua_Object object); int lua_lock (void); lua_Object lua_getlocked (int ref); void lua_pushlocked (int ref); void lua_unlock (int ref); lua_Object lua_createtable (void);