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 } } }
相关文章推荐
- 比较Java和C++的访问控制语法
- C++基础入门教程(七):一些比较特别的基础语法总结
- 比较java和C++的面向对象语法
- C++ 11中几个我比较喜欢的语法(二)
- 一些背我遗忘又比较有用的C++语法知识
- 比较Java和C++的访问控制语法
- C++ 和 C# 语法的一些比较
- PHP语法基础(与Java、C++等作比较)
- [语法比较][指针/引用]Lava和C/C++语法比较
- C++比较特殊的构造函数和初始化语法
- C++ 11:比较诡异的一些语法
- OpenCV程序加速及C++中一些编程语法速度比较
- c++、c#、Python、GO语言基本语法比较
- QTreeWidget的一个例子,C++语法和item迭代循环都比较巧妙
- lua语言和c++比较
- FORCAL与C/C++、MATLAB、Python、Lua等各种语言的速度比较
- 比较java和C++的面向对象语法
- C++.NET与C#的编码区别 ,vb.net与c#的语法比较
- Lua中table内建排序与C/C++/Java/php/等内排序算法的排序效率比较
- Lu与C/C++、Forcal、MATLAB、Python、Lua等各种语言的速度比较