diff options
author | deva <deva> | 2008-05-29 14:59:00 +0000 |
---|---|---|
committer | deva <deva> | 2008-05-29 14:59:00 +0000 |
commit | 57a06611188676a76111687fd3aec485260f4b58 (patch) | |
tree | 68177e17bffc780547b1a14a25e8044079dc1003 /client/lua.cc | |
parent | 321d81bbcb950d3bcbe41b845802b3fd3bd13618 (diff) |
Added initial LUA code.
Diffstat (limited to 'client/lua.cc')
-rw-r--r-- | client/lua.cc | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/client/lua.cc b/client/lua.cc new file mode 100644 index 0000000..46c8fbd --- /dev/null +++ b/client/lua.cc @@ -0,0 +1,91 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * lua.cc + * + * Thu May 29 16:30:50 CEST 2008 + * Copyright 2008 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "lua.h" + +LUA::LUA(Variables &variables) +{ + L = luaL_newstate(); + if(L == NULL) { + // throw LUADataParserException("Could not create LUA state."); + } + + luaL_openlibs(L); + + std::string preload; + Variables::iterator var = variables.begin(); + while(var != variables.end()) { + preload += (*var).first + " = \"" + (*var).second + "\"\n"; + var++; + } + + int s = luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload"); + switch(s) { + case 0: //no errors; + break; + case LUA_ERRSYNTAX: //syntax error during pre-compilation; + case LUA_ERRMEM: //memory allocation error. + case LUA_ERRFILE: //cannot open/read the file. + //throw LUADataParserException(lua_tostring(L, lua_gettop(L))); + break; + default: + //throw LUADataParserException("Unknown return value of luaL_loadfile."); + break; + } + + // Run program (init) + lua_pcall(L, 0, LUA_MULTRET, 0); +} + +LUA::~LUA() +{ + lua_close(L); +} + +std::string LUA::run(std::string &program) +{ + int s = luaL_loadbuffer(L, program.c_str(), program.size(), "program"); + switch(s) { + case 0: //no errors; + break; + case LUA_ERRSYNTAX: //syntax error during pre-compilation; + case LUA_ERRMEM: //memory allocation error. + case LUA_ERRFILE: //cannot open/read the file. + //throw LUADataParserException(lua_tostring(L, lua_gettop(L))); + break; + default: + //throw LUADataParserException("Unknown return value of luaL_loadfile."); + break; + } + + // Run the loaded code + lua_pcall(L, 0, LUA_MULTRET, 0); + + std::string res = lua_tostring(L, lua_gettop(L)); + lua_pop(L, 1); + + return res; +} |