眼下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. 每日技术总结:setInterval,setTimeout,文本溢出,小程序,wepy

    前言: 项目背景:vue,电商,商品详情页 1.倒计时,倒计到0秒时停止 data () { return { n: 10 } }, created () { let int = setInterva ...

  2. Geodatabase模型

    原文 Geodatabase模型 地理数据模型是地理实体及其关系的形式化抽象和数学描述.随着数据库.面向对象等技术的发展,面向对象的地理数据模型成为大型空间数据库的首选方案,它克服了传统地理数据模型的 ...

  3. [CSS] Target empty elements using the :empty pseudo-class

    You can target an element that has no child elements by using the :empty pseudo-class. With browser ...

  4. [AngularFire2] Pagination

    Let's see how to do pagination in Firebase: For the init loading, we only want 3 items: findLessonsK ...

  5. Maven基础教程 分类: C_OHTERS 2015-04-10 22:53 232人阅读 评论(0) 收藏

    更多内容请参考官方文档:http://maven.apache.org/guides/index.html 官方文档很详细,基本上可以查找到一切相关的内容. 另外,快速入门可参考视频:孔浩的maven ...

  6. 【u247】生物进化

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在一片茂密的原始森林中,生物学家们发现了几种远古时期的动物化石.他们将化石依次编号为1,2,3,--n ...

  7. 洛谷 P4013 数字梯形问题

    ->题目链接 题解: 网络流. #include<cstdio> #include<iostream> #include<queue> #include< ...

  8. 【29.70%】【codeforces 723D】Lakes in Berland

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. [Now] Configure secrets and environment variables with Zeit’s Now

    Often your project will require some secret keys or tokens - for instance, API keys or database auth ...

  10. Java编程思想第四版 *第五章 个人练习

    练习3:(1)创建一个带默认构造器(即无參构造器)的类.在构造器中打印一条消息.为这个类创建一个对象.P116 public class Test{ public Test(){ System.out ...