您的位置:首页 > 编程语言 > Lua

LUA与C++的语法比较

2015-05-11 14:46 309 查看
LUA语法

--global.lua

g_ID="";
g_FunctionCode="";
g_XmlTab={};

--writefile.lua

require ("global.lua");
local scriptPath = "./app_data/script/"
local luaFile;

luaFile =  scriptPath .. "dir/coco_" .. g_FunctionCode .. ".lua";  --字符串连接

dofile(luaFile); --脚本里面执行另一个脚本

------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
C++语法

//global.h

string g_ID="";
string g_FunctionCode="";
string g_XmlTab;

//writefile.cpp

#include "global.h"
string scriptPath = "./app_data/script/"
string luaFile;

luafileath = scriptPath + "dir/coco_";
luafileath += g_FunctionCode;
luafileath += ".lua";

//程序里面执行Lua脚本里面的函数
//利用StackDump()把Lua里面的内部信息打印到控制台
int iResult = luaL_dofile(L, luafileath); //1 先加载.lua的路径
int iLuaType = lua_getglobal(L, "WriteToFile"); //2 获取全局函数
CallLuaFunction(L,1,1); //3 执行脚本里面的一个函数

------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
问题1:QT的缺省工程在Windows下无法输出到控制台,给调试带来一定不便。

A Projects -- Run -- Run in terminal 打勾
B 在.pro中加上: CONFIG += console
后就可以用printf啦!!!
记得#include <stdio.h>

问题2:调试的时候怎样可以从IDE工具跳转到脚本编辑器?

1 LuaEditor--选项--编译调试设置--去掉勾,然后选择宿主程序和script目录
2 运行宿主程序--LuaEditor--调试--附加到进程--选择宿主exe
3 LuaEditor--调试--生成目标文件

问题3: 1>Warning: Could not find symbolic files!  (from LuaEditor)
////////////my.lua

function lua_test()
local avg, sum;                         --反向传参数(from Lua to C++)
avg, sum = average(10, 20, 30, 40, 50)  --Lua中调用文件C++中的函数 function:average()
print("The average is ", avg)
print("The sum is ", sum)

avg, sum = average(avg, sum)  --Lua中调用文件C++中的函数 function:average()
print("The average is:",avg,"The sum is:",sum)

local str = Tablename("Hello", "TT")

print("----*****######"..str)  --只需一次编译,以后任何的改动不需要再次编译.lua文件和.cpp文件! try to reduce one '#'

local sNow = os.date("%Y-%m-%d %H:%M:%S")
print(sNow)

return true
end

////////////////my.cpp

static int Tablename(lua_State *L)
{
int n = lua_gettop(L);
string str;

for(int i = 1; i <= n; i++) //算法实现
{
str += lua_tostring(L,i);
}

lua_pushstring(L, str.c_str());
return 1;
}

static int average(lua_State *L)
{
int n = lua_gettop(L); //得到参数个数(u see,u don't have to know there are how many parameters pre. at all!)
double sum = 0;

for(int i = 1; i <= n; i++) //算法实现
{
sum += lua_tonumber(L, i);
}

lua_pushnumber(L, sum/n); //压入平均值  正向传参数(from C++ to Lua)
lua_pushnumber(L, sum); //压入和

return 2; //返回返回值的个数
}

void on_btnTest_clicked()
{
lua_State *L = luaL_newstate(); //init
luaL_openlibs(L);
if(L==NULL) return;
lua_register(L, "average", average); //注册函数
lua_register(L, "Tablename", Tablename); //注册

lua_newtable(L);
//StackDump(L);
lua_pushstring(L, "A"); //入栈
lua_pushstring(L, "B"); //入栈
lua_settable(L, -3); //移到栈的下一个空位
lua_setglobal(L, "Tablename");    /* 'name' = table */

lua_newtable(L);
lua_pushstring(L, "Nash"); //入栈
lua_pushstring(L, "Alies"); //入栈
lua_settable(L, -3); //移到栈的下一个空位
lua_setglobal(L, "TT");    /* 'name' = table */
//lua_rawseti(L,-2,iCount);

char *luafileath = "D:\\2015\\my.lua";
int iResult = luaL_dofile(L, luafileath);
if( iResult != 0 )
{
string sError = lua_tostring(L,-1); //failed
}
else
{
//success
}

//C++中调用Lua文件中的函数 function:lua_test() --Parameter:0 Result:1(bool)
int iLuaType = lua_getglobal(L, "lua_test"); //in my.lua
if (iLuaType == LUA_TFUNCTION)
{
//函数调用
if(CallLuaFunction(L,0,1)) ////可试下传递一个自定义的结构体参数至lua函数里面去,只是不知道怎样把一个结构体入栈;如果可以入栈,那么lua文件头只需要再次重复定义一个相同形式的结构体就可以在自身接收和解析传进来的结构体了。
{
//succeed
}
else
{
//failed
}
}
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  脚本