diff options
author | David Walter Seikel | 2012-01-07 22:31:24 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-07 22:31:24 +1000 |
commit | 3953d90d3b42ac9e156262e2d2dec87bf912ed02 (patch) | |
tree | c1c5cca1ed3363b919cfb7b9d070aca572376da1 | |
parent | Clean up. exp -> ast. No more addExpression or outputExpression. Operation... (diff) | |
download | SledjHamr-3953d90d3b42ac9e156262e2d2dec87bf912ed02.zip SledjHamr-3953d90d3b42ac9e156262e2d2dec87bf912ed02.tar.gz SledjHamr-3953d90d3b42ac9e156262e2d2dec87bf912ed02.tar.bz2 SledjHamr-3953d90d3b42ac9e156262e2d2dec87bf912ed02.tar.xz |
More expression cleanup.
Diffstat (limited to '')
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.c | 325 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.h | 2 |
2 files changed, 169 insertions, 158 deletions
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 @@ | |||
3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
4 | #include <stdio.h> | 4 | #include <stdio.h> |
5 | 5 | ||
6 | static void evaluateExpressionToken(LSL_Leaf *content, LSL_Value *result); | 6 | static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); |
7 | static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); | ||
7 | static void outputIntegerToken(LSL_Leaf *content); | 8 | static void outputIntegerToken(LSL_Leaf *content); |
8 | static void outputOperationToken(LSL_Leaf *content); | 9 | static void outputOperationToken(LSL_Leaf *content); |
9 | static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *result); | ||
10 | 10 | ||
11 | LSL_Token LSL_Tokens[] = | 11 | LSL_Token LSL_Tokens[] = |
12 | { | 12 | { |
@@ -15,54 +15,54 @@ LSL_Token LSL_Tokens[] = | |||
15 | // Left to right, unless oterwise stated. | 15 | // Left to right, unless oterwise stated. |
16 | // According to http://wiki.secondlife.com/wiki/Category:LSL_Operators | 16 | // According to http://wiki.secondlife.com/wiki/Category:LSL_Operators |
17 | 17 | ||
18 | // {LSL_COMMA, ",", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 18 | // {LSL_COMMA, ",", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
19 | // {LSL_INCREMENT_PRE, "++", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, | 19 | // {LSL_INCREMENT_PRE, "++", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, |
20 | // {LSL_INCREMENT_POST, "++", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, | 20 | // {LSL_INCREMENT_POST, "++", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, |
21 | // {LSL_DECREMENT_PRE, "--", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, | 21 | // {LSL_DECREMENT_PRE, "--", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, |
22 | // {LSL_DECREMENT_POST, "--", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, | 22 | // {LSL_DECREMENT_POST, "--", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, |
23 | // {LSL_DOT, ".", LSL_RIGHT2LEFT, outputOperationToken, NULL, NULL}, | 23 | // {LSL_DOT, ".", LSL_RIGHT2LEFT, outputOperationToken, NULL, evaluateOperationToken}, |
24 | // {LSL_ASSIGNMENT_PLAIN, "=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, | 24 | // {LSL_ASSIGNMENT_PLAIN, "=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, |
25 | // {LSL_ASSIGNMENT_DIVIDE, "/=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, | 25 | // {LSL_ASSIGNMENT_DIVIDE, "/=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, |
26 | // {LSL_ASSIGNMENT_MODULO, "%=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, | 26 | // {LSL_ASSIGNMENT_MODULO, "%=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, |
27 | // {LSL_ASSIGNMENT_MULTIPLY, "*=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, | 27 | // {LSL_ASSIGNMENT_MULTIPLY, "*=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, |
28 | // {LSL_ASSIGNMENT_SUBTRACT, "-=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, | 28 | // {LSL_ASSIGNMENT_SUBTRACT, "-=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, |
29 | // {LSL_ASSIGNMENT_ADD, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, | 29 | // {LSL_ASSIGNMENT_ADD, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, |
30 | // {LSL_ASSIGNMENT_CONCATENATE, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, NULL}, | 30 | // {LSL_ASSIGNMENT_CONCATENATE, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, outputOperationToken, NULL, evaluateOperationToken}, |
31 | {LSL_PARENTHESIS_OPEN, "(", LSL_INNER2OUTER, outputOperationToken, NULL, NULL}, | 31 | {LSL_PARENTHESIS_OPEN, "(", LSL_INNER2OUTER, outputOperationToken, NULL, evaluateOperationToken}, |
32 | {LSL_PARENTHESIS_CLOSE, ")", LSL_INNER2OUTER, outputOperationToken, NULL, NULL}, | 32 | {LSL_PARENTHESIS_CLOSE, ")", LSL_INNER2OUTER, outputOperationToken, NULL, evaluateOperationToken}, |
33 | // {LSL_BRACKET_OPEN, "[", LSL_INNER2OUTER | LSL_CREATION, outputOperationToken, NULL, NULL}, | 33 | // {LSL_BRACKET_OPEN, "[", LSL_INNER2OUTER | LSL_CREATION, outputOperationToken, NULL, evaluateOperationToken}, |
34 | // {LSL_BRACKET_CLOSE, "]", LSL_INNER2OUTER | LSL_CREATION, outputOperationToken, NULL, NULL}, | 34 | // {LSL_BRACKET_CLOSE, "]", LSL_INNER2OUTER | LSL_CREATION, outputOperationToken, NULL, evaluateOperationToken}, |
35 | // {LSL_ANGLE_OPEN, "<", LSL_LEFT2RIGHT | LSL_CREATION, outputOperationToken, NULL, NULL}, | 35 | // {LSL_ANGLE_OPEN, "<", LSL_LEFT2RIGHT | LSL_CREATION, outputOperationToken, NULL, evaluateOperationToken}, |
36 | // {LSL_ANGLE_CLOSE, ">", LSL_LEFT2RIGHT | LSL_CREATION, outputOperationToken, NULL, NULL}, | 36 | // {LSL_ANGLE_CLOSE, ">", LSL_LEFT2RIGHT | LSL_CREATION, outputOperationToken, NULL, evaluateOperationToken}, |
37 | // {LSL_TYPECAST_OPEN, "(", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, | 37 | // {LSL_TYPECAST_OPEN, "(", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, |
38 | // {LSL_TYPECAST_CLOSE, ")", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, | 38 | // {LSL_TYPECAST_CLOSE, ")", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, |
39 | {LSL_BIT_NOT, "~", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, | 39 | {LSL_BIT_NOT, "~", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, |
40 | {LSL_BOOL_NOT, "!", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, | 40 | {LSL_BOOL_NOT, "!", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, |
41 | {LSL_NEGATION, "-", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, NULL}, | 41 | {LSL_NEGATION, "-", LSL_RIGHT2LEFT | LSL_UNARY, outputOperationToken, NULL, evaluateOperationToken}, |
42 | {LSL_DIVIDE, "/", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 42 | {LSL_DIVIDE, "/", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
43 | {LSL_MODULO, "%", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 43 | {LSL_MODULO, "%", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
44 | {LSL_MULTIPLY, "*", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 44 | {LSL_MULTIPLY, "*", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
45 | // {LSL_DOT_PRODUCT, "*", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 45 | // {LSL_DOT_PRODUCT, "*", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
46 | // {LSL_CROSS_PRODUCT, "%", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 46 | // {LSL_CROSS_PRODUCT, "%", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
47 | {LSL_SUBTRACT, "-", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 47 | {LSL_SUBTRACT, "-", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
48 | {LSL_ADD, "+", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 48 | {LSL_ADD, "+", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
49 | // {LSL_CONCATENATE, "+", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 49 | // {LSL_CONCATENATE, "+", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
50 | {LSL_LEFT_SHIFT, "<<", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 50 | {LSL_LEFT_SHIFT, "<<", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
51 | {LSL_RIGHT_SHIFT, ">>", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 51 | {LSL_RIGHT_SHIFT, ">>", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
52 | // QUIRK - Conditionals are executed right to left. Or left to right, depending on who you ask. lol | 52 | // QUIRK - Conditionals are executed right to left. Or left to right, depending on who you ask. lol |
53 | {LSL_LESS_THAN, "<", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 53 | {LSL_LESS_THAN, "<", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
54 | {LSL_GREATER_THAN, ">", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 54 | {LSL_GREATER_THAN, ">", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
55 | {LSL_LESS_EQUAL, "<=", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 55 | {LSL_LESS_EQUAL, "<=", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
56 | {LSL_GREATER_EQUAL, ">=", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 56 | {LSL_GREATER_EQUAL, ">=", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
57 | {LSL_EQUAL, "==", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 57 | {LSL_EQUAL, "==", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
58 | {LSL_NOT_EQUAL, "!=", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 58 | {LSL_NOT_EQUAL, "!=", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
59 | {LSL_BIT_AND, "&", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 59 | {LSL_BIT_AND, "&", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
60 | {LSL_BIT_XOR, "^", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 60 | {LSL_BIT_XOR, "^", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
61 | {LSL_BIT_OR, "|", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 61 | {LSL_BIT_OR, "|", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
62 | // QUIRK - Seems to be some disagreement about BOOL_AND/BOOL_OR precedence. Either they are equal, or OR is higher. | 62 | // QUIRK - Seems to be some disagreement about BOOL_AND/BOOL_OR precedence. Either they are equal, or OR is higher. |
63 | // QUIRK - No boolean short circuiting. | 63 | // QUIRK - No boolean short circuiting. |
64 | {LSL_BOOL_OR, "||", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 64 | {LSL_BOOL_OR, "||", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
65 | {LSL_BOOL_AND, "&&", LSL_LEFT2RIGHT, outputOperationToken, NULL, NULL}, | 65 | {LSL_BOOL_AND, "&&", LSL_LEFT2RIGHT, outputOperationToken, NULL, evaluateOperationToken}, |
66 | 66 | ||
67 | // Then the rest of the syntax tokens. | 67 | // Then the rest of the syntax tokens. |
68 | 68 | ||
@@ -79,7 +79,7 @@ LSL_Token LSL_Tokens[] = | |||
79 | // {LSL_ROTATION, "rotation", LSL_NONE, NULL, NULL, NULL}, | 79 | // {LSL_ROTATION, "rotation", LSL_NONE, NULL, NULL, NULL}, |
80 | // {LSL_LIST, "list", LSL_NONE, NULL, NULL, NULL}, | 80 | // {LSL_LIST, "list", LSL_NONE, NULL, NULL, NULL}, |
81 | // {LSL_LABEL, "@", LSL_NONE, NULL, NULL, NULL}, | 81 | // {LSL_LABEL, "@", LSL_NONE, NULL, NULL, NULL}, |
82 | {LSL_EXPRESSION, "expression", LSL_NONE, NULL, NULL, evaluateExpressionToken}, | 82 | {LSL_EXPRESSION, "expression", LSL_NONE, NULL, NULL, NULL}, |
83 | // {LSL_DO, "do", LSL_NONE, NULL, NULL, NULL}, | 83 | // {LSL_DO, "do", LSL_NONE, NULL, NULL, NULL}, |
84 | // {LSL_FOR, "for", LSL_NONE, NULL, NULL, NULL}, | 84 | // {LSL_FOR, "for", LSL_NONE, NULL, NULL, NULL}, |
85 | // {LSL_IF, "if", LSL_NONE, NULL, NULL, NULL}, | 85 | // {LSL_IF, "if", LSL_NONE, NULL, NULL, NULL}, |
@@ -146,8 +146,8 @@ LSL_AST *addOperation(LSL_Operation type, LSL_AST *left, LSL_AST *right) | |||
146 | { | 146 | { |
147 | if (LSL_EXPRESSION == type) | 147 | if (LSL_EXPRESSION == type) |
148 | { | 148 | { |
149 | ast->content.expressionValue = left; | 149 | ast->content.expressionValue = right; |
150 | ast->right = NULL; | 150 | ast->left = NULL; |
151 | } | 151 | } |
152 | else | 152 | else |
153 | ast->content.operationValue = type; | 153 | ast->content.operationValue = type; |
@@ -156,131 +156,133 @@ LSL_AST *addOperation(LSL_Operation type, LSL_AST *left, LSL_AST *right) | |||
156 | return ast; | 156 | return ast; |
157 | } | 157 | } |
158 | 158 | ||
159 | static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *result) | 159 | static void evaluateAST(LSL_AST *ast, LSL_Value *left, LSL_Value *right) |
160 | { | ||
161 | if (ast) | ||
162 | { | ||
163 | LSL_Value lresult; | ||
164 | LSL_Value rresult; | ||
165 | |||
166 | memcpy(&lresult, left, sizeof(LSL_Value)); | ||
167 | memcpy(&rresult, right, sizeof(LSL_Value)); | ||
168 | |||
169 | if (LSL_RIGHT2LEFT & ast->token->flags) | ||
170 | { | ||
171 | memcpy(&rresult, left, sizeof(LSL_Value)); | ||
172 | evaluateAST(ast->right, &rresult, right); | ||
173 | if (!(LSL_UNARY & ast->token->flags)) | ||
174 | { | ||
175 | evaluateAST(ast->left, &lresult, right); | ||
176 | } | ||
177 | } | ||
178 | else // Assume left to right. | ||
179 | { | ||
180 | evaluateAST(ast->left, &lresult, right); | ||
181 | if (!(LSL_UNARY & ast->token->flags)) | ||
182 | { | ||
183 | memcpy(&rresult, left, sizeof(LSL_Value)); | ||
184 | evaluateAST(ast->right, &rresult, right); | ||
185 | } | ||
186 | } | ||
187 | |||
188 | if (ast->token->evaluate) | ||
189 | { | ||
190 | ast->token->evaluate(&(ast->content), &lresult, &rresult); | ||
191 | memcpy(left, &lresult, sizeof(LSL_Value)); | ||
192 | } | ||
193 | else | ||
194 | { | ||
195 | printf(" eval <%s %d %d %d %d> ", ast->token->token, left->content.integerValue, right->content.integerValue, lresult.content.integerValue, rresult.content.integerValue); | ||
196 | memcpy(left, &rresult, sizeof(LSL_Value)); | ||
197 | } | ||
198 | } | ||
199 | } | ||
200 | |||
201 | static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) | ||
160 | { | 202 | { |
161 | if (content) | 203 | if (content) |
162 | { | 204 | { |
163 | #ifdef LUASL_DEBUG | 205 | #ifdef LUASL_DEBUG |
164 | printf(" %d ", content->integerValue); | 206 | printf(" <%d> ", content->integerValue); |
165 | #endif | 207 | #endif |
166 | result->content.integerValue = content->integerValue; | 208 | left->content.integerValue = content->integerValue; |
167 | result->type = LSL_INTEGER; | 209 | left->type = LSL_INTEGER; |
168 | } | 210 | } |
169 | } | 211 | } |
170 | 212 | ||
171 | static void evaluateExpression(LSL_AST *ast, LSL_Value *result) | 213 | static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) |
172 | { | 214 | { |
173 | LSL_Value left, right; | 215 | if ((content) && (content->operationValue)) |
174 | |||
175 | if ((NULL == ast) || (NULL == result)) | ||
176 | return; | ||
177 | |||
178 | if (LSL_INTEGER == ast->token->type) | ||
179 | { | ||
180 | evaluateIntegerToken(&(ast->content), result); | ||
181 | return; | ||
182 | } | ||
183 | else if (LSL_LEFT2RIGHT & ast->token->flags) | ||
184 | { | ||
185 | evaluateExpression(ast->left, &left); | ||
186 | if (!(LSL_UNARY & ast->token->flags)) | ||
187 | evaluateExpression(ast->right, &right); | ||
188 | } | ||
189 | else if (LSL_RIGHT2LEFT & ast->token->flags) | ||
190 | { | ||
191 | evaluateExpression(ast->right, &right); | ||
192 | if (!(LSL_UNARY & ast->token->flags)) | ||
193 | evaluateExpression(ast->left, &left); | ||
194 | } | ||
195 | else | ||
196 | { | 216 | { |
197 | } | ||
198 | |||
199 | #ifdef LUASL_DEBUG | 217 | #ifdef LUASL_DEBUG |
200 | printf(" %s ", ast->token->token); | 218 | printf(" [%s] ", tokens[content->operationValue - lowestToken]->token); |
201 | #endif | 219 | #endif |
202 | 220 | ||
203 | switch (ast->content.operationValue) | 221 | switch (content->operationValue) |
204 | { | 222 | { |
205 | #ifdef LUASL_USE_ENUM | 223 | #ifdef LUASL_USE_ENUM |
206 | case LSL_COMMA : | 224 | case LSL_COMMA : |
207 | case LSL_INCREMENT_PRE : | 225 | case LSL_INCREMENT_PRE : |
208 | case LSL_INCREMENT_POST : | 226 | case LSL_INCREMENT_POST : |
209 | case LSL_DECREMENT_PRE : | 227 | case LSL_DECREMENT_PRE : |
210 | case LSL_DECREMENT_POST : | 228 | case LSL_DECREMENT_POST : |
211 | case LSL_DOT : | 229 | case LSL_DOT : |
212 | case LSL_ASSIGNMENT_PLAIN : | 230 | case LSL_ASSIGNMENT_PLAIN : |
213 | case LSL_ASSIGNMENT_DIVIDE : | 231 | case LSL_ASSIGNMENT_DIVIDE : |
214 | case LSL_ASSIGNMENT_MODULO : | 232 | case LSL_ASSIGNMENT_MODULO : |
215 | case LSL_ASSIGNMENT_MULTIPLY : | 233 | case LSL_ASSIGNMENT_MULTIPLY : |
216 | case LSL_ASSIGNMENT_SUBTRACT : | 234 | case LSL_ASSIGNMENT_SUBTRACT : |
217 | case LSL_ASSIGNMENT_ADD : | 235 | case LSL_ASSIGNMENT_ADD : |
218 | case LSL_ASSIGNMENT_CONCATENATE : | 236 | case LSL_ASSIGNMENT_CONCATENATE : |
219 | case LSL_PARENTHESIS_OPEN : | 237 | case LSL_PARENTHESIS_OPEN : |
220 | case LSL_PARENTHESIS_CLOSE : | 238 | case LSL_PARENTHESIS_CLOSE : |
221 | case LSL_BRACKET_OPEN : | 239 | case LSL_BRACKET_OPEN : |
222 | case LSL_BRACKET_CLOSE : | 240 | case LSL_BRACKET_CLOSE : |
223 | case LSL_ANGLE_OPEN : | 241 | case LSL_ANGLE_OPEN : |
224 | case LSL_ANGLE_CLOSE : | 242 | case LSL_ANGLE_CLOSE : |
225 | case LSL_TYPECAST : | 243 | case LSL_TYPECAST : |
226 | break; | 244 | break; |
227 | #endif | 245 | #endif |
228 | case LSL_BIT_NOT : result->content.integerValue = ~ right.content.integerValue; break; | 246 | case LSL_BIT_NOT : left->content.integerValue = ~ right->content.integerValue; break; |
229 | case LSL_BOOL_NOT : result->content.integerValue = ! right.content.integerValue; break; | 247 | case LSL_BOOL_NOT : left->content.integerValue = ! right->content.integerValue; break; |
230 | case LSL_NEGATION : result->content.integerValue = 0 - right.content.integerValue; break; | 248 | case LSL_NEGATION : left->content.integerValue = 0 - right->content.integerValue; break; |
231 | case LSL_DIVIDE : result->content.integerValue = left.content.integerValue / right.content.integerValue; break; | 249 | case LSL_DIVIDE : left->content.integerValue = left->content.integerValue / right->content.integerValue; break; |
232 | case LSL_MODULO : result->content.integerValue = left.content.integerValue % right.content.integerValue; break; | 250 | case LSL_MODULO : left->content.integerValue = left->content.integerValue % right->content.integerValue; break; |
233 | case LSL_MULTIPLY : result->content.integerValue = left.content.integerValue * right.content.integerValue; break; | 251 | case LSL_MULTIPLY : left->content.integerValue = left->content.integerValue * right->content.integerValue; break; |
234 | #ifdef LUASL_USE_ENUM | 252 | #ifdef LUASL_USE_ENUM |
235 | case LSL_DOT_PRODUCT : break; | 253 | case LSL_DOT_PRODUCT : break; |
236 | case LSL_CROSS_PRODUCT : break; | 254 | case LSL_CROSS_PRODUCT : break; |
237 | #endif | 255 | #endif |
238 | case LSL_SUBTRACT : result->content.integerValue = left.content.integerValue - right.content.integerValue; break; | 256 | case LSL_SUBTRACT : left->content.integerValue = left->content.integerValue - right->content.integerValue; break; |
239 | case LSL_ADD : result->content.integerValue = left.content.integerValue + right.content.integerValue; break; | 257 | case LSL_ADD : left->content.integerValue = left->content.integerValue + right->content.integerValue; break; |
240 | #ifdef LUASL_USE_ENUM | 258 | #ifdef LUASL_USE_ENUM |
241 | case LSL_CONCATENATE : break; | 259 | case LSL_CONCATENATE : break; |
260 | #endif | ||
261 | case LSL_LEFT_SHIFT : left->content.integerValue = left->content.integerValue << right->content.integerValue; break; | ||
262 | case LSL_RIGHT_SHIFT : left->content.integerValue = left->content.integerValue >> right->content.integerValue; break; | ||
263 | case LSL_LESS_THAN : left->content.integerValue = left->content.integerValue < right->content.integerValue; break; | ||
264 | case LSL_GREATER_THAN : left->content.integerValue = left->content.integerValue > right->content.integerValue; break; | ||
265 | case LSL_LESS_EQUAL : left->content.integerValue = left->content.integerValue <= right->content.integerValue; break; | ||
266 | case LSL_GREATER_EQUAL : left->content.integerValue = left->content.integerValue >= right->content.integerValue; break; | ||
267 | case LSL_EQUAL : left->content.integerValue = left->content.integerValue == right->content.integerValue; break; | ||
268 | case LSL_NOT_EQUAL : left->content.integerValue = left->content.integerValue != right->content.integerValue; break; | ||
269 | case LSL_BIT_AND : left->content.integerValue = left->content.integerValue & right->content.integerValue; break; | ||
270 | case LSL_BIT_XOR : left->content.integerValue = left->content.integerValue ^ right->content.integerValue; break; | ||
271 | case LSL_BIT_OR : left->content.integerValue = left->content.integerValue | right->content.integerValue; break; | ||
272 | case LSL_BOOL_OR : left->content.integerValue = left->content.integerValue || right->content.integerValue; break; | ||
273 | case LSL_BOOL_AND : left->content.integerValue = left->content.integerValue && right->content.integerValue; break; | ||
274 | } | ||
275 | #ifdef LUASL_DEBUG | ||
276 | printf(" (=%d) ", left->content.integerValue); | ||
242 | #endif | 277 | #endif |
243 | case LSL_LEFT_SHIFT : result->content.integerValue = left.content.integerValue << right.content.integerValue; break; | ||
244 | case LSL_RIGHT_SHIFT : result->content.integerValue = left.content.integerValue >> right.content.integerValue; break; | ||
245 | case LSL_LESS_THAN : result->content.integerValue = left.content.integerValue < right.content.integerValue; break; | ||
246 | case LSL_GREATER_THAN : result->content.integerValue = left.content.integerValue > right.content.integerValue; break; | ||
247 | case LSL_LESS_EQUAL : result->content.integerValue = left.content.integerValue <= right.content.integerValue; break; | ||
248 | case LSL_GREATER_EQUAL : result->content.integerValue = left.content.integerValue >= right.content.integerValue; break; | ||
249 | case LSL_EQUAL : result->content.integerValue = left.content.integerValue == right.content.integerValue; break; | ||
250 | case LSL_NOT_EQUAL : result->content.integerValue = left.content.integerValue != right.content.integerValue; break; | ||
251 | case LSL_BIT_AND : result->content.integerValue = left.content.integerValue & right.content.integerValue; break; | ||
252 | case LSL_BIT_XOR : result->content.integerValue = left.content.integerValue ^ right.content.integerValue; break; | ||
253 | case LSL_BIT_OR : result->content.integerValue = left.content.integerValue | right.content.integerValue; break; | ||
254 | case LSL_BOOL_OR : result->content.integerValue = left.content.integerValue || right.content.integerValue; break; | ||
255 | case LSL_BOOL_AND : result->content.integerValue = left.content.integerValue && right.content.integerValue; break; | ||
256 | } | ||
257 | |||
258 | return; | ||
259 | } | ||
260 | |||
261 | static void evaluateExpressionToken(LSL_Leaf *content, LSL_Value *result) | ||
262 | { | ||
263 | if (content) | ||
264 | evaluateExpression(content->expressionValue, result); | ||
265 | } | ||
266 | |||
267 | static void evaluateAST(LSL_AST *ast, LSL_Value *result) | ||
268 | { | ||
269 | if (ast) | ||
270 | { | ||
271 | evaluateAST(ast->left, result); | ||
272 | if ((ast->token) && (ast->token->evaluate)) | ||
273 | ast->token->evaluate(&(ast->content), result); | ||
274 | evaluateAST(ast->right, result); | ||
275 | } | 278 | } |
276 | } | 279 | } |
277 | |||
278 | static void outputAST(LSL_AST *ast) | 280 | static void outputAST(LSL_AST *ast) |
279 | { | 281 | { |
280 | if (ast) | 282 | if (ast) |
281 | { | 283 | { |
282 | outputAST(ast->left); | 284 | outputAST(ast->left); |
283 | if ((ast->token) && (ast->token->output)) | 285 | if (ast->token->output) |
284 | ast->token->output(&(ast->content)); | 286 | ast->token->output(&(ast->content)); |
285 | else | 287 | else |
286 | printf(" <%s> ", ast->token->token); | 288 | printf(" <%s> ", ast->token->token); |
@@ -297,13 +299,20 @@ static void outputIntegerToken(LSL_Leaf *content) | |||
297 | static void outputOperationToken(LSL_Leaf *content) | 299 | static void outputOperationToken(LSL_Leaf *content) |
298 | { | 300 | { |
299 | if (content) | 301 | if (content) |
300 | printf(" [%s] ", tokens[content->operationValue - lowestToken]->token); | 302 | printf(" %s ", tokens[content->operationValue - lowestToken]->token); |
301 | } | 303 | } |
302 | 304 | ||
303 | static void convertAST2Lua(LSL_AST *ast) | 305 | static void convertAST2Lua(LSL_AST *ast) |
304 | { | 306 | { |
305 | if ((ast) && (ast->token) && (ast->token->convert)) | 307 | if (ast) |
306 | ast->token->convert(&(ast->content)); | 308 | { |
309 | convertAST2Lua(ast->left); | ||
310 | if (ast->token->convert) | ||
311 | ast->token->convert(&(ast->content)); | ||
312 | else if (ast->token->output) | ||
313 | ast->token->output(&(ast->content)); | ||
314 | convertAST2Lua(ast->right); | ||
315 | } | ||
307 | } | 316 | } |
308 | 317 | ||
309 | int yyerror(const char *msg) | 318 | int yyerror(const char *msg) |
@@ -366,16 +375,18 @@ int main(void) | |||
366 | // Run the parser on a test. | 375 | // Run the parser on a test. |
367 | if ((ast = newTree(test))) | 376 | if ((ast = newTree(test))) |
368 | { | 377 | { |
369 | LSL_Value result; | 378 | LSL_Value left, right; |
370 | 379 | ||
371 | result.content.integerValue = 0; | 380 | left.content.integerValue = 0; |
372 | result.type = LSL_INTEGER; | 381 | left.type = LSL_INTEGER; |
373 | evaluateAST(ast, &result); | 382 | right.content.integerValue = 0; |
383 | right.type = LSL_INTEGER; | ||
384 | evaluateAST(ast, &left, &right); | ||
374 | 385 | ||
375 | #ifdef LUASL_DEBUG | 386 | #ifdef LUASL_DEBUG |
376 | printf("\n"); | 387 | printf("\n"); |
377 | #endif | 388 | #endif |
378 | printf("Result of '%s' is %d\n", test, result.content.integerValue); | 389 | printf("Result of '%s' is %d %d\n", test, left.content.integerValue, right.content.integerValue); |
379 | outputAST(ast); | 390 | outputAST(ast); |
380 | printf("\n"); | 391 | printf("\n"); |
381 | convertAST2Lua(ast); | 392 | 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 | |||
190 | 190 | ||
191 | typedef void (*convertToken2Lua) (LSL_Leaf *content); | 191 | typedef void (*convertToken2Lua) (LSL_Leaf *content); |
192 | typedef void (*outputToken) (LSL_Leaf *content); | 192 | typedef void (*outputToken) (LSL_Leaf *content); |
193 | typedef void (*evaluateToken) (LSL_Leaf *content, LSL_Value *result); | 193 | typedef void (*evaluateToken) (LSL_Leaf *content, LSL_Value *left, LSL_Value *right); |
194 | 194 | ||
195 | typedef struct | 195 | typedef struct |
196 | { | 196 | { |