aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL
diff options
context:
space:
mode:
Diffstat (limited to 'LuaSL')
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.h33
-rw-r--r--LuaSL/src/LuaSL_compile.c40
-rw-r--r--LuaSL/src/LuaSL_lemon_yaccer.y18
3 files changed, 59 insertions, 32 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h
index 2c2bfa6..c8d1cc6 100644
--- a/LuaSL/src/LuaSL_LSL_tree.h
+++ b/LuaSL/src/LuaSL_LSL_tree.h
@@ -225,6 +225,7 @@ struct _LSL_Block
225 LSL_Block *outerBlock; 225 LSL_Block *outerBlock;
226// Eina_Hash *statements; // Probably should be some sort of eina list. 226// Eina_Hash *statements; // Probably should be some sort of eina list.
227 Eina_Hash *variables; // Those variables in this scope. 227 Eina_Hash *variables; // Those variables in this scope.
228 LSL_Function *function;
228}; 229};
229 230
230struct _LSL_Function 231struct _LSL_Function
@@ -232,6 +233,7 @@ struct _LSL_Function
232 const char *name; 233 const char *name;
233 LSL_Leaf *type; 234 LSL_Leaf *type;
234 LSL_Leaf *params; 235 LSL_Leaf *params;
236 Eina_Hash *variables;
235 LSL_Leaf *block; 237 LSL_Leaf *block;
236}; 238};
237 239
@@ -255,18 +257,18 @@ struct _LSL_Script
255 257
256typedef struct 258typedef struct
257{ 259{
258 gameGlobals *game; 260 gameGlobals *game;
259 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. 261 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.
260 int argc; 262 int argc;
261 char **argv; 263 char **argv;
262 char fileName[PATH_MAX]; 264 char fileName[PATH_MAX];
263 FILE *file; 265 FILE *file;
264 LSL_Leaf *ast; 266 LSL_Leaf *ast;
265 LSL_Script script; 267 LSL_Script script;
266 Eina_Strbuf *ignorableText; 268 Eina_Strbuf *ignorableText;
267 LSL_Leaf *lval; 269 LSL_Leaf *lval;
268 int column, line; 270 int column, line;
269 LSL_Block *currentBlock; 271 LSL_Block *currentBlock;
270} LuaSL_compiler; 272} LuaSL_compiler;
271 273
272 274
@@ -276,9 +278,10 @@ typedef struct
276 278
277 279
278void burnLeaf(void *data); 280void burnLeaf(void *data);
279LSL_Leaf *addFunction(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close, LSL_Leaf *block); 281LSL_Leaf *addFunction(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close);
282LSL_Leaf *addFunctionBody(LuaSL_compiler *compiler, LSL_Leaf *function, LSL_Leaf *block);
280LSL_Leaf *addOperation(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right); 283LSL_Leaf *addOperation(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right);
281LSL_Leaf *addParameter(LSL_Leaf *type, LSL_Leaf *newParam); 284LSL_Leaf *addParameter(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *newParam);
282LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Type type, LSL_Leaf *rval); 285LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Type type, LSL_Leaf *rval);
283LSL_Leaf *addState(LuaSL_compiler *compiler, LSL_Leaf *identifier, LSL_Leaf *block); 286LSL_Leaf *addState(LuaSL_compiler *compiler, LSL_Leaf *identifier, LSL_Leaf *block);
284LSL_Leaf *addStatement(LSL_Leaf *lval, LSL_Type type, LSL_Leaf *expr); 287LSL_Leaf *addStatement(LSL_Leaf *lval, LSL_Type type, LSL_Leaf *expr);
@@ -287,7 +290,7 @@ LSL_Leaf *addVariable(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identi
287 290
288void beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block); 291void beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block);
289LSL_Leaf *checkVariable(LuaSL_compiler *compiler, LSL_Leaf *identifier); 292LSL_Leaf *checkVariable(LuaSL_compiler *compiler, LSL_Leaf *identifier);
290LSL_Leaf *collectParameters(LSL_Leaf *list, LSL_Leaf *comma, LSL_Leaf *newParam); 293LSL_Leaf *collectParameters(LuaSL_compiler *compiler, LSL_Leaf *list, LSL_Leaf *comma, LSL_Leaf *newParam);
291void endBlock(LuaSL_compiler *compiler, LSL_Leaf *block); 294void endBlock(LuaSL_compiler *compiler, LSL_Leaf *block);
292 295
293void *ParseAlloc(void *(*mallocProc)(size_t)); 296void *ParseAlloc(void *(*mallocProc)(size_t));
diff --git a/LuaSL/src/LuaSL_compile.c b/LuaSL/src/LuaSL_compile.c
index c28a6fc..4e089c7 100644
--- a/LuaSL/src/LuaSL_compile.c
+++ b/LuaSL/src/LuaSL_compile.c
@@ -219,10 +219,13 @@ static LSL_Leaf *findVariable(LuaSL_compiler *compiler, const char *name)
219 219
220 while ((block) && (NULL == var)) 220 while ((block) && (NULL == var))
221 { 221 {
222 if (block->variables) 222 if (block->function)
223 var = eina_hash_find(block->function->variables, name);
224 if ((NULL == var) && block->variables)
223 var = eina_hash_find(block->variables, name); 225 var = eina_hash_find(block->variables, name);
224 block = block->outerBlock; 226 block = block->outerBlock;
225 } 227 }
228
226 if (NULL == var) 229 if (NULL == var)
227 var = eina_hash_find(compiler->script.variables, name); 230 var = eina_hash_find(compiler->script.variables, name);
228 } 231 }
@@ -351,7 +354,7 @@ LSL_Leaf *addOperation(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval,
351 return lval; 354 return lval;
352} 355}
353 356
354LSL_Leaf *addParameter(LSL_Leaf *type, LSL_Leaf *identifier) 357LSL_Leaf *addParameter(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier)
355{ 358{
356 LSL_Identifier *result = calloc(1, sizeof(LSL_Identifier)); 359 LSL_Identifier *result = calloc(1, sizeof(LSL_Identifier));
357 360
@@ -370,7 +373,7 @@ LSL_Leaf *addParameter(LSL_Leaf *type, LSL_Leaf *identifier)
370 return identifier; 373 return identifier;
371} 374}
372 375
373LSL_Leaf *collectParameters(LSL_Leaf *list, LSL_Leaf *comma, LSL_Leaf *newParam) 376LSL_Leaf *collectParameters(LuaSL_compiler *compiler, LSL_Leaf *list, LSL_Leaf *comma, LSL_Leaf *newParam)
374{ 377{
375 LSL_Leaf *newList = newLeaf(LSL_PARAMETER_LIST, NULL, NULL); 378 LSL_Leaf *newList = newLeaf(LSL_PARAMETER_LIST, NULL, NULL);
376 379
@@ -379,15 +382,13 @@ LSL_Leaf *collectParameters(LSL_Leaf *list, LSL_Leaf *comma, LSL_Leaf *newParam)
379 newList->left = list; 382 newList->left = list;
380 newList->value.listValue = newParam; 383 newList->value.listValue = newParam;
381 if ((list) && (list->value.listValue)) 384 if ((list) && (list->value.listValue))
382 {
383 list->value.listValue->right = comma; 385 list->value.listValue->right = comma;
384 }
385 } 386 }
386 387
387 return newList; 388 return newList;
388} 389}
389 390
390LSL_Leaf *addFunction(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close, LSL_Leaf *block) 391LSL_Leaf *addFunction(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close)
391{ 392{
392 LSL_Function *func = calloc(1, sizeof(LSL_Function)); 393 LSL_Function *func = calloc(1, sizeof(LSL_Function));
393 394
@@ -398,19 +399,40 @@ LSL_Leaf *addFunction(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identi
398 func->name = identifier->value.stringValue; 399 func->name = identifier->value.stringValue;
399 identifier->token = tokens[LSL_FUNCTION - lowestToken]; 400 identifier->token = tokens[LSL_FUNCTION - lowestToken];
400 identifier->value.functionValue = func; 401 identifier->value.functionValue = func;
401 identifier->value.functionValue->block = block;
402 func->type = type; 402 func->type = type;
403 if (type) 403 if (type)
404 identifier->basicType = type->basicType; 404 identifier->basicType = type->basicType;
405 else 405 else
406 identifier->basicType = OT_nothing; 406 identifier->basicType = OT_nothing;
407 func->params = addParenthesis(open, params, LSL_PARAMETER_LIST, close);
408 eina_hash_add(compiler->script.functions, func->name, identifier); 407 eina_hash_add(compiler->script.functions, func->name, identifier);
408 func->variables = eina_hash_stringshared_new(burnLeaf);
409 func->params = addParenthesis(open, params, LSL_PARAMETER_LIST, close);
410 while (params)
411 {
412 if (params->value.listValue)
413 {
414 LSL_Identifier *identifier = params->value.listValue->value.identifierValue;
415
416 if (identifier)
417 eina_hash_add(func->variables, identifier->name, identifier);
418 }
419 params = params->left;
420 }
421 if (compiler->currentBlock)
422 compiler->currentBlock->function = func;
409 } 423 }
410 } 424 }
411 return identifier; 425 return identifier;
412} 426}
413 427
428LSL_Leaf *addFunctionBody(LuaSL_compiler *compiler, LSL_Leaf *function, LSL_Leaf *block)
429{
430 if (function)
431 function->value.functionValue->block = block;
432
433 return function;
434}
435
414LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Type type, LSL_Leaf *rval) 436LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Type type, LSL_Leaf *rval)
415{ 437{
416 LSL_Parenthesis *parens = malloc(sizeof(LSL_Parenthesis)); 438 LSL_Parenthesis *parens = malloc(sizeof(LSL_Parenthesis));
@@ -514,7 +536,7 @@ LSL_Leaf *addVariable(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identi
514 536
515void beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block) 537void beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block)
516{ 538{
517 LSL_Block *blok = malloc(sizeof(LSL_Block)); 539 LSL_Block *blok = calloc(1, sizeof(LSL_Block));
518 540
519 if (blok) 541 if (blok)
520 { 542 {
diff --git a/LuaSL/src/LuaSL_lemon_yaccer.y b/LuaSL/src/LuaSL_lemon_yaccer.y
index d5a6751..a0defc5 100644
--- a/LuaSL/src/LuaSL_lemon_yaccer.y
+++ b/LuaSL/src/LuaSL_lemon_yaccer.y
@@ -24,7 +24,7 @@ program ::= script LSL_SCRIPT(A). { if (NULL != A) A->left = compiler->ast;
24 24
25%nonassoc LSL_SCRIPT. 25%nonassoc LSL_SCRIPT.
26script ::= script state(A). { if (NULL != A) A->left = compiler->ast; compiler->ast = A; } 26script ::= script state(A). { if (NULL != A) A->left = compiler->ast; compiler->ast = A; }
27script ::= script function(A). { if (NULL != A) A->left = compiler->ast; compiler->ast = A; } 27script ::= script functionBody(A). { if (NULL != A) A->left = compiler->ast; compiler->ast = A; }
28script ::= script statement(A). { if (NULL != A) A->left = compiler->ast; compiler->ast = A; } 28script ::= script statement(A). { if (NULL != A) A->left = compiler->ast; compiler->ast = A; }
29script ::= . 29script ::= .
30 30
@@ -37,16 +37,18 @@ state(S) ::= LSL_IDENTIFIER(I) stateBlock(B). { S = addState(compiler, I, B)
37// Function definitions. 37// Function definitions.
38 38
39%nonassoc LSL_PARAMETER LSL_PARAMETER_LIST LSL_FUNCTION. 39%nonassoc LSL_PARAMETER LSL_PARAMETER_LIST LSL_FUNCTION.
40functionList ::= functionList function. 40functionList ::= functionList functionBody.
41functionList ::= . 41functionList ::= .
42 42
43parameterList(A) ::= parameterList(B) LSL_COMMA(C) parameter(D). { A = collectParameters(B, C, D); } 43functionBody(A) ::= function(B) funcBlock(C). { A = addFunctionBody(compiler, B, C); }
44parameterList(A) ::= parameter(D). { A = collectParameters(NULL, NULL, D); } 44
45parameterList(A) ::= . { A = collectParameters(NULL, NULL, NULL); } 45parameterList(A) ::= parameterList(B) LSL_COMMA(C) parameter(D). { A = collectParameters(compiler, B, C, D); }
46parameter(A) ::= type(B) LSL_IDENTIFIER(C). { A = addParameter(B, C); } 46parameterList(A) ::= parameter(D). { A = collectParameters(compiler, NULL, NULL, D); }
47parameterList(A) ::= . { A = collectParameters(compiler, NULL, NULL, NULL); }
48parameter(A) ::= type(B) LSL_IDENTIFIER(C). { A = addParameter(compiler, B, C); }
47// Causes a conflict when it's an empty parameterList with calling the same type of function. 49// Causes a conflict when it's an empty parameterList with calling the same type of function.
48function(A) ::= LSL_IDENTIFIER(C) LSL_PARENTHESIS_OPEN(D) parameterList(E) LSL_PARENTHESIS_CLOSE(F) funcBlock(G). { A = addFunction(compiler, NULL, C, D, E, F, G); } 50function(A) ::= LSL_IDENTIFIER(C) LSL_PARENTHESIS_OPEN(D) parameterList(E) LSL_PARENTHESIS_CLOSE(F). { A = addFunction(compiler, NULL, C, D, E, F); }
49function(A) ::= type(B) LSL_IDENTIFIER(C) LSL_PARENTHESIS_OPEN(D) parameterList(E) LSL_PARENTHESIS_CLOSE(F) funcBlock(G). { A = addFunction(compiler, B, C, D, E, F, G); } 51function(A) ::= type(B) LSL_IDENTIFIER(C) LSL_PARENTHESIS_OPEN(D) parameterList(E) LSL_PARENTHESIS_CLOSE(F). { A = addFunction(compiler, B, C, D, E, F); }
50 52
51// Blocks. 53// Blocks.
52 54