function add ( x, y ) return x + y end function f (x, y ) return x * y end
function p(x, y ) for i=2,10 do print("fg"+i) end print(x,y,"asdasd","你好啊") end
VC++代码如下
#include <windows.h> #include <CONIO.H> extern"C" ...{ #include "srclua.h" #include "srclualib.h" #include "srclauxlib.h" }; lua_State *L = lua_open(); int luaadd (char*fname, int x, int y ) ...{ int sum; /**//* the function name */ lua_getglobal(L, fname); /**//* the first argument */ lua_pushnumber(L, x); /**//* the second argument */ lua_pushnumber(L, y); /**//* call the function with 2 arguments, return 1 result */ lua_call(L, 2, 1); /**//* get the result */ sum = (int)lua_tonumber(L, -1); lua_pop(L, 1); return sum; } void luak (char*fname, char*x, char*y) ...{ /**//* the function name */ lua_getglobal(L, fname); /**//* the first argument */ lua_pushstring(L, x); /**//* the second argument */ lua_pushstring(L, y); /**//* call the function with 2 arguments, return 1 result */ lua_call(L, 2, 1); /**//* get the result */ //sum = (int)lua_tonumber(L, -1); lua_pop(L, 1); return; } staticint average(lua_State *L) ...{ /**//* get number of arguments */ int n = lua_gettop(L); double sum =0; int i; /**//* loop through each argument */ for (i =1; i <= n; i++) ...{ /**//* total the arguments */ sum += lua_tonumber(L, i); } /**//* push the average */ lua_pushnumber(L, sum / n); /**//* push the sum */ lua_pushnumber(L, sum); /**//* return the number of results */ return2; } staticint p(lua_State *L) ...{ /**//* get number of arguments */ int n = lua_gettop(L); double sum =0; int i; /**//* loop through each argument */ for (i =1; i <= n; i++) ...{ unsigned int k; char l[100]=...{0}; /**//* total the arguments */ printf("%s ",lua_tolstring(L, i,&k)); } /**//* return the number of results */ return0; } int main(int argc, char* argv[]) ...{ int sum; /**//* initialize Lua */ L = lua_open(); luaopen_base(L); luaopen_table(L); //luaopen_io(L); luaopen_string(L); luaopen_math(L); lua_register(L, "print", p); /**//* load the script */ luaL_dofile(L, "test.lua"); /**//* call the add function */ sum = luaadd("add", 10, 15 ); /**//* print the result */ printf( "The sum is %d ", sum ); sum = luaadd("f", 10, 15 ); /**//* print the result */ printf( "The sum is %d ", sum ); luak("p", "asd", "ddd"); /**//* cleanup Lua */ lua_close(L); getch(); return0; }