aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/src/LuaSL_LSL_tree.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-01-07 04:28:24 +1000
committerDavid Walter Seikel2012-01-07 04:28:24 +1000
commit1603cadc4b3345e01f866f6f94bf69df078b4808 (patch)
treedd18f3e7c6d2a1b4e73acd5bba68ce457b2dcda8 /LuaSL/src/LuaSL_LSL_tree.c
parentNo need to actually compile the LSL flex and yacc sources, they are just refe... (diff)
downloadSledjHamr-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.c275
1 files changed, 183 insertions, 92 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
6static void outputExpressionToken(LSL_Leaf *content);
7static LSL_Leaf *evaluateExpressionToken(LSL_Leaf *content, LSL_Type oldType, LSL_Leaf *old);
8static void outputIntegerToken(LSL_Leaf *content);
9static LSL_Leaf *evaluateIntegerToken(LSL_Leaf *content, LSL_Type oldType, LSL_Leaf *old);
10
11LSL_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
102LSL_Token **tokens = NULL;
103int lowestToken = 999999;
104
8 105
9static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right) 106static 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
24void burnAST(LSL_AST *ast) 125static 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
56void burnLSLExpression(LSL_Expression *exp) 162static 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
85int evaluateExpression(LSL_Expression *exp, int old) 197static 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
165int evaluateAST(LSL_AST *ast, int old) 273static 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
280static 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
292static 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
217void outputExpression(LSL_Expression *exp) 347static 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
238void outputAST(LSL_AST *ast) 364static 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
279void convertAST2Lua(LSL_AST *ast) 370static 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 { 376static 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; 382static 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
318int yyerror(const char *msg) 388int yyerror(const char *msg)
@@ -321,7 +391,7 @@ int yyerror(const char *msg)
321 return 0; 391 return 0;
322} 392}
323 393
324LSL_AST *newTree(const char *expr) 394static 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