diff options
Diffstat (limited to '')
-rw-r--r-- | src/LuaSL/LuaSL_compile.c | 73 |
1 files changed, 20 insertions, 53 deletions
diff --git a/src/LuaSL/LuaSL_compile.c b/src/LuaSL/LuaSL_compile.c index afe7ec0..61fce8f 100644 --- a/src/LuaSL/LuaSL_compile.c +++ b/src/LuaSL/LuaSL_compile.c | |||
@@ -2170,6 +2170,14 @@ static void outputStringToken(FILE *file, outputMode mode, LSL_Leaf *content) | |||
2170 | fprintf(file, "%s", content->value.stringValue); // The quotes are part of the string value already. | 2170 | fprintf(file, "%s", content->value.stringValue); // The quotes are part of the string value already. |
2171 | } | 2171 | } |
2172 | 2172 | ||
2173 | static void _compileCb(LuaCompiler *compiler) | ||
2174 | { | ||
2175 | free(compiler->luaName); | ||
2176 | free(compiler->SID); | ||
2177 | free(compiler->file); | ||
2178 | free(compiler); | ||
2179 | } | ||
2180 | |||
2173 | boolean compilerSetup(gameGlobals *ourGlobals) | 2181 | boolean compilerSetup(gameGlobals *ourGlobals) |
2174 | { | 2182 | { |
2175 | int i; | 2183 | int i; |
@@ -2184,6 +2192,7 @@ boolean compilerSetup(gameGlobals *ourGlobals) | |||
2184 | if (tokens) | 2192 | if (tokens) |
2185 | { | 2193 | { |
2186 | char buf[PATH_MAX]; | 2194 | char buf[PATH_MAX]; |
2195 | LuaCompiler *compiler = calloc(1, sizeof(LuaCompiler)); | ||
2187 | 2196 | ||
2188 | // Sort the token table. | 2197 | // Sort the token table. |
2189 | for (i = 0; LSL_Tokens[i].toKen != NULL; i++) | 2198 | for (i = 0; LSL_Tokens[i].toKen != NULL; i++) |
@@ -2197,7 +2206,12 @@ boolean compilerSetup(gameGlobals *ourGlobals) | |||
2197 | snprintf(buf, sizeof(buf), "lua -e 'require(\"LSL\").gimmeLSL()' > %s/constants.lsl", prefix_lib_get()); | 2206 | snprintf(buf, sizeof(buf), "lua -e 'require(\"LSL\").gimmeLSL()' > %s/constants.lsl", prefix_lib_get()); |
2198 | system(buf); | 2207 | system(buf); |
2199 | snprintf(buf, sizeof(buf), "%s/constants.lsl", prefix_lib_get()); | 2208 | snprintf(buf, sizeof(buf), "%s/constants.lsl", prefix_lib_get()); |
2200 | compileLSL(ourGlobals, NULL, "FAKE_SID", buf, TRUE); | 2209 | compiler->file = strdup(buf); |
2210 | compiler->SID = strdup("FAKE_SID"); | ||
2211 | compiler->data = ourGlobals; | ||
2212 | compiler->cb = _compileCb; | ||
2213 | if (!compileLSL(compiler, ourGlobals, NULL, "FAKE_SID", buf, TRUE)) | ||
2214 | _compileCb(compiler); | ||
2201 | 2215 | ||
2202 | return TRUE; | 2216 | return TRUE; |
2203 | } | 2217 | } |
@@ -2207,17 +2221,7 @@ boolean compilerSetup(gameGlobals *ourGlobals) | |||
2207 | return FALSE; | 2221 | return FALSE; |
2208 | } | 2222 | } |
2209 | 2223 | ||
2210 | static int luaWriter(lua_State *L, const void* p, size_t sz, void* ud) | 2224 | boolean compileLSL(LuaCompiler *lCompiler, gameGlobals *ourGlobals, Ecore_Con_Client *client, char *SID, char *script, boolean doConstants) |
2211 | { | ||
2212 | FILE *out = ud; | ||
2213 | int result = 0; | ||
2214 | |||
2215 | if (sz != fwrite(p, 1, sz, out)) | ||
2216 | result = -1; | ||
2217 | return result; | ||
2218 | } | ||
2219 | |||
2220 | boolean compileLSL(gameGlobals *ourGlobals, Ecore_Con_Client *client, char *SID, char *script, boolean doConstants) | ||
2221 | { | 2225 | { |
2222 | boolean result = FALSE; | 2226 | boolean result = FALSE; |
2223 | LuaSL_compiler compiler; | 2227 | LuaSL_compiler compiler; |
@@ -2348,8 +2352,6 @@ boolean compileLSL(gameGlobals *ourGlobals, Ecore_Con_Client *client, char *SID, | |||
2348 | // Generate the Lua source code. | 2352 | // Generate the Lua source code. |
2349 | if (out) | 2353 | if (out) |
2350 | { | 2354 | { |
2351 | lua_State *L; | ||
2352 | int err; | ||
2353 | char *ext; | 2355 | char *ext; |
2354 | 2356 | ||
2355 | fprintf(out, "--// Generated code goes here.\n\n"); | 2357 | fprintf(out, "--// Generated code goes here.\n\n"); |
@@ -2366,49 +2368,14 @@ boolean compileLSL(gameGlobals *ourGlobals, Ecore_Con_Client *client, char *SID, | |||
2366 | fclose(out); | 2368 | fclose(out); |
2367 | 2369 | ||
2368 | // Compile the Lua source code. | 2370 | // Compile the Lua source code. |
2369 | if ((L = luaL_newstate())) | 2371 | lCompiler->luaName = strdup(luaName); |
2370 | { | 2372 | lCompiler->bugCount = compiler.script.bugCount; |
2371 | luaL_openlibs(L); | 2373 | compileScript(lCompiler); |
2372 | // This ends up pushing a function onto the stack. The function is the compiled code. | 2374 | result = TRUE; |
2373 | err = luaL_loadfile(L, luaName); | ||
2374 | if (err) | ||
2375 | { | ||
2376 | compiler.script.bugCount++; | ||
2377 | if (LUA_ERRSYNTAX == err) | ||
2378 | PE("Lua syntax error in %s: %s", luaName, lua_tostring(L, -1)); | ||
2379 | else if (LUA_ERRFILE == err) | ||
2380 | PE("Lua compile file error in %s: %s", luaName, lua_tostring(L, -1)); | ||
2381 | else if (LUA_ERRMEM == err) | ||
2382 | PE("Lua compile memory allocation error in %s: %s", luaName, lua_tostring(L, -1)); | ||
2383 | } | ||
2384 | else | ||
2385 | { | ||
2386 | // Write the compiled code to a file. | ||
2387 | strcat(luaName, ".out"); | ||
2388 | out = fopen(luaName, "w"); | ||
2389 | if (out) | ||
2390 | { | ||
2391 | err = lua_dump(L, luaWriter, out); | ||
2392 | if (err) | ||
2393 | { | ||
2394 | compiler.script.bugCount++; | ||
2395 | PE("Lua compile file error writing to %s", luaName); | ||
2396 | } | ||
2397 | fclose(out); | ||
2398 | } | ||
2399 | else | ||
2400 | PC("Unable to open file %s for writing!", luaName); | ||
2401 | } | ||
2402 | } | ||
2403 | else | ||
2404 | PE("Can't create a new Lua state!"); | ||
2405 | } | 2375 | } |
2406 | else | 2376 | else |
2407 | PC("Unable to open file %s for writing!", luaName); | 2377 | PC("Unable to open file %s for writing!", luaName); |
2408 | } | 2378 | } |
2409 | |||
2410 | if (0 == compiler.script.bugCount) | ||
2411 | result = TRUE; | ||
2412 | } | 2379 | } |
2413 | 2380 | ||
2414 | if (NULL != compiler.file) | 2381 | if (NULL != compiler.file) |