From eb8dda3811a849ef633faa225cf1427bf93a4494 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Tue, 10 Jan 2012 03:30:17 +1000 Subject: Get rid of the LSL_AST structure, it's all in LSL_Leaf now. --- LuaSL/src/LuaSL_LSL_tree.c | 184 +++++++++++++++++++++------------------------ LuaSL/src/LuaSL_LSL_tree.h | 29 +++---- LuaSL/src/LuaSL_lexer.l | 1 - 3 files changed, 95 insertions(+), 119 deletions(-) (limited to 'LuaSL') diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index 016dbc9..cc6ed3f 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c @@ -128,118 +128,102 @@ LSL_Token **tokens = NULL; int lowestToken = 999999; -static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right) +static LSL_Leaf *newLeaf(LSL_Type type, LSL_Leaf *left, LSL_Leaf *right) { - LSL_AST *ast = malloc(sizeof(LSL_AST)); + LSL_Leaf *leaf = malloc(sizeof(LSL_Leaf)); - if (ast == NULL) return NULL; - - ast->left = left; - ast->right = right; - ast->content.token = tokens[type - lowestToken]; - - return ast; -} - -static void burnAST(LSL_AST *ast) -{ - if (ast == NULL) return; + if (leaf) + { + leaf->left = left; + leaf->right = right; + leaf->token = tokens[type - lowestToken]; + } - burnAST(ast->left); - burnAST(ast->right); - free(ast->content.ignorableText); - free(ast); + return leaf; } -static LSL_AST *checkAndAddIgnorable(LSL_AST *root) +static void burnLeaf(LSL_Leaf *leaf) { - if (root) + if (leaf) { - if (root->content.ignorableText) - { - LSL_AST *ast = newAST(LSL_SPACE, NULL, root); - - if (ast) - { - ast->content.value.spaceValue = root->content.ignorableText; - return ast; - } - } + burnLeaf(leaf->left); + burnLeaf(leaf->right); + // TODO - Should free up the value to. + free(leaf->ignorableText); + free(leaf); } - return root; } -LSL_AST *addInteger(LSL_Leaf *lval, int value) +LSL_Leaf *addInteger(LSL_Leaf *lval, int value) { - LSL_AST *ast = newAST(LSL_INTEGER, NULL, NULL); + LSL_Leaf *leaf = newLeaf(LSL_INTEGER, NULL, NULL); - if (ast) + if (leaf) { - ast->content.value.integerValue = value; - ast->content.ignorableText = lval->ignorableText; + leaf->value.integerValue = value; + leaf->ignorableText = lval->ignorableText; } - return checkAndAddIgnorable(ast); + return leaf; } -LSL_AST *addOperation(LSL_Leaf *lval, LSL_Type type, LSL_AST *left, LSL_AST *right) +LSL_Leaf *addOperation(LSL_Leaf *lval, LSL_Type type, LSL_Leaf *left, LSL_Leaf *right) { - LSL_AST *ast = newAST(type, left, right); + LSL_Leaf *leaf = newLeaf(type, left, right); - if (ast) + if (leaf) { if (LSL_EXPRESSION == type) { - ast->content.value.expressionValue = right; - ast->left = NULL; + leaf->value.expressionValue = right; + leaf->left = NULL; } else { - ast->content.value.operationValue = type; - ast->content.ignorableText = lval->ignorableText; + leaf->value.operationValue = type; + leaf->ignorableText = lval->ignorableText; } } - return checkAndAddIgnorable(ast); + return leaf; } -LSL_AST *addParenthesis(LSL_Leaf *lval, LSL_AST *expr) +LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr) { - LSL_AST *ast = newAST(LSL_PARENTHESIS_OPEN, NULL, expr); + LSL_Leaf *leaf = newLeaf(LSL_PARENTHESIS_OPEN, NULL, expr); - if (ast) + if (leaf) { - ast = newAST(LSL_PARENTHESIS_CLOSE, ast, NULL); - ast->content.ignorableText = lval->ignorableText; + leaf->ignorableText = lval->ignorableText; + leaf = newLeaf(LSL_PARENTHESIS_CLOSE, leaf, NULL); } - return checkAndAddIgnorable(ast); + return leaf; } -LSL_Statement *createStatement(LSL_Type type, LSL_AST *expr) +LSL_Statement *createStatement(LSL_Type type, LSL_Leaf *expr) { LSL_Statement *stat = malloc(sizeof(LSL_Statement)); - if (stat == NULL) return NULL; - - stat->expressions = expr; + if (stat) + stat->expressions = expr; return stat; } -LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root) +LSL_Leaf *addStatement(LSL_Statement *statement, LSL_Leaf *root) { - LSL_AST *ast = newAST(LSL_STATEMENT, root, NULL); + LSL_Leaf *leaf = newLeaf(LSL_STATEMENT, root, NULL); - if (ast) - ast->content.value.statementValue = statement; + if (leaf) + leaf->value.statementValue = statement; - return checkAndAddIgnorable(ast); + return leaf; } -static void evaluateAST(LSL_AST *ast, LSL_Leaf *left, LSL_Leaf *right) +static void evaluateLeaf(LSL_Leaf *leaf, LSL_Leaf *left, LSL_Leaf *right) { - if (ast) + if (leaf) { LSL_Leaf lresult; LSL_Leaf rresult; @@ -247,34 +231,34 @@ static void evaluateAST(LSL_AST *ast, LSL_Leaf *left, LSL_Leaf *right) memcpy(&lresult, left, sizeof(LSL_Leaf)); memcpy(&rresult, right, sizeof(LSL_Leaf)); - if (LSL_RIGHT2LEFT & ast->content.token->flags) + if (LSL_RIGHT2LEFT & leaf->token->flags) { memcpy(&rresult, left, sizeof(LSL_Leaf)); - evaluateAST(ast->right, &rresult, right); - if (!(LSL_UNARY & ast->content.token->flags)) + evaluateLeaf(leaf->right, &rresult, right); + if (!(LSL_UNARY & leaf->token->flags)) { - evaluateAST(ast->left, &lresult, right); + evaluateLeaf(leaf->left, &lresult, right); } } else // Assume left to right. { - evaluateAST(ast->left, &lresult, right); - if (!(LSL_UNARY & ast->content.token->flags)) + evaluateLeaf(leaf->left, &lresult, right); + if (!(LSL_UNARY & leaf->token->flags)) { memcpy(&rresult, left, sizeof(LSL_Leaf)); - evaluateAST(ast->right, &rresult, right); + evaluateLeaf(leaf->right, &rresult, right); } } - if (ast->content.token->evaluate) + if (leaf->token->evaluate) { - ast->content.token->evaluate(&(ast->content), &lresult, &rresult); + leaf->token->evaluate(leaf, &lresult, &rresult); memcpy(left, &lresult, sizeof(LSL_Leaf)); } else { #ifdef LUASL_DEBUG - printf(" eval <%s %d %d %d %d> ", ast->content.token->token, left->value.integerValue, right->value.integerValue, lresult.value.integerValue, rresult.value.integerValue); + printf(" eval <%s %d %d %d %d> ", leaf->token->token, left->value.integerValue, right->value.integerValue, lresult.value.integerValue, rresult.value.integerValue); #endif memcpy(left, &rresult, sizeof(LSL_Leaf)); } @@ -289,7 +273,7 @@ static void evaluateIntegerToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *ri printf(" <%d> ", content->value.integerValue); #endif left->value.integerValue = content->value.integerValue; - left->type = LSL_INTEGER; + left->token = tokens[LSL_INTEGER - lowestToken]; } } @@ -364,19 +348,21 @@ static void evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf * static void evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { if (content) - evaluateAST(content->value.statementValue->expressions, left, right); + evaluateLeaf(content->value.statementValue->expressions, left, right); } -static void outputAST(LSL_AST *ast) +static void outputLeaf(LSL_Leaf *leaf) { - if (ast) + if (leaf) { - outputAST(ast->left); - if (ast->content.token->output) - ast->content.token->output(&(ast->content)); + outputLeaf(leaf->left); + if (leaf->ignorableText) + printf("%s", leaf->ignorableText); + if (leaf->token->output) + leaf->token->output(leaf); else - printf("%s", ast->content.token->token); - outputAST(ast->right); + printf("%s", leaf->token->token); + outputLeaf(leaf->right); } } @@ -395,7 +381,7 @@ static void outputOperationToken(LSL_Leaf *content) static void outputStatementToken(LSL_Leaf *content) { if (content) - outputAST(content->value.statementValue->expressions); + outputLeaf(content->value.statementValue->expressions); printf(";"); } @@ -405,18 +391,18 @@ static void outputSpaceToken(LSL_Leaf *content) printf("%s", content->value.spaceValue); } -static void convertAST2Lua(LSL_AST *ast) +static void convertLeaf2Lua(LSL_Leaf *leaf) { - if (ast) + if (leaf) { - convertAST2Lua(ast->left); - if (ast->content.token->convert) - ast->content.token->convert(&(ast->content)); - else if (ast->content.token->output) - ast->content.token->output(&(ast->content)); + convertLeaf2Lua(leaf->left); + if (leaf->token->convert) + leaf->token->convert(leaf); + else if (leaf->token->output) + leaf->token->output(leaf); else - printf("%s", ast->content.token->token); - convertAST2Lua(ast->right); + printf("%s", leaf->token->token); + convertLeaf2Lua(leaf->right); } } @@ -435,7 +421,6 @@ int main(int argc, char **argv) if (tokens) { char buffer[4096]; - LSL_AST *ast; LuaSL_yyparseParam param; int count; FILE *file; @@ -533,28 +518,27 @@ int main(int argc, char **argv) if (!yyparse(¶m)) { yylex_destroy(param.scanner); - ast = param.ast; - if (ast) + if (param.ast) { LSL_Leaf left, right; left.value.integerValue = 0; - left.type = LSL_INTEGER; + left.token = tokens[LSL_INTEGER - lowestToken]; right.value.integerValue = 0; - right.type = LSL_INTEGER; - evaluateAST(ast, &left, &right); + right.token = tokens[LSL_INTEGER - lowestToken]; + evaluateLeaf(param.ast, &left, &right); #ifdef LUASL_DEBUG printf("\n"); #endif printf("Result of -\n"); - outputAST(ast); + outputLeaf(param.ast); printf("\n"); printf("is %d %d. And converted to Lua it is -\n", left.value.integerValue, right.value.integerValue); - convertAST2Lua(ast); + convertLeaf2Lua(param.ast); printf("\n"); - burnAST(ast); + burnLeaf(param.ast); } } } diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 5d39b3b..4676171 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -28,7 +28,6 @@ typedef struct _LSL_Block LSL_Block; typedef struct _LSL_Function LSL_Function; typedef struct _LSL_State LSL_State; typedef struct _LSL_Script LSL_Script; -typedef struct _LSL_AST LSL_AST; extern LSL_Token **tokens; extern int lowestToken; @@ -70,13 +69,15 @@ struct _LSL_Token struct _LSL_Leaf { + LSL_Leaf *left; + LSL_Leaf *right; union { char *commentValue; char *spaceValue; LSL_Type operationValue; - LSL_AST *expressionValue; + LSL_Leaf *expressionValue; float floatValue; int integerValue; @@ -110,7 +111,6 @@ struct _LSL_Leaf } value; char *ignorableText; LSL_Token *token; - LSL_Type type; int line, column; }; @@ -122,7 +122,7 @@ struct _LSL_Identifier // For variables and function parameters. struct _LSL_Statement { - LSL_AST *expressions; /// For things like a for statement, might hold three expressions. + LSL_Leaf *expressions; /// For things like a for statement, might hold three expressions. }; struct _LSL_Block @@ -152,13 +152,6 @@ struct _LSL_Script LSL_Identifier *variables; }; -struct _LSL_AST -{ - LSL_AST *left; - LSL_AST *right; - LSL_Leaf content; -}; - // define the type for flex and bison #define YYSTYPE LSL_Leaf @@ -172,7 +165,7 @@ struct _LSL_AST typedef struct { yyscan_t scanner; - LSL_AST *ast; + LSL_Leaf *ast; } LuaSL_yyparseParam; // the parameter name (of the reentrant 'yyparse' function) @@ -183,12 +176,12 @@ typedef struct #define YYLEX_PARAM ((LuaSL_yyparseParam*)data)->scanner -LSL_AST *addExpression(LSL_AST *exp); -LSL_AST *addInteger(LSL_Leaf *lval, int value); -LSL_AST *addOperation(LSL_Leaf *lval, LSL_Type type, LSL_AST *left, LSL_AST *right); -LSL_AST *addParenthesis(LSL_Leaf *lval, LSL_AST *expr); -LSL_Statement *createStatement(LSL_Type type, LSL_AST *root); -LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root); +LSL_Leaf *addExpression(LSL_Leaf *exp); +LSL_Leaf *addInteger(LSL_Leaf *lval, int value); +LSL_Leaf *addOperation(LSL_Leaf *lval, LSL_Type type, LSL_Leaf *left, LSL_Leaf *right); +LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr); +LSL_Statement *createStatement(LSL_Type type, LSL_Leaf *root); +LSL_Leaf *addStatement(LSL_Statement *statement, LSL_Leaf *root); int yyerror(const char *msg); int yyparse(void *param); diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index 30e0b6b..aaba74a 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l @@ -140,7 +140,6 @@ int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type) else column++; - lval->type = type; lval->token = tokens[type - lowestToken]; lval->line = line; lval->column = column; -- cgit v1.1