lua--从白开始(2)
阅读原文时间:2023年07月15日阅读:2

眼下lua最新的版本号,5.2.3。

这个例子是一个简单lua分析器,来源自《Lua游戏开发实践指南》。

测试程序的功能:解决简单lua说明,例如:print("Hello world!");

function fun(x ,y) return x + y end

z =fun(1,1);

print(z);

结果例如以下图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2VuX2Jsb2c=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

源代码例如以下:

simple_main.cpp:

#include
#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;  

}

cLua.h:

#ifndef __CLUA__
#define __CLUA__

struct lua_State;

#define LuaGlue extern "C" int
extern "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);
};

#endif

cLua.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);
}

源代码链接:http://download.csdn.net/detail/shinhwalin/7831493

版权声明:本文博主原创文章,博客,未经同意不得转载。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章