1, 类型
local bTemp = true;
print(bTemp);
local iTemp = 0;
print(iTemp);
local szTemp = "Hello World";
print(szTemp .. ":" .. #szTemp);
szTemp = string.gsub(szTemp, "World", "Lua");
print(szTemp .. ":" .. #szTemp);
local tab = {}
for i=1, 10, 1
do
if 5 ~= i then
tab[i] = i;
end
end
for i=1,#tab, 1
do
print(tab[i]);
end
for i=1,table.maxn(tab), 1
do
print(tab[i]);
end
2, 函数
local fun = function(x,y)
return x + y;
end
local fun1 = function(x,y,funAdd)
return x + y + funAdd(x,y);
end
print( fun(3,2));
print( fun1(3,2,fun));
local tab = {}
tab[1] = fun;
tab[2] = fun1;
print( tab[1](3,2));
print( tab[2](3,2,tab[1]));
--lua支持尾调用消除,即如果函数最后一条语句是调用其它函数,则当前函数会消除栈然后转到其它函数,从而避免递归时栈溢出问题。
local fun2;
fun2 = function(n)
if 1 == n then
return n;
else
return n+fun2(n-1);
end
end
print( fun2(3));
function newCount()
local dNum = 0
local function Count() --[[ 闭包(双重函数): 外部变量成为静态变量]]
dNum = dNum + 1
return dNum
end
return Count
end
print("...");
funCount = newCount()
funCount1 = newCount()
print(newCount())
print(funCount())
print(funCount())
print(funCount())
print(funCount1())
print(funCount1())
print(funCount1())
3, 迭代器
4, C里调用Lua
lua文件:
function newHellow()
local function doHellow()
return "Hello Lua"
end
return doHellow
end
funHellow = newHellow()
function newCount()
local iCount = 0
local function Count()
iCount = iCount + 1
return iCount
end
return Count
end
funCount = newCount()
function funAdd(x, y)
local function doAdd()
return x + y
end
return doAdd()
end
C++的文件:
/********************************************************************
file name : LuaAPI.h
author : Clark
created : 1:8:2011
purpose :
*********************************************************************/
#pragma once
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
class LuaAPI
{
public:
LuaAPI(void);
virtual ~LuaAPI(void);
void init(const char* file_name) throw(char*);
bool operator()(const char* func, const char* sig, ...); //LuaAPI("func","di>d",x,y,&z)
private:
void uninit();
lua_State *m_pL;
};
//----------------------------
#include "LuaAPI.h"
#include <string.h>
#pragma comment(lib,"lua5.1.lib")
LuaAPI::LuaAPI():m_pL(NULL)
{
}
LuaAPI::~LuaAPI()
{
uninit();
}
void LuaAPI::init(const char* file_name) throw(char*)
{
uninit();
m_pL = lua_open();
luaL_openlibs(m_pL);
if( luaL_loadfile(m_pL, file_name))
{
char strError[512];
strcpy(strError,"Failed in init -> ");
strncat(strError,file_name,256);
strcat(strError,"\n");
throw strError;
}
lua_resume(m_pL, 0);
}
void LuaAPI::uninit()
{
if( NULL != m_pL)
{
lua_close(m_pL);
}
}
//LuaAPI("func","di>d",x,y,&z)
bool LuaAPI::operator()(const char* func, const char* sig, ...)
{
if( NULL == m_pL)
return false;
va_list vl;
int narg, nres;
va_start(vl, sig);
lua_getglobal(m_pL,func);
//压入参数
for(narg = 0; *sig; narg++)
{
luaL_checkstack(m_pL, 1, "too many arguments");
switch(*sig++)
{
case 'd': lua_pushnumber(m_pL, va_arg(vl, double)); break;
case 'i': lua_pushinteger(m_pL, va_arg(vl, int)); break;
case 's': lua_pushstring(m_pL, va_arg(vl, char*)); break;
case '>': goto endargs;
default: NULL;
}
}
endargs:
nres = strlen(sig);
if( 0 != lua_pcall(m_pL, narg, nres, 0))
{
//error(m_pL,"error calling '%s': %s", func, lua_tostring(m_pL,-1));
return false;
}
//检索结果
nres = -nres;
while(*sig)
{
switch(*sig++)
{
case 'd':
{
if(!lua_isnumber(m_pL, nres))
break;
*va_arg(vl, double*) = lua_tonumber(m_pL,nres);
}
break;
case 'i':
{
if(!lua_isnumber(m_pL, nres))
break;
*va_arg(vl, int*) = lua_tointeger(m_pL,nres);
}
break;
case 's':
{
if(!lua_isstring(m_pL, nres))
break;
*va_arg(vl, const char**) = lua_tostring(m_pL,nres);
}
break;
default: NULL;
}
nres++;
}
va_end(vl);
return true;
}
//--------------------
#include <iostream>
#include "LuaAPI.h"
using namespace std;
void main()
{
int iSum = 0;
LuaAPI lua_test;
LuaAPI lua_test1;
try
{
lua_test.init("lua_test.lua");
lua_test1.init("lua_test1.lua");
}
catch(char* pError)
{
cout<<pError<<endl;
system("pause");
exit(0);
}
lua_test("funAdd", "ii>i", 2,3,&iSum);
printf("%d\n",iSum);
lua_test1("funAdd", "ii>i", 5,6,&iSum);
printf("%d\n",iSum);
system("pause");
}