眼下lua最新的版本号,5.2.3。
这个例子是一个简单lua分析器,来源自《Lua游戏开发实践指南》。
测试程序的功能:解决简单lua说明,例如:print("Hello world!");
function fun(x ,y) return x + y end
z =fun(1,1);
print(z);
结果例如以下图:
源代码例如以下:
simple_main.cpp:
#includecLua.h:#include #include "cLua.h"LuaGlue _Version(lua_State *L){ puts("This is 2.o fuck you!"); return 0;}char gpCommandBuffer[254];const char* GetCommand(){ memset(gpCommandBuffer,0,sizeof(gpCommandBuffer)); printf("Read>"); fgets(gpCommandBuffer,254,stdin); //printf("&&&&%s&&&&&",gpCommandBuffer); gpCommandBuffer[strlen(gpCommandBuffer) - 1] = 0; //printf("-----%s----",gpCommandBuffer); return gpCommandBuffer;}int main(){ puts("SKLDB"); puts("fky"); cLua *pLua = new cLua; pLua->AddFunction("Version",_Version); const char *pCommand = GetCommand(); while (strcmp(pCommand,"QUIT") != 0) { if (! pLua->RunString(pCommand)) { printf("Error is:%s",pLua->GetErrorString()); } pCommand = GetCommand(); } delete pLua; return 0;}
#ifndef __CLUA__#define __CLUA__struct lua_State;#define LuaGlue extern "C" intextern "C" {typedef int (*LuaFunctionType)(struct lua_State *pLuaState);};class cLua{public: cLua(); virtual ~cLua(); bool RunScript(const char *pFilename); bool RunString(const char *pCommand); const char *GetErrorString(void); bool AddFunction(const char *pFunctionName, LuaFunctionType pFunction); const char *GetStringArgument(int num, const char *pDefault=NULL); double GetNumberArgument(int num, double dDefault=0.0); void PushString(const char *pString); void PushNumber(double value); void SetErrorHandler(void(*pErrHandler)(const char *pError)) {m_pErrorHandler = pErrHandler;} lua_State *GetScriptContext(void) {return m_pScriptContext;}private: lua_State *m_pScriptContext; void(*m_pErrorHandler)(const char *pError);};#endifcLua.cpp:
#include源代码链接:#include #include #include "cLua.h"extern "C" {#include #include #include }cLua::cLua(){ m_pErrorHandler = NULL; m_pScriptContext = luaL_newstate(); luaL_openlibs(m_pScriptContext); //luaopen_base(m_pScriptContext); //luaopen_io(m_pScriptContext); //luaopen_string(m_pScriptContext); //luaopen_math(m_pScriptContext); //luaopen_debug(m_pScriptContext); //luaopen_table(m_pScriptContext);}cLua::~cLua(){ if(m_pScriptContext) lua_close(m_pScriptContext);}static std::string findScript(const char *pFname){ FILE *fTest; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _splitpath( pFname, drive, dir, fname, ext ); std::string strTestFile = (std::string) drive + dir + "Scripts\\" + fname + ".LUB"; fTest = fopen(strTestFile.c_str(), "r"); if(fTest == NULL) { //not that one... strTestFile = (std::string) drive + dir + "Scripts\\" + fname + ".LUA"; fTest = fopen(strTestFile.c_str(), "r"); } if(fTest == NULL) { //not that one... strTestFile = (std::string) drive + dir + fname + ".LUB"; fTest = fopen(strTestFile.c_str(), "r"); } if(fTest == NULL) { //not that one... //not that one... strTestFile = (std::string) drive + dir + fname + ".LUA"; fTest = fopen(strTestFile.c_str(), "r"); } if(fTest != NULL) { fclose(fTest); } return strTestFile;}bool cLua::RunScript(const char *pFname){ std::string strFilename = findScript(pFname); const char *pFilename = strFilename.c_str(); if (0 != luaL_loadfile(m_pScriptContext, pFilename)) { if(m_pErrorHandler) { char buf[256]; sprintf(buf, "Lua Error - Script Load\nScript Name:%s\nError Message:%s\n", pFilename, luaL_checkstring(m_pScriptContext, -1)); m_pErrorHandler(buf); } return false; } if (0 != lua_pcall(m_pScriptContext, 0, LUA_MULTRET, 0)) { if(m_pErrorHandler) { char buf[256]; sprintf(buf, "Lua Error - Script Run\nScript Name:%s\nError Message:%s\n", pFilename, luaL_checkstring(m_pScriptContext, -1)); m_pErrorHandler(buf); } return false; } return true;}bool cLua::RunString(const char *pCommand){ if (0 != luaL_loadbuffer(m_pScriptContext, pCommand, strlen(pCommand), NULL)) { if(m_pErrorHandler) { char buf[256]; sprintf(buf, "Lua Error - String Load\nString:%s\nError Message:%s\n", pCommand, luaL_checkstring(m_pScriptContext, -1)); m_pErrorHandler(buf); } return false; } if (0 != lua_pcall(m_pScriptContext, 0, LUA_MULTRET, 0)) { if(m_pErrorHandler) { char buf[256]; sprintf(buf, "Lua Error - String Run\nString:%s\nError Message:%s\n", pCommand, luaL_checkstring(m_pScriptContext, -1)); m_pErrorHandler(buf); } return false; } return true;}const char *cLua::GetErrorString(void){ return luaL_checkstring(m_pScriptContext, -1);}bool cLua::AddFunction(const char *pFunctionName, LuaFunctionType pFunction){ lua_register(m_pScriptContext, pFunctionName, pFunction); return true;}const char *cLua::GetStringArgument(int num, const char *pDefault){ return luaL_optstring(m_pScriptContext, num, pDefault);}double cLua::GetNumberArgument(int num, double dDefault){ return luaL_optnumber(m_pScriptContext, num, dDefault);}void cLua::PushString(const char *pString){ lua_pushstring(m_pScriptContext, pString);}void cLua::PushNumber(double value){ lua_pushnumber(m_pScriptContext, value);}
版权声明:本文博主原创文章,博客,未经同意不得转载。