aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-08-14 06:37:29 +1000
committerDavid Walter Seikel2014-08-14 06:37:29 +1000
commitba57301e92831606a18289ec10c314edd756f311 (patch)
treebb07db3340db45afc5bf795518e288a991c6312d
parentRemove a warning in lemon. (diff)
downloadSledjHamr-ba57301e92831606a18289ec10c314edd756f311.zip
SledjHamr-ba57301e92831606a18289ec10c314edd756f311.tar.gz
SledjHamr-ba57301e92831606a18289ec10c314edd756f311.tar.bz2
SledjHamr-ba57301e92831606a18289ec10c314edd756f311.tar.xz
Considerable cleanup of the top level compiling code.
-rw-r--r--src/LuaSL/LuaSL_LSL_tree.h13
-rw-r--r--src/LuaSL/LuaSL_compile.c169
-rw-r--r--src/LuaSL/LuaSL_lemon_yaccer.y6
-rw-r--r--src/LuaSL/LuaSL_main.c28
-rw-r--r--src/libraries/Runnr.c157
-rw-r--r--src/libraries/Runnr.h2
6 files changed, 144 insertions, 231 deletions
diff --git a/src/LuaSL/LuaSL_LSL_tree.h b/src/LuaSL/LuaSL_LSL_tree.h
index 5b61926..b08d1db 100644
--- a/src/LuaSL/LuaSL_LSL_tree.h
+++ b/src/LuaSL/LuaSL_LSL_tree.h
@@ -5,7 +5,7 @@
5#define LUASL_DEBUG 0 5#define LUASL_DEBUG 0
6#define COMPILE_THREADED 0 6#define COMPILE_THREADED 0
7#define LUASL_DIFF_CHECK 0 7#define LUASL_DIFF_CHECK 0
8#define LUASL_BAD_CHECK 0 8#define LUASL_BAD_CHECK 1
9 9
10 10
11#include <stddef.h> // So we can have NULL defined. 11#include <stddef.h> // So we can have NULL defined.
@@ -300,7 +300,7 @@ struct _LSL_Script
300 Eina_Hash *functions; 300 Eina_Hash *functions;
301 Eina_Hash *states; 301 Eina_Hash *states;
302 Eina_Hash *variables; 302 Eina_Hash *variables;
303 int bugCount, warningCount; 303 int warningCount;
304}; 304};
305 305
306/* Tracking variables. 306/* Tracking variables.
@@ -372,11 +372,8 @@ Need to do something about that.
372 372
373typedef struct 373typedef struct
374{ 374{
375 LuaCompiler compiler; 375 LuaCompiler *compiler;
376 void *scanner; // This should be of type yyscan_t, which is typedef to void * anyway, but that does not get defined until LuaSL_lexer.h, which depends on this struct being defined first. 376 void *scanner; // This should be of type yyscan_t, which is typedef to void * anyway, but that does not get defined until LuaSL_lexer.h, which depends on this struct being defined first.
377 int argc;
378 char **argv;
379 FILE *file;
380 LSL_Leaf *ast; 377 LSL_Leaf *ast;
381 LSL_Script script; 378 LSL_Script script;
382 LSL_State state; 379 LSL_State state;
@@ -390,8 +387,6 @@ typedef struct
390 int column, line; 387 int column, line;
391 int undeclared; 388 int undeclared;
392 boolean inState; 389 boolean inState;
393 boolean doConstants;
394 boolean result;
395} LuaSL_compiler; 390} LuaSL_compiler;
396 391
397 392
@@ -402,7 +397,7 @@ typedef struct
402 397
403 398
404boolean compilerSetup(gameGlobals *ourGlobals); 399boolean compilerSetup(gameGlobals *ourGlobals);
405boolean compileLSL(LuaSL_compiler *lCompiler); 400void compileLSL(LuaCompiler *compiler);
406void burnLeaf(void *data); 401void burnLeaf(void *data);
407 402
408LSL_Leaf *addBlock(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right); 403LSL_Leaf *addBlock(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right);
diff --git a/src/LuaSL/LuaSL_compile.c b/src/LuaSL/LuaSL_compile.c
index 0b27cf5..d1ef48b 100644
--- a/src/LuaSL/LuaSL_compile.c
+++ b/src/LuaSL/LuaSL_compile.c
@@ -361,8 +361,8 @@ LSL_Leaf *checkVariable(LuaSL_compiler *compiler, LSL_Leaf *identifier, LSL_Leaf
361 } 361 }
362 else 362 else
363 { 363 {
364 compiler->script.bugCount++; 364 compiler->compiler->bugCount++;
365 sendBack(compiler->compiler.client, compiler->compiler.SID, "compilerError(%d,%d,NOT FOUND variable %s)", identifier->line, identifier->column, identifier->value.stringValue); 365 sendBack(compiler->compiler->client, compiler->compiler->SID, "compilerError(%d,%d,NOT FOUND variable %s)", identifier->line, identifier->column, identifier->value.stringValue);
366 } 366 }
367 } 367 }
368 368
@@ -404,7 +404,7 @@ LSL_Leaf *addOperation(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval,
404 if (OT_undeclared == lType) 404 if (OT_undeclared == lType)
405 { 405 {
406 compiler->script.warningCount++; 406 compiler->script.warningCount++;
407// sendBack(compiler->compiler.client, compiler->compiler.SID, "compilerWarning(%d,%d,Undeclared identifier issue, deferring this until the second pass)", lval->line, lval->column); 407// sendBack(compiler->compiler->client, compiler->compiler->SID, "compilerWarning(%d,%d,Undeclared identifier issue, deferring this until the second pass)", lval->line, lval->column);
408 lval->basicType = OT_undeclared; 408 lval->basicType = OT_undeclared;
409 return lval; 409 return lval;
410 } 410 }
@@ -432,7 +432,7 @@ LSL_Leaf *addOperation(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval,
432 if (OT_undeclared == rType) 432 if (OT_undeclared == rType)
433 { 433 {
434 compiler->script.warningCount++; 434 compiler->script.warningCount++;
435// sendBack(compiler->compiler.client, compiler->compiler.SID, "compilerWarning(%d,%d,Undeclared identifier issue, deferring this until the second pass)", lval->line, lval->column); 435// sendBack(compiler->compiler->client, compiler->compiler->SID, "compilerWarning(%d,%d,Undeclared identifier issue, deferring this until the second pass)", lval->line, lval->column);
436 lval->basicType = OT_undeclared; 436 lval->basicType = OT_undeclared;
437 return lval; 437 return lval;
438 } 438 }
@@ -632,8 +632,8 @@ else
632 rightType = allowed[right->basicType].name; 632 rightType = allowed[right->basicType].name;
633 } 633 }
634 634
635 compiler->script.bugCount++; 635 compiler->compiler->bugCount++;
636 sendBack(compiler->compiler.client, compiler->compiler.SID, "compilerError(%d,%d,Invalid operation [%s(%s) %s %s(%s)])", lval->line, lval->column, leftType, leftToken, lval->toKen->toKen, rightType, rightToken); 636 sendBack(compiler->compiler->client, compiler->compiler->SID, "compilerError(%d,%d,Invalid operation [%s(%s) %s %s(%s)])", lval->line, lval->column, leftType, leftToken, lval->toKen->toKen, rightType, rightToken);
637 } 637 }
638 } 638 }
639 639
@@ -1152,7 +1152,7 @@ LSL_Leaf *addStatement(LuaSL_compiler *compiler, LSL_Leaf *lval, LSL_Leaf *flow,
1152 } 1152 }
1153 default : 1153 default :
1154 { 1154 {
1155 compiler->script.bugCount++; 1155 compiler->compiler->bugCount++;
1156 PE("Should not be here %d.", stat->type); 1156 PE("Should not be here %d.", stat->type);
1157 break; 1157 break;
1158 } 1158 }
@@ -2169,14 +2169,6 @@ static void outputStringToken(FILE *file, outputMode mode, LSL_Leaf *content)
2169 fprintf(file, "%s", content->value.stringValue); // The quotes are part of the string value already. 2169 fprintf(file, "%s", content->value.stringValue); // The quotes are part of the string value already.
2170} 2170}
2171 2171
2172static void _compileCb(LuaCompiler *compiler)
2173{
2174 free(compiler->luaName);
2175 free(compiler->SID);
2176 free(compiler->file);
2177 free(compiler);
2178}
2179
2180boolean compilerSetup(gameGlobals *ourGlobals) 2172boolean compilerSetup(gameGlobals *ourGlobals)
2181{ 2173{
2182 int i; 2174 int i;
@@ -2191,7 +2183,7 @@ boolean compilerSetup(gameGlobals *ourGlobals)
2191 if (tokens) 2183 if (tokens)
2192 { 2184 {
2193 char buf[PATH_MAX]; 2185 char buf[PATH_MAX];
2194 LuaSL_compiler *compiler = calloc(1, sizeof(LuaSL_compiler)); 2186 LuaCompiler *compiler = calloc(1, sizeof(LuaCompiler));
2195 2187
2196 // Sort the token table. 2188 // Sort the token table.
2197 for (i = 0; LSL_Tokens[i].toKen != NULL; i++) 2189 for (i = 0; LSL_Tokens[i].toKen != NULL; i++)
@@ -2205,13 +2197,11 @@ boolean compilerSetup(gameGlobals *ourGlobals)
2205 snprintf(buf, sizeof(buf), "luajit -e 'require(\"LSL\").gimmeLSL()' > %s/constants.lsl", prefix_lib_get()); 2197 snprintf(buf, sizeof(buf), "luajit -e 'require(\"LSL\").gimmeLSL()' > %s/constants.lsl", prefix_lib_get());
2206 system(buf); 2198 system(buf);
2207 snprintf(buf, sizeof(buf), "%s/constants.lsl", prefix_lib_get()); 2199 snprintf(buf, sizeof(buf), "%s/constants.lsl", prefix_lib_get());
2208 compiler->compiler.file = strdup(buf); 2200 compiler->file = strdup(buf);
2209 compiler->compiler.SID = strdup("FAKE_SID"); 2201 compiler->SID = strdup("FAKE_SID");
2210 compiler->compiler.data = ourGlobals;
2211 compiler->compiler.cb = _compileCb;
2212 compiler->doConstants = TRUE; 2202 compiler->doConstants = TRUE;
2213 if (!compileLSL(compiler)) 2203 compiler->parser = (compileCb) compileLSL;
2214 _compileCb(&compiler->compiler); 2204 compileScript(compiler, COMPILE_THREADED);
2215 2205
2216 return TRUE; 2206 return TRUE;
2217 } 2207 }
@@ -2221,77 +2211,85 @@ boolean compilerSetup(gameGlobals *ourGlobals)
2221 return FALSE; 2211 return FALSE;
2222} 2212}
2223 2213
2224boolean compileLSL(LuaSL_compiler *compiler) 2214void compileLSL(LuaCompiler *compiler)
2225{ 2215{
2216 LuaSL_compiler *lcompiler = calloc(1, sizeof(LuaSL_compiler));
2226 void *pParser = ParseAlloc(malloc); 2217 void *pParser = ParseAlloc(malloc);
2227#if LUASL_BAD_CHECK 2218#if LUASL_BAD_CHECK
2228 char tempName[PATH_MAX]; 2219 char tempName[PATH_MAX];
2229#endif 2220#endif
2221 FILE *in;
2230 int yv; 2222 int yv;
2231 2223
2224 lcompiler->compiler = compiler;
2225
2232// Parse the LSL script, validating it and reporting errors. 2226// Parse the LSL script, validating it and reporting errors.
2233// Just pass all LSL constants and ll*() )function names through to Lua, assume they are globals there. 2227// Just pass all LSL constants and ll*() )function names through to Lua, assume they are globals there.
2234 2228
2235 compiler->result = FALSE; 2229 lcompiler->script.functions = eina_hash_stringshared_new(burnLeaf);
2236 compiler->script.functions = eina_hash_stringshared_new(burnLeaf); 2230 lcompiler->script.states = eina_hash_stringshared_new(burnLeaf);
2237 compiler->script.states = eina_hash_stringshared_new(burnLeaf); 2231 lcompiler->script.variables = eina_hash_stringshared_new(burnLeaf);
2238 compiler->script.variables = eina_hash_stringshared_new(burnLeaf); 2232 eina_clist_init(&(lcompiler->danglingCalls));
2239 eina_clist_init(&(compiler->danglingCalls));
2240#if LUASL_DIFF_CHECK 2233#if LUASL_DIFF_CHECK
2241 compiler->ignorable = eina_strbuf_new(); 2234 lcompiler->ignorable = eina_strbuf_new();
2242#endif 2235#endif
2243 2236
2244 PI("Compiling %s.", compiler->compiler.file); 2237 PI("Compiling %s.", lcompiler->compiler->file);
2245 2238
2246 compiler->file = fopen(compiler->compiler.file, "r"); 2239 in = fopen(lcompiler->compiler->file, "r");
2247 if (NULL == compiler->file) 2240 if (NULL == in)
2248 { 2241 {
2249 PE("Error opening file %s.", compiler->compiler.file); 2242 PE("Error opening file %s.", lcompiler->compiler->file);
2250 return FALSE; 2243 return;
2251 } 2244 }
2252#if LUASL_BAD_CHECK 2245#if LUASL_BAD_CHECK
2253 // Mark the file as bad, in case we crash while compiling it. 2246 // Mark the file as bad, in case we crash while compiling it.
2254 // NOTE - wont work so well when we are threaded. 2247 // NOTE - wont work so well when we are threaded.
2255 sprintf(tempName, "%s.BAD", compiler->compiler.file); 2248 sprintf(tempName, "%s.BAD", lcompiler->compiler->file);
2256 ecore_file_mv(compiler->compiler.file, tempName); 2249 ecore_file_mv(lcompiler->compiler->file, tempName);
2257#endif 2250#endif
2258 compiler->ast = NULL; 2251 lcompiler->ast = NULL;
2259 compiler->lval = newLeaf(LSL_UNKNOWN, NULL, NULL); 2252 lcompiler->lval = newLeaf(LSL_UNKNOWN, NULL, NULL);
2260 // Text editors usually start counting at 1, even programmers editors. mcedit is an exception, but you can deal with that yourself. 2253 // Text editors usually start counting at 1, even programmers editors. mcedit is an exception, but you can deal with that yourself.
2261 compiler->column = 1; 2254 lcompiler->column = 1;
2262 compiler->line = 1; 2255 lcompiler->line = 1;
2263 2256
2264 if (yylex_init_extra(compiler, &(compiler->scanner))) 2257 if (yylex_init_extra(lcompiler, &(lcompiler->scanner)))
2265 return compiler->result; 2258 return;
2266 if (LUASL_DEBUG) 2259 if (LUASL_DEBUG)
2267 { 2260 {
2268 yyset_debug(1, compiler->scanner); 2261 yyset_debug(1, lcompiler->scanner);
2269 ParseTrace(stdout, "LSL_lemon "); 2262 ParseTrace(stdout, "LSL_lemon ");
2270 } 2263 }
2271 yyset_in(compiler->file, compiler->scanner); 2264 yyset_in(in, lcompiler->scanner);
2272 // on EOF yylex will return 0 2265 // on EOF yylex will return 0
2273 while((yv = yylex(compiler->lval, compiler->scanner)) != 0) 2266 while((yv = yylex(lcompiler->lval, lcompiler->scanner)) != 0)
2274 { 2267 {
2275 Parse(pParser, yv, compiler->lval, compiler); 2268 Parse(pParser, yv, lcompiler->lval, lcompiler);
2276 if (LSL_SCRIPT == yv) 2269 if (LSL_SCRIPT == yv)
2277 break; 2270 break;
2278 compiler->lval = newLeaf(LSL_UNKNOWN, NULL, NULL); 2271 lcompiler->lval = newLeaf(LSL_UNKNOWN, NULL, NULL);
2279 } 2272 }
2280 2273
2281 yylex_destroy(compiler->scanner); 2274 yylex_destroy(lcompiler->scanner);
2282 Parse(pParser, 0, compiler->lval, compiler); 2275 Parse(pParser, 0, lcompiler->lval, lcompiler);
2283 ParseFree(pParser, free); 2276 ParseFree(pParser, free);
2277 if (NULL != in)
2278 {
2279 fclose(in);
2280 in = NULL;
2281 }
2284 2282
2285 if (compiler->undeclared) 2283 if (lcompiler->undeclared)
2286 { 2284 {
2287// PW("A second pass is needed to check if functions where used before they where declared. To avoid this second pass, don't do that."); 2285// PW("A second pass is needed to check if functions where used before they where declared. To avoid this second pass, don't do that.");
2288 if (eina_clist_count(&(compiler->danglingCalls))) 2286 if (eina_clist_count(&(lcompiler->danglingCalls)))
2289 { 2287 {
2290 LSL_FunctionCall *call = NULL; 2288 LSL_FunctionCall *call = NULL;
2291 2289
2292 EINA_CLIST_FOR_EACH_ENTRY(call, &(compiler->danglingCalls), LSL_FunctionCall, dangler) 2290 EINA_CLIST_FOR_EACH_ENTRY(call, &(lcompiler->danglingCalls), LSL_FunctionCall, dangler)
2293 { 2291 {
2294 LSL_Leaf *func = findFunction(compiler, call->call->value.stringValue); 2292 LSL_Leaf *func = findFunction(lcompiler, call->call->value.stringValue);
2295 2293
2296 if (func) 2294 if (func)
2297 { 2295 {
@@ -2302,23 +2300,18 @@ boolean compileLSL(LuaSL_compiler *compiler)
2302 call->call->basicType = func->basicType; 2300 call->call->basicType = func->basicType;
2303 } 2301 }
2304 else 2302 else
2305 sendBack(compiler->compiler.client, compiler->compiler.SID, "compilerError(%d,%d,NOT FOUND function %s called)", call->call->line, call->call->column, call->call->value.stringValue); 2303 sendBack(lcompiler->compiler->client, lcompiler->compiler->SID, "compilerError(%d,%d,NOT FOUND function %s called)", call->call->line, call->call->column, call->call->value.stringValue);
2306 } 2304 }
2307 } 2305 }
2308 secondPass(compiler, compiler->ast); 2306 secondPass(lcompiler, lcompiler->ast);
2309// PD("Second pass completed."); 2307// PD("Second pass completed.");
2310 } 2308 }
2311 2309
2312 if (compiler->doConstants) 2310 if (lcompiler->compiler->doConstants)
2313 { 2311 memcpy(&constants, &(lcompiler->script), sizeof(LSL_Script));
2314 memcpy(&constants, &(compiler->script), sizeof(LSL_Script));
2315 compiler->result = TRUE;
2316 }
2317 else 2312 else
2318 { 2313 {
2319 compiler->result = FALSE; 2314 if (lcompiler->ast)
2320
2321 if (compiler->ast)
2322 { 2315 {
2323 FILE *out; 2316 FILE *out;
2324 char buffer[PATH_MAX]; 2317 char buffer[PATH_MAX];
@@ -2328,28 +2321,32 @@ boolean compileLSL(LuaSL_compiler *compiler)
2328 2321
2329 if (LUASL_DIFF_CHECK) 2322 if (LUASL_DIFF_CHECK)
2330 { 2323 {
2331 strcpy(outName, compiler->compiler.file); 2324 strcpy(outName, lcompiler->compiler->file);
2332 strcat(outName, "2"); 2325 strcat(outName, "2");
2333 out = fopen(outName, "w"); 2326 out = fopen(outName, "w");
2334 if (out) 2327 if (out)
2335 { 2328 {
2336 char diffName[PATH_MAX]; 2329 char diffName[PATH_MAX];
2337 2330
2338 strcpy(diffName, compiler->compiler.file); 2331 strcpy(diffName, lcompiler->compiler->file);
2339 strcat(diffName, ".diff"); 2332 strcat(diffName, ".diff");
2340 outputLeaf(out, OM_LSL, compiler->ast); 2333 outputLeaf(out, OM_LSL, lcompiler->ast);
2341 fclose(out); 2334 fclose(out);
2342 sprintf(buffer, "diff -u \"%s\" \"%s\" > \"%s\"", compiler->compiler.file, outName, diffName); 2335 sprintf(buffer, "diff -u \"%s\" \"%s\" > \"%s\"", lcompiler->compiler->file, outName, diffName);
2343 count = system(buffer); 2336 count = system(buffer);
2344 if (0 != count) 2337 if (0 != count)
2338 {
2339 lcompiler->compiler->bugCount++;
2345 PE("LSL output file is different - %s!", outName); 2340 PE("LSL output file is different - %s!", outName);
2346 else 2341 }
2347 compiler->result = TRUE;
2348 } 2342 }
2349 else 2343 else
2344 {
2345 lcompiler->compiler->bugCount++;
2350 PC("Unable to open file %s for writing!", outName); 2346 PC("Unable to open file %s for writing!", outName);
2347 }
2351 } 2348 }
2352 strcpy(luaName, compiler->compiler.file); 2349 strcpy(luaName, lcompiler->compiler->file);
2353 strcat(luaName, ".lua"); 2350 strcat(luaName, ".lua");
2354 out = fopen(luaName, "w"); 2351 out = fopen(luaName, "w");
2355 // Generate the Lua source code. 2352 // Generate the Lua source code.
@@ -2360,38 +2357,32 @@ boolean compileLSL(LuaSL_compiler *compiler)
2360 fprintf(out, "--// Generated code goes here.\n\n"); 2357 fprintf(out, "--// Generated code goes here.\n\n");
2361 fprintf(out, "local _bit = require(\"bit\")\n"); 2358 fprintf(out, "local _bit = require(\"bit\")\n");
2362 fprintf(out, "local _LSL = require(\"LSL\")\n\n"); 2359 fprintf(out, "local _LSL = require(\"LSL\")\n\n");
2363 fprintf(out, "local _SID = [=[%s]=]\n\n", compiler->compiler.SID); 2360 fprintf(out, "local _SID = [=[%s]=]\n\n", lcompiler->compiler->SID);
2364 strcpy(buffer, basename(compiler->compiler.file)); 2361 strcpy(buffer, basename(lcompiler->compiler->file));
2365 if ((ext = rindex(buffer, '.'))) 2362 if ((ext = rindex(buffer, '.')))
2366 ext[0] = '\0'; 2363 ext[0] = '\0';
2367 fprintf(out, "local _scriptName = [=[%s]=]\n\n", buffer); 2364 fprintf(out, "local _scriptName = [=[%s]=]\n\n", buffer);
2368 outputLeaf(out, OM_LUA, compiler->ast); 2365 outputLeaf(out, OM_LUA, lcompiler->ast);
2369 fprintf(out, "\n\n_LSL.mainLoop(_SID, _scriptName, _defaultState)\n"); // This actually starts the script running. 2366 fprintf(out, "\n\n_LSL.mainLoop(_SID, _scriptName, _defaultState)\n"); // This actually starts the script running.
2370 fprintf(out, "\n--// End of generated code.\n\n"); 2367 fprintf(out, "\n--// End of generated code.\n\n");
2371 fclose(out); 2368 fclose(out);
2372 2369
2373 // Compile the Lua source code. 2370 // Make sure the output gets compiled by Lua.
2374 compiler->compiler.luaName = strdup(luaName); 2371 lcompiler->compiler->luaName = strdup(luaName);
2375 compiler->compiler.bugCount = compiler->script.bugCount;
2376 compileScript(&compiler->compiler, COMPILE_THREADED);
2377 compiler->result = TRUE;
2378 } 2372 }
2379 else 2373 else
2374 {
2375 lcompiler->compiler->bugCount++;
2380 PC("Unable to open file %s for writing!", luaName); 2376 PC("Unable to open file %s for writing!", luaName);
2377 }
2381 } 2378 }
2379 burnLeaf(lcompiler->ast);
2382 } 2380 }
2383 2381
2384 if (NULL != compiler->file)
2385 {
2386 fclose(compiler->file);
2387 compiler->file = NULL;
2388 }
2389
2390 if (!compiler->doConstants)
2391 burnLeaf(compiler->ast);
2392
2393#if LUASL_BAD_CHECK 2382#if LUASL_BAD_CHECK
2394 ecore_file_mv(tempName, compiler->compiler.file); 2383 ecore_file_mv(tempName, lcompiler->compiler->file);
2395#endif 2384#endif
2396 return compiler->result; 2385
2386 free(lcompiler);
2387 return;
2397} 2388}
diff --git a/src/LuaSL/LuaSL_lemon_yaccer.y b/src/LuaSL/LuaSL_lemon_yaccer.y
index f03837e..2a23e84 100644
--- a/src/LuaSL/LuaSL_lemon_yaccer.y
+++ b/src/LuaSL/LuaSL_lemon_yaccer.y
@@ -229,19 +229,19 @@ expr(A) ::= LSL_STRING(B). { B->basicType = OT_string; A = B; }
229 229
230%parse_failure 230%parse_failure
231{ 231{
232 compiler->script.bugCount++; 232 compiler->compiler->bugCount++;
233 PE("Giving up. Parser is hopelessly lost!"); 233 PE("Giving up. Parser is hopelessly lost!");
234} 234}
235 235
236%stack_overflow 236%stack_overflow
237{ 237{
238 compiler->script.bugCount++; 238 compiler->compiler->bugCount++;
239 PE("Giving up. Parser stack overflow @ line %d, column %d!", yypMinor->yy0->line, yypMinor->yy0->column); // Gotta love consistancy, if it ever happens. 239 PE("Giving up. Parser stack overflow @ line %d, column %d!", yypMinor->yy0->line, yypMinor->yy0->column); // Gotta love consistancy, if it ever happens.
240} 240}
241 241
242%syntax_error 242%syntax_error
243{ 243{
244 compiler->script.bugCount++; 244 compiler->compiler->bugCount++;
245 PE("Syntax error @ line %d, column %d!", yyminor.yy0->line, yyminor.yy0->column); 245 PE("Syntax error @ line %d, column %d!", yyminor.yy0->line, yyminor.yy0->column);
246} 246}
247 247
diff --git a/src/LuaSL/LuaSL_main.c b/src/LuaSL/LuaSL_main.c
index 3ed9004..f316fdb 100644
--- a/src/LuaSL/LuaSL_main.c
+++ b/src/LuaSL/LuaSL_main.c
@@ -186,10 +186,6 @@ static void _compileCb(LuaCompiler *compiler)
186 sendBack(compiler->client, compiler->SID, "compiled(true)"); 186 sendBack(compiler->client, compiler->SID, "compiled(true)");
187 else 187 else
188 sendBack(compiler->client, compiler->SID, "compiled(false)"); 188 sendBack(compiler->client, compiler->SID, "compiled(false)");
189 free(compiler->luaName);
190 free(compiler->SID);
191 free(compiler->file);
192 free(compiler);
193} 189}
194 190
195static Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Client_Data *ev) 191static Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Client_Data *ev)
@@ -218,7 +214,7 @@ static Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Client_D
218 { 214 {
219 char *temp; 215 char *temp;
220 char *file; 216 char *file;
221 LuaSL_compiler *compiler = calloc(1, sizeof(LuaSL_compiler)); 217 LuaCompiler *compiler = calloc(1, sizeof(LuaCompiler));
222 218
223 strcpy(buf, &command[8]); 219 strcpy(buf, &command[8]);
224 temp = buf; 220 temp = buf;
@@ -227,23 +223,13 @@ static Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Client_D
227 temp++; 223 temp++;
228 temp[0] = '\0'; 224 temp[0] = '\0';
229 225
230 compiler->compiler.file = strdup(file); 226 compiler->file = strdup(file);
231 compiler->compiler.SID = strdup(SID); 227 compiler->SID = strdup(SID);
232 compiler->compiler.client = ev->client; 228 compiler->client = ev->client;
233 compiler->compiler.data = ourGlobals;
234 compiler->compiler.cb = _compileCb;
235 compiler->doConstants = FALSE; 229 compiler->doConstants = FALSE;
236#if COMPILE_THREADED 230 compiler->parser = (compileCb) compileLSL;
237 compiler->compiler.parser = (compileCb) compileLSL; 231 compiler->cb = _compileCb;
238 compileScript(&compiler->compiler); 232 compileScript(compiler, COMPILE_THREADED);
239#else
240 if (!compileLSL(compiler))
241 {
242 compiler->compiler.bugCount++;
243 PE("Compile of %s failed in a mysterious way.", file);
244 _compileCb(&(compiler->compiler));
245 }
246#endif
247 } 233 }
248 else if (0 == strncmp(command, "run(", 4)) 234 else if (0 == strncmp(command, "run(", 4))
249 { 235 {
diff --git a/src/libraries/Runnr.c b/src/libraries/Runnr.c
index 760a860..1f10d33 100644
--- a/src/libraries/Runnr.c
+++ b/src/libraries/Runnr.c
@@ -410,85 +410,20 @@ static int luaWriter(lua_State *L, const void* p, size_t sz, void* ud)
410 return result; 410 return result;
411} 411}
412 412
413// TODO - This didn't help the compile time much, perhaps move the rest of the compiling stage into this thread as a callback? 413// Gets called on the main thread when the compile thread ends or fails.
414/* 414static void _compileEnd(void *data, Ecore_Thread *thread)
415
416LuaSL.c : _data( ... "compile(" ...
417 allocate LuaSL_compiler
418 extract SID, file, and name from data stream
419 massage data into LuaCompiler
420 compileLSL(LuaCompiler, FALSE)
421LuaSL_compile.c : compileLSL()
422 init it
423* open the .LSL file
424* init lexer
425* run lexer in a loop
426* setup second pass in a loop
427* may do a sendBack()
428* secondPass(LuaSL_compiler, ... )
429* open the .lua file
430* generate Lua source code
431 compileScript(LuaSL_compiler)
432Runnr;c : compileScript(
433 start a feedback thread
434
435LuaSL.c : _compileCb
436 scriptAdd( ... )
437 Needs LuaCompiler-> file, SID, client, data
438 adds the compiled script to the list of scripts being run.
439 sendBack results
440 frees LuaCompiler-> luaName, SID, file. Alse frees LuaCompiler.
441
442
443typedef void (* compileCb)(LuaCompile *compiler);
444
445typedef struct _LuaCompile
446{
447 char *file, *SID, *luaName;
448 int bugCount;
449 void *data; OurGlobals, Passed to the call back, to be passed to scriptAdd, which stores it as a void pointer, then ... ????? Don't think it's actually used.
450 Ecore_Con_Client *client;
451 compileCb parser;
452 compileCb cb;
453} LuaCompiler;
454
455typedef struct
456{
457 LuaCompiler compiler;
458 void *scanner; // This should be of type yyscan_t, which is typedef to void * anyway, but that does not get defined until LuaSL_lexer.h, which depends on this struct being defined first.
459 int argc;
460 char **argv;
461 FILE *file;
462 LSL_Leaf *ast;
463 LSL_Script script;
464 LSL_State state;
465#if LUASL_DIFF_CHECK
466 Eina_Strbuf *ignorable;
467#endif
468 LSL_Leaf *lval;
469 LSL_Block *currentBlock;
470 LSL_Function *currentFunction;
471 Eina_Clist danglingCalls; // HEAD for function calls used before the function is defined.
472 int column, line;
473 int undeclared;
474 boolean inState;
475 boolean doConstants;
476 boolean result;
477} LuaSL_compiler;
478
479
480*/
481
482static void _compileNotify(void *data, Ecore_Thread *thread, void *message)
483{ 415{
484 LuaCompiler *compiler = data; 416 LuaCompiler *compiler = data;
485 417
486 if (compiler->cb) compiler->cb(compiler); 418 if (compiler->cb) compiler->cb(compiler);
419 free(compiler->file);
420 free(compiler->luaName);
421 free(compiler->SID);
487} 422}
488 423
489// TODO - Should pass error messages back through a linked list. 424// TODO - Should pass error messages back through a linked list.
490// To eventually get passed back to the calling app via compiler->cb 425// To eventually get passed back to the calling app via compiler->errors and compiler->warnings.
491// We use - luaName, bugCount, cb 426// Will need ageneric "add this formatted string to a linked list" function, in SledjHamr.c
492static void _compileThread(void *data, Ecore_Thread *thread) 427static void _compileThread(void *data, Ecore_Thread *thread)
493{ 428{
494 LuaCompiler *compiler = data; 429 LuaCompiler *compiler = data;
@@ -499,62 +434,68 @@ static void _compileThread(void *data, Ecore_Thread *thread)
499 434
500 if (compiler->parser) compiler->parser(compiler); 435 if (compiler->parser) compiler->parser(compiler);
501 436
502 strcpy(name, compiler->luaName); 437 if (compiler->luaName)
503 if ((L = luaL_newstate()))
504 { 438 {
505 luaL_openlibs(L); 439 strcpy(name, compiler->luaName);
506 // This ends up pushing a function onto the stack. The function is the compiled code. 440 if ((L = luaL_newstate()))
507 err = luaL_loadfile(L, name);
508 if (err)
509 { 441 {
510 compiler->bugCount++; 442 luaL_openlibs(L);
511 if (LUA_ERRSYNTAX == err) 443 // This ends up pushing a function onto the stack. The function is the compiled code.
512 printf("Lua syntax error in %s: %s\n", name, lua_tostring(L, -1)); 444 err = luaL_loadfile(L, name);
513 else if (LUA_ERRFILE == err) 445 if (err)
514 printf("Lua compile file error in %s: %s\n", name, lua_tostring(L, -1));
515 else if (LUA_ERRMEM == err)
516 printf("Lua compile memory allocation error in %s: %s\n", name, lua_tostring(L, -1));
517 }
518 else
519 {
520 // Write the compiled code to a file.
521 strcat(name, ".out");
522 out = fopen(name, "w");
523 if (out)
524 { 446 {
525 err = lua_dump(L, luaWriter, out); 447 compiler->bugCount++;
526 if (err) 448 if (LUA_ERRSYNTAX == err)
527 { 449 printf("Lua syntax error in %s: %s\n", name, lua_tostring(L, -1));
528 compiler->bugCount++; 450 else if (LUA_ERRFILE == err)
529 printf("Lua compile file error writing to %s\n", name); 451 printf("Lua compile file error in %s: %s\n", name, lua_tostring(L, -1));
530 } 452 else if (LUA_ERRMEM == err)
531 fclose(out); 453 printf("Lua compile memory allocation error in %s: %s\n", name, lua_tostring(L, -1));
532 } 454 }
533 else 455 else
534 { 456 {
535 compiler->bugCount++; 457 // Write the compiled code to a file.
536 printf("CRITICAL! Unable to open file %s for writing!\n", name); 458 strcat(name, ".out");
459 out = fopen(name, "w");
460 if (out)
461 {
462 err = lua_dump(L, luaWriter, out);
463 if (err)
464 {
465 compiler->bugCount++;
466 printf("Lua compile file error writing to %s\n", name);
467 }
468 fclose(out);
469 }
470 else
471 {
472 compiler->bugCount++;
473 printf("CRITICAL! Unable to open file %s for writing!\n", name);
474 }
537 } 475 }
538 } 476 }
477 else if (!compiler->doConstants)
478 {
479 compiler->bugCount++;
480 printf("Can't create a new Lua state!\n");
481 }
539 } 482 }
540 else 483 else
541 { 484 {
542 compiler->bugCount++; 485 compiler->bugCount++;
543 printf("Can't create a new Lua state!\n"); 486 printf("Nothing for Lua to compile!\n");
544 } 487 }
545
546 if (thread)
547 ecore_thread_feedback(thread, compiler);
548 else
549 _compileNotify(compiler, thread, NULL);
550} 488}
551 489
552void compileScript(LuaCompiler *compiler, int threadIt) 490void compileScript(LuaCompiler *compiler, int threadIt)
553{ 491{
554 if (threadIt) 492 if (threadIt)
555 ecore_thread_feedback_run(_compileThread, _compileNotify, NULL, NULL, compiler, EINA_FALSE); 493 ecore_thread_run(_compileThread, _compileEnd, _compileEnd, compiler);
556 else 494 else
495 {
557 _compileThread(compiler, NULL); 496 _compileThread(compiler, NULL);
497 _compileEnd(compiler, NULL);
498 }
558} 499}
559 500
560// Assumes the scripts mutex is taken already. 501// Assumes the scripts mutex is taken already.
diff --git a/src/libraries/Runnr.h b/src/libraries/Runnr.h
index 8837eed..63e0afc 100644
--- a/src/libraries/Runnr.h
+++ b/src/libraries/Runnr.h
@@ -34,10 +34,10 @@ typedef struct _LuaCompile
34{ 34{
35 char *file, *SID, *luaName; 35 char *file, *SID, *luaName;
36 int bugCount; 36 int bugCount;
37 void *data;
38 Ecore_Con_Client *client; 37 Ecore_Con_Client *client;
39 compileCb parser; 38 compileCb parser;
40 compileCb cb; 39 compileCb cb;
40 boolean doConstants;
41} LuaCompiler; 41} LuaCompiler;
42 42
43typedef struct _script script; 43typedef struct _script script;