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);
结果例如以下图:
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)的更多相关文章
- #研发解决方案#基于Apriori算法的Nginx+Lua+ELK异常流量拦截方案
郑昀 基于杨海波的设计文档 创建于2015/8/13 最后更新于2015/8/25 关键词:异常流量.rate limiting.Nginx.Apriori.频繁项集.先验算法.Lua.ELK 本文档 ...
- nginx+lua实现简单的waf网页防火墙功能
原文:http://www.2cto.com/net/201608/534272.html 安装LuaJIT http://luajit.org/download/LuaJIT-2.0.4.tar.g ...
- 用Nginx+Lua(OpenResty)开发高性能Web应用
在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等场景:而把Nginx作为一个Web容器使用的还不是那么广泛.Nginx的高性能是大家公认的,而Nginx开 ...
- 我和Lua并非一见钟情,我们期待着日久生情(相遇篇)
Lua作为一款轻量级的脚本语言,由标准C编写而成,可被C/C++调用,也可调用C/C++的函数. 在目前的脚本引擎中,Lua的速度是最快的... Lua可直接在EditPlus文本处理器上开发,只需搭 ...
- Nginx+Lua(OpenResty)开发高性能Web应用
使用Nginx+Lua(OpenResty)开发高性能Web应用 博客分类: 跟我学Nginx+Lua开发 架构 ngx_luaopenresty 在互联网公司,Nginx可以说是标配组件,但是主要场 ...
- 51CTO专访淘宝清无:漫谈Nginx服务器与Lua语言
http://os.51cto.com/art/201112/307610.htm 说到Web服务器,也许你第一时间会想到Apache,也许你会想到Nginx.虽然说Apache依然是Web服务器的老 ...
- cocos2d-x 使用Lua
转自:http://www.benmutou.com/blog/archives/49 1. Lua的堆栈和全局表 我们来简单解释一下Lua的堆栈和全局表,堆栈大家应该会比较熟悉,它主要是用来让C++ ...
- nginx + lua 构建网站防护waf(一)
最近在帮朋友维护一个站点.这个站点是一个Php网站.坑爹的是用IIS做代理.出了无数问题之后忍无可忍,于是要我帮他切换到nginx上面,前期被不断的扫描和CC.最后找到了waf这样一个解决方案缓解一下 ...
- 基于Lua的清除类游戏算法
最近在开发游戏,用Lua语言.习惯了其它的语言,然后对Lua的一些语法很不习惯. 比如table的元素个数的取值,比switch语句等等. 不过没有办法,还是要运用Lua来写游戏的.看来学C++还真的 ...
- 基于Apriori算法的Nginx+Lua+ELK异常流量拦截方案 郑昀 基于杨海波的设计文档(转)
郑昀 基于杨海波的设计文档 创建于2015/8/13 最后更新于2015/8/25 关键词:异常流量.rate limiting.Nginx.Apriori.频繁项集.先验算法.Lua.ELK 本文档 ...
随机推荐
- [Nuxt] Build a Vue.js Form then use Vuex Actions to Post to an API in Nuxt
The default behavior of submitting an HTML form is to reload the page. You can use the Vue.js @submi ...
- 【Codeforces Round #185 (Div. 2) D】Cats Transport
[链接] 链接 [题意] 有n座山,m只猫. 每只猫都在其中的一些山上玩. 第i只猫在h[i]山上玩,且会在t[i]时刻出现在山脚下(然后就一直在那里等) 然后有p个人. 它们听从你的安排. 在某个时 ...
- jboss-as-7.1.1.Final与jdk1.8不兼容解决方案
今天在安装1.8电脑上装了jboss7.1.1,配置好了运行的时候就是无法启动,最后得出答案是:jboss-as-7.1.1.Final与jdk1.8不兼容 1.如果你的电脑安装了jdk1.8,那么在 ...
- [Angular] Using NO_ERRORS_SCHEMA
For example, when testing container component we might import lots of children components, but we di ...
- [AngularFire2] Signup and logout
import {AuthProviders, FirebaseAuthState, FirebaseAuth, AuthMethods} from "angularfire2";i ...
- js判断是否微信客户端
上周接到个需求,需求是这样的:用户扫一扫二维码会产生一个链接,该链接会向后端发送个请求,返回一个 apk 的下载地址,用户点击下载按钮可以下载此 apk.然后就发生了问题,经过测试,发现用微信扫一扫打 ...
- [Compose] 16. Apply multiple functors as arguments to a function (Applicatives)
We find a couple of DOM nodes that may or may not exist and run a calculation on the page height usi ...
- nopCommerce 3.9 大波浪系列 之 使用部署在Docker中的Redis缓存主从服务
一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集群. 一般来说,只有一台Redis是不可行的,原因如下: 单台Redis服务器会发 ...
- MySQL的表空间管理
表空间: MySQL没有真正意义上的表空间管理. MySQL的Innodb包含两种表空间文件模式,默认的共享表空间和每个表分离的独立表空间. 一般来说,当数据量很小的时候建议使用共享表空间的管理方式. ...
- 【54.38%】【BZOJ 4300】绝世好题
Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1120 Solved: 609 [Submit][Status][Discuss] Descript ...