aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-01-10 01:43:10 +1000
committerDavid Walter Seikel2012-01-10 01:43:10 +1000
commitff5b1c802a26789ff769521262db79854b24939b (patch)
tree609c9ff857c837bf84d9dd71fe5c1fb7cf3778fd /LuaSL
parentHmmm, we have a circular dependencie with the include fiels each of flex and ... (diff)
downloadSledjHamr-ff5b1c802a26789ff769521262db79854b24939b.zip
SledjHamr-ff5b1c802a26789ff769521262db79854b24939b.tar.gz
SledjHamr-ff5b1c802a26789ff769521262db79854b24939b.tar.bz2
SledjHamr-ff5b1c802a26789ff769521262db79854b24939b.tar.xz
Almost got white space and comments working. Still a bug left somewhere, I think it's in operations ordering.
Diffstat (limited to 'LuaSL')
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.c132
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.h88
-rw-r--r--LuaSL/src/LuaSL_lexer.l172
-rw-r--r--LuaSL/src/LuaSL_yaccer.y65
-rw-r--r--LuaSL/test.lsl3
5 files changed, 249 insertions, 211 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c
index 76dd433..ff7da8b 100644
--- a/LuaSL/src/LuaSL_LSL_tree.c
+++ b/LuaSL/src/LuaSL_LSL_tree.c
@@ -149,20 +149,42 @@ static void burnAST(LSL_AST *ast)
149 149
150 burnAST(ast->left); 150 burnAST(ast->left);
151 burnAST(ast->right); 151 burnAST(ast->right);
152 free(ast->content.ignorableText);
152 free(ast); 153 free(ast);
153} 154}
154 155
155LSL_AST *addInteger(int value) 156static LSL_AST *checkAndAddIgnorable(LSL_AST *root)
157{
158 if (root)
159 {
160 if (root->content.ignorableText)
161 {
162 LSL_AST *ast = newAST(LSL_SPACE, NULL, root);
163
164 if (ast)
165 {
166 ast->content.value.spaceValue = root->content.ignorableText;
167 return ast;
168 }
169 }
170 }
171 return root;
172}
173
174LSL_AST *addInteger(LSL_Leaf *lval, int value)
156{ 175{
157 LSL_AST *ast = newAST(LSL_INTEGER, NULL, NULL); 176 LSL_AST *ast = newAST(LSL_INTEGER, NULL, NULL);
158 177
159 if (ast) 178 if (ast)
160 ast->content.integerValue = value; 179 {
180 ast->content.value.integerValue = value;
181 ast->content.ignorableText = lval->ignorableText;
182 }
161 183
162 return ast; 184 return checkAndAddIgnorable(ast);
163} 185}
164 186
165LSL_AST *addOperation(LSL_Type type, LSL_AST *left, LSL_AST *right) 187LSL_AST *addOperation(LSL_Leaf *lval, LSL_Type type, LSL_AST *left, LSL_AST *right)
166{ 188{
167 LSL_AST *ast = newAST(type, left, right); 189 LSL_AST *ast = newAST(type, left, right);
168 190
@@ -170,24 +192,30 @@ LSL_AST *addOperation(LSL_Type type, LSL_AST *left, LSL_AST *right)
170 { 192 {
171 if (LSL_EXPRESSION == type) 193 if (LSL_EXPRESSION == type)
172 { 194 {
173 ast->content.expressionValue = right; 195 ast->content.value.expressionValue = right;
174 ast->left = NULL; 196 ast->left = NULL;
175 } 197 }
176 else 198 else
177 ast->content.operationValue = type; 199 {
200 ast->content.value.operationValue = type;
201 ast->content.ignorableText = lval->ignorableText;
202 }
178 } 203 }
179 204
180 return ast; 205 return checkAndAddIgnorable(ast);
181} 206}
182 207
183LSL_AST *addParenthesis(LSL_AST *expr) 208LSL_AST *addParenthesis(LSL_Leaf *lval, LSL_AST *expr)
184{ 209{
185 LSL_AST *ast = newAST(LSL_PARENTHESIS_OPEN, NULL, expr); 210 LSL_AST *ast = newAST(LSL_PARENTHESIS_OPEN, NULL, expr);
186 211
187 if (ast) 212 if (ast)
213 {
188 ast = newAST(LSL_PARENTHESIS_CLOSE, ast, NULL); 214 ast = newAST(LSL_PARENTHESIS_CLOSE, ast, NULL);
215 ast->content.ignorableText = lval->ignorableText;
216 }
189 217
190 return ast; 218 return checkAndAddIgnorable(ast);
191} 219}
192 220
193LSL_Statement *createStatement(LSL_Type type, LSL_AST *expr) 221LSL_Statement *createStatement(LSL_Type type, LSL_AST *expr)
@@ -202,24 +230,14 @@ LSL_Statement *createStatement(LSL_Type type, LSL_AST *expr)
202 return stat; 230 return stat;
203} 231}
204 232
205LSL_AST *addSpace(char *text, LSL_AST *root)
206{
207 LSL_AST *ast = newAST(LSL_SPACE, root, NULL);
208
209 if (ast)
210 ast->content.spaceValue = text;
211
212 return ast;
213}
214
215LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root) 233LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root)
216{ 234{
217 LSL_AST *ast = newAST(LSL_STATEMENT, root, NULL); 235 LSL_AST *ast = newAST(LSL_STATEMENT, root, NULL);
218 236
219 if (ast) 237 if (ast)
220 ast->content.statementValue = statement; 238 ast->content.value.statementValue = statement;
221 239
222 return ast; 240 return checkAndAddIgnorable(ast);
223} 241}
224 242
225static void evaluateAST(LSL_AST *ast, LSL_Value *left, LSL_Value *right) 243static void evaluateAST(LSL_AST *ast, LSL_Value *left, LSL_Value *right)
@@ -259,7 +277,7 @@ static void evaluateAST(LSL_AST *ast, LSL_Value *left, LSL_Value *right)
259 else 277 else
260 { 278 {
261#ifdef LUASL_DEBUG 279#ifdef LUASL_DEBUG
262 printf(" eval <%s %d %d %d %d> ", ast->token->token, left->content.integerValue, right->content.integerValue, lresult.content.integerValue, rresult.content.integerValue); 280 printf(" eval <%s %d %d %d %d> ", ast->token->token, left->content.value.integerValue, right->content.value.integerValue, lresult.content.value.integerValue, rresult.content.value.integerValue);
263#endif 281#endif
264 memcpy(left, &rresult, sizeof(LSL_Value)); 282 memcpy(left, &rresult, sizeof(LSL_Value));
265 } 283 }
@@ -271,9 +289,9 @@ static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *
271 if (content) 289 if (content)
272 { 290 {
273#ifdef LUASL_DEBUG 291#ifdef LUASL_DEBUG
274 printf(" <%d> ", content->integerValue); 292 printf(" <%d> ", content->value.integerValue);
275#endif 293#endif
276 left->content.integerValue = content->integerValue; 294 left->content.value.integerValue = content->value.integerValue;
277 left->type = LSL_INTEGER; 295 left->type = LSL_INTEGER;
278 } 296 }
279} 297}
@@ -284,13 +302,13 @@ static void evaluateNoToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right
284 302
285static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) 303static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right)
286{ 304{
287 if ((content) && (content->operationValue)) 305 if ((content) && (content->value.operationValue))
288 { 306 {
289#ifdef LUASL_DEBUG 307#ifdef LUASL_DEBUG
290 printf(" [%s] ", tokens[content->operationValue - lowestToken]->token); 308 printf(" [%s] ", tokens[content->value.operationValue - lowestToken]->token);
291#endif 309#endif
292 310
293 switch (content->operationValue) 311 switch (content->value.operationValue)
294 { 312 {
295 case LSL_COMMA : 313 case LSL_COMMA :
296 case LSL_INCREMENT_PRE : 314 case LSL_INCREMENT_PRE :
@@ -314,33 +332,33 @@ static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value
314// case LSL_TYPECAST_OPEN : 332// case LSL_TYPECAST_OPEN :
315// case LSL_TYPECAST_CLOSE : 333// case LSL_TYPECAST_CLOSE :
316 break; 334 break;
317 case LSL_BIT_NOT : left->content.integerValue = ~ right->content.integerValue; break; 335 case LSL_BIT_NOT : left->content.value.integerValue = ~ right->content.value.integerValue; break;
318 case LSL_BOOL_NOT : left->content.integerValue = ! right->content.integerValue; break; 336 case LSL_BOOL_NOT : left->content.value.integerValue = ! right->content.value.integerValue; break;
319 case LSL_NEGATION : left->content.integerValue = 0 - right->content.integerValue; break; 337 case LSL_NEGATION : left->content.value.integerValue = 0 - right->content.value.integerValue; break;
320 case LSL_DIVIDE : left->content.integerValue = left->content.integerValue / right->content.integerValue; break; 338 case LSL_DIVIDE : left->content.value.integerValue = left->content.value.integerValue / right->content.value.integerValue; break;
321 case LSL_MODULO : left->content.integerValue = left->content.integerValue % right->content.integerValue; break; 339 case LSL_MODULO : left->content.value.integerValue = left->content.value.integerValue % right->content.value.integerValue; break;
322 case LSL_MULTIPLY : left->content.integerValue = left->content.integerValue * right->content.integerValue; break; 340 case LSL_MULTIPLY : left->content.value.integerValue = left->content.value.integerValue * right->content.value.integerValue; break;
323// case LSL_DOT_PRODUCT : break; 341// case LSL_DOT_PRODUCT : break;
324// case LSL_CROSS_PRODUCT : break; 342// case LSL_CROSS_PRODUCT : break;
325 case LSL_SUBTRACT : left->content.integerValue = left->content.integerValue - right->content.integerValue; break; 343 case LSL_SUBTRACT : left->content.value.integerValue = left->content.value.integerValue - right->content.value.integerValue; break;
326 case LSL_ADD : left->content.integerValue = left->content.integerValue + right->content.integerValue; break; 344 case LSL_ADD : left->content.value.integerValue = left->content.value.integerValue + right->content.value.integerValue; break;
327// case LSL_CONCATENATE : break; 345// case LSL_CONCATENATE : break;
328 case LSL_LEFT_SHIFT : left->content.integerValue = left->content.integerValue << right->content.integerValue; break; 346 case LSL_LEFT_SHIFT : left->content.value.integerValue = left->content.value.integerValue << right->content.value.integerValue; break;
329 case LSL_RIGHT_SHIFT : left->content.integerValue = left->content.integerValue >> right->content.integerValue; break; 347 case LSL_RIGHT_SHIFT : left->content.value.integerValue = left->content.value.integerValue >> right->content.value.integerValue; break;
330 case LSL_LESS_THAN : left->content.integerValue = left->content.integerValue < right->content.integerValue; break; 348 case LSL_LESS_THAN : left->content.value.integerValue = left->content.value.integerValue < right->content.value.integerValue; break;
331 case LSL_GREATER_THAN : left->content.integerValue = left->content.integerValue > right->content.integerValue; break; 349 case LSL_GREATER_THAN : left->content.value.integerValue = left->content.value.integerValue > right->content.value.integerValue; break;
332 case LSL_LESS_EQUAL : left->content.integerValue = left->content.integerValue <= right->content.integerValue; break; 350 case LSL_LESS_EQUAL : left->content.value.integerValue = left->content.value.integerValue <= right->content.value.integerValue; break;
333 case LSL_GREATER_EQUAL : left->content.integerValue = left->content.integerValue >= right->content.integerValue; break; 351 case LSL_GREATER_EQUAL : left->content.value.integerValue = left->content.value.integerValue >= right->content.value.integerValue; break;
334 case LSL_EQUAL : left->content.integerValue = left->content.integerValue == right->content.integerValue; break; 352 case LSL_EQUAL : left->content.value.integerValue = left->content.value.integerValue == right->content.value.integerValue; break;
335 case LSL_NOT_EQUAL : left->content.integerValue = left->content.integerValue != right->content.integerValue; break; 353 case LSL_NOT_EQUAL : left->content.value.integerValue = left->content.value.integerValue != right->content.value.integerValue; break;
336 case LSL_BIT_AND : left->content.integerValue = left->content.integerValue & right->content.integerValue; break; 354 case LSL_BIT_AND : left->content.value.integerValue = left->content.value.integerValue & right->content.value.integerValue; break;
337 case LSL_BIT_XOR : left->content.integerValue = left->content.integerValue ^ right->content.integerValue; break; 355 case LSL_BIT_XOR : left->content.value.integerValue = left->content.value.integerValue ^ right->content.value.integerValue; break;
338 case LSL_BIT_OR : left->content.integerValue = left->content.integerValue | right->content.integerValue; break; 356 case LSL_BIT_OR : left->content.value.integerValue = left->content.value.integerValue | right->content.value.integerValue; break;
339 case LSL_BOOL_OR : left->content.integerValue = left->content.integerValue || right->content.integerValue; break; 357 case LSL_BOOL_OR : left->content.value.integerValue = left->content.value.integerValue || right->content.value.integerValue; break;
340 case LSL_BOOL_AND : left->content.integerValue = left->content.integerValue && right->content.integerValue; break; 358 case LSL_BOOL_AND : left->content.value.integerValue = left->content.value.integerValue && right->content.value.integerValue; break;
341 } 359 }
342#ifdef LUASL_DEBUG 360#ifdef LUASL_DEBUG
343 printf(" (=%d) ", left->content.integerValue); 361 printf(" (=%d) ", left->content.value.integerValue);
344#endif 362#endif
345 } 363 }
346} 364}
@@ -348,7 +366,7 @@ static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value
348static void evaluateStatementToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) 366static void evaluateStatementToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right)
349{ 367{
350 if (content) 368 if (content)
351 evaluateAST(content->statementValue->expression, left, right); 369 evaluateAST(content->value.statementValue->expression, left, right);
352} 370}
353 371
354static void outputAST(LSL_AST *ast) 372static void outputAST(LSL_AST *ast)
@@ -367,26 +385,26 @@ static void outputAST(LSL_AST *ast)
367static void outputIntegerToken(LSL_Leaf *content) 385static void outputIntegerToken(LSL_Leaf *content)
368{ 386{
369 if (content) 387 if (content)
370 printf("%d", content->integerValue); 388 printf("%d", content->value.integerValue);
371} 389}
372 390
373static void outputOperationToken(LSL_Leaf *content) 391static void outputOperationToken(LSL_Leaf *content)
374{ 392{
375 if (content) 393 if (content)
376 printf("%s", tokens[content->operationValue - lowestToken]->token); 394 printf("%s", tokens[content->value.operationValue - lowestToken]->token);
377} 395}
378 396
379static void outputStatementToken(LSL_Leaf *content) 397static void outputStatementToken(LSL_Leaf *content)
380{ 398{
381 if (content) 399 if (content)
382 outputAST(content->statementValue->expression); 400 outputAST(content->value.statementValue->expression);
383 printf(";"); 401 printf(";");
384} 402}
385 403
386static void outputSpaceToken(LSL_Leaf *content) 404static void outputSpaceToken(LSL_Leaf *content)
387{ 405{
388 if (content) 406 if (content)
389 printf("%s", content->spaceValue); 407 printf("%s", content->value.spaceValue);
390} 408}
391 409
392static void convertAST2Lua(LSL_AST *ast) 410static void convertAST2Lua(LSL_AST *ast)
@@ -523,9 +541,9 @@ int main(int argc, char **argv)
523 { 541 {
524 LSL_Value left, right; 542 LSL_Value left, right;
525 543
526 left.content.integerValue = 0; 544 left.content.value.integerValue = 0;
527 left.type = LSL_INTEGER; 545 left.type = LSL_INTEGER;
528 right.content.integerValue = 0; 546 right.content.value.integerValue = 0;
529 right.type = LSL_INTEGER; 547 right.type = LSL_INTEGER;
530 evaluateAST(ast, &left, &right); 548 evaluateAST(ast, &left, &right);
531 549
@@ -535,7 +553,7 @@ int main(int argc, char **argv)
535 printf("Result of -\n"); 553 printf("Result of -\n");
536 outputAST(ast); 554 outputAST(ast);
537 printf("\n"); 555 printf("\n");
538 printf("is %d %d. And converted to Lua it is -\n", left.content.integerValue, right.content.integerValue); 556 printf("is %d %d. And converted to Lua it is -\n", left.content.value.integerValue, right.content.value.integerValue);
539 convertAST2Lua(ast); 557 convertAST2Lua(ast);
540 printf("\n"); 558 printf("\n");
541 burnAST(ast); 559 burnAST(ast);
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h
index adefa07..fe56c29 100644
--- a/LuaSL/src/LuaSL_LSL_tree.h
+++ b/LuaSL/src/LuaSL_LSL_tree.h
@@ -20,7 +20,7 @@ extern int yydebug;
20// http://w-hat.com/stackdepth is a useful discussion about some aspects of the LL parser. 20// http://w-hat.com/stackdepth is a useful discussion about some aspects of the LL parser.
21 21
22 22
23typedef union _LSL_Leaf LSL_Leaf; 23typedef struct _LSL_Leaf LSL_Leaf;
24typedef struct _LSL_Value LSL_Value; 24typedef struct _LSL_Value LSL_Value;
25typedef struct _LSL_Identifier LSL_Identifier; 25typedef struct _LSL_Identifier LSL_Identifier;
26typedef struct _LSL_Statement LSL_Statement; 26typedef struct _LSL_Statement LSL_Statement;
@@ -65,43 +65,48 @@ typedef struct
65 evaluateToken evaluate; 65 evaluateToken evaluate;
66} LSL_Token; 66} LSL_Token;
67 67
68union _LSL_Leaf 68struct _LSL_Leaf
69{ 69{
70 char *commentValue; 70 union
71 char *spaceValue; 71 {
72 72 char *commentValue;
73 LSL_Type operationValue; 73 char *spaceValue;
74 LSL_AST *expressionValue; 74
75 75 LSL_Type operationValue;
76 float floatValue; 76 LSL_AST *expressionValue;
77 int integerValue; 77
78 char *keyValue; 78 float floatValue;
79 LSL_Leaf *listValue; 79 int integerValue;
80 char *stringValue; 80 char *keyValue;
81 float vectorValue[3]; 81 LSL_Leaf *listValue;
82 float rotationValue[4]; 82 char *stringValue;
83 83 float vectorValue[3];
84 LSL_Identifier *variableValue; 84 float rotationValue[4];
85 85
86 char *labelValue; 86 LSL_Identifier *variableValue;
87 LSL_Statement *doValue; 87
88 LSL_Statement *forValue; 88 char *labelValue;
89 LSL_Statement *elseIfValue; 89 LSL_Statement *doValue;
90 LSL_Statement *elseValue; 90 LSL_Statement *forValue;
91 LSL_Statement *ifValue; 91 LSL_Statement *elseIfValue;
92 char *jumpValue; 92 LSL_Statement *elseValue;
93 LSL_Statement *returnValue; 93 LSL_Statement *ifValue;
94 char *stateChangeValue; 94 char *jumpValue;
95 LSL_Statement *whileValue; 95 LSL_Statement *returnValue;
96 LSL_Statement *statementValue; 96 char *stateChangeValue;
97 97 LSL_Statement *whileValue;
98 LSL_Block *blockValue; 98 LSL_Statement *statementValue;
99 LSL_Identifier *parameterValue; 99
100 LSL_Function *functionValue; 100 LSL_Block *blockValue;
101 LSL_State *stateValue; 101 LSL_Identifier *parameterValue;
102 LSL_Script *scriptValue; 102 LSL_Function *functionValue;
103 103 LSL_State *stateValue;
104 char *unknownValue; 104 LSL_Script *scriptValue;
105
106 char *unknownValue;
107 } value;
108 char *ignorableText;
109 int line, column;
105}; 110};
106 111
107struct _LSL_Value 112struct _LSL_Value
@@ -176,7 +181,7 @@ typedef struct
176} LuaSL_yyparseParam; 181} LuaSL_yyparseParam;
177 182
178// the parameter name (of the reentrant 'yyparse' function) 183// the parameter name (of the reentrant 'yyparse' function)
179// data is a pointer to a 'SParserParam' structure 184// data is a pointer to a 'yyparseParam' structure
180#define YYPARSE_PARAM data 185#define YYPARSE_PARAM data
181 186
182// the argument for the 'yylex' function 187// the argument for the 'yylex' function
@@ -184,12 +189,11 @@ typedef struct
184 189
185 190
186LSL_AST *addExpression(LSL_AST *exp); 191LSL_AST *addExpression(LSL_AST *exp);
187LSL_AST *addInteger(int value); 192LSL_AST *addInteger(LSL_Leaf *lval, int value);
188LSL_AST *addOperation(LSL_Type type, LSL_AST *left, LSL_AST *right); 193LSL_AST *addOperation(LSL_Leaf *lval, LSL_Type type, LSL_AST *left, LSL_AST *right);
189LSL_AST *addParenthesis(LSL_AST *expr); 194LSL_AST *addParenthesis(LSL_Leaf *lval, LSL_AST *expr);
190LSL_Statement *createStatement(LSL_Type type, LSL_AST *root); 195LSL_Statement *createStatement(LSL_Type type, LSL_AST *root);
191LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root); 196LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root);
192LSL_AST *addSpace(char *text, LSL_AST *root);
193 197
194int yyerror(const char *msg); 198int yyerror(const char *msg);
195int yyparse(void *param); 199int yyparse(void *param);
diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l
index 3728ab9..08a68ef 100644
--- a/LuaSL/src/LuaSL_lexer.l
+++ b/LuaSL/src/LuaSL_lexer.l
@@ -5,15 +5,8 @@
5#include <stdio.h> 5#include <stdio.h>
6 6
7void comment(yyscan_t yyscanner); 7void comment(yyscan_t yyscanner);
8void count(char *text); 8void common(YYSTYPE *lval, char *text, boolean checkIgnorable);
9 9void ignorable(char *text);
10#ifdef LUASL_DEBUG
11 #undef ECHO
12 #define ECHO count(yytext); printf ("[%s]\n", yytext)
13#else
14 #undef ECHO
15 #define ECHO count(yytext)
16#endif
17 10
18%} 11%}
19 12
@@ -35,85 +28,84 @@ NAME [[:alpha:]](_|[[:alpha:]]|[[:digit:]])*
35 28
36 /* The order here is important, in mysterious ways. The more specific the lower in case of ambiguities like "floats contain integers". I think, not tested that well yet. */ 29 /* The order here is important, in mysterious ways. The more specific the lower in case of ambiguities like "floats contain integers". I think, not tested that well yet. */
37 30
38 /* White space. */ 31 /* Ignorables. */
39[[:space:]]+ %{ ECHO; /* yylval->spaceValue = strdup(yytext); return LSL_SPACE; */ %} 32[[:space:]]+ %{ common(yylval, yytext, FALSE); ignorable(yytext); %}
40"/*" %{ ECHO; comment(yyscanner); %} 33 /* Yes I know this will have problems with huge comments, just being simple to get it to work for now. */
41"//"[^\n]* %{ ECHO; /* consume //-comment */ %} 34"/*"([^"*/"]*)"*/" %{ common(yylval, yytext, FALSE); ignorable(yytext); %}
35"//"[^\n]* %{ common(yylval, yytext, FALSE); ignorable(yytext); %}
42 36
43 /* Operations. */ 37 /* Operations. */
44"&&" { ECHO; return LSL_BOOL_AND; } 38"&&" { common(yylval, yytext, TRUE); return LSL_BOOL_AND; }
45"||" { ECHO; return LSL_BOOL_OR; } 39"||" { common(yylval, yytext, TRUE); return LSL_BOOL_OR; }
46"|" { ECHO; return LSL_BIT_OR; } 40"|" { common(yylval, yytext, TRUE); return LSL_BIT_OR; }
47"^" { ECHO; return LSL_BIT_XOR; } 41"^" { common(yylval, yytext, TRUE); return LSL_BIT_XOR; }
48"&" { ECHO; return LSL_BIT_AND; } 42"&" { common(yylval, yytext, TRUE); return LSL_BIT_AND; }
49"!=" { ECHO; return LSL_NOT_EQUAL; } 43"!=" { common(yylval, yytext, TRUE); return LSL_NOT_EQUAL; }
50"==" { ECHO; return LSL_EQUAL; } 44"==" { common(yylval, yytext, TRUE); return LSL_EQUAL; }
51">=" { ECHO; return LSL_GREATER_EQUAL; } 45">=" { common(yylval, yytext, TRUE); return LSL_GREATER_EQUAL; }
52"<=" { ECHO; return LSL_LESS_EQUAL; } 46"<=" { common(yylval, yytext, TRUE); return LSL_LESS_EQUAL; }
53">" { ECHO; return LSL_GREATER_THAN; } 47">" { common(yylval, yytext, TRUE); return LSL_GREATER_THAN; }
54"<" { ECHO; return LSL_LESS_THAN; } 48"<" { common(yylval, yytext, TRUE); return LSL_LESS_THAN; }
55">>" { ECHO; return LSL_RIGHT_SHIFT; } 49">>" { common(yylval, yytext, TRUE); return LSL_RIGHT_SHIFT; }
56"<<" { ECHO; return LSL_LEFT_SHIFT; } 50"<<" { common(yylval, yytext, TRUE); return LSL_LEFT_SHIFT; }
57"+" { ECHO; return LSL_ADD; } 51"+" { common(yylval, yytext, TRUE); return LSL_ADD; }
58"-" { ECHO; return LSL_SUBTRACT; } 52"-" { common(yylval, yytext, TRUE); return LSL_SUBTRACT; }
59"*" { ECHO; return LSL_MULTIPLY; } 53"*" { common(yylval, yytext, TRUE); return LSL_MULTIPLY; }
60"%" { ECHO; return LSL_MODULO; } 54"%" { common(yylval, yytext, TRUE); return LSL_MODULO; }
61"/" { ECHO; return LSL_DIVIDE; } 55"/" { common(yylval, yytext, TRUE); return LSL_DIVIDE; }
62"!" { ECHO; return LSL_BOOL_NOT; } 56"!" { common(yylval, yytext, TRUE); return LSL_BOOL_NOT; }
63"~" { ECHO; return LSL_BIT_NOT; } 57"~" { common(yylval, yytext, TRUE); return LSL_BIT_NOT; }
64 /* "<" { ECHO; return LSL_ANGLE_OPEN; } */ 58"[" { common(yylval, yytext, TRUE); return LSL_BRACKET_OPEN; }
65 /* ">" { ECHO; return LSL_ANGLE_CLOSE; } */ 59"]" { common(yylval, yytext, TRUE); return LSL_BRACKET_CLOSE; }
66"[" { ECHO; return LSL_BRACKET_OPEN; } 60"(" { common(yylval, yytext, TRUE); return LSL_PARENTHESIS_OPEN; }
67"]" { ECHO; return LSL_BRACKET_CLOSE; } 61")" { common(yylval, yytext, TRUE); return LSL_PARENTHESIS_CLOSE; }
68"(" { ECHO; return LSL_PARENTHESIS_OPEN; } 62"+=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_ADD; }
69")" { ECHO; return LSL_PARENTHESIS_CLOSE; } 63"-=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_SUBTRACT; }
70"+=" { ECHO; return LSL_ASSIGNMENT_ADD; } 64"*=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_MULTIPLY; }
71"-=" { ECHO; return LSL_ASSIGNMENT_SUBTRACT; } 65"%=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_MODULO; }
72"*=" { ECHO; return LSL_ASSIGNMENT_MULTIPLY; } 66"/=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_DIVIDE; }
73"%=" { ECHO; return LSL_ASSIGNMENT_MODULO; } 67"=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_PLAIN; }
74"/=" { ECHO; return LSL_ASSIGNMENT_DIVIDE; } 68"." { common(yylval, yytext, TRUE); return LSL_DOT; }
75"=" { ECHO; return LSL_ASSIGNMENT_PLAIN; } 69"--" { common(yylval, yytext, TRUE); return LSL_DECREMENT_PRE; }
76"." { ECHO; return LSL_DOT; } 70"++" { common(yylval, yytext, TRUE); return LSL_INCREMENT_PRE; }
77"--" { ECHO; return LSL_DECREMENT_PRE; } 71"," { common(yylval, yytext, TRUE); return LSL_COMMA; }
78"++" { ECHO; return LSL_INCREMENT_PRE; }
79"," { ECHO; return LSL_COMMA; }
80 72
81 /* Types. */ 73 /* Types. */
82{INTEGER} %{ ECHO; yylval->integerValue = atoi(yytext); return LSL_INTEGER; %} 74{INTEGER} %{ common(yylval, yytext, TRUE); yylval->value.integerValue = atoi(yytext); return LSL_INTEGER; %}
83{FLOAT} %{ ECHO; yylval->floatValue = atof(yytext); return LSL_FLOAT; %} 75{FLOAT} %{ common(yylval, yytext, TRUE); yylval->value.floatValue = atof(yytext); return LSL_FLOAT; %}
84 76
85 /* Type keywords. */ 77 /* Type keywords. */
86"float" %{ ECHO; return LSL_TYPE_FLOAT; %} 78"float" %{ common(yylval, yytext, TRUE); return LSL_TYPE_FLOAT; %}
87"integer" %{ ECHO; return LSL_TYPE_INTEGER; %} 79"integer" %{ common(yylval, yytext, TRUE); return LSL_TYPE_INTEGER; %}
88"key" %{ ECHO; return LSL_TYPE_KEY; %} 80"key" %{ common(yylval, yytext, TRUE); return LSL_TYPE_KEY; %}
89"list" %{ ECHO; return LSL_TYPE_LIST; %} 81"list" %{ common(yylval, yytext, TRUE); return LSL_TYPE_LIST; %}
90"quaternion" %{ ECHO; return LSL_TYPE_ROTATION; %} 82"quaternion" %{ common(yylval, yytext, TRUE); return LSL_TYPE_ROTATION; %}
91"rotation" %{ ECHO; return LSL_TYPE_ROTATION; %} 83"rotation" %{ common(yylval, yytext, TRUE); return LSL_TYPE_ROTATION; %}
92"string" %{ ECHO; return LSL_TYPE_STRING; %} 84"string" %{ common(yylval, yytext, TRUE); return LSL_TYPE_STRING; %}
93"vector" %{ ECHO; return LSL_TYPE_VECTOR; %} 85"vector" %{ common(yylval, yytext, TRUE); return LSL_TYPE_VECTOR; %}
94 86
95 /* Statement keywords. */ 87 /* Statement keywords. */
96"do" %{ ECHO; return LSL_DO; %} 88"do" %{ common(yylval, yytext, TRUE); return LSL_DO; %}
97"for" %{ ECHO; return LSL_FOR; %} 89"for" %{ common(yylval, yytext, TRUE); return LSL_FOR; %}
98"else" %{ ECHO; return LSL_ELSE; %} 90"else" %{ common(yylval, yytext, TRUE); return LSL_ELSE; %}
99"if" %{ ECHO; return LSL_IF; %} 91"if" %{ common(yylval, yytext, TRUE); return LSL_IF; %}
100"jump" %{ ECHO; return LSL_JUMP; %} 92"jump" %{ common(yylval, yytext, TRUE); return LSL_JUMP; %}
101"return" %{ ECHO; return LSL_RETURN; %} 93"return" %{ common(yylval, yytext, TRUE); return LSL_RETURN; %}
102"state" %{ ECHO; return LSL_STATE_CHANGE; %} 94"state" %{ common(yylval, yytext, TRUE); return LSL_STATE_CHANGE; %}
103"while" %{ ECHO; return LSL_WHILE; %} 95"while" %{ common(yylval, yytext, TRUE); return LSL_WHILE; %}
104 96
105{NAME} %{ ECHO; /* yylval->nameValue = strdup(yytext); return LSL_NAME; */ %} 97{NAME} %{ common(yylval, yytext, TRUE); /* yylval->value.nameValue = strdup(yytext); return LSL_NAME; */ %}
106 98
107 /* Other symbols. */ 99 /* Other symbols. */
108"@" %{ ECHO; return LSL_LABEL; %} 100"@" %{ common(yylval, yytext, TRUE); return LSL_LABEL; %}
109"{" %{ ECHO; return LSL_BLOCK_OPEN; %} 101"{" %{ common(yylval, yytext, TRUE); return LSL_BLOCK_OPEN; %}
110"}" %{ ECHO; return LSL_BLOCK_CLOSE; %} 102"}" %{ common(yylval, yytext, TRUE); return LSL_BLOCK_CLOSE; %}
111";" { ECHO; return LSL_STATEMENT; } 103";" %{ common(yylval, yytext, TRUE); return LSL_STATEMENT; %}
112 104
113<<EOF>> { yyterminate(); } 105<<EOF>> { yyterminate(); }
114 106
115 /* Everything else */ 107 /* Everything else */
116. %{ ECHO; printf(" unexpected character.\n"); %} 108. %{ common(yylval, yytext, TRUE); printf(" unexpected character.\n"); %}
117 109
118%% 110%%
119 111
@@ -130,10 +122,11 @@ void comment(yyscan_t yyscanner)
130 yyerror("unterminated comment"); 122 yyerror("unterminated comment");
131} 123}
132 124
133int column = 0; 125static char *ignorableText = NULL;
134int line = 0; 126static int column = 0;
127static int line = 0;
135 128
136void count(char *text) 129void common(YYSTYPE *lval, char *text, boolean checkIgnorable)
137{ 130{
138 int i; 131 int i;
139 132
@@ -147,6 +140,33 @@ void count(char *text)
147 column += 8 - (column % 8); 140 column += 8 - (column % 8);
148 else 141 else
149 column++; 142 column++;
143
144 lval->line = line;
145 lval->column = column;
146
147 if (checkIgnorable)
148 {
149 lval->ignorableText = ignorableText;
150 ignorableText = NULL;
151 }
152
153#ifdef LUASL_DEBUG
154 printf ("%04d, %04d [%s]\n", line, column, text);
155#endif
156}
157
158void ignorable(char *text)
159{
160 if (ignorableText)
161 {
162 int lenI = strlen(ignorableText);
163 int lenT = strlen(text);
164
165 ignorableText = realloc(ignorableText, lenI + lenT + 1);
166 sprintf(&ignorableText[lenI], "%s", text);
167 }
168 else
169 ignorableText = strdup(text);
150} 170}
151 171
152int yyerror(const char *msg) 172int yyerror(const char *msg)
diff --git a/LuaSL/src/LuaSL_yaccer.y b/LuaSL/src/LuaSL_yaccer.y
index 4b456b1..ed69dd3 100644
--- a/LuaSL/src/LuaSL_yaccer.y
+++ b/LuaSL/src/LuaSL_yaccer.y
@@ -10,10 +10,9 @@
10%define api.pure 10%define api.pure
11 11
12 12
13%type <spaceValue> ignorable 13%token <value.spaceValue> LSL_SPACE /* Never actually emitted, but we need it in the token table. */
14%token <spaceValue> LSL_SPACE
15 14
16%type <expressionValue> expr 15%type <value.expressionValue> expr
17%left LSL_BOOL_AND 16%left LSL_BOOL_AND
18%left LSL_BOOL_OR 17%left LSL_BOOL_OR
19%left LSL_BIT_AND LSL_BIT_XOR LSL_BIT_OR 18%left LSL_BIT_AND LSL_BIT_XOR LSL_BIT_OR
@@ -31,8 +30,8 @@
31%right LSL_DECREMENT_PRE LSL_INCREMENT_PRE 30%right LSL_DECREMENT_PRE LSL_INCREMENT_PRE
32%token LSL_COMMA 31%token LSL_COMMA
33 32
34%token <floatValue> LSL_FLOAT 33%token <value.floatValue> LSL_FLOAT
35%token <integerValue> LSL_INTEGER 34%token <value.integerValue> LSL_INTEGER
36 35
37%nonassoc LSL_TYPE_FLOAT LSL_TYPE_INTEGER LSL_TYPE_KEY LSL_TYPE_LIST LSL_TYPE_ROTATION LSL_TYPE_STRING LSL_TYPE_VECTOR 36%nonassoc LSL_TYPE_FLOAT LSL_TYPE_INTEGER LSL_TYPE_KEY LSL_TYPE_LIST LSL_TYPE_ROTATION LSL_TYPE_STRING LSL_TYPE_VECTOR
38 37
@@ -42,47 +41,43 @@
42 41
43%nonassoc LSL_BLOCK_OPEN LSL_BLOCK_CLOSE 42%nonassoc LSL_BLOCK_OPEN LSL_BLOCK_CLOSE
44 43
45%type <statementValue> statement 44%type <value.statementValue> statement
46%nonassoc LSL_STATEMENT 45%nonassoc LSL_STATEMENT
47 46
48%type <scriptValue> script 47%type <value.scriptValue> script
49 48
50%% 49%%
51 50
52input : 51input :
53 ignorable 52 expr { ((LuaSL_yyparseParam*)data)->ast = addOperation(&yylval, LSL_EXPRESSION, $1, $1); }
54 | expr { ((LuaSL_yyparseParam*)data)->ast = addOperation(LSL_EXPRESSION, $1, $1); }
55 | statement { ((LuaSL_yyparseParam*)data)->ast = addStatement($1, ((LuaSL_yyparseParam*)data)->ast); } 53 | statement { ((LuaSL_yyparseParam*)data)->ast = addStatement($1, ((LuaSL_yyparseParam*)data)->ast); }
56 | script 54 | script
57; 55;
58 56
59ignorable : LSL_SPACE { ((LuaSL_yyparseParam*)data)->ast = addSpace($1, ((LuaSL_yyparseParam*)data)->ast); }
60;
61
62expr : 57expr :
63 expr LSL_BOOL_AND expr { $$ = addOperation( LSL_BOOL_AND, $1, $3 ); } 58 expr LSL_BOOL_AND expr { $$ = addOperation(&yylval, LSL_BOOL_AND, $1, $3); }
64 | expr LSL_BOOL_OR expr { $$ = addOperation( LSL_BOOL_OR, $1, $3 ); } 59 | expr LSL_BOOL_OR expr { $$ = addOperation(&yylval, LSL_BOOL_OR, $1, $3); }
65 | expr LSL_BIT_OR expr { $$ = addOperation( LSL_BIT_OR, $1, $3 ); } 60 | expr LSL_BIT_OR expr { $$ = addOperation(&yylval, LSL_BIT_OR, $1, $3); }
66 | expr LSL_BIT_XOR expr { $$ = addOperation( LSL_BIT_XOR, $1, $3 ); } 61 | expr LSL_BIT_XOR expr { $$ = addOperation(&yylval, LSL_BIT_XOR, $1, $3); }
67 | expr LSL_BIT_AND expr { $$ = addOperation( LSL_BIT_AND, $1, $3 ); } 62 | expr LSL_BIT_AND expr { $$ = addOperation(&yylval, LSL_BIT_AND, $1, $3); }
68 | expr LSL_NOT_EQUAL expr { $$ = addOperation( LSL_NOT_EQUAL, $1, $3 ); } 63 | expr LSL_NOT_EQUAL expr { $$ = addOperation(&yylval, LSL_NOT_EQUAL, $1, $3); }
69 | expr LSL_EQUAL expr { $$ = addOperation( LSL_EQUAL, $1, $3 ); } 64 | expr LSL_EQUAL expr { $$ = addOperation(&yylval, LSL_EQUAL, $1, $3); }
70 | expr LSL_GREATER_EQUAL expr { $$ = addOperation( LSL_GREATER_EQUAL, $1, $3 ); } 65 | expr LSL_GREATER_EQUAL expr { $$ = addOperation(&yylval, LSL_GREATER_EQUAL, $1, $3); }
71 | expr LSL_LESS_EQUAL expr { $$ = addOperation( LSL_LESS_EQUAL, $1, $3 ); } 66 | expr LSL_LESS_EQUAL expr { $$ = addOperation(&yylval, LSL_LESS_EQUAL, $1, $3); }
72 | expr LSL_GREATER_THAN expr { $$ = addOperation( LSL_GREATER_THAN, $1, $3 ); } 67 | expr LSL_GREATER_THAN expr { $$ = addOperation(&yylval, LSL_GREATER_THAN, $1, $3); }
73 | expr LSL_LESS_THAN expr { $$ = addOperation( LSL_LESS_THAN, $1, $3 ); } 68 | expr LSL_LESS_THAN expr { $$ = addOperation(&yylval, LSL_LESS_THAN, $1, $3); }
74 | expr LSL_RIGHT_SHIFT expr { $$ = addOperation( LSL_RIGHT_SHIFT, $1, $3 ); } 69 | expr LSL_RIGHT_SHIFT expr { $$ = addOperation(&yylval, LSL_RIGHT_SHIFT, $1, $3); }
75 | expr LSL_LEFT_SHIFT expr { $$ = addOperation( LSL_LEFT_SHIFT, $1, $3 ); } 70 | expr LSL_LEFT_SHIFT expr { $$ = addOperation(&yylval, LSL_LEFT_SHIFT, $1, $3); }
76 | expr LSL_ADD expr { $$ = addOperation( LSL_ADD, $1, $3 ); } 71 | expr LSL_ADD expr { $$ = addOperation(&yylval, LSL_ADD, $1, $3); }
77 | expr LSL_SUBTRACT expr { $$ = addOperation( LSL_SUBTRACT, $1, $3 ); } 72 | expr LSL_SUBTRACT expr { $$ = addOperation(&yylval, LSL_SUBTRACT, $1, $3); }
78 | expr LSL_MULTIPLY expr { $$ = addOperation( LSL_MULTIPLY, $1, $3 ); } 73 | expr LSL_MULTIPLY expr { $$ = addOperation(&yylval, LSL_MULTIPLY, $1, $3); }
79 | expr LSL_MODULO expr { $$ = addOperation( LSL_MODULO, $1, $3 ); } 74 | expr LSL_MODULO expr { $$ = addOperation(&yylval, LSL_MODULO, $1, $3); }
80 | expr LSL_DIVIDE expr { $$ = addOperation( LSL_DIVIDE, $1, $3 ); } 75 | expr LSL_DIVIDE expr { $$ = addOperation(&yylval, LSL_DIVIDE, $1, $3); }
81 | LSL_BIT_NOT expr { $$ = addOperation( LSL_BIT_NOT, NULL, $2 ); } 76 | LSL_BIT_NOT expr { $$ = addOperation(&yylval, LSL_BIT_NOT, NULL, $2); }
82 | LSL_BOOL_NOT expr { $$ = addOperation( LSL_BOOL_NOT, NULL, $2 ); } 77 | LSL_BOOL_NOT expr { $$ = addOperation(&yylval, LSL_BOOL_NOT, NULL, $2); }
83 | LSL_SUBTRACT expr { $$ = addOperation( LSL_NEGATION, NULL, $2 ); } %prec LSL_NEGATION 78 | LSL_SUBTRACT expr { $$ = addOperation(&yylval, LSL_NEGATION, NULL, $2); } %prec LSL_NEGATION
84 | LSL_PARENTHESIS_OPEN expr LSL_PARENTHESIS_CLOSE { $$ = addParenthesis($2); } 79 | LSL_PARENTHESIS_OPEN expr LSL_PARENTHESIS_CLOSE { $$ = addParenthesis(&yylval, $2); }
85 | LSL_INTEGER { $$ = addInteger($1); } 80 | LSL_INTEGER { $$ = addInteger(&yylval, $1); }
86; 81;
87 82
88statement : expr LSL_STATEMENT { $$ = createStatement(LSL_EXPRESSION, $1); YYVALID; } 83statement : expr LSL_STATEMENT { $$ = createStatement(LSL_EXPRESSION, $1); YYVALID; }
diff --git a/LuaSL/test.lsl b/LuaSL/test.lsl
index ddfa533..40d98b3 100644
--- a/LuaSL/test.lsl
+++ b/LuaSL/test.lsl
@@ -1,2 +1,3 @@
14 + 2 * 10 + 3 * (5 + 1); 14 + /* c0 */ 2 /* c1 */ * /* c2 */ 10 /* c3 */ + /* c4 */
2 3 /* c5 */ * /* c6 */ (5 /* c7 */ + /* c8 */ 1);
2 3