diff options
author | David Walter Seikel | 2012-01-07 04:28:24 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-07 04:28:24 +1000 |
commit | 1603cadc4b3345e01f866f6f94bf69df078b4808 (patch) | |
tree | dd18f3e7c6d2a1b4e73acd5bba68ce457b2dcda8 /LuaSL/src | |
parent | No need to actually compile the LSL flex and yacc sources, they are just refe... (diff) | |
download | SledjHamr-1603cadc4b3345e01f866f6f94bf69df078b4808.zip SledjHamr-1603cadc4b3345e01f866f6f94bf69df078b4808.tar.gz SledjHamr-1603cadc4b3345e01f866f6f94bf69df078b4808.tar.bz2 SledjHamr-1603cadc4b3345e01f866f6f94bf69df078b4808.tar.xz |
Make the parser more generic with function pointers and a big arse table.
Diffstat (limited to '')
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.c | 275 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.h | 136 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_yaccer.y | 2 |
3 files changed, 209 insertions, 204 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index 8462112..6c91012 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c | |||
@@ -1,10 +1,107 @@ | |||
1 | 1 | ||
2 | #define LSL_Keywords_define | ||
3 | #define LSL_Tokens_define | ||
4 | #include "LuaSL_LSL_tree.h" | 2 | #include "LuaSL_LSL_tree.h" |
5 | #include <stdlib.h> | 3 | #include <stdlib.h> |
6 | #include <stdio.h> | 4 | #include <stdio.h> |
7 | 5 | ||
6 | static void outputExpressionToken(LSL_Leaf *content); | ||
7 | static LSL_Leaf *evaluateExpressionToken(LSL_Leaf *content, LSL_Type oldType, LSL_Leaf *old); | ||
8 | static void outputIntegerToken(LSL_Leaf *content); | ||
9 | static LSL_Leaf *evaluateIntegerToken(LSL_Leaf *content, LSL_Type oldType, LSL_Leaf *old); | ||
10 | |||
11 | LSL_Token LSL_Tokens[] = | ||
12 | { | ||
13 | // Start with expression operators. | ||
14 | // In order of precedence, high to low. | ||
15 | // Left to right, unless oterwise stated. | ||
16 | // According to http://wiki.secondlife.com/wiki/Category:LSL_Operators | ||
17 | |||
18 | // {LSL_COMMA, ",", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
19 | // {LSL_INCREMENT_PRE, "++", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, NULL}, | ||
20 | // {LSL_INCREMENT_POST, "++", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, NULL}, | ||
21 | // {LSL_DECREMENT_PRE, "--", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, NULL}, | ||
22 | // {LSL_DECREMENT_POST, "--", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, NULL}, | ||
23 | // {LSL_DOT, ".", LSL_RIGHT2LEFT, NULL, NULL, NULL}, | ||
24 | // {LSL_ASSIGNMENT_PLAIN, "=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, NULL}, | ||
25 | // {LSL_ASSIGNMENT_DIVIDE, "/=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, NULL}, | ||
26 | // {LSL_ASSIGNMENT_MODULO, "%=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, NULL}, | ||
27 | // {LSL_ASSIGNMENT_MULTIPLY, "*=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, NULL}, | ||
28 | // {LSL_ASSIGNMENT_SUBTRACT, "-=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, NULL}, | ||
29 | // {LSL_ASSIGNMENT_ADD, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, NULL}, | ||
30 | // {LSL_ASSIGNMENT_CONCATENATE, "+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT, NULL, NULL, NULL}, | ||
31 | {LSL_PARENTHESIS_OPEN, "(", LSL_INNER2OUTER, NULL, NULL, NULL}, | ||
32 | {LSL_PARENTHESIS_CLOSE, ")", LSL_INNER2OUTER, NULL, NULL, NULL}, | ||
33 | // {LSL_BRACKET_OPEN, "[", LSL_INNER2OUTER | LSL_CREATION, NULL, NULL, NULL}, | ||
34 | // {LSL_BRACKET_CLOSE, "]", LSL_INNER2OUTER | LSL_CREATION, NULL, NULL, NULL}, | ||
35 | // {LSL_ANGLE_OPEN, "<", LSL_LEFT2RIGHT | LSL_CREATION, NULL, NULL, NULL}, | ||
36 | // {LSL_ANGLE_CLOSE, ">", LSL_LEFT2RIGHT | LSL_CREATION, NULL, NULL, NULL}, | ||
37 | // {LSL_TYPECAST_OPEN, "(", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, NULL}, | ||
38 | // {LSL_TYPECAST_CLOSE, ")", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, NULL}, | ||
39 | {LSL_BIT_NOT, "~", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, NULL}, | ||
40 | {LSL_BOOL_NOT, "!", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, NULL}, | ||
41 | {LSL_NEGATION, "-", LSL_RIGHT2LEFT | LSL_UNARY, NULL, NULL, NULL}, | ||
42 | {LSL_DIVIDE, "/", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
43 | {LSL_MODULO, "%", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
44 | {LSL_MULTIPLY, "*", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
45 | // {LSL_DOT_PRODUCT, "*", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
46 | // {LSL_CROSS_PRODUCT, "%", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
47 | {LSL_SUBTRACT, "-", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
48 | {LSL_ADD, "+", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
49 | // {LSL_CONCATENATE, "+", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
50 | {LSL_LEFT_SHIFT, "<<", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
51 | {LSL_RIGHT_SHIFT, ">>", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
52 | // QUIRK - Conditionals are executed right to left. Or left to right, depending on who you ask. lol | ||
53 | {LSL_LESS_THAN, "<", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
54 | {LSL_GREATER_THAN, ">", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
55 | {LSL_LESS_EQUAL, "<=", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
56 | {LSL_GREATER_EQUAL, ">=", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
57 | {LSL_EQUAL, "==", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
58 | {LSL_NOT_EQUAL, "!=", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
59 | {LSL_BIT_AND, "&", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
60 | {LSL_BIT_XOR, "^", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
61 | {LSL_BIT_OR, "|", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
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. | ||
64 | {LSL_BOOL_OR, "||", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
65 | {LSL_BOOL_AND, "&&", LSL_LEFT2RIGHT, NULL, NULL, NULL}, | ||
66 | |||
67 | // Then the rest of the syntax tokens. | ||
68 | |||
69 | // {LSL_COMMENT_LINE, "//", LSL_NONE, NULL, NULL, NULL}, | ||
70 | // {LSL_COMMENT, "/*", LSL_NONE, NULL, NULL, NULL}, | ||
71 | // {LSL_TYPE, "", LSL_NONE, NULL, NULL, NULL}, | ||
72 | // {LSL_NAME, "", LSL_NONE, NULL, NULL, NULL}, | ||
73 | // {LSL_IDENTIFIER, "", LSL_NONE, NULL, NULL, NULL}, | ||
74 | // {LSL_FLOAT, "float", LSL_NONE, NULL, NULL, NULL}, | ||
75 | {LSL_INTEGER, "integer", LSL_NONE, outputIntegerToken, NULL, evaluateIntegerToken}, | ||
76 | // {LSL_STRING, "string", LSL_NONE, NULL, NULL, NULL}, | ||
77 | // {LSL_KEY, "key", LSL_NONE, NULL, NULL, NULL}, | ||
78 | // {LSL_VECTOR, "vector", LSL_NONE, NULL, NULL, NULL}, | ||
79 | // {LSL_ROTATION, "rotation", LSL_NONE, NULL, NULL, NULL}, | ||
80 | // {LSL_LIST, "list", LSL_NONE, NULL, NULL, NULL}, | ||
81 | // {LSL_LABEL, "@", LSL_NONE, NULL, NULL, NULL}, | ||
82 | {LSL_EXPRESSION, "", LSL_NONE, outputExpressionToken, NULL, evaluateExpressionToken}, | ||
83 | // {LSL_DO, "do", LSL_NONE, NULL, NULL, NULL}, | ||
84 | // {LSL_FOR, "for", LSL_NONE, NULL, NULL, NULL}, | ||
85 | // {LSL_IF, "if", LSL_NONE, NULL, NULL, NULL}, | ||
86 | // {LSL_ELSE, "else", LSL_NONE, NULL, NULL, NULL}, | ||
87 | // {LSL_ELSE_IF, "else if", LSL_NONE, NULL, NULL, NULL}, | ||
88 | // {LSL_JUMP, "jump", LSL_NONE, NULL, NULL, NULL}, | ||
89 | // {LSL_STATE_CHANGE, "state", LSL_NONE, NULL, NULL, NULL}, | ||
90 | // {LSL_WHILE, "while", LSL_NONE, NULL, NULL, NULL}, | ||
91 | // {LSL_RETURN, "return", LSL_NONE, NULL, NULL, NULL}, | ||
92 | // {LSL_STATEMENT, ";", LSL_NONE, NULL, NULL, NULL}, | ||
93 | // {LSL_BLOCK_OPEN, "{", LSL_NONE, NULL, NULL, NULL}, | ||
94 | // {LSL_BLOCK_CLOSE, "}", LSL_NONE, NULL, NULL, NULL}, | ||
95 | // {LSL_PARAMETER, "", LSL_NONE, NULL, NULL, NULL}, | ||
96 | // {LSL_FUNCTION, "", LSL_NONE, NULL, NULL, NULL}, | ||
97 | // {LSL_STATE, "", LSL_NONE, NULL, NULL, NULL}, | ||
98 | // {LSL_SCRIPT, "", LSL_NONE, NULL, NULL, NULL}, | ||
99 | {999999, NULL, LSL_NONE, NULL, NULL, NULL} | ||
100 | }; | ||
101 | |||
102 | LSL_Token **tokens = NULL; | ||
103 | int lowestToken = 999999; | ||
104 | |||
8 | 105 | ||
9 | static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right) | 106 | static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right) |
10 | { | 107 | { |
@@ -17,16 +114,21 @@ static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right) | |||
17 | ast->right = right; | 114 | ast->right = right; |
18 | ast->line = -1; | 115 | ast->line = -1; |
19 | ast->character = -1; | 116 | ast->character = -1; |
117 | if (tokens) | ||
118 | ast->token = tokens[type - lowestToken]; | ||
119 | else | ||
120 | ast->token = NULL; | ||
20 | 121 | ||
21 | return ast; | 122 | return ast; |
22 | } | 123 | } |
23 | 124 | ||
24 | void burnAST(LSL_AST *ast) | 125 | static void burnAST(LSL_AST *ast) |
25 | { | 126 | { |
26 | if (ast == NULL) return; | 127 | if (ast == NULL) return; |
27 | 128 | ||
28 | burnAST(ast->left); | 129 | burnAST(ast->left); |
29 | burnAST(ast->right); | 130 | burnAST(ast->right); |
131 | // TODO - burn the contents to. | ||
30 | free(ast); | 132 | free(ast); |
31 | } | 133 | } |
32 | 134 | ||
@@ -49,11 +151,15 @@ static LSL_Expression *newLSLExpression(LSL_Type type, LSL_Expression *left, LSL | |||
49 | exp->type = type; | 151 | exp->type = type; |
50 | exp->left = left; | 152 | exp->left = left; |
51 | exp->right = right; | 153 | exp->right = right; |
154 | if (tokens) | ||
155 | exp->token = tokens[type - lowestToken]; | ||
156 | else | ||
157 | exp->token = NULL; | ||
52 | 158 | ||
53 | return exp; | 159 | return exp; |
54 | } | 160 | } |
55 | 161 | ||
56 | void burnLSLExpression(LSL_Expression *exp) | 162 | static void burnLSLExpression(LSL_Expression *exp) |
57 | { | 163 | { |
58 | if (exp == NULL) return; | 164 | if (exp == NULL) return; |
59 | 165 | ||
@@ -77,21 +183,23 @@ LSL_Expression *addOperation(LSL_Operation type, LSL_Expression *left, LSL_Expre | |||
77 | LSL_Expression *exp = newLSLExpression(LSL_EXPRESSION, left, right); | 183 | LSL_Expression *exp = newLSLExpression(LSL_EXPRESSION, left, right); |
78 | 184 | ||
79 | if (exp) | 185 | if (exp) |
186 | { | ||
80 | exp->content.operationValue = type; | 187 | exp->content.operationValue = type; |
188 | if (tokens) | ||
189 | exp->token = tokens[type - lowestToken]; | ||
190 | else | ||
191 | exp->token = NULL; | ||
192 | } | ||
81 | 193 | ||
82 | return exp; | 194 | return exp; |
83 | } | 195 | } |
84 | 196 | ||
85 | int evaluateExpression(LSL_Expression *exp, int old) | 197 | static int evaluateExpression(LSL_Expression *exp, int old) |
86 | { | 198 | { |
87 | if (NULL == exp) | 199 | if (NULL == exp) |
88 | return old; | 200 | return old; |
89 | #ifdef LUASL_DEBUG | 201 | #ifdef LUASL_DEBUG |
90 | #ifdef LUASL_USE_ENUM | 202 | printf(" %s ", exp->token->token); |
91 | printf(" %s ", LSL_Tokens[exp->content.operationValue - LSL_COMMA].token); | ||
92 | #else | ||
93 | printf(" # "); | ||
94 | #endif | ||
95 | #endif | 203 | #endif |
96 | 204 | ||
97 | if (LSL_INTEGER == exp->type) | 205 | if (LSL_INTEGER == exp->type) |
@@ -162,8 +270,30 @@ int evaluateExpression(LSL_Expression *exp, int old) | |||
162 | return old; | 270 | return old; |
163 | } | 271 | } |
164 | 272 | ||
165 | int evaluateAST(LSL_AST *ast, int old) | 273 | static LSL_Leaf *evaluateExpressionToken(LSL_Leaf *content, LSL_Type oldType, LSL_Leaf *old) |
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 | { | ||
282 | if (content) | ||
283 | { | ||
284 | #ifdef LUASL_DEBUG | ||
285 | printf("%d", content->integerValue); | ||
286 | #endif | ||
287 | return content; | ||
288 | } | ||
289 | return old; | ||
290 | } | ||
291 | |||
292 | static int evaluateAST(LSL_AST *ast, int old) | ||
166 | { | 293 | { |
294 | // if ((ast) && (ast->token) && (ast->token->evaluate)) | ||
295 | // return ast->token->evaluate(&(ast->content), oldType, old); | ||
296 | |||
167 | if (NULL == ast) | 297 | if (NULL == ast) |
168 | return old; | 298 | return old; |
169 | switch(ast->type) | 299 | switch(ast->type) |
@@ -214,7 +344,7 @@ int evaluateAST(LSL_AST *ast, int old) | |||
214 | return old; | 344 | return old; |
215 | } | 345 | } |
216 | 346 | ||
217 | void outputExpression(LSL_Expression *exp) | 347 | static void outputExpression(LSL_Expression *exp) |
218 | { | 348 | { |
219 | if (NULL == exp) | 349 | if (NULL == exp) |
220 | return; | 350 | return; |
@@ -226,93 +356,33 @@ void outputExpression(LSL_Expression *exp) | |||
226 | else | 356 | else |
227 | { | 357 | { |
228 | outputExpression(exp->left); | 358 | outputExpression(exp->left); |
229 | #ifdef LUASL_USE_ENUM | 359 | printf(" %s ", exp->token->token); |
230 | printf(" %s ", LSL_Tokens[exp->content.operationValue - LSL_COMMA].token); | ||
231 | #else | ||
232 | printf(" # "); | ||
233 | #endif | ||
234 | outputExpression(exp->right); | 360 | outputExpression(exp->right); |
235 | } | 361 | } |
236 | } | 362 | } |
237 | 363 | ||
238 | void outputAST(LSL_AST *ast) | 364 | static void outputExpressionToken(LSL_Leaf *content) |
239 | { | 365 | { |
240 | if (NULL == ast) | 366 | if (content) |
241 | return; | 367 | outputExpression(content->expressionValue); |
242 | switch(ast->type) | ||
243 | { | ||
244 | #ifdef LUASL_USE_ENUM | ||
245 | case LSL_COMMENT : return; | ||
246 | case LSL_TYPE : return; | ||
247 | case LSL_NAME : return; | ||
248 | case LSL_IDENTIFIER : return; | ||
249 | case LSL_FLOAT : printf("%f", ast->content.floatValue); break; | ||
250 | case LSL_INTEGER : printf("%d", ast->content.integerValue); break; | ||
251 | case LSL_STRING : return; | ||
252 | case LSL_KEY : return; | ||
253 | case LSL_VECTOR : return; | ||
254 | case LSL_ROTATION : return; | ||
255 | case LSL_LIST : return; | ||
256 | case LSL_LABEL : return; | ||
257 | #endif | ||
258 | case LSL_EXPRESSION : outputExpression(ast->content.expressionValue); break; | ||
259 | #ifdef LUASL_USE_ENUM | ||
260 | case LSL_DO : return; | ||
261 | case LSL_FOR : return; | ||
262 | case LSL_IF : return; | ||
263 | case LSL_ELSE : return; | ||
264 | case LSL_ELSEIF : return; | ||
265 | case LSL_JUMP : return; | ||
266 | case LSL_STATE_CHANGE : return; | ||
267 | case LSL_WHILE : return; | ||
268 | case LSL_RETURN : return; | ||
269 | case LSL_STATEMENT : return; | ||
270 | case LSL_BLOCK : return; | ||
271 | case LSL_PARAMETER : return; | ||
272 | case LSL_FUNCTION : return; | ||
273 | case LSL_STATE : return; | ||
274 | case LSL_SCRIPT : return; | ||
275 | #endif | ||
276 | } | ||
277 | } | 368 | } |
278 | 369 | ||
279 | void convertAST2Lua(LSL_AST *ast) | 370 | static void outputIntegerToken(LSL_Leaf *content) |
280 | { | 371 | { |
281 | #ifdef LUASL_USE_ENUM | 372 | if (content) |
282 | if (NULL == ast) | 373 | printf("%d", content->integerValue); |
283 | return; | 374 | } |
284 | switch(ast->type) | 375 | |
285 | { | 376 | static void outputAST(LSL_AST *ast) |
286 | case LSL_COMMENT : return; | 377 | { |
287 | case LSL_TYPE : return; | 378 | if ((ast) && (ast->token) && (ast->token->output)) |
288 | case LSL_NAME : return; | 379 | ast->token->output(&(ast->content)); |
289 | case LSL_IDENTIFIER : return; | 380 | } |
290 | case LSL_FLOAT : return; | 381 | |
291 | case LSL_INTEGER : return; | 382 | static void convertAST2Lua(LSL_AST *ast) |
292 | case LSL_STRING : return; | 383 | { |
293 | case LSL_KEY : return; | 384 | if ((ast) && (ast->token) && (ast->token->convert)) |
294 | case LSL_VECTOR : return; | 385 | ast->token->convert(&(ast->content)); |
295 | case LSL_ROTATION : return; | ||
296 | case LSL_LIST : return; | ||
297 | case LSL_LABEL : return; | ||
298 | case LSL_EXPRESSION : return; | ||
299 | case LSL_DO : return; | ||
300 | case LSL_FOR : return; | ||
301 | case LSL_IF : return; | ||
302 | case LSL_ELSE : return; | ||
303 | case LSL_ELSEIF : return; | ||
304 | case LSL_JUMP : return; | ||
305 | case LSL_STATE_CHANGE : return; | ||
306 | case LSL_WHILE : return; | ||
307 | case LSL_RETURN : return; | ||
308 | case LSL_STATEMENT : return; | ||
309 | case LSL_BLOCK : return; | ||
310 | case LSL_PARAMETER : return; | ||
311 | case LSL_FUNCTION : return; | ||
312 | case LSL_STATE : return; | ||
313 | case LSL_SCRIPT : return; | ||
314 | } | ||
315 | #endif | ||
316 | } | 386 | } |
317 | 387 | ||
318 | int yyerror(const char *msg) | 388 | int yyerror(const char *msg) |
@@ -321,7 +391,7 @@ int yyerror(const char *msg) | |||
321 | return 0; | 391 | return 0; |
322 | } | 392 | } |
323 | 393 | ||
324 | LSL_AST *newTree(const char *expr) | 394 | static LSL_AST *newTree(const char *expr) |
325 | { | 395 | { |
326 | LuaSL_yyparseParam param; | 396 | LuaSL_yyparseParam param; |
327 | YY_BUFFER_STATE state; | 397 | YY_BUFFER_STATE state; |
@@ -352,6 +422,25 @@ int main(void) | |||
352 | { | 422 | { |
353 | const char test[] = " 4 + 2 * 10 + 3 * ( 5 + 1 )"; | 423 | const char test[] = " 4 + 2 * 10 + 3 * ( 5 + 1 )"; |
354 | LSL_AST *ast; | 424 | LSL_AST *ast; |
425 | int i; | ||
426 | |||
427 | // Figure out what numbers yacc gave to our tokens. | ||
428 | for (i = 0; LSL_Tokens[i].token != NULL; i++) | ||
429 | { | ||
430 | if (lowestToken > LSL_Tokens[i].type) | ||
431 | lowestToken = LSL_Tokens[i].type; | ||
432 | } | ||
433 | tokens = malloc(sizeof(LSL_Token *) * (i + 1)); | ||
434 | if (tokens) | ||
435 | { | ||
436 | // Sort the token table. | ||
437 | for (i = 0; LSL_Tokens[i].token != NULL; i++) | ||
438 | { | ||
439 | int j = LSL_Tokens[i].type - lowestToken; | ||
440 | |||
441 | tokens[j] = &(LSL_Tokens[i]); | ||
442 | } | ||
443 | } | ||
355 | 444 | ||
356 | if ((ast = newTree(test))) | 445 | if ((ast = newTree(test))) |
357 | { | 446 | { |
@@ -363,6 +452,8 @@ int main(void) | |||
363 | printf("Result of '%s' is %d\n", test, result); | 452 | printf("Result of '%s' is %d\n", test, result); |
364 | outputAST(ast); | 453 | outputAST(ast); |
365 | printf("\n"); | 454 | printf("\n"); |
455 | convertAST2Lua(ast); | ||
456 | printf("\n"); | ||
366 | burnAST(ast); | 457 | burnAST(ast); |
367 | } | 458 | } |
368 | 459 | ||
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index a392474..1c6710d 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h | |||
@@ -9,6 +9,8 @@ | |||
9 | #include "LuaSL_yaccer.tab.h" | 9 | #include "LuaSL_yaccer.tab.h" |
10 | #endif | 10 | #endif |
11 | 11 | ||
12 | #include <stddef.h> // So we can have NULL defined. | ||
13 | |||
12 | #define YYERRCODE 256 | 14 | #define YYERRCODE 256 |
13 | #define YYDEBUG 1 | 15 | #define YYDEBUG 1 |
14 | extern int yydebug; | 16 | extern int yydebug; |
@@ -18,12 +20,13 @@ extern int yydebug; | |||
18 | 20 | ||
19 | typedef enum | 21 | typedef enum |
20 | { | 22 | { |
21 | LSL_LEFT2RIGHT = 0, | 23 | LSL_NONE = 0, |
22 | LSL_RIGHT2LEFT = 1, | 24 | LSL_LEFT2RIGHT = 1, |
23 | LSL_INNER2OUTER = 2, | 25 | LSL_RIGHT2LEFT = 2, |
24 | LSL_UNARY = 4, | 26 | LSL_INNER2OUTER = 4, |
25 | LSL_ASSIGNMENT = 8, | 27 | LSL_UNARY = 8, |
26 | LSL_CREATION = 16 | 28 | LSL_ASSIGNMENT = 16, |
29 | LSL_CREATION = 32 | ||
27 | } LSL_Flags; | 30 | } LSL_Flags; |
28 | 31 | ||
29 | #ifdef LUASL_USE_ENUM | 32 | #ifdef LUASL_USE_ENUM |
@@ -80,68 +83,6 @@ typedef enum // In order of precedence, high to low. | |||
80 | typedef int LSL_Operation; | 83 | typedef int LSL_Operation; |
81 | #endif | 84 | #endif |
82 | 85 | ||
83 | typedef struct | ||
84 | { | ||
85 | // LSL_Operation operation, | ||
86 | char *token; | ||
87 | LSL_Flags flags; | ||
88 | } LSL_Operator; | ||
89 | |||
90 | // QUIRK - Seems to be some disagreement about BOOL_AND/BOOL_OR precedence. Either they are equal, or OR is higher. | ||
91 | // QUIRK - Conditionals are executed right to left. Or left to right, depending on who you ask. lol | ||
92 | // QUIRK - No boolean short circuiting. | ||
93 | |||
94 | #ifdef LSL_Tokens_define | ||
95 | LSL_Operator LSL_Tokens[] = | ||
96 | { | ||
97 | {",", LSL_LEFT2RIGHT}, | ||
98 | {"++", LSL_RIGHT2LEFT | LSL_UNARY}, | ||
99 | {"++", LSL_RIGHT2LEFT | LSL_UNARY}, | ||
100 | {"--", LSL_RIGHT2LEFT | LSL_UNARY}, | ||
101 | {"--", LSL_RIGHT2LEFT | LSL_UNARY}, | ||
102 | {".", LSL_RIGHT2LEFT}, | ||
103 | {"=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT}, | ||
104 | {"/=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT}, | ||
105 | {"%=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT}, | ||
106 | {"*=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT}, | ||
107 | {"-=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT}, | ||
108 | {"+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT}, | ||
109 | {"+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT}, | ||
110 | {"(", LSL_INNER2OUTER}, | ||
111 | {")", LSL_INNER2OUTER}, | ||
112 | {"[", LSL_INNER2OUTER | LSL_CREATION}, | ||
113 | {"]", LSL_INNER2OUTER | LSL_CREATION}, | ||
114 | {"<", LSL_LEFT2RIGHT | LSL_CREATION}, | ||
115 | {">", LSL_LEFT2RIGHT | LSL_CREATION}, | ||
116 | {"()", LSL_RIGHT2LEFT | LSL_UNARY}, | ||
117 | {"~", LSL_RIGHT2LEFT | LSL_UNARY}, | ||
118 | {"!", LSL_RIGHT2LEFT | LSL_UNARY}, | ||
119 | {"-", LSL_RIGHT2LEFT | LSL_UNARY}, | ||
120 | {"/", LSL_LEFT2RIGHT}, | ||
121 | {"%", LSL_LEFT2RIGHT}, | ||
122 | {"*", LSL_LEFT2RIGHT}, | ||
123 | {"*", LSL_LEFT2RIGHT}, | ||
124 | {"%", LSL_LEFT2RIGHT}, | ||
125 | {"-", LSL_LEFT2RIGHT}, | ||
126 | {"+", LSL_LEFT2RIGHT}, | ||
127 | {"+", LSL_LEFT2RIGHT}, | ||
128 | {"<<", LSL_LEFT2RIGHT}, | ||
129 | {">>", LSL_LEFT2RIGHT}, | ||
130 | {"<", LSL_LEFT2RIGHT}, | ||
131 | {">", LSL_LEFT2RIGHT}, | ||
132 | {"<=", LSL_LEFT2RIGHT}, | ||
133 | {">=", LSL_LEFT2RIGHT}, | ||
134 | {"==", LSL_LEFT2RIGHT}, | ||
135 | {"!=", LSL_LEFT2RIGHT}, | ||
136 | {"&", LSL_LEFT2RIGHT}, | ||
137 | {"^", LSL_LEFT2RIGHT}, | ||
138 | {"|", LSL_LEFT2RIGHT}, | ||
139 | {"||", LSL_LEFT2RIGHT}, | ||
140 | {"&&", LSL_LEFT2RIGHT} | ||
141 | }; | ||
142 | #endif | ||
143 | |||
144 | |||
145 | #ifdef LUASL_USE_ENUM | 86 | #ifdef LUASL_USE_ENUM |
146 | typedef enum | 87 | typedef enum |
147 | { | 88 | { |
@@ -176,41 +117,6 @@ typedef enum | |||
176 | } LSL_Type; | 117 | } LSL_Type; |
177 | #else | 118 | #else |
178 | typedef int LSL_Type; | 119 | typedef int LSL_Type; |
179 | #define LSL_EXPRESSION 1 | ||
180 | #endif | ||
181 | |||
182 | #ifdef LSL_Keywords_define | ||
183 | char *LSL_Keywords[] = | ||
184 | { | ||
185 | "//", // Also "/*", | ||
186 | "", | ||
187 | "", | ||
188 | "", | ||
189 | "float", | ||
190 | "integer", | ||
191 | "string", | ||
192 | "key", | ||
193 | "vector", | ||
194 | "rotation", | ||
195 | "list", | ||
196 | "@", | ||
197 | "", | ||
198 | "do", | ||
199 | "for", | ||
200 | "if", | ||
201 | "else", | ||
202 | "else if", | ||
203 | "jump", | ||
204 | "state", | ||
205 | "while", | ||
206 | "return", | ||
207 | ";", | ||
208 | "{}", | ||
209 | "", | ||
210 | "", | ||
211 | "", | ||
212 | "" | ||
213 | }; | ||
214 | #endif | 120 | #endif |
215 | 121 | ||
216 | typedef struct | 122 | typedef struct |
@@ -276,6 +182,20 @@ typedef union LSL_Leaf | |||
276 | LSL_Script *scriptValue; | 182 | LSL_Script *scriptValue; |
277 | } LSL_Leaf; | 183 | } LSL_Leaf; |
278 | 184 | ||
185 | typedef void (*convertToken2Lua) (LSL_Leaf *content); | ||
186 | typedef void (*outputToken) (LSL_Leaf *content); | ||
187 | typedef LSL_Leaf *(*evaluateToken) (LSL_Leaf *content, LSL_Type oldType, LSL_Leaf *old); | ||
188 | |||
189 | typedef struct | ||
190 | { | ||
191 | LSL_Type type; | ||
192 | char *token; | ||
193 | LSL_Flags flags; | ||
194 | outputToken output; | ||
195 | convertToken2Lua convert; | ||
196 | evaluateToken evaluate; | ||
197 | } LSL_Token; | ||
198 | |||
279 | typedef struct | 199 | typedef struct |
280 | { | 200 | { |
281 | char *name; | 201 | char *name; |
@@ -287,6 +207,7 @@ typedef struct LSL_Expression | |||
287 | { | 207 | { |
288 | struct LSL_Expression *left; | 208 | struct LSL_Expression *left; |
289 | struct LSL_Expression *right; | 209 | struct LSL_Expression *right; |
210 | LSL_Token *token; | ||
290 | LSL_Type type; | 211 | LSL_Type type; |
291 | LSL_Leaf content; | 212 | LSL_Leaf content; |
292 | } LSL_Expression; | 213 | } LSL_Expression; |
@@ -297,6 +218,7 @@ typedef struct LSL_AST | |||
297 | struct LSL_AST *right; | 218 | struct LSL_AST *right; |
298 | int line; | 219 | int line; |
299 | int character; | 220 | int character; |
221 | LSL_Token *token; | ||
300 | LSL_Type type; | 222 | LSL_Type type; |
301 | LSL_Leaf content; | 223 | LSL_Leaf content; |
302 | } LSL_AST; | 224 | } LSL_AST; |
@@ -325,17 +247,9 @@ typedef struct | |||
325 | #define YYLEX_PARAM ((LuaSL_yyparseParam*)data)->scanner | 247 | #define YYLEX_PARAM ((LuaSL_yyparseParam*)data)->scanner |
326 | 248 | ||
327 | 249 | ||
328 | void burnLSLExpression(LSL_Expression *exp); | ||
329 | void burnAST(LSL_AST *ast); | ||
330 | LSL_AST *addExpression(LSL_Expression *exp); | 250 | LSL_AST *addExpression(LSL_Expression *exp); |
331 | LSL_Expression *addInteger(int value); | 251 | LSL_Expression *addInteger(int value); |
332 | LSL_Expression *addOperation(LSL_Operation type, LSL_Expression *left, LSL_Expression *right); | 252 | LSL_Expression *addOperation(LSL_Operation type, LSL_Expression *left, LSL_Expression *right); |
333 | int evaluateExpression(LSL_Expression *exp, int old); | ||
334 | int evaluateAST(LSL_AST *ast, int old); | ||
335 | void outputExpression(LSL_Expression *exp); | ||
336 | void outputAST(LSL_AST *ast); | ||
337 | void convertAST2Lua(LSL_AST *ast); | ||
338 | LSL_AST *newTree(const char *expr); | ||
339 | 253 | ||
340 | int yyerror(const char *msg); | 254 | int yyerror(const char *msg); |
341 | int yyparse(void *param); | 255 | int yyparse(void *param); |
diff --git a/LuaSL/src/LuaSL_yaccer.y b/LuaSL/src/LuaSL_yaccer.y index e208cce..bbec1be 100644 --- a/LuaSL/src/LuaSL_yaccer.y +++ b/LuaSL/src/LuaSL_yaccer.y | |||
@@ -21,7 +21,7 @@ | |||
21 | %left LSL_DIVIDE LSL_MODULO LSL_MULTIPLY | 21 | %left LSL_DIVIDE LSL_MODULO LSL_MULTIPLY |
22 | %right LSL_BIT_NOT LSL_BOOL_NOT LSL_NEGATION | 22 | %right LSL_BIT_NOT LSL_BOOL_NOT LSL_NEGATION |
23 | 23 | ||
24 | %token LSL_PARENTHESIS_OPEN LSL_PARENTHESIS_CLOSE | 24 | %token LSL_PARENTHESIS_OPEN LSL_PARENTHESIS_CLOSE LSL_EXPRESSION |
25 | 25 | ||
26 | %type <expressionValue> expr | 26 | %type <expressionValue> expr |
27 | 27 | ||