• lua--从白开始(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);

    结果例如以下图:


    源代码例如以下:

    simple_main.cpp:

    #include <stdio.h>
    #include <string.h>
    #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 <stdio.h>
    #include <string.h>
    #include <string>
    
    
    #include "cLua.h"
    extern "C" {
    #include <lua.h>
    #include <lualib.h>
    #include <lauxlib.h>
    }
    
    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
    Script Name:%s
    Error Message:%s
    ", 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
    Script Name:%s
    Error Message:%s
    ", 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
    String:%s
    Error Message:%s
    ", 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
    String:%s
    Error Message:%s
    ", 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


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

  • 相关阅读:
    优步合肥上线首日引发试乘热行程单破万
    全北京都在开优步,你还在开那啥?
    freemarker常见语法大全
    Freemarker入门案例
    管理和感悟文章
    人不成熟的几大特征
    hadoop面试题一
    文章收集
    Java:基于LinkedList实现栈和队列
    JQuery上传插件Uploadify API详解
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4830627.html
Copyright © 2020-2023  润新知