aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/src/LuaSL_LSL_tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'LuaSL/src/LuaSL_LSL_tree.c')
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.c188
1 files changed, 177 insertions, 11 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c
index c593378..e37f794 100644
--- a/LuaSL/src/LuaSL_LSL_tree.c
+++ b/LuaSL/src/LuaSL_LSL_tree.c
@@ -10,9 +10,13 @@ static LSL_Leaf *evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_
10static LSL_Leaf *eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); 10static LSL_Leaf *eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
11static LSL_Leaf *evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); 11static LSL_Leaf *evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
12static void outputFloatToken(FILE *file, LSL_Leaf *content); 12static void outputFloatToken(FILE *file, LSL_Leaf *content);
13static void outputFunctionToken(FILE *file, LSL_Leaf *content);
13static void outputIntegerToken(FILE *file, LSL_Leaf *content); 14static void outputIntegerToken(FILE *file, LSL_Leaf *content);
15static void outputParameterToken(FILE *file, LSL_Leaf *content);
16static void outputParameterListToken(FILE *file, LSL_Leaf *content);
14static void outputParenthesisToken(FILE *file, LSL_Leaf *content); 17static void outputParenthesisToken(FILE *file, LSL_Leaf *content);
15static void outputStatementToken(FILE *file, LSL_Leaf *content); 18static void outputStatementToken(FILE *file, LSL_Leaf *content);
19static void outputVariableToken(FILE *file, LSL_Leaf *content);
16 20
17LSL_Token LSL_Tokens[] = 21LSL_Token LSL_Tokens[] =
18{ 22{
@@ -95,7 +99,7 @@ LSL_Token LSL_Tokens[] =
95 {LSL_TYPE_VECTOR, ST_NONE, "vector", LSL_NONE, NULL, NULL, NULL}, 99 {LSL_TYPE_VECTOR, ST_NONE, "vector", LSL_NONE, NULL, NULL, NULL},
96 100
97 // Then the rest of the syntax tokens. 101 // Then the rest of the syntax tokens.
98 {LSL_IDENTIFIER, ST_NONE, "identifier", LSL_NONE, NULL, NULL, NULL}, 102 {LSL_IDENTIFIER, ST_NONE, "identifier", LSL_NONE, outputVariableToken, NULL, NULL},
99 103
100 {LSL_LABEL, ST_NONE, "@", LSL_NONE, NULL, NULL, NULL}, 104 {LSL_LABEL, ST_NONE, "@", LSL_NONE, NULL, NULL, NULL},
101 105
@@ -112,8 +116,9 @@ LSL_Token LSL_Tokens[] =
112 116
113 {LSL_BLOCK_CLOSE, ST_NONE, "}", LSL_NONE, NULL, NULL, NULL}, 117 {LSL_BLOCK_CLOSE, ST_NONE, "}", LSL_NONE, NULL, NULL, NULL},
114 {LSL_BLOCK_OPEN, ST_NONE, "{", LSL_NONE, NULL, NULL, NULL}, 118 {LSL_BLOCK_OPEN, ST_NONE, "{", LSL_NONE, NULL, NULL, NULL},
115 {LSL_PARAMETER, ST_NONE, "parameter", LSL_NONE, NULL, NULL, NULL}, 119 {LSL_PARAMETER, ST_NONE, "parameter", LSL_NONE, outputParameterToken, NULL, NULL},
116 {LSL_FUNCTION, ST_NONE, "function", LSL_NONE, NULL, NULL, NULL}, 120 {LSL_PARAMETER_LIST, ST_NONE, "plist", LSL_NONE, outputParameterListToken, NULL, NULL},
121 {LSL_FUNCTION, ST_NONE, "function", LSL_NONE, outputFunctionToken, NULL, NULL},
117 {LSL_STATE, ST_NONE, "state", LSL_NONE, NULL, NULL, NULL}, 122 {LSL_STATE, ST_NONE, "state", LSL_NONE, NULL, NULL, NULL},
118 {LSL_SCRIPT, ST_NONE, "", LSL_NONE, NULL, NULL, NULL}, 123 {LSL_SCRIPT, ST_NONE, "", LSL_NONE, NULL, NULL, NULL},
119 124
@@ -179,7 +184,6 @@ LSL_Token **tokens = NULL;
179int lowestToken = 999999; 184int lowestToken = 999999;
180 185
181 186
182/* Not actually used, but it might be some day.
183static LSL_Leaf *newLeaf(LSL_Type type, LSL_Leaf *left, LSL_Leaf *right) 187static LSL_Leaf *newLeaf(LSL_Type type, LSL_Leaf *left, LSL_Leaf *right)
184{ 188{
185 LSL_Leaf *leaf = calloc(1, sizeof(LSL_Leaf)); 189 LSL_Leaf *leaf = calloc(1, sizeof(LSL_Leaf));
@@ -193,7 +197,6 @@ static LSL_Leaf *newLeaf(LSL_Type type, LSL_Leaf *left, LSL_Leaf *right)
193 197
194 return leaf; 198 return leaf;
195} 199}
196*/
197 200
198void burnLeaf(LSL_Leaf *leaf) 201void burnLeaf(LSL_Leaf *leaf)
199{ 202{
@@ -278,14 +281,76 @@ LSL_Leaf *addOperation(LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right)
278 return lval; 281 return lval;
279} 282}
280 283
281LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Leaf *rval) 284LSL_Leaf *addParameter(LuaSL_yyparseParam *param, LSL_Leaf *type, LSL_Leaf *identifier)
285{
286 LSL_Identifier *result = calloc(1, sizeof(LSL_Identifier));
287
288 if ( (identifier) && (result))
289 {
290 result->name = identifier->value.stringValue;
291 identifier->value.variableValue = result;
292 identifier->token = tokens[LSL_PARAMETER - lowestToken];
293 identifier->left = type;
294 if (type)
295 {
296 identifier->basicType = type->basicType;
297 result->value.basicType = type->basicType;
298 }
299 }
300 return identifier;
301}
302
303LSL_Leaf *collectParameters(LuaSL_yyparseParam *param, LSL_Leaf *list, LSL_Leaf *comma, LSL_Leaf *newParam)
304{
305 LSL_Leaf *newList = newLeaf(LSL_PARAMETER_LIST, NULL, NULL);
306
307 if (newList)
308 {
309 newList->left = list;
310 newList->value.listValue = newParam;
311 if ((list) && (list->value.listValue))
312 {
313 list->value.listValue->right = comma;
314 }
315 }
316
317 return newList;
318}
319
320LSL_Leaf *addFunction(LuaSL_yyparseParam *param, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close, LSL_Leaf *block)
321{
322 LSL_Function *func = calloc(1, sizeof(LSL_Function));
323
324 if (func)
325 {
326 if (identifier)
327 {
328 char *temp = identifier->value.stringValue;
329
330 identifier->token = tokens[LSL_FUNCTION - lowestToken];
331 identifier->value.functionValue = func;
332 identifier->value.functionValue->name = temp;
333 identifier->value.functionValue->block = block;
334 func->type = type;
335 if (type)
336 identifier->basicType = type->basicType;
337 else
338 identifier->basicType = OT_nothing;
339 func->params = addParenthesis(open, params, LSL_PARAMETER_LIST, close);
340 }
341 }
342 return identifier;
343}
344
345LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Type type, LSL_Leaf *rval)
282{ 346{
283 LSL_Parenthesis *parens = malloc(sizeof(LSL_Parenthesis)); 347 LSL_Parenthesis *parens = malloc(sizeof(LSL_Parenthesis));
284 348
285 if (parens) 349 if (parens)
286 { 350 {
287 parens->left = lval; 351 parens->left = lval;
288 parens->expression = expr; 352 parens->contents = expr;
353 parens->type = type;
289 parens->right = rval; 354 parens->right = rval;
290 if (lval) 355 if (lval)
291 { 356 {
@@ -297,6 +362,21 @@ LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Leaf *rval)
297 return lval; 362 return lval;
298} 363}
299 364
365LSL_Leaf *addState(LuaSL_yyparseParam *param, char *name, LSL_Leaf *state)
366{
367 LSL_State *result = calloc(1, sizeof(LSL_State));
368
369 if (result)
370 {
371 result->name = name;
372 param->script.scount++;
373 param->script.states = realloc(param->script.states, param->script.scount * sizeof(LSL_State *));
374 param->script.states[param->script.scount - 1] = result;
375 }
376
377 return state;
378}
379
300LSL_Leaf *addStatement(LSL_Leaf *lval, LSL_Type type, LSL_Leaf *expr) 380LSL_Leaf *addStatement(LSL_Leaf *lval, LSL_Type type, LSL_Leaf *expr)
301{ 381{
302 LSL_Statement *stat = malloc(sizeof(LSL_Statement)); 382 LSL_Statement *stat = malloc(sizeof(LSL_Statement));
@@ -319,7 +399,8 @@ LSL_Leaf *addTypecast(LSL_Leaf *lval, LSL_Leaf *type, LSL_Leaf *rval, LSL_Leaf *
319 if (parens) 399 if (parens)
320 { 400 {
321 parens->left = lval; 401 parens->left = lval;
322 parens->expression = expr; 402 parens->contents = expr;
403 parens->type = LSL_TYPECAST_OPEN;
323 parens->right = rval; 404 parens->right = rval;
324 if (lval) 405 if (lval)
325 { 406 {
@@ -336,6 +417,57 @@ LSL_Leaf *addTypecast(LSL_Leaf *lval, LSL_Leaf *type, LSL_Leaf *rval, LSL_Leaf *
336 return lval; 417 return lval;
337} 418}
338 419
420LSL_Leaf *addVariable(LuaSL_yyparseParam *param, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *assignment, LSL_Leaf *expr)
421{
422 LSL_Identifier *result = calloc(1, sizeof(LSL_Identifier));
423
424 if ( (identifier) && (result))
425 {
426 result->name = identifier->value.stringValue;
427 identifier->value.variableValue = result;
428 identifier->left = type;
429 identifier->right = assignment;
430 if (assignment)
431 assignment->right = expr;
432 if (type)
433 {
434 identifier->basicType = type->basicType;
435 result->value.basicType = type->basicType;
436 }
437 if (param->currentBlock)
438 {
439 param->currentBlock->vcount++;
440 param->currentBlock->variables = realloc(param->currentBlock->variables, param->currentBlock->vcount * sizeof(LSL_Identifier *));
441 param->currentBlock->variables[param->currentBlock->vcount - 1] = result;
442 }
443 else
444 {
445 param->script.vcount++;
446 param->script.variables = realloc(param->script.variables, param->script.vcount * sizeof(LSL_Identifier *));
447 param->script.variables[param->script.vcount - 1] = result;
448 }
449 }
450
451 return identifier;
452}
453
454void beginBlock(LuaSL_yyparseParam *param, LSL_Leaf *block)
455{
456 LSL_Block *blok = malloc(sizeof(LSL_Block));
457
458 if (blok)
459 {
460 block->value.blockValue = blok;
461 blok->outerBlock = param->currentBlock;
462 param->currentBlock = blok;
463 }
464}
465
466void endBlock(LuaSL_yyparseParam *param, LSL_Leaf *block)
467{
468 param->currentBlock = param->currentBlock->outerBlock;
469}
470
339static LSL_Leaf *evaluateLeaf(LSL_Leaf *leaf, LSL_Leaf *left, LSL_Leaf *right) 471static LSL_Leaf *evaluateLeaf(LSL_Leaf *leaf, LSL_Leaf *left, LSL_Leaf *right)
340{ 472{
341 LSL_Leaf *result = NULL; 473 LSL_Leaf *result = NULL;
@@ -601,7 +733,10 @@ static LSL_Leaf *eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL
601 LSL_Leaf *result = NULL; 733 LSL_Leaf *result = NULL;
602 734
603 if (content) 735 if (content)
604 result = evaluateLeaf(content->value.parenthesis->expression, left, right); 736 {
737 if (LSL_PARAMETER_LIST != content->value.parenthesis->type)
738 result = evaluateLeaf(content->value.parenthesis->contents, left, right);
739 }
605 return result; 740 return result;
606} 741}
607 742
@@ -653,18 +788,43 @@ static void outputFloatToken(FILE *file, LSL_Leaf *content)
653 fprintf(file, "%g", content->value.floatValue); 788 fprintf(file, "%g", content->value.floatValue);
654} 789}
655 790
791static void outputFunctionToken(FILE *file, LSL_Leaf *content)
792{
793 if (content)
794 {
795 LSL_Function *func = content->value.functionValue;
796
797 outputLeaf(file, func->type);
798 fprintf(file, "%s", func->name);
799 outputLeaf(file, func->params);
800 outputLeaf(file, func->block);
801 }
802}
803
656static void outputIntegerToken(FILE *file, LSL_Leaf *content) 804static void outputIntegerToken(FILE *file, LSL_Leaf *content)
657{ 805{
658 if (content) 806 if (content)
659 fprintf(file, "%d", content->value.integerValue); 807 fprintf(file, "%d", content->value.integerValue);
660} 808}
661 809
810static void outputParameterToken(FILE *file, LSL_Leaf *content)
811{
812 if (content)
813 fprintf(file, "%s", content->value.parameterValue->name);
814}
815
816static void outputParameterListToken(FILE *file, LSL_Leaf *content)
817{
818 if (content)
819 outputLeaf(file, content->value.listValue);
820}
821
662static void outputParenthesisToken(FILE *file, LSL_Leaf *content) 822static void outputParenthesisToken(FILE *file, LSL_Leaf *content)
663{ 823{
664 if (content) 824 if (content)
665 { 825 {
666 fprintf(file, "%s", content->token->token); 826 fprintf(file, "%s", content->token->token);
667 outputLeaf(file, content->value.parenthesis->expression); 827 outputLeaf(file, content->value.parenthesis->contents);
668 outputLeaf(file, content->value.parenthesis->right); 828 outputLeaf(file, content->value.parenthesis->right);
669 } 829 }
670} 830}
@@ -680,6 +840,13 @@ static void outputStatementToken(FILE *file, LSL_Leaf *content)
680 } 840 }
681} 841}
682 842
843static void outputVariableToken(FILE *file, LSL_Leaf *content)
844{
845 if (content)
846 fprintf(file, "%s", content->value.variableValue->name);
847}
848
849
683static void convertLeaf2Lua(FILE *file, LSL_Leaf *leaf) 850static void convertLeaf2Lua(FILE *file, LSL_Leaf *leaf)
684{ 851{
685 if (leaf) 852 if (leaf)
@@ -705,7 +872,6 @@ static void doneParsing(LuaSL_yyparseParam *param)
705 char buffer[PATH_MAX]; 872 char buffer[PATH_MAX];
706 char outName[PATH_MAX]; 873 char outName[PATH_MAX];
707 char luaName[PATH_MAX]; 874 char luaName[PATH_MAX];
708 int count;
709 875
710 outputLeaf(stdout, param->ast); 876 outputLeaf(stdout, param->ast);
711 printf("\n"); 877 printf("\n");