From 3953d90d3b42ac9e156262e2d2dec87bf912ed02 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sat, 7 Jan 2012 22:31:24 +1000 Subject: More expression cleanup. --- LuaSL/src/LuaSL_LSL_tree.c | 325 +++++++++++++++++++++++---------------------- LuaSL/src/LuaSL_LSL_tree.h | 2 +- 2 files changed, 169 insertions(+), 158 deletions(-) (limited to 'LuaSL/src') diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index 83c6779..549e955 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c @@ -3,10 +3,10 @@ #include #include -static void evaluateExpressionToken(LSL_Leaf *content, LSL_Value *result); +static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); +static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); static void outputIntegerToken(LSL_Leaf *content); static void outputOperationToken(LSL_Leaf *content); -static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *result); LSL_Token LSL_Tokens[] = { @@ -15,54 +15,54 @@ LSL_Token LSL_Tokens[] = // Left to right, unless oterwise stated. // According to http://wiki.secondlife.com/wiki/Category:LSL_Operators -// {LSL_COMMA, ",", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, -// {LSL_INCREMENT_PRE, "++", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, -// {LSL_INCREMENT_POST, "++", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, -// {LSL_DECREMENT_PRE, "--", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, -// {LSL_DECREMENT_POST, "--", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, -// {LSL_DOT, ".", LSL_RIGHT2LEFT, outputOperationToken, NULL, NULL}, -// {LSL_ASSIGNMENT_PLAIN, "=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, -// {LSL_ASSIGNMENT_DIVIDE, "/=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, -// {LSL_ASSIGNMENT_MODULO, "%=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, -// {LSL_ASSIGNMENT_MULTIPLY, "*=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, -// {LSL_ASSIGNMENT_SUBTRACT, "-=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, -// {LSL_ASSIGNMENT_ADD, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, -// {LSL_ASSIGNMENT_CONCATENATE, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, - {LSL_PARENTHESIS_OPEN, "(", LSL_INNER2OUTER, outputOperationToken, NULL, NULL}, - {LSL_PARENTHESIS_CLOSE, ")", LSL_INNER2OUTER, outputOperationToken, NULL, NULL}, -// {LSL_BRACKET_OPEN, "[", LSL_INNER2OUTER | LSL_CREATION, outputOperationToken, NULL, NULL}, -// {LSL_BRACKET_CLOSE, "]", LSL_INNER2OUTER | LSL_CREATION, outputOperationToken, NULL, NULL}, -// {LSL_ANGLE_OPEN, "<", LSL_LEFT2RIGHT | LSL_CREATION, outputOperationToken, NULL, NULL}, -// {LSL_ANGLE_CLOSE, ">", LSL_LEFT2RIGHT | LSL_CREATION, outputOperationToken, NULL, NULL}, -// {LSL_TYPECAST_OPEN, "(", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, -// {LSL_TYPECAST_CLOSE, ")", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, - {LSL_BIT_NOT, "~", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, - {LSL_BOOL_NOT, "!", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, - {LSL_NEGATION, "-", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, - {LSL_DIVIDE, "/", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_MODULO, "%", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_MULTIPLY, "*", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, -// {LSL_DOT_PRODUCT, "*", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, -// {LSL_CROSS_PRODUCT, "%", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_SUBTRACT, "-", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_ADD, "+", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, -// {LSL_CONCATENATE, "+", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_LEFT_SHIFT, "<<", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_RIGHT_SHIFT, ">>", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, +// {LSL_COMMA, ",", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_INCREMENT_PRE, "++", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_INCREMENT_POST, "++", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_DECREMENT_PRE, "--", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_DECREMENT_POST, "--", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_DOT, ".", LSL_RIGHT2LEFT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_ASSIGNMENT_PLAIN, "=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_ASSIGNMENT_DIVIDE, "/=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_ASSIGNMENT_MODULO, "%=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_ASSIGNMENT_MULTIPLY, "*=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_ASSIGNMENT_SUBTRACT, "-=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_ASSIGNMENT_ADD, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_ASSIGNMENT_CONCATENATE, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_PARENTHESIS_OPEN, "(", LSL_INNER2OUTER, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_PARENTHESIS_CLOSE, ")", LSL_INNER2OUTER, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_BRACKET_OPEN, "[", LSL_INNER2OUTER | LSL_CREATION, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_BRACKET_CLOSE, "]", LSL_INNER2OUTER | LSL_CREATION, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_ANGLE_OPEN, "<", LSL_LEFT2RIGHT | LSL_CREATION, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_ANGLE_CLOSE, ">", LSL_LEFT2RIGHT | LSL_CREATION, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_TYPECAST_OPEN, "(", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_TYPECAST_CLOSE, ")", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_BIT_NOT, "~", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_BOOL_NOT, "!", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_NEGATION, "-", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_DIVIDE, "/", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_MODULO, "%", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_MULTIPLY, "*", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_DOT_PRODUCT, "*", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_CROSS_PRODUCT, "%", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_SUBTRACT, "-", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_ADD, "+", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, +// {LSL_CONCATENATE, "+", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_LEFT_SHIFT, "<<", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_RIGHT_SHIFT, ">>", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, // QUIRK - Conditionals are executed right to left. Or left to right, depending on who you ask. lol - {LSL_LESS_THAN, "<", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_GREATER_THAN, ">", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_LESS_EQUAL, "<=", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_GREATER_EQUAL, ">=", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_EQUAL, "==", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_NOT_EQUAL, "!=", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_BIT_AND, "&", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_BIT_XOR, "^", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_BIT_OR, "|", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, + {LSL_LESS_THAN, "<", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_GREATER_THAN, ">", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_LESS_EQUAL, "<=", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_GREATER_EQUAL, ">=", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_EQUAL, "==", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_NOT_EQUAL, "!=", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_BIT_AND, "&", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_BIT_XOR, "^", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_BIT_OR, "|", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, // QUIRK - Seems to be some disagreement about BOOL_AND/BOOL_OR precedence. Either they are equal, or OR is higher. // QUIRK - No boolean short circuiting. - {LSL_BOOL_OR, "||", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, - {LSL_BOOL_AND, "&&", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, + {LSL_BOOL_OR, "||", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, + {LSL_BOOL_AND, "&&", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, // Then the rest of the syntax tokens. @@ -79,7 +79,7 @@ LSL_Token LSL_Tokens[] = // {LSL_ROTATION, "rotation", LSL_NONE, NULL, NULL, NULL}, // {LSL_LIST, "list", LSL_NONE, NULL, NULL, NULL}, // {LSL_LABEL, "@", LSL_NONE, NULL, NULL, NULL}, - {LSL_EXPRESSION, "expression", LSL_NONE, NULL, NULL, evaluateExpressionToken}, + {LSL_EXPRESSION, "expression", LSL_NONE, NULL, NULL, NULL}, // {LSL_DO, "do", LSL_NONE, NULL, NULL, NULL}, // {LSL_FOR, "for", LSL_NONE, NULL, NULL, NULL}, // {LSL_IF, "if", LSL_NONE, NULL, NULL, NULL}, @@ -146,8 +146,8 @@ LSL_AST *addOperation(LSL_Operation type, LSL_AST *left, LSL_AST *right) { if (LSL_EXPRESSION == type) { - ast->content.expressionValue = left; - ast->right = NULL; + ast->content.expressionValue = right; + ast->left = NULL; } else ast->content.operationValue = type; @@ -156,131 +156,133 @@ LSL_AST *addOperation(LSL_Operation type, LSL_AST *left, LSL_AST *right) return ast; } -static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *result) +static void evaluateAST(LSL_AST *ast, LSL_Value *left, LSL_Value *right) +{ + if (ast) + { + LSL_Value lresult; + LSL_Value rresult; + + memcpy(&lresult, left, sizeof(LSL_Value)); + memcpy(&rresult, right, sizeof(LSL_Value)); + + if (LSL_RIGHT2LEFT & ast->token->flags) + { + memcpy(&rresult, left, sizeof(LSL_Value)); + evaluateAST(ast->right, &rresult, right); + if (!(LSL_UNARY & ast->token->flags)) + { + evaluateAST(ast->left, &lresult, right); + } + } + else // Assume left to right. + { + evaluateAST(ast->left, &lresult, right); + if (!(LSL_UNARY & ast->token->flags)) + { + memcpy(&rresult, left, sizeof(LSL_Value)); + evaluateAST(ast->right, &rresult, right); + } + } + + if (ast->token->evaluate) + { + ast->token->evaluate(&(ast->content), &lresult, &rresult); + memcpy(left, &lresult, sizeof(LSL_Value)); + } + else + { + printf(" eval <%s %d %d %d %d> ", ast->token->token, left->content.integerValue, right->content.integerValue, lresult.content.integerValue, rresult.content.integerValue); + memcpy(left, &rresult, sizeof(LSL_Value)); + } + } +} + +static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) { if (content) { #ifdef LUASL_DEBUG - printf(" %d ", content->integerValue); + printf(" <%d> ", content->integerValue); #endif - result->content.integerValue = content->integerValue; - result->type = LSL_INTEGER; + left->content.integerValue = content->integerValue; + left->type = LSL_INTEGER; } } -static void evaluateExpression(LSL_AST *ast, LSL_Value *result) +static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) { - LSL_Value left, right; - - if ((NULL == ast) || (NULL == result)) - return; - - if (LSL_INTEGER == ast->token->type) - { - evaluateIntegerToken(&(ast->content), result); - return; - } - else if (LSL_LEFT2RIGHT & ast->token->flags) - { - evaluateExpression(ast->left, &left); - if (!(LSL_UNARY & ast->token->flags)) - evaluateExpression(ast->right, &right); - } - else if (LSL_RIGHT2LEFT & ast->token->flags) - { - evaluateExpression(ast->right, &right); - if (!(LSL_UNARY & ast->token->flags)) - evaluateExpression(ast->left, &left); - } - else + if ((content) && (content->operationValue)) { - } - #ifdef LUASL_DEBUG - printf(" %s ", ast->token->token); + printf(" [%s] ", tokens[content->operationValue - lowestToken]->token); #endif - switch (ast->content.operationValue) - { + switch (content->operationValue) + { #ifdef LUASL_USE_ENUM - case LSL_COMMA : - case LSL_INCREMENT_PRE : - case LSL_INCREMENT_POST : - case LSL_DECREMENT_PRE : - case LSL_DECREMENT_POST : - case LSL_DOT : - case LSL_ASSIGNMENT_PLAIN : - case LSL_ASSIGNMENT_DIVIDE : - case LSL_ASSIGNMENT_MODULO : - case LSL_ASSIGNMENT_MULTIPLY : - case LSL_ASSIGNMENT_SUBTRACT : - case LSL_ASSIGNMENT_ADD : - case LSL_ASSIGNMENT_CONCATENATE : - case LSL_PARENTHESIS_OPEN : - case LSL_PARENTHESIS_CLOSE : - case LSL_BRACKET_OPEN : - case LSL_BRACKET_CLOSE : - case LSL_ANGLE_OPEN : - case LSL_ANGLE_CLOSE : - case LSL_TYPECAST : - break; + case LSL_COMMA : + case LSL_INCREMENT_PRE : + case LSL_INCREMENT_POST : + case LSL_DECREMENT_PRE : + case LSL_DECREMENT_POST : + case LSL_DOT : + case LSL_ASSIGNMENT_PLAIN : + case LSL_ASSIGNMENT_DIVIDE : + case LSL_ASSIGNMENT_MODULO : + case LSL_ASSIGNMENT_MULTIPLY : + case LSL_ASSIGNMENT_SUBTRACT : + case LSL_ASSIGNMENT_ADD : + case LSL_ASSIGNMENT_CONCATENATE : + case LSL_PARENTHESIS_OPEN : + case LSL_PARENTHESIS_CLOSE : + case LSL_BRACKET_OPEN : + case LSL_BRACKET_CLOSE : + case LSL_ANGLE_OPEN : + case LSL_ANGLE_CLOSE : + case LSL_TYPECAST : + break; #endif - case LSL_BIT_NOT : result->content.integerValue = ~ right.content.integerValue; break; - case LSL_BOOL_NOT : result->content.integerValue = ! right.content.integerValue; break; - case LSL_NEGATION : result->content.integerValue = 0 - right.content.integerValue; break; - case LSL_DIVIDE : result->content.integerValue = left.content.integerValue / right.content.integerValue; break; - case LSL_MODULO : result->content.integerValue = left.content.integerValue % right.content.integerValue; break; - case LSL_MULTIPLY : result->content.integerValue = left.content.integerValue * right.content.integerValue; break; + case LSL_BIT_NOT : left->content.integerValue = ~ right->content.integerValue; break; + case LSL_BOOL_NOT : left->content.integerValue = ! right->content.integerValue; break; + case LSL_NEGATION : left->content.integerValue = 0 - right->content.integerValue; break; + case LSL_DIVIDE : left->content.integerValue = left->content.integerValue / right->content.integerValue; break; + case LSL_MODULO : left->content.integerValue = left->content.integerValue % right->content.integerValue; break; + case LSL_MULTIPLY : left->content.integerValue = left->content.integerValue * right->content.integerValue; break; #ifdef LUASL_USE_ENUM - case LSL_DOT_PRODUCT : break; - case LSL_CROSS_PRODUCT : break; + case LSL_DOT_PRODUCT : break; + case LSL_CROSS_PRODUCT : break; #endif - case LSL_SUBTRACT : result->content.integerValue = left.content.integerValue - right.content.integerValue; break; - case LSL_ADD : result->content.integerValue = left.content.integerValue + right.content.integerValue; break; + case LSL_SUBTRACT : left->content.integerValue = left->content.integerValue - right->content.integerValue; break; + case LSL_ADD : left->content.integerValue = left->content.integerValue + right->content.integerValue; break; #ifdef LUASL_USE_ENUM - case LSL_CONCATENATE : break; + case LSL_CONCATENATE : break; +#endif + case LSL_LEFT_SHIFT : left->content.integerValue = left->content.integerValue << right->content.integerValue; break; + case LSL_RIGHT_SHIFT : left->content.integerValue = left->content.integerValue >> right->content.integerValue; break; + case LSL_LESS_THAN : left->content.integerValue = left->content.integerValue < right->content.integerValue; break; + case LSL_GREATER_THAN : left->content.integerValue = left->content.integerValue > right->content.integerValue; break; + case LSL_LESS_EQUAL : left->content.integerValue = left->content.integerValue <= right->content.integerValue; break; + case LSL_GREATER_EQUAL : left->content.integerValue = left->content.integerValue >= right->content.integerValue; break; + case LSL_EQUAL : left->content.integerValue = left->content.integerValue == right->content.integerValue; break; + case LSL_NOT_EQUAL : left->content.integerValue = left->content.integerValue != right->content.integerValue; break; + case LSL_BIT_AND : left->content.integerValue = left->content.integerValue & right->content.integerValue; break; + case LSL_BIT_XOR : left->content.integerValue = left->content.integerValue ^ right->content.integerValue; break; + case LSL_BIT_OR : left->content.integerValue = left->content.integerValue | right->content.integerValue; break; + case LSL_BOOL_OR : left->content.integerValue = left->content.integerValue || right->content.integerValue; break; + case LSL_BOOL_AND : left->content.integerValue = left->content.integerValue && right->content.integerValue; break; + } +#ifdef LUASL_DEBUG + printf(" (=%d) ", left->content.integerValue); #endif - case LSL_LEFT_SHIFT : result->content.integerValue = left.content.integerValue << right.content.integerValue; break; - case LSL_RIGHT_SHIFT : result->content.integerValue = left.content.integerValue >> right.content.integerValue; break; - case LSL_LESS_THAN : result->content.integerValue = left.content.integerValue < right.content.integerValue; break; - case LSL_GREATER_THAN : result->content.integerValue = left.content.integerValue > right.content.integerValue; break; - case LSL_LESS_EQUAL : result->content.integerValue = left.content.integerValue <= right.content.integerValue; break; - case LSL_GREATER_EQUAL : result->content.integerValue = left.content.integerValue >= right.content.integerValue; break; - case LSL_EQUAL : result->content.integerValue = left.content.integerValue == right.content.integerValue; break; - case LSL_NOT_EQUAL : result->content.integerValue = left.content.integerValue != right.content.integerValue; break; - case LSL_BIT_AND : result->content.integerValue = left.content.integerValue & right.content.integerValue; break; - case LSL_BIT_XOR : result->content.integerValue = left.content.integerValue ^ right.content.integerValue; break; - case LSL_BIT_OR : result->content.integerValue = left.content.integerValue | right.content.integerValue; break; - case LSL_BOOL_OR : result->content.integerValue = left.content.integerValue || right.content.integerValue; break; - case LSL_BOOL_AND : result->content.integerValue = left.content.integerValue && right.content.integerValue; break; - } - - return; -} - -static void evaluateExpressionToken(LSL_Leaf *content, LSL_Value *result) -{ - if (content) - evaluateExpression(content->expressionValue, result); -} - -static void evaluateAST(LSL_AST *ast, LSL_Value *result) -{ - if (ast) - { - evaluateAST(ast->left, result); - if ((ast->token) && (ast->token->evaluate)) - ast->token->evaluate(&(ast->content), result); - evaluateAST(ast->right, result); } } - static void outputAST(LSL_AST *ast) { if (ast) { outputAST(ast->left); - if ((ast->token) && (ast->token->output)) + if (ast->token->output) ast->token->output(&(ast->content)); else printf(" <%s> ", ast->token->token); @@ -297,13 +299,20 @@ static void outputIntegerToken(LSL_Leaf *content) static void outputOperationToken(LSL_Leaf *content) { if (content) - printf(" [%s] ", tokens[content->operationValue - lowestToken]->token); + printf(" %s ", tokens[content->operationValue - lowestToken]->token); } static void convertAST2Lua(LSL_AST *ast) { - if ((ast) && (ast->token) && (ast->token->convert)) - ast->token->convert(&(ast->content)); + if (ast) + { + convertAST2Lua(ast->left); + if (ast->token->convert) + ast->token->convert(&(ast->content)); + else if (ast->token->output) + ast->token->output(&(ast->content)); + convertAST2Lua(ast->right); + } } int yyerror(const char *msg) @@ -366,16 +375,18 @@ int main(void) // Run the parser on a test. if ((ast = newTree(test))) { - LSL_Value result; + LSL_Value left, right; - result.content.integerValue = 0; - result.type = LSL_INTEGER; - evaluateAST(ast, &result); + left.content.integerValue = 0; + left.type = LSL_INTEGER; + right.content.integerValue = 0; + right.type = LSL_INTEGER; + evaluateAST(ast, &left, &right); #ifdef LUASL_DEBUG printf("\n"); #endif - printf("Result of '%s' is %d\n", test, result.content.integerValue); + printf("Result of '%s' is %d %d\n", test, left.content.integerValue, right.content.integerValue); outputAST(ast); printf("\n"); convertAST2Lua(ast); diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 4764ce8..750d3f7 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -190,7 +190,7 @@ typedef struct typedef void (*convertToken2Lua) (LSL_Leaf *content); typedef void (*outputToken) (LSL_Leaf *content); -typedef void (*evaluateToken) (LSL_Leaf *content, LSL_Value *result); +typedef void (*evaluateToken) (LSL_Leaf *content, LSL_Value *left, LSL_Value *right); typedef struct { -- cgit v1.1