diff options
Diffstat (limited to '')
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.c | 271 |
1 files changed, 119 insertions, 152 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index 6c91012..e6dfa4c 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c | |||
@@ -4,9 +4,9 @@ | |||
4 | #include <stdio.h> | 4 | #include <stdio.h> |
5 | 5 | ||
6 | static void outputExpressionToken(LSL_Leaf *content); | 6 | static void outputExpressionToken(LSL_Leaf *content); |
7 | static LSL_Leaf *evaluateExpressionToken(LSL_Leaf *content, LSL_Type oldType, LSL_Leaf *old); | 7 | static void evaluateExpressionToken(LSL_Leaf *content, LSL_Value *result); |
8 | static void outputIntegerToken(LSL_Leaf *content); | 8 | static void outputIntegerToken(LSL_Leaf *content); |
9 | static LSL_Leaf *evaluateIntegerToken(LSL_Leaf *content, LSL_Type oldType, LSL_Leaf *old); | 9 | static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *result); |
10 | 10 | ||
11 | LSL_Token LSL_Tokens[] = | 11 | LSL_Token LSL_Tokens[] = |
12 | { | 12 | { |
@@ -96,6 +96,7 @@ LSL_Token LSL_Tokens[] = | |||
96 | // {LSL_FUNCTION, "", LSL_NONE, NULL, NULL, NULL}, | 96 | // {LSL_FUNCTION, "", LSL_NONE, NULL, NULL, NULL}, |
97 | // {LSL_STATE, "", LSL_NONE, NULL, NULL, NULL}, | 97 | // {LSL_STATE, "", LSL_NONE, NULL, NULL, NULL}, |
98 | // {LSL_SCRIPT, "", LSL_NONE, NULL, NULL, NULL}, | 98 | // {LSL_SCRIPT, "", LSL_NONE, NULL, NULL, NULL}, |
99 | // {LSL_UNKNOWN, "", LSL_NONE, NULL, NULL, NULL}, | ||
99 | {999999, NULL, LSL_NONE, NULL, NULL, NULL} | 100 | {999999, NULL, LSL_NONE, NULL, NULL, NULL} |
100 | }; | 101 | }; |
101 | 102 | ||
@@ -103,21 +104,39 @@ LSL_Token **tokens = NULL; | |||
103 | int lowestToken = 999999; | 104 | int lowestToken = 999999; |
104 | 105 | ||
105 | 106 | ||
107 | static LSL_Expression *newLSLExpression(LSL_Type type, LSL_Expression *left, LSL_Expression *right) | ||
108 | { | ||
109 | LSL_Expression *exp = malloc(sizeof(LSL_Expression)); | ||
110 | |||
111 | if (exp == NULL) return NULL; | ||
112 | |||
113 | exp->left = left; | ||
114 | exp->right = right; | ||
115 | exp->token = tokens[type - lowestToken]; | ||
116 | |||
117 | return exp; | ||
118 | } | ||
119 | |||
120 | static void burnLSLExpression(LSL_Expression *exp) | ||
121 | { | ||
122 | if (exp == NULL) return; | ||
123 | |||
124 | burnLSLExpression(exp->left); | ||
125 | burnLSLExpression(exp->right); | ||
126 | free(exp); | ||
127 | } | ||
128 | |||
106 | static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right) | 129 | static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right) |
107 | { | 130 | { |
108 | LSL_AST *ast = malloc(sizeof(LSL_AST)); | 131 | LSL_AST *ast = malloc(sizeof(LSL_AST)); |
109 | 132 | ||
110 | if (ast == NULL) return NULL; | 133 | if (ast == NULL) return NULL; |
111 | 134 | ||
112 | ast->type = type; | ||
113 | ast->left = left; | 135 | ast->left = left; |
114 | ast->right = right; | 136 | ast->right = right; |
115 | ast->line = -1; | 137 | ast->line = -1; |
116 | ast->character = -1; | 138 | ast->character = -1; |
117 | if (tokens) | 139 | ast->token = tokens[type - lowestToken]; |
118 | ast->token = tokens[type - lowestToken]; | ||
119 | else | ||
120 | ast->token = NULL; | ||
121 | 140 | ||
122 | return ast; | 141 | return ast; |
123 | } | 142 | } |
@@ -128,7 +147,9 @@ static void burnAST(LSL_AST *ast) | |||
128 | 147 | ||
129 | burnAST(ast->left); | 148 | burnAST(ast->left); |
130 | burnAST(ast->right); | 149 | burnAST(ast->right); |
131 | // TODO - burn the contents to. | 150 | // Burn the contents to. |
151 | if ((ast->token) && (ast->token->type == LSL_EXPRESSION)) | ||
152 | burnLSLExpression(ast->content.expressionValue); | ||
132 | free(ast); | 153 | free(ast); |
133 | } | 154 | } |
134 | 155 | ||
@@ -142,32 +163,6 @@ LSL_AST *addExpression(LSL_Expression *exp) | |||
142 | return ast; | 163 | return ast; |
143 | } | 164 | } |
144 | 165 | ||
145 | static LSL_Expression *newLSLExpression(LSL_Type type, LSL_Expression *left, LSL_Expression *right) | ||
146 | { | ||
147 | LSL_Expression *exp = malloc(sizeof(LSL_Expression)); | ||
148 | |||
149 | if (exp == NULL) return NULL; | ||
150 | |||
151 | exp->type = type; | ||
152 | exp->left = left; | ||
153 | exp->right = right; | ||
154 | if (tokens) | ||
155 | exp->token = tokens[type - lowestToken]; | ||
156 | else | ||
157 | exp->token = NULL; | ||
158 | |||
159 | return exp; | ||
160 | } | ||
161 | |||
162 | static void burnLSLExpression(LSL_Expression *exp) | ||
163 | { | ||
164 | if (exp == NULL) return; | ||
165 | |||
166 | burnLSLExpression(exp->left); | ||
167 | burnLSLExpression(exp->right); | ||
168 | free(exp); | ||
169 | } | ||
170 | |||
171 | LSL_Expression *addInteger(int value) | 166 | LSL_Expression *addInteger(int value) |
172 | { | 167 | { |
173 | LSL_Expression *exp = newLSLExpression(LSL_INTEGER, NULL, NULL); | 168 | LSL_Expression *exp = newLSLExpression(LSL_INTEGER, NULL, NULL); |
@@ -185,30 +180,55 @@ LSL_Expression *addOperation(LSL_Operation type, LSL_Expression *left, LSL_Expre | |||
185 | if (exp) | 180 | if (exp) |
186 | { | 181 | { |
187 | exp->content.operationValue = type; | 182 | exp->content.operationValue = type; |
188 | if (tokens) | 183 | exp->token = tokens[type - lowestToken]; |
189 | exp->token = tokens[type - lowestToken]; | ||
190 | else | ||
191 | exp->token = NULL; | ||
192 | } | 184 | } |
193 | 185 | ||
194 | return exp; | 186 | return exp; |
195 | } | 187 | } |
196 | 188 | ||
197 | static int evaluateExpression(LSL_Expression *exp, int old) | 189 | static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *result) |
198 | { | 190 | { |
199 | if (NULL == exp) | 191 | if (content) |
200 | return old; | 192 | { |
201 | #ifdef LUASL_DEBUG | 193 | #ifdef LUASL_DEBUG |
202 | printf(" %s ", exp->token->token); | 194 | printf(" %d ", content->integerValue); |
203 | #endif | 195 | #endif |
196 | result->content.integerValue = content->integerValue; | ||
197 | result->type = LSL_INTEGER; | ||
198 | } | ||
199 | } | ||
200 | |||
201 | static void evaluateExpression(LSL_Expression *exp, LSL_Value *result) | ||
202 | { | ||
203 | LSL_Value left, right; | ||
204 | 204 | ||
205 | if (LSL_INTEGER == exp->type) | 205 | if ((NULL == exp) || (NULL == result)) |
206 | return; | ||
207 | |||
208 | if (LSL_INTEGER == exp->token->type) | ||
209 | { | ||
210 | evaluateIntegerToken(&(exp->content), result); | ||
211 | return; | ||
212 | } | ||
213 | else if (LSL_LEFT2RIGHT & exp->token->flags) | ||
214 | { | ||
215 | evaluateExpression(exp->left, &left); | ||
216 | if (!(LSL_UNARY & exp->token->flags)) | ||
217 | evaluateExpression(exp->right, &right); | ||
218 | } | ||
219 | else if (LSL_RIGHT2LEFT & exp->token->flags) | ||
220 | { | ||
221 | evaluateExpression(exp->right, &right); | ||
222 | if (!(LSL_UNARY & exp->token->flags)) | ||
223 | evaluateExpression(exp->left, &left); | ||
224 | } | ||
225 | else | ||
206 | { | 226 | { |
227 | } | ||
228 | |||
207 | #ifdef LUASL_DEBUG | 229 | #ifdef LUASL_DEBUG |
208 | printf("%d", exp->content.integerValue); | 230 | printf(" %s ", exp->token->token); |
209 | #endif | 231 | #endif |
210 | return exp->content.integerValue; | ||
211 | } | ||
212 | 232 | ||
213 | switch (exp->content.operationValue) | 233 | switch (exp->content.operationValue) |
214 | { | 234 | { |
@@ -235,113 +255,49 @@ static int evaluateExpression(LSL_Expression *exp, int old) | |||
235 | case LSL_TYPECAST : | 255 | case LSL_TYPECAST : |
236 | break; | 256 | break; |
237 | #endif | 257 | #endif |
238 | case LSL_BIT_NOT : return ~ evaluateExpression(exp->right, old); | 258 | case LSL_BIT_NOT : result->content.integerValue = ~ right.content.integerValue; break; |
239 | case LSL_BOOL_NOT : return ! evaluateExpression(exp->right, old); | 259 | case LSL_BOOL_NOT : result->content.integerValue = ! right.content.integerValue; break; |
240 | case LSL_NEGATION : return 0 - evaluateExpression(exp->right, old); | 260 | case LSL_NEGATION : result->content.integerValue = 0 - right.content.integerValue; break; |
241 | case LSL_DIVIDE : return evaluateExpression(exp->left, old) / evaluateExpression(exp->right, old); | 261 | case LSL_DIVIDE : result->content.integerValue = left.content.integerValue / right.content.integerValue; break; |
242 | #ifdef LUASL_USE_ENUM | 262 | case LSL_MODULO : result->content.integerValue = left.content.integerValue % right.content.integerValue; break; |
243 | case LSL_MODULO : return evaluateExpression(exp->left, old) % evaluateExpression(exp->right, old); | 263 | case LSL_MULTIPLY : result->content.integerValue = left.content.integerValue * right.content.integerValue; break; |
244 | #endif | ||
245 | case LSL_MULTIPLY : return evaluateExpression(exp->left, old) * evaluateExpression(exp->right, old); | ||
246 | #ifdef LUASL_USE_ENUM | 264 | #ifdef LUASL_USE_ENUM |
247 | case LSL_DOT_PRODUCT : break; | 265 | case LSL_DOT_PRODUCT : break; |
248 | case LSL_CROSS_PRODUCT : break; | 266 | case LSL_CROSS_PRODUCT : break; |
249 | #endif | 267 | #endif |
250 | case LSL_SUBTRACT : return evaluateExpression(exp->left, old) - evaluateExpression(exp->right, old); | 268 | case LSL_SUBTRACT : result->content.integerValue = left.content.integerValue - right.content.integerValue; break; |
251 | case LSL_ADD : return evaluateExpression(exp->left, old) + evaluateExpression(exp->right, old); | 269 | case LSL_ADD : result->content.integerValue = left.content.integerValue + right.content.integerValue; break; |
252 | #ifdef LUASL_USE_ENUM | 270 | #ifdef LUASL_USE_ENUM |
253 | case LSL_CONCATENATE : break; | 271 | case LSL_CONCATENATE : break; |
254 | #endif | 272 | #endif |
255 | case LSL_LEFT_SHIFT : return evaluateExpression(exp->left, old) << evaluateExpression(exp->right, old); | 273 | case LSL_LEFT_SHIFT : result->content.integerValue = left.content.integerValue << right.content.integerValue; break; |
256 | case LSL_RIGHT_SHIFT : return evaluateExpression(exp->left, old) >> evaluateExpression(exp->right, old); | 274 | case LSL_RIGHT_SHIFT : result->content.integerValue = left.content.integerValue >> right.content.integerValue; break; |
257 | case LSL_LESS_THAN : return evaluateExpression(exp->left, old) < evaluateExpression(exp->right, old); | 275 | case LSL_LESS_THAN : result->content.integerValue = left.content.integerValue < right.content.integerValue; break; |
258 | case LSL_GREATER_THAN : return evaluateExpression(exp->left, old) > evaluateExpression(exp->right, old); | 276 | case LSL_GREATER_THAN : result->content.integerValue = left.content.integerValue > right.content.integerValue; break; |
259 | case LSL_LESS_EQUAL : return evaluateExpression(exp->left, old) <= evaluateExpression(exp->right, old); | 277 | case LSL_LESS_EQUAL : result->content.integerValue = left.content.integerValue <= right.content.integerValue; break; |
260 | case LSL_GREATER_EQUAL : return evaluateExpression(exp->left, old) >= evaluateExpression(exp->right, old); | 278 | case LSL_GREATER_EQUAL : result->content.integerValue = left.content.integerValue >= right.content.integerValue; break; |
261 | case LSL_EQUAL : return evaluateExpression(exp->left, old) == evaluateExpression(exp->right, old); | 279 | case LSL_EQUAL : result->content.integerValue = left.content.integerValue == right.content.integerValue; break; |
262 | case LSL_NOT_EQUAL : return evaluateExpression(exp->left, old) != evaluateExpression(exp->right, old); | 280 | case LSL_NOT_EQUAL : result->content.integerValue = left.content.integerValue != right.content.integerValue; break; |
263 | case LSL_BIT_AND : return evaluateExpression(exp->left, old) & evaluateExpression(exp->right, old); | 281 | case LSL_BIT_AND : result->content.integerValue = left.content.integerValue & right.content.integerValue; break; |
264 | case LSL_BIT_XOR : return evaluateExpression(exp->left, old) ^ evaluateExpression(exp->right, old); | 282 | case LSL_BIT_XOR : result->content.integerValue = left.content.integerValue ^ right.content.integerValue; break; |
265 | case LSL_BIT_OR : return evaluateExpression(exp->left, old) | evaluateExpression(exp->right, old); | 283 | case LSL_BIT_OR : result->content.integerValue = left.content.integerValue | right.content.integerValue; break; |
266 | case LSL_BOOL_OR : return evaluateExpression(exp->left, old) || evaluateExpression(exp->right, old); | 284 | case LSL_BOOL_OR : result->content.integerValue = left.content.integerValue || right.content.integerValue; break; |
267 | case LSL_BOOL_AND : return evaluateExpression(exp->left, old) && evaluateExpression(exp->right, old); | 285 | case LSL_BOOL_AND : result->content.integerValue = left.content.integerValue && right.content.integerValue; break; |
268 | } | 286 | } |
269 | 287 | ||
270 | return old; | 288 | return; |
271 | } | 289 | } |
272 | 290 | ||
273 | static LSL_Leaf *evaluateExpressionToken(LSL_Leaf *content, LSL_Type oldType, LSL_Leaf *old) | 291 | static void evaluateExpressionToken(LSL_Leaf *content, LSL_Value *result) |
274 | { | ||
275 | // if (content) | ||
276 | // return evaluateExpression(content->expressionValue, old->integerValue); | ||
277 | return old; | ||
278 | } | ||
279 | |||
280 | static LSL_Leaf *evaluateIntegerToken(LSL_Leaf *content, LSL_Type oldType, LSL_Leaf *old) | ||
281 | { | 292 | { |
282 | if (content) | 293 | if (content) |
283 | { | 294 | evaluateExpression(content->expressionValue, result); |
284 | #ifdef LUASL_DEBUG | ||
285 | printf("%d", content->integerValue); | ||
286 | #endif | ||
287 | return content; | ||
288 | } | ||
289 | return old; | ||
290 | } | 295 | } |
291 | 296 | ||
292 | static int evaluateAST(LSL_AST *ast, int old) | 297 | static void evaluateAST(LSL_AST *ast, LSL_Value *result) |
293 | { | 298 | { |
294 | // if ((ast) && (ast->token) && (ast->token->evaluate)) | 299 | if ((ast) && (ast->token) && (ast->token->evaluate)) |
295 | // return ast->token->evaluate(&(ast->content), oldType, old); | 300 | ast->token->evaluate(&(ast->content), result); |
296 | |||
297 | if (NULL == ast) | ||
298 | return old; | ||
299 | switch(ast->type) | ||
300 | { | ||
301 | #ifdef LUASL_USE_ENUM | ||
302 | case LSL_COMMENT : | ||
303 | case LSL_TYPE : | ||
304 | case LSL_NAME : | ||
305 | case LSL_IDENTIFIER : | ||
306 | break; | ||
307 | case LSL_FLOAT : return (int) ast->content.floatValue; | ||
308 | #endif | ||
309 | case LSL_INTEGER : | ||
310 | #ifdef LUASL_DEBUG | ||
311 | printf("%d", ast->content.integerValue); | ||
312 | #endif | ||
313 | return ast->content.integerValue; | ||
314 | #ifdef LUASL_USE_ENUM | ||
315 | case LSL_STRING : | ||
316 | case LSL_KEY : | ||
317 | case LSL_VECTOR : | ||
318 | case LSL_ROTATION : | ||
319 | case LSL_LIST : | ||
320 | case LSL_LABEL : | ||
321 | break; | ||
322 | #endif | ||
323 | case LSL_EXPRESSION : return evaluateExpression(ast->content.expressionValue, old); | ||
324 | #ifdef LUASL_USE_ENUM | ||
325 | case LSL_DO : | ||
326 | case LSL_FOR : | ||
327 | case LSL_IF : | ||
328 | case LSL_ELSE : | ||
329 | case LSL_ELSEIF : | ||
330 | case LSL_JUMP : | ||
331 | case LSL_STATE_CHANGE : | ||
332 | case LSL_WHILE : | ||
333 | case LSL_RETURN : | ||
334 | case LSL_STATEMENT : | ||
335 | case LSL_BLOCK : | ||
336 | case LSL_PARAMETER : | ||
337 | case LSL_FUNCTION : | ||
338 | case LSL_STATE : | ||
339 | case LSL_SCRIPT : | ||
340 | break; | ||
341 | #endif | ||
342 | } | ||
343 | |||
344 | return old; | ||
345 | } | 301 | } |
346 | 302 | ||
347 | static void outputExpression(LSL_Expression *exp) | 303 | static void outputExpression(LSL_Expression *exp) |
@@ -349,7 +305,7 @@ static void outputExpression(LSL_Expression *exp) | |||
349 | if (NULL == exp) | 305 | if (NULL == exp) |
350 | return; | 306 | return; |
351 | 307 | ||
352 | if (LSL_INTEGER == exp->type) | 308 | if (LSL_INTEGER == exp->token->type) |
353 | { | 309 | { |
354 | printf("%d", exp->content.integerValue); | 310 | printf("%d", exp->content.integerValue); |
355 | } | 311 | } |
@@ -420,8 +376,6 @@ static LSL_AST *newTree(const char *expr) | |||
420 | 376 | ||
421 | int main(void) | 377 | int main(void) |
422 | { | 378 | { |
423 | const char test[] = " 4 + 2 * 10 + 3 * ( 5 + 1 )"; | ||
424 | LSL_AST *ast; | ||
425 | int i; | 379 | int i; |
426 | 380 | ||
427 | // Figure out what numbers yacc gave to our tokens. | 381 | // Figure out what numbers yacc gave to our tokens. |
@@ -430,9 +384,12 @@ int main(void) | |||
430 | if (lowestToken > LSL_Tokens[i].type) | 384 | if (lowestToken > LSL_Tokens[i].type) |
431 | lowestToken = LSL_Tokens[i].type; | 385 | lowestToken = LSL_Tokens[i].type; |
432 | } | 386 | } |
433 | tokens = malloc(sizeof(LSL_Token *) * (i + 1)); | 387 | tokens = calloc(i + 1, sizeof(LSL_Token *)); |
434 | if (tokens) | 388 | if (tokens) |
435 | { | 389 | { |
390 | const char test[] = " 4 + 2 * 10 + 3 * ( 5 + 1 )"; | ||
391 | LSL_AST *ast; | ||
392 | |||
436 | // Sort the token table. | 393 | // Sort the token table. |
437 | for (i = 0; LSL_Tokens[i].token != NULL; i++) | 394 | for (i = 0; LSL_Tokens[i].token != NULL; i++) |
438 | { | 395 | { |
@@ -440,21 +397,31 @@ int main(void) | |||
440 | 397 | ||
441 | tokens[j] = &(LSL_Tokens[i]); | 398 | tokens[j] = &(LSL_Tokens[i]); |
442 | } | 399 | } |
443 | } | ||
444 | 400 | ||
445 | if ((ast = newTree(test))) | 401 | // Run the parser on a test. |
446 | { | 402 | if ((ast = newTree(test))) |
447 | int result = evaluateAST(ast, 0); | 403 | { |
404 | LSL_Value result; | ||
405 | |||
406 | result.content.integerValue = 0; | ||
407 | result.type = LSL_INTEGER; | ||
408 | evaluateAST(ast, &result); | ||
448 | 409 | ||
449 | #ifdef LUASL_DEBUG | 410 | #ifdef LUASL_DEBUG |
450 | printf("\n"); | 411 | printf("\n"); |
451 | #endif | 412 | #endif |
452 | printf("Result of '%s' is %d\n", test, result); | 413 | printf("Result of '%s' is %d\n", test, result.content.integerValue); |
453 | outputAST(ast); | 414 | outputAST(ast); |
454 | printf("\n"); | 415 | printf("\n"); |
455 | convertAST2Lua(ast); | 416 | convertAST2Lua(ast); |
456 | printf("\n"); | 417 | printf("\n"); |
457 | burnAST(ast); | 418 | burnAST(ast); |
419 | } | ||
420 | } | ||
421 | else | ||
422 | { | ||
423 | fprintf(stderr, "No memory for tokens!"); | ||
424 | return 1; | ||
458 | } | 425 | } |
459 | 426 | ||
460 | return 0; | 427 | return 0; |