眼下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 <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\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

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

lua--从白开始(2)的更多相关文章

  1. #研发解决方案#基于Apriori算法的Nginx+Lua+ELK异常流量拦截方案

    郑昀 基于杨海波的设计文档 创建于2015/8/13 最后更新于2015/8/25 关键词:异常流量.rate limiting.Nginx.Apriori.频繁项集.先验算法.Lua.ELK 本文档 ...

  2. nginx+lua实现简单的waf网页防火墙功能

    原文:http://www.2cto.com/net/201608/534272.html 安装LuaJIT http://luajit.org/download/LuaJIT-2.0.4.tar.g ...

  3. 用Nginx+Lua(OpenResty)开发高性能Web应用

    在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等场景:而把Nginx作为一个Web容器使用的还不是那么广泛.Nginx的高性能是大家公认的,而Nginx开 ...

  4. 我和Lua并非一见钟情,我们期待着日久生情(相遇篇)

    Lua作为一款轻量级的脚本语言,由标准C编写而成,可被C/C++调用,也可调用C/C++的函数. 在目前的脚本引擎中,Lua的速度是最快的... Lua可直接在EditPlus文本处理器上开发,只需搭 ...

  5. Nginx+Lua(OpenResty)开发高性能Web应用

    使用Nginx+Lua(OpenResty)开发高性能Web应用 博客分类: 跟我学Nginx+Lua开发 架构 ngx_luaopenresty 在互联网公司,Nginx可以说是标配组件,但是主要场 ...

  6. 51CTO专访淘宝清无:漫谈Nginx服务器与Lua语言

    http://os.51cto.com/art/201112/307610.htm 说到Web服务器,也许你第一时间会想到Apache,也许你会想到Nginx.虽然说Apache依然是Web服务器的老 ...

  7. cocos2d-x 使用Lua

    转自:http://www.benmutou.com/blog/archives/49 1. Lua的堆栈和全局表 我们来简单解释一下Lua的堆栈和全局表,堆栈大家应该会比较熟悉,它主要是用来让C++ ...

  8. nginx + lua 构建网站防护waf(一)

    最近在帮朋友维护一个站点.这个站点是一个Php网站.坑爹的是用IIS做代理.出了无数问题之后忍无可忍,于是要我帮他切换到nginx上面,前期被不断的扫描和CC.最后找到了waf这样一个解决方案缓解一下 ...

  9. 基于Lua的清除类游戏算法

    最近在开发游戏,用Lua语言.习惯了其它的语言,然后对Lua的一些语法很不习惯. 比如table的元素个数的取值,比switch语句等等. 不过没有办法,还是要运用Lua来写游戏的.看来学C++还真的 ...

  10. 基于Apriori算法的Nginx+Lua+ELK异常流量拦截方案 郑昀 基于杨海波的设计文档(转)

    郑昀 基于杨海波的设计文档 创建于2015/8/13 最后更新于2015/8/25 关键词:异常流量.rate limiting.Nginx.Apriori.频繁项集.先验算法.Lua.ELK 本文档 ...

随机推荐

  1. C#添加水印

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  2. [Angular 2] BYPASSING PROVIDERS IN ANGULAR 2

    Artical --> BYPASSING PROVIDERS IN ANGULAR 2 Here trying to solve one problem: On the left hand s ...

  3. swift项目第七天:构建访客界面以及监听按钮点击

    一:访客界面效果如图 二:xib封装访客视图的view 1:业务逻辑分析:1:由于用户未登录时要显示访客视图,要先进行判断用户是否登录,未登录则显示访客视图,登录则显示正常的登陆界面,由于要在四个子控 ...

  4. 数据结构与算法实验题 6.1 s_sin’s bonus

    数据结构与算法实验题 6.1 s_sin's bonus ★实验任务 正如你所知道的 s_sin 是一个非常贪玩的人 QAQ(如果你非常讨厌他请直接从第二段开 始看),并且令人感到非常遗憾的是,他是一 ...

  5. 安卓 WebView加载本地图片时居中显示

    在一个项目中使用WebView显示gif图片(自定义的View无法放大gif),当图片过小时只在左侧显示,经过研究发现无论设置android:layout_gravity="center_h ...

  6. URAL 1542. Autocompletion 字典树

    给你最多10w个单词和相应的频率 接下来最多1w5千次询问 每次输入一个字符串让你从前面的单词中依照频率从大到小输出最多10个以该字符串为前缀的单词 開始把单词建成了字典树 然后每次询问找到全部满足条 ...

  7. Oracle列加密

    加密函数 create or replace function encrypt_des(p_text varchar2, p_key varchar2) return varchar2 isv_tex ...

  8. win7

    http://www.xitongiso.com/?pot http://www.potplayer.org/

  9. MouseGestureL.ini shift up/down/left/right edge

    MouseGestureL.ini [ShiftPress]Icon=C:\Windows\System32\explorer.exe,6Custom=GetKeyState("Shift& ...

  10. 在CentOS上使用Nginx和Tomcat搭建高可用高并发网站

    目录 目录 前言 创建CentOS虚拟机 安装Nginx 安装Tomcat 安装lvs和keepalived 反向代理 部署网站 搭建数据库 编写网站项目 解决session一致性 注意 参考资料 前 ...