#include "LuaSL_LSL_tree.h" #include #include static LSL_Leaf *evaluateFloatToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); static LSL_Leaf *evaluateIntegerToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); static LSL_Leaf *evaluateNoToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); static LSL_Leaf *evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); static LSL_Leaf *eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); static LSL_Leaf *evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); static void outputFloatToken(FILE *file, LSL_Leaf *content); static void outputIntegerToken(FILE *file, LSL_Leaf *content); static void outputParenthesisToken(FILE *file, LSL_Leaf *content); static void outputStatementToken(FILE *file, LSL_Leaf *content); LSL_Token LSL_Tokens[] = { // Various forms of "space". {LSL_COMMENT, ST_NONE, "/*", LSL_NONE, NULL, NULL, NULL}, {LSL_COMMENT_LINE, ST_NONE, "//", LSL_NONE, NULL, NULL, NULL}, {LSL_SPACE, ST_NONE, " ", LSL_NONE, NULL, NULL, NULL}, // Operators, in order of precedence, low to high // Left to right, unless oterwise stated. // According to http://wiki.secondlife.com/wiki/Category:LSL_Operators {LSL_BOOL_AND, ST_BOOLEAN, "&&", LSL_RIGHT2LEFT, NULL, 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. // QUIRK - Booleans and conditionals are executed right to left. Or maybe not, depending on who you believe. {LSL_BOOL_OR, ST_BOOLEAN, "||", LSL_RIGHT2LEFT, NULL, NULL, evaluateOperationToken}, {LSL_BIT_OR, ST_BITWISE, "|", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, {LSL_BIT_XOR, ST_BITWISE, "^", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, {LSL_BIT_AND, ST_BITWISE, "&", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, // QUIRK - Booleans and conditionals are executed right to left. Or maybe not, depending on who you believe. {LSL_NOT_EQUAL, ST_EQUALITY, "!=", LSL_RIGHT2LEFT, NULL, NULL, evaluateOperationToken}, {LSL_EQUAL, ST_EQUALITY, "==", LSL_RIGHT2LEFT, NULL, NULL, evaluateOperationToken}, {LSL_GREATER_EQUAL, ST_COMPARISON, ">=", LSL_RIGHT2LEFT, NULL, NULL, evaluateOperationToken}, {LSL_LESS_EQUAL, ST_COMPARISON, "<=", LSL_RIGHT2LEFT, NULL, NULL, evaluateOperationToken}, {LSL_GREATER_THAN, ST_COMPARISON, ">", LSL_RIGHT2LEFT, NULL, NULL, evaluateOperationToken}, {LSL_LESS_THAN, ST_COMPARISON, "<", LSL_RIGHT2LEFT, NULL, NULL, evaluateOperationToken}, {LSL_RIGHT_SHIFT, ST_BITWISE, ">>", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, {LSL_LEFT_SHIFT, ST_BITWISE, "<<", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, // {LSL_CONCATENATE, ST_ADD, "+", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, {LSL_ADD, ST_ADD, "+", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, {LSL_SUBTRACT, ST_SUBTRACT, "-", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, // {LSL_CROSS_PRODUCT, ST_NONE, "%", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, // {LSL_DOT_PRODUCT, ST_NONE, "*", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, {LSL_MULTIPLY, ST_MULTIPLY, "*", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, {LSL_MODULO, ST_MODULO, "%", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, {LSL_DIVIDE, ST_MULTIPLY, "/", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, {LSL_NEGATION, ST_NEGATE, "-", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, evaluateOperationToken}, {LSL_BOOL_NOT, ST_BOOL_NOT, "!", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, evaluateOperationToken}, {LSL_BIT_NOT, ST_BIT_NOT, "~", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, evaluateOperationToken}, // {LSL_TYPECAST_CLOSE, ST_NONE, ")", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, evaluateOperationToken}, // {LSL_TYPECAST_OPEN, ST_NONE, "(", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, evaluateOperationToken}, {LSL_ANGLE_CLOSE, ST_NONE, ">", LSL_LEFT2RIGHT | LSL_CREATION, NULL, NULL, evaluateOperationToken}, {LSL_ANGLE_OPEN, ST_NONE, "<", LSL_LEFT2RIGHT | LSL_CREATION, NULL, NULL, evaluateOperationToken}, {LSL_BRACKET_CLOSE, ST_NONE, "]", LSL_INNER2OUTER | LSL_CREATION, NULL, NULL, evaluateOperationToken}, {LSL_BRACKET_OPEN, ST_NONE, "[", LSL_INNER2OUTER | LSL_CREATION, NULL, NULL, evaluateOperationToken}, {LSL_PARENTHESIS_CLOSE, ST_NONE, ")", LSL_INNER2OUTER, NULL, NULL, evaluateNoToken}, {LSL_PARENTHESIS_OPEN, ST_NONE, "(", LSL_INNER2OUTER, outputParenthesisToken, NULL, eveluateParenthesisToken}, // {LSL_ASSIGNMENT_CONCATENATE, ST_CONCATENATION "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, evaluateOperationToken}, {LSL_ASSIGNMENT_ADD, ST_CONCATENATION, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, evaluateOperationToken}, {LSL_ASSIGNMENT_SUBTRACT, ST_ASSIGNMENT, "-=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, evaluateOperationToken}, {LSL_ASSIGNMENT_MULTIPLY, ST_ASSIGNMENT, "*=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, evaluateOperationToken}, {LSL_ASSIGNMENT_MODULO, ST_MODULO, "%=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, evaluateOperationToken}, {LSL_ASSIGNMENT_DIVIDE, ST_ASSIGNMENT, "/=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, evaluateOperationToken}, {LSL_ASSIGNMENT_PLAIN, ST_CONCATENATION, "=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, evaluateOperationToken}, {LSL_DOT, ST_NONE, ".", LSL_RIGHT2LEFT, NULL, NULL, evaluateOperationToken}, // {LSL_DECREMENT_POST, ST_NONE, "--", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, evaluateOperationToken}, {LSL_DECREMENT_PRE, ST_NONE, "--", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, evaluateOperationToken}, // {LSL_INCREMENT_POST, ST_NONE, "++", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, evaluateOperationToken}, {LSL_INCREMENT_PRE, ST_NONE, "++", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, evaluateOperationToken}, {LSL_COMMA, ST_NONE, ",", LSL_LEFT2RIGHT, NULL, NULL, evaluateOperationToken}, {LSL_EXPRESSION, ST_NONE, "expression", LSL_NONE , NULL, NULL, NULL}, // Types. {LSL_FLOAT, ST_NONE, "float", LSL_NONE, outputFloatToken, NULL, evaluateFloatToken}, {LSL_INTEGER, ST_NONE, "integer", LSL_NONE, outputIntegerToken, NULL, evaluateIntegerToken}, // {LSL_KEY, ST_NONE, "key", LSL_NONE, NULL, NULL, NULL}, // {LSL_LIST, ST_NONE, "list", LSL_NONE, NULL, NULL, NULL}, // {LSL_ROTATION, ST_NONE, "rotation", LSL_NONE, NULL, NULL, NULL}, // {LSL_STRING, ST_NONE, "string", LSL_NONE, NULL, NULL, NULL}, // {LSL_VECTOR, ST_NONE, "vector", LSL_NONE, NULL, NULL, NULL}, // Types names. {LSL_TYPE_FLOAT, ST_NONE, "float", LSL_NONE, NULL, NULL, NULL}, {LSL_TYPE_INTEGER, ST_NONE, "integer", LSL_NONE, NULL, NULL, NULL}, {LSL_TYPE_KEY, ST_NONE, "key", LSL_NONE, NULL, NULL, NULL}, {LSL_TYPE_LIST, ST_NONE, "list", LSL_NONE, NULL, NULL, NULL}, {LSL_TYPE_ROTATION, ST_NONE, "rotation", LSL_NONE, NULL, NULL, NULL}, {LSL_TYPE_STRING, ST_NONE, "string", LSL_NONE, NULL, NULL, NULL}, {LSL_TYPE_VECTOR, ST_NONE, "vector", LSL_NONE, NULL, NULL, NULL}, // Then the rest of the syntax tokens. {LSL_IDENTIFIER, ST_NONE, "identifier", LSL_NONE, NULL, NULL, NULL}, {LSL_LABEL, ST_NONE, "@", LSL_NONE, NULL, NULL, NULL}, {LSL_DO, ST_NONE, "do", LSL_NONE, NULL, NULL, NULL}, {LSL_FOR, ST_NONE, "for", LSL_NONE, NULL, NULL, NULL}, // {LSL_ELSE_IF, ST_NONE, "else if", LSL_NONE, NULL, NULL, NULL}, {LSL_ELSE, ST_NONE, "else", LSL_NONE, NULL, NULL, NULL}, {LSL_IF, ST_NONE, "if", LSL_NONE, NULL, NULL, NULL}, {LSL_JUMP, ST_NONE, "jump", LSL_NONE, NULL, NULL, NULL}, {LSL_RETURN, ST_NONE, "return", LSL_NONE, NULL, NULL, NULL}, {LSL_STATE_CHANGE, ST_NONE, "state", LSL_NONE, NULL, NULL, NULL}, {LSL_WHILE, ST_NONE, "while", LSL_NONE, NULL, NULL, NULL}, {LSL_STATEMENT, ST_NONE, ";", LSL_NOIGNORE, outputStatementToken, NULL, evaluateStatementToken}, {LSL_BLOCK_CLOSE, ST_NONE, "}", LSL_NONE, NULL, NULL, NULL}, {LSL_BLOCK_OPEN, ST_NONE, "{", LSL_NONE, NULL, NULL, NULL}, // {LSL_PARAMETER, ST_NONE, "parameter", LSL_NONE, NULL, NULL, NULL}, // {LSL_FUNCTION, ST_NONE, "function", LSL_NONE, NULL, NULL, NULL}, // {LSL_STATE, ST_NONE, "state", LSL_NONE, NULL, NULL, NULL}, {LSL_SCRIPT, ST_NONE, "", LSL_NONE, NULL, NULL, NULL}, {LSL_UNKNOWN, ST_NONE, "unknown", LSL_NONE, NULL, NULL, NULL}, // A sentinal. {999999, ST_NONE, NULL, LSL_NONE, NULL, NULL, NULL} }; allowedTypes allowed[] = { {OT_nothing, (ST_NONE)}, {OT_bool, (ST_BOOL_NOT)}, {OT_integer, (ST_BIT_NOT | ST_NEGATE)}, {OT_float, (ST_NONE)}, {OT_key, (ST_NONE)}, {OT_list, (ST_NONE)}, {OT_rotation, (ST_NONE)}, {OT_string, (ST_NONE)}, {OT_vector, (ST_NONE)}, {OT_other, (ST_NONE)}, {OT_bool, (ST_BOOLEAN | ST_EQUALITY)}, {OT_integer, (ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_COMPARISON | ST_CONCATENATION | ST_ASSIGNMENT | ST_MODULO | ST_BITWISE)}, {OT_float, (ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_COMPARISON | ST_CONCATENATION | ST_ASSIGNMENT)}, {OT_float, (ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_COMPARISON | ST_CONCATENATION | ST_ASSIGNMENT)}, {OT_float, (ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_COMPARISON | ST_CONCATENATION | ST_ASSIGNMENT)}, {OT_string, (ST_ADD | ST_EQUALITY | ST_CONCATENATION)}, {OT_string, (ST_ADD | ST_EQUALITY | ST_CONCATENATION)}, {OT_string, (ST_ADD | ST_EQUALITY | ST_CONCATENATION)}, {OT_list, (ST_ADD | ST_EQUALITY | ST_CONCATENATION)}, {OT_list, (ST_ADD | ST_COMPARISON | ST_CONCATENATION)}, {OT_list, (ST_ADD | ST_COMPARISON | ST_CONCATENATION)}, {OT_integer, (ST_ADD | ST_COMPARISON)}, {OT_float, (ST_ADD | ST_COMPARISON)}, {OT_list, (ST_ADD | ST_CONCATENATION)}, {OT_vector, (ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_CONCATENATION | ST_ASSIGNMENT | ST_MODULO)}, {OT_vector, (ST_MULTIPLY)}, {OT_vector, (ST_MULTIPLY)}, {OT_rotation, (ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_CONCATENATION | ST_ASSIGNMENT)}, {OT_other, (ST_NONE)}, {OT_invalid, (ST_NONE)} }; opType opExpr[][10] = { {OT_nothing, OT_bool, OT_integer, OT_float, OT_key, OT_list, OT_rotation, OT_string, OT_vector, OT_other}, {OT_bool, OT_boolBool, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid}, {OT_integer, OT_invalid, OT_intInt, OT_intFloat, OT_invalid, OT_intList, OT_invalid, OT_invalid, OT_invalid, OT_invalid}, {OT_float, OT_invalid, OT_floatInt, OT_floatFloat, OT_invalid, OT_floatList, OT_invalid, OT_invalid, OT_invalid, OT_invalid}, {OT_key, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_keyString, OT_invalid, OT_invalid}, {OT_list, OT_invalid, OT_listInt, OT_listFloat, OT_invalid, OT_listList, OT_invalid, OT_invalid, OT_invalid, OT_listOther}, {OT_rotation, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_rotationRotation, OT_invalid, OT_invalid, OT_invalid}, {OT_string, OT_invalid, OT_invalid, OT_invalid, OT_stringKey, OT_invalid, OT_invalid, OT_stringString, OT_invalid, OT_invalid}, {OT_vector, OT_invalid, OT_invalid, OT_vectorFloat, OT_invalid, OT_invalid, OT_vectorRotation, OT_invalid, OT_vectorVector, OT_invalid}, {OT_other, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_invalid, OT_otherOther} }; LSL_Token **tokens = NULL; int lowestToken = 999999; /* Not actually used, but it might be some day. static LSL_Leaf *newLeaf(LSL_Type type, LSL_Leaf *left, LSL_Leaf *right) { LSL_Leaf *leaf = calloc(1, sizeof(LSL_Leaf)); if (leaf) { leaf->left = left; leaf->right = right; leaf->token = tokens[type - lowestToken]; } return leaf; } */ void burnLeaf(LSL_Leaf *leaf) { if (leaf) { burnLeaf(leaf->left); burnLeaf(leaf->right); // TODO - Should free up the value to. free(leaf->ignorableText); free(leaf); } } LSL_Leaf *addOperation(LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right) { if (lval) { opType lType, rType; lval->left = left; lval->right = right; // Try to figure out what type of operation this is. if (NULL == left) lType = OT_nothing; else { lType = left->basicType; if (OT_vector < lType) lType = allowed[lType].result; } if (NULL == right) rType = OT_nothing; else { rType = right->basicType; if (OT_vector < rType) rType = allowed[rType].result; } // The basic lookup. lval->basicType = opExpr[lType][rType]; if (OT_invalid != lval->basicType) { // Check if it's an allowed operation. if (0 == (lval->token->subType & allowed[lval->basicType].subTypes)) { lval->basicType = OT_invalid; } else { // Double check the corner cases. switch (lval->token->subType) { case ST_BOOLEAN : lval->basicType = OT_bool; break; case ST_COMPARISON : lval->basicType = OT_bool; break; case ST_MULTIPLY : if (OT_vectorVector == lval->basicType) { if (LSL_MULTIPLY == lval->token->type) { lval->basicType = OT_float; // lval->token = tokens[LSL_DOT_PRODUCT - lowestToken]; } else lval->basicType = OT_vector; } break; default : break; } } } if (OT_invalid == lval->basicType) fprintf(stderr, "Invalid operation [%s] type at line %d column %d\n", lval->token->token, lval->line, lval->column); } return lval; } LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Leaf *rval) { LSL_Parenthesis *parens = malloc(sizeof(LSL_Parenthesis)); if (parens) { parens->left = lval; parens->expression = expr; parens->right = rval; if (lval) { lval->value.parenthesis = parens; lval->basicType = expr->basicType; } } return lval; } LSL_Leaf *addStatement(LSL_Leaf *lval, LSL_Type type, LSL_Leaf *expr) { LSL_Statement *stat = malloc(sizeof(LSL_Statement)); if (stat) { stat->type = type; stat->expressions = expr; if (lval) lval->value.statementValue = stat; } return lval; } static LSL_Leaf *evaluateLeaf(LSL_Leaf *leaf, LSL_Leaf *left, LSL_Leaf *right) { LSL_Leaf *result = NULL; if (leaf) { LSL_Leaf *lresult = NULL; LSL_Leaf *rresult = NULL; if (LSL_RIGHT2LEFT & leaf->token->flags) { rresult = evaluateLeaf(leaf->right, left, right); if (!(LSL_UNARY & leaf->token->flags)) lresult = evaluateLeaf(leaf->left, left, right); } else // Assume left to right. { lresult = evaluateLeaf(leaf->left, left, right); if (!(LSL_UNARY & leaf->token->flags)) rresult = evaluateLeaf(leaf->right, left, right); } if (leaf->token->evaluate) result = leaf->token->evaluate(leaf, lresult, rresult); else { result = calloc(1, sizeof(LSL_Leaf)); if (rresult && result) memcpy(result, rresult, sizeof(LSL_Leaf)); } if (lresult) free(lresult); if (rresult) free(rresult); } return result; } static LSL_Leaf *evaluateFloatToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { LSL_Leaf *result = malloc(sizeof(LSL_Leaf)); if (content && result) { #ifdef LUASL_DEBUG printf(" <%g> ", content->value.floatValue); #endif memcpy(result, content, sizeof(LSL_Leaf)); result->basicType = OT_float; } return result; } static LSL_Leaf *evaluateIntegerToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { LSL_Leaf *result = malloc(sizeof(LSL_Leaf)); if (content && result) { #ifdef LUASL_DEBUG printf(" <%d> ", content->value.integerValue); #endif memcpy(result, content, sizeof(LSL_Leaf)); result->basicType = OT_integer; } return result; } static LSL_Leaf *evaluateNoToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { // Do nothing, that's the point. return content; } /* Typecasting LSL is statically typed, so stored values are not converted, only the values used in expressions are. Lua is dynamically typed, so stored values are changed (sometimes I think). LSL implicitly typecasts - There is a shitload of QUIRKs about this. Apparently some don't work anyway. integer -> float (Says in lslwiki that precision is never lost, which is bullshit, since they are both 32 bit. Would be true if the float is 64 bit. Lua suggest to use 64 bit floats to emulate 32 bit integers.) string -> key Some functions need help with this or the other way around. string -> vector (Maybe, should test that.) vector -> string (Maybe, should test that.) Also happens when getting stuff from lists. Explicit type casting - string -> integer Leading spaces are ignored, as are any characters after the run of digits. All other strings convert to 0. Which means "" and " " convert to 0. Strings in hexadecimal format will work. keys <-> string No other typecasting can be done with keys. float -> string You get a bunch of trailing 0s. QUIRK - I have seen cases where a double explicit typecast was needed in SL, but was considered to be invalid syntax in OS. Any binary operation involving a float and an integer implicitly casts the integer to float. A boolean operation deals with TRUE (1) and FALSE (0). Any non zero value is a TRUE (generally sigh). Bitwise operations only apply to integers. The shifts are arithmatic, not logical. Right shifted bits are dropped, left shifts the sign bit. integer = integer0 % integer1; // Apparently only applies to integers. string = string0 + string1; // Concatenation. list = list0 + list1; // Concatenation. Also works if either is not a list, it's promoted to a list first. list = (list=[]) + list + ["new_item"]; // Voodoo needed for old LSL, works in Mono but not needed, does not work in OS. Works for strings to. bool = list == != int // Only compares the lengths, probably applies to the other conditionals to. vector = vector0 + vector1; // Add elements together. vector = vector0 - vector1; // Subtract elements of vector1 from elements of vector0. float = vector0 * vector1; // A dot product of the vectors. vector = vector0 % vector1; // A cross product of the vectors. vector = vector * float; // Scale the vector, works the other way around I think. Works for integer to, but it will end up being cast to float. vector = vector / float; // Scale the vector, works the other way around I think. Works for integer to, but it will end up being cast to float. vector = vector * rotation; // Rotate the vector by the rotation. Other way around wont compile. vector = vector / rotation; // Rotate the vector by the rotation, in the opposite direction. Other way around wont compile. rotation = llGetRot() * rotation; // Rotate an object around the global axis. rotation = rotation * llGetLocalRot(); // Rotate an object around the local axis. rotation = rotation0 * rotation1; // Add two rotations, so the result is as if you applied each rotation one after the other. // Division rotates in the opposite direction. rotation = rotation0 + rotation1; // Similar to vector, but it's a meaningless thing as far as rotations go. rotation = rotation0 - rotation1; // Similar to vector, but it's a meaningless thing as far as rotations go. A boolean operator results in a boolean value. (any types) A comparison operator results in a boolean value. (any types) A bitwise operator results in an integer value. (intInt or int) A dot product operator results in a float value. (vector * vector) A vectorFloat results in a vector value. */ static LSL_Leaf *evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { LSL_Leaf *result = calloc(1, sizeof(LSL_Leaf)); if (content && result) { #ifdef LUASL_DEBUG printf(" [%s] ", content->token->token); #endif memcpy(result, content, sizeof(LSL_Leaf)); // Figure out the type of the operation. if (OT_vector < result->basicType) result->basicType = allowed[result->basicType].result; switch (result->basicType) { case OT_float : { float fleft = left->value.floatValue; float fright = right->value.floatValue; // Do the casting. if (OT_floatInt == content->basicType) fright = right->value.integerValue; if (OT_intFloat == content->basicType) fleft = left->value.integerValue; switch (result->token->type) { case LSL_COMMA : case LSL_INCREMENT_PRE : // case LSL_INCREMENT_POST : case LSL_DECREMENT_PRE : // case LSL_DECREMENT_POST : case LSL_ASSIGNMENT_PLAIN : case LSL_ASSIGNMENT_DIVIDE : case LSL_ASSIGNMENT_MULTIPLY : case LSL_ASSIGNMENT_SUBTRACT : case LSL_ASSIGNMENT_ADD : case LSL_BRACKET_OPEN : case LSL_BRACKET_CLOSE : case LSL_ANGLE_OPEN : case LSL_ANGLE_CLOSE : // case LSL_TYPECAST_OPEN : // case LSL_TYPECAST_CLOSE : // case LSL_DOT_PRODUCT : break; case LSL_NEGATION : result->value.floatValue = 0 - fright; break; case LSL_DIVIDE : result->value.floatValue = fleft / fright; break; case LSL_MULTIPLY : result->value.floatValue = fleft * fright; break; case LSL_SUBTRACT : result->value.floatValue = fleft - fright; break; case LSL_ADD : result->value.floatValue = fleft + fright; break; case LSL_LESS_THAN : result->value.floatValue = fleft < fright; break; case LSL_GREATER_THAN : result->value.floatValue = fleft > fright; break; case LSL_LESS_EQUAL : result->value.floatValue = fleft <= fright; break; case LSL_GREATER_EQUAL : result->value.floatValue = fleft >= fright; break; case LSL_EQUAL : result->value.floatValue = fleft == fright; break; case LSL_NOT_EQUAL : result->value.floatValue = fleft != fright; break; } #ifdef LUASL_DEBUG printf(" (=%g) ", result->value.floatValue); #endif break; } case OT_integer : { switch (result->token->type) { 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_BRACKET_OPEN : case LSL_BRACKET_CLOSE : case LSL_ANGLE_OPEN : case LSL_ANGLE_CLOSE : // case LSL_TYPECAST_OPEN : // case LSL_TYPECAST_CLOSE : break; case LSL_BIT_NOT : result->value.integerValue = ~ right->value.integerValue; break; case LSL_BOOL_NOT : result->value.integerValue = ! right->value.integerValue; break; case LSL_NEGATION : result->value.integerValue = 0 - right->value.integerValue; break; case LSL_DIVIDE : result->value.integerValue = left->value.integerValue / right->value.integerValue; break; case LSL_MODULO : result->value.integerValue = left->value.integerValue % right->value.integerValue; break; case LSL_MULTIPLY : result->value.integerValue = left->value.integerValue * right->value.integerValue; break; case LSL_SUBTRACT : result->value.integerValue = left->value.integerValue - right->value.integerValue; break; case LSL_ADD : result->value.integerValue = left->value.integerValue + right->value.integerValue; break; case LSL_LEFT_SHIFT : result->value.integerValue = left->value.integerValue << right->value.integerValue; break; case LSL_RIGHT_SHIFT : result->value.integerValue = left->value.integerValue >> right->value.integerValue; break; case LSL_LESS_THAN : result->value.integerValue = left->value.integerValue < right->value.integerValue; break; case LSL_GREATER_THAN : result->value.integerValue = left->value.integerValue > right->value.integerValue; break; case LSL_LESS_EQUAL : result->value.integerValue = left->value.integerValue <= right->value.integerValue; break; case LSL_GREATER_EQUAL : result->value.integerValue = left->value.integerValue >= right->value.integerValue; break; case LSL_EQUAL : result->value.integerValue = left->value.integerValue == right->value.integerValue; break; case LSL_NOT_EQUAL : result->value.integerValue = left->value.integerValue != right->value.integerValue; break; case LSL_BIT_AND : result->value.integerValue = left->value.integerValue & right->value.integerValue; break; case LSL_BIT_XOR : result->value.integerValue = left->value.integerValue ^ right->value.integerValue; break; case LSL_BIT_OR : result->value.integerValue = left->value.integerValue | right->value.integerValue; break; case LSL_BOOL_OR : result->value.integerValue = left->value.integerValue || right->value.integerValue; break; case LSL_BOOL_AND : result->value.integerValue = left->value.integerValue && right->value.integerValue; break; } #ifdef LUASL_DEBUG printf(" (=%d) ", result->value.integerValue); #endif break; } default : break; } } return result; } static LSL_Leaf *eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { LSL_Leaf *result = NULL; if (content) result = evaluateLeaf(content->value.parenthesis->expression, left, right); return result; } static LSL_Leaf *evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { LSL_Leaf *result = NULL; if (content) { result = evaluateLeaf(content->value.statementValue->expressions, left, right); if (result) { switch (result->basicType) { case OT_float : printf("\nResult is the float %g.\n", result->value.floatValue); break; case OT_integer : printf("\nResult is the integer %d.\n", result->value.integerValue); break; default : printf("\nResult of an unknown type [%d] %d!\n", result->basicType, result->value.integerValue); break; } free(result); result = NULL; } if (left) left->value.integerValue = 0; if (right) right->value.integerValue = 0; } return result; } static void outputLeaf(FILE *file, LSL_Leaf *leaf) { if (leaf) { outputLeaf(file, leaf->left); if ((!(LSL_NOIGNORE & leaf->token->flags)) && (leaf->ignorableText)) fprintf(file, "%s", leaf->ignorableText); if (leaf->token->output) leaf->token->output(file, leaf); else fprintf(file, "%s", leaf->token->token); outputLeaf(file, leaf->right); } } static void outputFloatToken(FILE *file, LSL_Leaf *content) { if (content) fprintf(file, "%g", content->value.floatValue); } static void outputIntegerToken(FILE *file, LSL_Leaf *content) { if (content) fprintf(file, "%d", content->value.integerValue); } static void outputParenthesisToken(FILE *file, LSL_Leaf *content) { if (content) { fprintf(file, "%s", content->token->token); outputLeaf(file, content->value.parenthesis->expression); outputLeaf(file, content->value.parenthesis->right); } } static void outputStatementToken(FILE *file, LSL_Leaf *content) { if (content) { outputLeaf(file, content->value.statementValue->expressions); if (content->ignorableText) fprintf(file, "%s", content->ignorableText); fprintf(file, "%s", content->token->token); } } static void convertLeaf2Lua(FILE *file, LSL_Leaf *leaf) { if (leaf) { convertLeaf2Lua(file, leaf->left); if ((!(LSL_NOIGNORE & leaf->token->flags)) && (leaf->ignorableText)) fprintf(file, "%s", leaf->ignorableText); if (leaf->token->convert) leaf->token->convert(file, leaf); else if (leaf->token->output) leaf->token->output(file, leaf); else fprintf(file, "%s", leaf->token->token); convertLeaf2Lua(file, leaf->right); } } int nextFile(LuaSL_yyparseExtra *extra) { if (NULL != extra->file) fclose(extra->file); if (--(extra->argc) > 0 && *++(extra->argv) != '\0') { strncpy(extra->fileName, *(extra->argv), PATH_MAX - 1); extra->fileName[PATH_MAX - 1] = '\0'; extra->file = fopen(extra->fileName, "r"); if (NULL == extra->file) { fprintf(stderr, "Error opening file %s.\n", extra->fileName); return 1; } printf("Opened %s.\n", extra->fileName); return(0); } return(1); } int main(int argc, char **argv) { // char *programName = argv[0]; int i; // Figure out what numbers yacc gave to our tokens. for (i = 0; LSL_Tokens[i].token != NULL; i++) { if (lowestToken > LSL_Tokens[i].type) lowestToken = LSL_Tokens[i].type; } tokens = calloc(i + 1, sizeof(LSL_Token *)); if (tokens) { char buffer[PATH_MAX]; char fileName[PATH_MAX]; LuaSL_yyparseParam param; LuaSL_yyparseExtra extra; int count; // Sort the token table. for (i = 0; LSL_Tokens[i].token != NULL; i++) { int j = LSL_Tokens[i].type - lowestToken; tokens[j] = &(LSL_Tokens[i]); } fileName[0] = '\0'; param.ast = NULL; param.lval = calloc(1, sizeof(LSL_Leaf)); memset(&extra, 0, sizeof(extra)); extra.argc = argc; extra.argv = argv; extra.fileName = fileName; extra.file = NULL; // Grab the first file name, if any. if (1 == nextFile(&extra)) return 1; /* if ('\0' == fileName[0]) { //strcpy(fileName, "test.lsl"); count = read(STDIN_FILENO, fileName, PATH_MAX - 1); if (0 > count) { printf("Error in stdin!\n"); return 1; } else if (0 == count) { printf("No bytes in stdin!\n"); return 1; } else { fileName[count] = '\0'; printf("Filename %s in stdin.\n", fileName); } } */ if (yylex_init_extra(&extra, &(param.scanner))) return 1; #ifdef LUASL_DEBUG // yydebug= 5; #endif #ifdef LUASL_DEBUG yyset_debug(1, param.scanner); #endif #ifdef LUASL_FILES yyset_in(extra.file, &(param.scanner)); #endif { void *pParser = ParseAlloc(malloc); int yv; ParseTrace(stdout, "LSL_lemon "); #ifndef LUASL_FILES while ((i = fread(buffer, 1, PATH_MAX - 1, extra.file)) > 0) #endif { #ifndef LUASL_FILES buffer[i] = '\0'; yy_scan_string(buffer, param.scanner); #endif // on EOF yylex will return 0 while((yv = yylex(param.lval, param.scanner)) != 0) { Parse(pParser, yv, param.lval, ¶m); if (LSL_SCRIPT == yv) break; param.lval = calloc(1, sizeof(LSL_Leaf)); } } yylex_destroy(param.scanner); Parse (pParser, 0, param.lval, ¶m); ParseFree(pParser, free); if (param.ast) { FILE *out; char outName[PATH_MAX]; char luaName[PATH_MAX]; outputLeaf(stdout, param.ast); printf("\n"); evaluateLeaf(param.ast, NULL, NULL); printf("\n"); strcpy(outName, fileName); strcat(outName, "2"); strcpy(luaName, fileName); strcat(luaName, ".lua"); out = fopen(outName, "w"); if (out) { outputLeaf(out, param.ast); fclose(out); sprintf(buffer, "diff %s %s", fileName, outName); count = system(buffer); printf("Return value of %s is %d\n", buffer, count); if (0 != count) fprintf(stderr, "%s says they are different!\n", buffer); } else fprintf(stderr, "Unable to open file %s for writing!\n", outName); out = fopen(luaName, "w"); if (out) { convertLeaf2Lua(out, param.ast); fclose(out); } else fprintf(stderr, "Unable to open file %s for writing!\n", luaName); burnLeaf(param.ast); } } } else { fprintf(stderr, "No memory for tokens!"); return 1; } return 0; }