From b96977724164fc9f973b4d30b697b4ed228fa882 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sat, 21 Jan 2012 10:46:28 +1000 Subject: Add LSL constants by simply compiling an LSL script with them all, and keeping the result. --- LuaSL/src/LuaSL.h | 2 +- LuaSL/src/LuaSL_compile.c | 27 ++++++++++++++++++++++++--- LuaSL/src/LuaSL_main.c | 4 ++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/LuaSL/src/LuaSL.h b/LuaSL/src/LuaSL.h index 852847d..59a3fbd 100644 --- a/LuaSL/src/LuaSL.h +++ b/LuaSL/src/LuaSL.h @@ -57,7 +57,7 @@ typedef struct Eina_Bool compilerSetup(gameGlobals *game); -Eina_Bool compileLSL(gameGlobals *game, char *script); +Eina_Bool compileLSL(gameGlobals *game, char *script, boolean doConstants); void loggingStartup(gameGlobals *game); char *getDateTime(struct tm **nowOut, char *dateOut, time_t *tiemOut); diff --git a/LuaSL/src/LuaSL_compile.c b/LuaSL/src/LuaSL_compile.c index fd5bbb2..29c45f3 100644 --- a/LuaSL/src/LuaSL_compile.c +++ b/LuaSL/src/LuaSL_compile.c @@ -200,6 +200,7 @@ opType opExpr[][10] = LSL_Token **tokens = NULL; +LSL_Script constants; int lowestToken = 999999; @@ -263,6 +264,8 @@ static LSL_Leaf *findVariable(LuaSL_compiler *compiler, const char *name) } if (NULL == var) + var = eina_hash_find(constants.variables, name); + if (NULL == var) var = eina_hash_find(compiler->script.variables, name); } @@ -1092,6 +1095,8 @@ Eina_Bool compilerSetup(gameGlobals *game) tokens = calloc(i + 1, sizeof(LSL_Token *)); if (tokens) { + char buf[PATH_MAX]; + // Sort the token table. for (i = 0; LSL_Tokens[i].token != NULL; i++) { @@ -1099,6 +1104,11 @@ Eina_Bool compilerSetup(gameGlobals *game) tokens[j] = &(LSL_Tokens[i]); } + + // Compile the constants. + snprintf(buf, sizeof(buf), "%s/src/constants.lsl", PACKAGE_DATA_DIR); + compileLSL(game, buf, TRUE); + return EINA_TRUE; } else @@ -1107,7 +1117,7 @@ Eina_Bool compilerSetup(gameGlobals *game) return EINA_FALSE; } -Eina_Bool compileLSL(gameGlobals *game, char *script) +Eina_Bool compileLSL(gameGlobals *game, char *script, boolean doConstants) { Eina_Bool result = EINA_FALSE; LuaSL_compiler compiler; @@ -1160,7 +1170,14 @@ Eina_Bool compileLSL(gameGlobals *game, char *script) yylex_destroy(compiler.scanner); Parse (pParser, 0, compiler.lval, &compiler); ParseFree(pParser, free); - doneParsing(&compiler); + + if (doConstants) + { + memcpy(&constants, &(compiler.script), sizeof(LSL_Script)); + } + else + { + doneParsing(&compiler); // Take the result of the parse, and convert it into Lua source. // Each LSL script becomes a Lua state. @@ -1169,12 +1186,16 @@ Eina_Bool compileLSL(gameGlobals *game, char *script) // Compile the Lua source by the Lua compiler. + } + if (NULL != compiler.file) { fclose(compiler.file); compiler.file = NULL; } - burnLeaf(compiler.ast); + + if (!doConstants) + burnLeaf(compiler.ast); return result; } diff --git a/LuaSL/src/LuaSL_main.c b/LuaSL/src/LuaSL_main.c index 3891cb4..e0d2629 100644 --- a/LuaSL/src/LuaSL_main.c +++ b/LuaSL/src/LuaSL_main.c @@ -164,12 +164,12 @@ main(int argc, char **argv) compilerSetup(&game); // snprintf(buf, sizeof(buf), "%s/Test sim/objects/onefang's test bed/~run", PACKAGE_DATA_DIR); snprintf(buf, sizeof(buf), "%s/test2.lsl", PACKAGE_DATA_DIR); - if (compileLSL(&game, buf)) + if (compileLSL(&game, buf, FALSE)) PIm("Against all odds, the compile of %s worked! lol", buf); else PEm("The compile of %s failed!", buf); snprintf(buf, sizeof(buf), "%s/test.lsl", PACKAGE_DATA_DIR); - compileLSL(&game, buf); + compileLSL(&game, buf, FALSE); // ecore_main_loop_begin(); -- cgit v1.1