From 02d2afee7e161f69e38900e60f191186262cbb8c Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sat, 4 Feb 2012 20:49:07 +1000 Subject: Move the predefined Lua stuff into the new LSL.lua module, then actualy use it. --- LuaSL/src/LSL.lua | 25 ++++++++++++++++++++++--- LuaSL/src/LuaSL_compile.c | 22 ++++++++-------------- 2 files changed, 30 insertions(+), 17 deletions(-) (limited to 'LuaSL/src') diff --git a/LuaSL/src/LSL.lua b/LuaSL/src/LSL.lua index 7557668..cc9b454 100644 --- a/LuaSL/src/LSL.lua +++ b/LuaSL/src/LSL.lua @@ -1,12 +1,15 @@ -- A module of LSL stuffs. -- TODO - currently there is a constants.lsl file. Move it into here. -- It contains LSL constants and ll*() functions stubs. --- The compiler compiles this into a LSL_Scripts structure at startup, +-- The compiler compiles that into a LSL_Scripts structure at startup, -- then uses that for function and variable lookups, as well as looking up in the current script. --- Using a module means it gets compiled each time? Maybe not if I can use bytecode files. Perhaps LuaJIT caches these? +-- I can run this at compiler startup time, then iterate through the LSL table from C to generate that LSL_Script structure. + +-- Using a module means it gets compiled each time? Maybe not if I can use bytecode files. Perhaps LuaJIT caches these? +-- Does not seem to be slowing it down noticably, but that might change once the stubs are filled out. -- Use it like this - --- local lsl = require 'lsl' +-- local _LSL = require 'LSL' --[[ From http://lua-users.org/wiki/LuaModuleFunctionCritiqued A related note on C code: The luaL_register [9] function in C is @@ -292,6 +295,22 @@ function LSL.llWhisper(--[[integer]] channel, --[[string]] text) end; function LSL.llMessageLinked(--[[integer]] link,--[[integer]] num, --[[string]] text, --[[key]] aKey) end; +function LSL.preDecrement(name) _G[name] = _G[name] - 1; return _G[name]; end; +function LSL.preIncrement(name) _G[name] = _G[name] + 1; return _G[name]; end; +function LSL.postDecrement(name) local temp = _G[name]; _G[name] = _G[name] - 1; return temp; end; +function LSL.postIncrement(name) local temp = _G[name]; _G[name] = _G[name] + 1; return temp; end; + +local currentState = {} +function LSL.stateChange(x) + if nil ~= currentState.state_exit then + currentState.state_exit(); + end + currentState = x; + if nil ~= currentState.state_entry then + currentState.state_entry(); + end +end; + return LSL; diff --git a/LuaSL/src/LuaSL_compile.c b/LuaSL/src/LuaSL_compile.c index f997d62..7fa7cd7 100644 --- a/LuaSL/src/LuaSL_compile.c +++ b/LuaSL/src/LuaSL_compile.c @@ -1931,7 +1931,7 @@ static void outputRawStatement(FILE *file, outputMode mode, LSL_Statement *state } else if (OM_LUA == mode) { - fprintf(file, "stateChange(_"); + fprintf(file, "_LSL.stateChange(_"); if (statement->identifier.text) outputText(file, &(statement->identifier), TRUE); fprintf(file, "State)"); @@ -2121,10 +2121,10 @@ static void outputCrementsToken(FILE *file, outputMode mode, LSL_Leaf *content) { switch (content->toKen->type) { - case LSL_DECREMENT_PRE : fprintf(file, " _preDecrement"); break; - case LSL_INCREMENT_PRE : fprintf(file, " _preIncrement"); break; - case LSL_DECREMENT_POST : fprintf(file, " _postDecrement"); break; - case LSL_INCREMENT_POST : fprintf(file, " _postIncrement"); break; + case LSL_DECREMENT_PRE : fprintf(file, " _LSL.preDecrement"); break; + case LSL_INCREMENT_PRE : fprintf(file, " _LSL.preIncrement"); break; + case LSL_DECREMENT_POST : fprintf(file, " _LSL.postDecrement"); break; + case LSL_INCREMENT_POST : fprintf(file, " _LSL.postIncrement"); break; default : break; } @@ -2423,17 +2423,11 @@ static boolean doneParsing(LuaSL_compiler *compiler) out = fopen(luaName, "w"); if (out) { - fprintf(out, "--// Pre declared helper stuff.\n"); - fprintf(out, "local _bit = require(\"bit\")\n"); - fprintf(out, "_currentState = {}\n"); - fprintf(out, "function _preDecrement(name) _G[name] = _G[name] - 1; return _G[name]; end;\n"); - fprintf(out, "function _preIncrement(name) _G[name] = _G[name] + 1; return _G[name]; end;\n"); - fprintf(out, "function _postDecrement(name) local temp = _G[name]; _G[name] = _G[name] - 1; return temp; end;\n"); - fprintf(out, "function _postIncrement(name) local temp = _G[name]; _G[name] = _G[name] + 1; return temp; end;\n"); - fprintf(out, "function _stateChange(x) if nil ~= _currentState.state_exit then _currentState.state_exit(); end _currentState = x; if nil ~= _currentState.state_entry then _currentState.state_entry(); end end;\n"); fprintf(out, "--// Generated code goes here.\n\n"); + fprintf(out, "local _bit = require(\"bit\")\n"); + fprintf(out, "local _LSL = require(\"LSL\")\n\n"); outputLeaf(out, OM_LUA, compiler->ast); - fprintf(out, "\n\n--_stateChange(_defaultState)\n"); // This actually starts the script running. Not ready for that yet, gotta implement some ll*() functions first. So commented it out in Lua. + fprintf(out, "\n\n--_LSL.stateChange(_defaultState)\n"); // This actually starts the script running. Not ready for that yet, gotta implement some ll*() functions first. So commented it out in Lua. fprintf(out, "\n--// End of generated code.\n\n"); fclose(out); sprintf(buffer, "../../libraries/luajit-2.0/src/luajit \"%s\"", luaName); -- cgit v1.1