From e74bc7d3918dc3483aa8dd19eacc6d2b9f93b398 Mon Sep 17 00:00:00 2001 From: josh Date: Sat, 12 Sep 2009 15:17:53 +0000 Subject: [PATCH] added two lua tutorials git-svn-id: svn://anubis/misc/lua@130 bd8a9e45-a331-0410-811e-c64571078777 --- tut/Makefile | 9 +++++++ tut/hello_world.lua | 3 +++ tut/tut.cc | 48 +++++++++++++++++++++++++++++++++++ tut2/Makefile | 9 +++++++ tut2/test.lua | 4 +++ tut2/tut.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+) create mode 100644 tut/Makefile create mode 100644 tut/hello_world.lua create mode 100644 tut/tut.cc create mode 100644 tut2/Makefile create mode 100644 tut2/test.lua create mode 100644 tut2/tut.cc diff --git a/tut/Makefile b/tut/Makefile new file mode 100644 index 0000000..859e6dd --- /dev/null +++ b/tut/Makefile @@ -0,0 +1,9 @@ + +TARGET := tut +CPPFLAGS := -I/usr/include/lua5.1 +LDFLAGS := -llua5.1 + +all: $(TARGET) + +$(TARGET): $(TARGET).cc + $(CXX) -o $@ $(CPPFLAGS) $(CXXFLAGS) $^ $(LDFLAGS) diff --git a/tut/hello_world.lua b/tut/hello_world.lua new file mode 100644 index 0000000..3445349 --- /dev/null +++ b/tut/hello_world.lua @@ -0,0 +1,3 @@ + +-- The first program in every language +io.write("Hello world, from", _VERSION, "!\n") diff --git a/tut/tut.cc b/tut/tut.cc new file mode 100644 index 0000000..681f0bc --- /dev/null +++ b/tut/tut.cc @@ -0,0 +1,48 @@ + +#include +#include +using namespace std; + +void report_errors(lua_State * L, int status) +{ + if ( status != 0 ) + { + cerr << "-- " << lua_tostring(L, -1) << endl; + lua_pop(L, 1); // remove error message + } +} + +int main(int argc, char ** argv) +{ + for ( int n = 1; n < argc; ++n ) + { + const char * file = argv[n]; + + lua_State * L = lua_open(); + + luaL_openlibs(L); +#if 0 + luaopen_io(L); // provides io.* + luaopen_base(L); + luaopen_table(L); + luaopen_string(L); + luaopen_math(L); +#endif + + cerr << "-- Loading file: " << file << endl; + + int s = luaL_loadfile(L, file); + + if ( s == 0 ) + { + // execute Lua program + s = lua_pcall(L, 0, LUA_MULTRET, 0); + } + + report_errors(L, s); + lua_close(L); + cerr << endl; + } + + return 0; +} diff --git a/tut2/Makefile b/tut2/Makefile new file mode 100644 index 0000000..859e6dd --- /dev/null +++ b/tut2/Makefile @@ -0,0 +1,9 @@ + +TARGET := tut +CPPFLAGS := -I/usr/include/lua5.1 +LDFLAGS := -llua5.1 + +all: $(TARGET) + +$(TARGET): $(TARGET).cc + $(CXX) -o $@ $(CPPFLAGS) $(CXXFLAGS) $^ $(LDFLAGS) diff --git a/tut2/test.lua b/tut2/test.lua new file mode 100644 index 0000000..c84c0de --- /dev/null +++ b/tut2/test.lua @@ -0,0 +1,4 @@ + +io.write("Running ", _VERSION, "\n") +a = my_function(1, 2, 3, "abc", "def") +io.write("my_function() returned ", a, "\n") diff --git a/tut2/tut.cc b/tut2/tut.cc new file mode 100644 index 0000000..4525f45 --- /dev/null +++ b/tut2/tut.cc @@ -0,0 +1,61 @@ + +#include +#include +using namespace std; + +int my_function(lua_State * L) +{ + int argc = lua_gettop(L); + + cerr << "-- my_function() called with " << argc + << " arguments:" << endl; + + for ( int n = 1; n <= argc; ++n ) + { + cerr << "-- argument " << n << ": " + << lua_tostring(L, n) << endl; + } + + lua_pushnumber(L, 123); // return value + return 1; // number of return values +} + +void report_errors(lua_State *L, int status) +{ + if ( status!=0 ) + { + cerr << "-- " << lua_tostring(L, -1) << endl; + lua_pop(L, 1); // remove error message + } +} + +int main(int argc, char ** argv) +{ + for ( int n = 1; n < argc; ++n ) + { + const char * file = argv[n]; + + lua_State * L = lua_open(); + + luaL_openlibs(L); + + // make my_function() available to Lua programs + lua_register(L, "my_function", my_function); + + cerr << "-- Loading file: " << file << endl; + + int s = luaL_loadfile(L, file); + + if ( s == 0 ) + { + // execute Lua program + s = lua_pcall(L, 0, LUA_MULTRET, 0); + } + + report_errors(L, s); + lua_close(L); + cerr << endl; + } + + return 0; +}