aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.c140
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.h43
-rw-r--r--LuaSL/src/LuaSL_lexer.l158
-rw-r--r--LuaSL/src/LuaSL_yaccer.y5
4 files changed, 173 insertions, 173 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c
index ff7da8b..016dbc9 100644
--- a/LuaSL/src/LuaSL_LSL_tree.c
+++ b/LuaSL/src/LuaSL_LSL_tree.c
@@ -3,10 +3,10 @@
3#include <stdlib.h> 3#include <stdlib.h>
4#include <stdio.h> 4#include <stdio.h>
5 5
6static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); 6static void evaluateIntegerToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
7static void evaluateNoToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); 7static void evaluateNoToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
8static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); 8static void evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
9static void evaluateStatementToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); 9static void evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
10static void outputIntegerToken(LSL_Leaf *content); 10static void outputIntegerToken(LSL_Leaf *content);
11static void outputOperationToken(LSL_Leaf *content); 11static void outputOperationToken(LSL_Leaf *content);
12static void outputStatementToken(LSL_Leaf *content); 12static void outputStatementToken(LSL_Leaf *content);
@@ -14,8 +14,8 @@ static void outputSpaceToken(LSL_Leaf *content);
14 14
15LSL_Token LSL_Tokens[] = 15LSL_Token LSL_Tokens[] =
16{ 16{
17// {LSL_COMMENT, "/*", LSL_NONE, NULL, NULL, NULL}, 17 {LSL_COMMENT, "/*", LSL_NONE, NULL, NULL, NULL},
18// {LSL_COMMENT_LINE, "//", LSL_NONE, NULL, NULL, NULL}, 18 {LSL_COMMENT_LINE, "//", LSL_NONE, NULL, NULL, NULL},
19 {LSL_SPACE, " ", LSL_NONE, outputSpaceToken, NULL, NULL}, 19 {LSL_SPACE, " ", LSL_NONE, outputSpaceToken, NULL, NULL},
20 20
21 // Operators, in order of precedence, low to high 21 // Operators, in order of precedence, low to high
@@ -95,7 +95,7 @@ LSL_Token LSL_Tokens[] =
95 95
96 // Then the rest of the syntax tokens. 96 // Then the rest of the syntax tokens.
97 97
98// {LSL_IDENTIFIER, "identifier", LSL_NONE, NULL, NULL, NULL}, 98 {LSL_IDENTIFIER, "identifier", LSL_NONE, NULL, NULL, NULL},
99 99
100 {LSL_LABEL, "@", LSL_NONE, NULL, NULL, NULL}, 100 {LSL_LABEL, "@", LSL_NONE, NULL, NULL, NULL},
101 101
@@ -117,7 +117,7 @@ LSL_Token LSL_Tokens[] =
117// {LSL_STATE, "state", LSL_NONE, NULL, NULL, NULL}, 117// {LSL_STATE, "state", LSL_NONE, NULL, NULL, NULL},
118// {LSL_SCRIPT, "script", LSL_NONE, NULL, NULL, NULL}, 118// {LSL_SCRIPT, "script", LSL_NONE, NULL, NULL, NULL},
119 119
120// {LSL_UNKNOWN, "unknown", LSL_NONE, NULL, NULL, NULL}, 120 {LSL_UNKNOWN, "unknown", LSL_NONE, NULL, NULL, NULL},
121 121
122 // A sentinal. 122 // A sentinal.
123 123
@@ -136,9 +136,7 @@ static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right)
136 136
137 ast->left = left; 137 ast->left = left;
138 ast->right = right; 138 ast->right = right;
139 ast->line = -1; 139 ast->content.token = tokens[type - lowestToken];
140 ast->character = -1;
141 ast->token = tokens[type - lowestToken];
142 140
143 return ast; 141 return ast;
144} 142}
@@ -224,8 +222,7 @@ LSL_Statement *createStatement(LSL_Type type, LSL_AST *expr)
224 222
225 if (stat == NULL) return NULL; 223 if (stat == NULL) return NULL;
226 224
227 stat->type = type; 225 stat->expressions = expr;
228 stat->expression = expr;
229 226
230 return stat; 227 return stat;
231} 228}
@@ -240,21 +237,21 @@ LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root)
240 return checkAndAddIgnorable(ast); 237 return checkAndAddIgnorable(ast);
241} 238}
242 239
243static void evaluateAST(LSL_AST *ast, LSL_Value *left, LSL_Value *right) 240static void evaluateAST(LSL_AST *ast, LSL_Leaf *left, LSL_Leaf *right)
244{ 241{
245 if (ast) 242 if (ast)
246 { 243 {
247 LSL_Value lresult; 244 LSL_Leaf lresult;
248 LSL_Value rresult; 245 LSL_Leaf rresult;
249 246
250 memcpy(&lresult, left, sizeof(LSL_Value)); 247 memcpy(&lresult, left, sizeof(LSL_Leaf));
251 memcpy(&rresult, right, sizeof(LSL_Value)); 248 memcpy(&rresult, right, sizeof(LSL_Leaf));
252 249
253 if (LSL_RIGHT2LEFT & ast->token->flags) 250 if (LSL_RIGHT2LEFT & ast->content.token->flags)
254 { 251 {
255 memcpy(&rresult, left, sizeof(LSL_Value)); 252 memcpy(&rresult, left, sizeof(LSL_Leaf));
256 evaluateAST(ast->right, &rresult, right); 253 evaluateAST(ast->right, &rresult, right);
257 if (!(LSL_UNARY & ast->token->flags)) 254 if (!(LSL_UNARY & ast->content.token->flags))
258 { 255 {
259 evaluateAST(ast->left, &lresult, right); 256 evaluateAST(ast->left, &lresult, right);
260 } 257 }
@@ -262,50 +259,51 @@ static void evaluateAST(LSL_AST *ast, LSL_Value *left, LSL_Value *right)
262 else // Assume left to right. 259 else // Assume left to right.
263 { 260 {
264 evaluateAST(ast->left, &lresult, right); 261 evaluateAST(ast->left, &lresult, right);
265 if (!(LSL_UNARY & ast->token->flags)) 262 if (!(LSL_UNARY & ast->content.token->flags))
266 { 263 {
267 memcpy(&rresult, left, sizeof(LSL_Value)); 264 memcpy(&rresult, left, sizeof(LSL_Leaf));
268 evaluateAST(ast->right, &rresult, right); 265 evaluateAST(ast->right, &rresult, right);
269 } 266 }
270 } 267 }
271 268
272 if (ast->token->evaluate) 269 if (ast->content.token->evaluate)
273 { 270 {
274 ast->token->evaluate(&(ast->content), &lresult, &rresult); 271 ast->content.token->evaluate(&(ast->content), &lresult, &rresult);
275 memcpy(left, &lresult, sizeof(LSL_Value)); 272 memcpy(left, &lresult, sizeof(LSL_Leaf));
276 } 273 }
277 else 274 else
278 { 275 {
279#ifdef LUASL_DEBUG 276#ifdef LUASL_DEBUG
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); 277 printf(" eval <%s %d %d %d %d> ", ast->content.token->token, left->value.integerValue, right->value.integerValue, lresult.value.integerValue, rresult.value.integerValue);
281#endif 278#endif
282 memcpy(left, &rresult, sizeof(LSL_Value)); 279 memcpy(left, &rresult, sizeof(LSL_Leaf));
283 } 280 }
284 } 281 }
285} 282}
286 283
287static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) 284static void evaluateIntegerToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
288{ 285{
289 if (content) 286 if (content)
290 { 287 {
291#ifdef LUASL_DEBUG 288#ifdef LUASL_DEBUG
292 printf(" <%d> ", content->value.integerValue); 289 printf(" <%d> ", content->value.integerValue);
293#endif 290#endif
294 left->content.value.integerValue = content->value.integerValue; 291 left->value.integerValue = content->value.integerValue;
295 left->type = LSL_INTEGER; 292 left->type = LSL_INTEGER;
296 } 293 }
297} 294}
298 295
299static void evaluateNoToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) 296static void evaluateNoToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
300{ 297{
298 // Do nothing, that's the point.
301} 299}
302 300
303static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) 301static void evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
304{ 302{
305 if ((content) && (content->value.operationValue)) 303 if ((content) && (content->value.operationValue))
306 { 304 {
307#ifdef LUASL_DEBUG 305#ifdef LUASL_DEBUG
308 printf(" [%s] ", tokens[content->value.operationValue - lowestToken]->token); 306 printf(" [%s] ", content->token->token);
309#endif 307#endif
310 308
311 switch (content->value.operationValue) 309 switch (content->value.operationValue)
@@ -332,41 +330,41 @@ static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value
332// case LSL_TYPECAST_OPEN : 330// case LSL_TYPECAST_OPEN :
333// case LSL_TYPECAST_CLOSE : 331// case LSL_TYPECAST_CLOSE :
334 break; 332 break;
335 case LSL_BIT_NOT : left->content.value.integerValue = ~ right->content.value.integerValue; break; 333 case LSL_BIT_NOT : left->value.integerValue = ~ right->value.integerValue; break;
336 case LSL_BOOL_NOT : left->content.value.integerValue = ! right->content.value.integerValue; break; 334 case LSL_BOOL_NOT : left->value.integerValue = ! right->value.integerValue; break;
337 case LSL_NEGATION : left->content.value.integerValue = 0 - right->content.value.integerValue; break; 335 case LSL_NEGATION : left->value.integerValue = 0 - right->value.integerValue; break;
338 case LSL_DIVIDE : left->content.value.integerValue = left->content.value.integerValue / right->content.value.integerValue; break; 336 case LSL_DIVIDE : left->value.integerValue = left->value.integerValue / right->value.integerValue; break;
339 case LSL_MODULO : left->content.value.integerValue = left->content.value.integerValue % right->content.value.integerValue; break; 337 case LSL_MODULO : left->value.integerValue = left->value.integerValue % right->value.integerValue; break;
340 case LSL_MULTIPLY : left->content.value.integerValue = left->content.value.integerValue * right->content.value.integerValue; break; 338 case LSL_MULTIPLY : left->value.integerValue = left->value.integerValue * right->value.integerValue; break;
341// case LSL_DOT_PRODUCT : break; 339// case LSL_DOT_PRODUCT : break;
342// case LSL_CROSS_PRODUCT : break; 340// case LSL_CROSS_PRODUCT : break;
343 case LSL_SUBTRACT : left->content.value.integerValue = left->content.value.integerValue - right->content.value.integerValue; break; 341 case LSL_SUBTRACT : left->value.integerValue = left->value.integerValue - right->value.integerValue; break;
344 case LSL_ADD : left->content.value.integerValue = left->content.value.integerValue + right->content.value.integerValue; break; 342 case LSL_ADD : left->value.integerValue = left->value.integerValue + right->value.integerValue; break;
345// case LSL_CONCATENATE : break; 343// case LSL_CONCATENATE : break;
346 case LSL_LEFT_SHIFT : left->content.value.integerValue = left->content.value.integerValue << right->content.value.integerValue; break; 344 case LSL_LEFT_SHIFT : left->value.integerValue = left->value.integerValue << right->value.integerValue; break;
347 case LSL_RIGHT_SHIFT : left->content.value.integerValue = left->content.value.integerValue >> right->content.value.integerValue; break; 345 case LSL_RIGHT_SHIFT : left->value.integerValue = left->value.integerValue >> right->value.integerValue; break;
348 case LSL_LESS_THAN : left->content.value.integerValue = left->content.value.integerValue < right->content.value.integerValue; break; 346 case LSL_LESS_THAN : left->value.integerValue = left->value.integerValue < right->value.integerValue; break;
349 case LSL_GREATER_THAN : left->content.value.integerValue = left->content.value.integerValue > right->content.value.integerValue; break; 347 case LSL_GREATER_THAN : left->value.integerValue = left->value.integerValue > right->value.integerValue; break;
350 case LSL_LESS_EQUAL : left->content.value.integerValue = left->content.value.integerValue <= right->content.value.integerValue; break; 348 case LSL_LESS_EQUAL : left->value.integerValue = left->value.integerValue <= right->value.integerValue; break;
351 case LSL_GREATER_EQUAL : left->content.value.integerValue = left->content.value.integerValue >= right->content.value.integerValue; break; 349 case LSL_GREATER_EQUAL : left->value.integerValue = left->value.integerValue >= right->value.integerValue; break;
352 case LSL_EQUAL : left->content.value.integerValue = left->content.value.integerValue == right->content.value.integerValue; break; 350 case LSL_EQUAL : left->value.integerValue = left->value.integerValue == right->value.integerValue; break;
353 case LSL_NOT_EQUAL : left->content.value.integerValue = left->content.value.integerValue != right->content.value.integerValue; break; 351 case LSL_NOT_EQUAL : left->value.integerValue = left->value.integerValue != right->value.integerValue; break;
354 case LSL_BIT_AND : left->content.value.integerValue = left->content.value.integerValue & right->content.value.integerValue; break; 352 case LSL_BIT_AND : left->value.integerValue = left->value.integerValue & right->value.integerValue; break;
355 case LSL_BIT_XOR : left->content.value.integerValue = left->content.value.integerValue ^ right->content.value.integerValue; break; 353 case LSL_BIT_XOR : left->value.integerValue = left->value.integerValue ^ right->value.integerValue; break;
356 case LSL_BIT_OR : left->content.value.integerValue = left->content.value.integerValue | right->content.value.integerValue; break; 354 case LSL_BIT_OR : left->value.integerValue = left->value.integerValue | right->value.integerValue; break;
357 case LSL_BOOL_OR : left->content.value.integerValue = left->content.value.integerValue || right->content.value.integerValue; break; 355 case LSL_BOOL_OR : left->value.integerValue = left->value.integerValue || right->value.integerValue; break;
358 case LSL_BOOL_AND : left->content.value.integerValue = left->content.value.integerValue && right->content.value.integerValue; break; 356 case LSL_BOOL_AND : left->value.integerValue = left->value.integerValue && right->value.integerValue; break;
359 } 357 }
360#ifdef LUASL_DEBUG 358#ifdef LUASL_DEBUG
361 printf(" (=%d) ", left->content.value.integerValue); 359 printf(" (=%d) ", left->value.integerValue);
362#endif 360#endif
363 } 361 }
364} 362}
365 363
366static void evaluateStatementToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) 364static void evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
367{ 365{
368 if (content) 366 if (content)
369 evaluateAST(content->value.statementValue->expression, left, right); 367 evaluateAST(content->value.statementValue->expressions, left, right);
370} 368}
371 369
372static void outputAST(LSL_AST *ast) 370static void outputAST(LSL_AST *ast)
@@ -374,10 +372,10 @@ static void outputAST(LSL_AST *ast)
374 if (ast) 372 if (ast)
375 { 373 {
376 outputAST(ast->left); 374 outputAST(ast->left);
377 if (ast->token->output) 375 if (ast->content.token->output)
378 ast->token->output(&(ast->content)); 376 ast->content.token->output(&(ast->content));
379 else 377 else
380 printf("%s", ast->token->token); 378 printf("%s", ast->content.token->token);
381 outputAST(ast->right); 379 outputAST(ast->right);
382 } 380 }
383} 381}
@@ -391,13 +389,13 @@ static void outputIntegerToken(LSL_Leaf *content)
391static void outputOperationToken(LSL_Leaf *content) 389static void outputOperationToken(LSL_Leaf *content)
392{ 390{
393 if (content) 391 if (content)
394 printf("%s", tokens[content->value.operationValue - lowestToken]->token); 392 printf("%s", content->token->token);
395} 393}
396 394
397static void outputStatementToken(LSL_Leaf *content) 395static void outputStatementToken(LSL_Leaf *content)
398{ 396{
399 if (content) 397 if (content)
400 outputAST(content->value.statementValue->expression); 398 outputAST(content->value.statementValue->expressions);
401 printf(";"); 399 printf(";");
402} 400}
403 401
@@ -412,12 +410,12 @@ static void convertAST2Lua(LSL_AST *ast)
412 if (ast) 410 if (ast)
413 { 411 {
414 convertAST2Lua(ast->left); 412 convertAST2Lua(ast->left);
415 if (ast->token->convert) 413 if (ast->content.token->convert)
416 ast->token->convert(&(ast->content)); 414 ast->content.token->convert(&(ast->content));
417 else if (ast->token->output) 415 else if (ast->content.token->output)
418 ast->token->output(&(ast->content)); 416 ast->content.token->output(&(ast->content));
419 else 417 else
420 printf("%s", ast->token->token); 418 printf("%s", ast->content.token->token);
421 convertAST2Lua(ast->right); 419 convertAST2Lua(ast->right);
422 } 420 }
423} 421}
@@ -539,11 +537,11 @@ int main(int argc, char **argv)
539 537
540 if (ast) 538 if (ast)
541 { 539 {
542 LSL_Value left, right; 540 LSL_Leaf left, right;
543 541
544 left.content.value.integerValue = 0; 542 left.value.integerValue = 0;
545 left.type = LSL_INTEGER; 543 left.type = LSL_INTEGER;
546 right.content.value.integerValue = 0; 544 right.value.integerValue = 0;
547 right.type = LSL_INTEGER; 545 right.type = LSL_INTEGER;
548 evaluateAST(ast, &left, &right); 546 evaluateAST(ast, &left, &right);
549 547
@@ -553,7 +551,7 @@ int main(int argc, char **argv)
553 printf("Result of -\n"); 551 printf("Result of -\n");
554 outputAST(ast); 552 outputAST(ast);
555 printf("\n"); 553 printf("\n");
556 printf("is %d %d. And converted to Lua it is -\n", left.content.value.integerValue, right.content.value.integerValue); 554 printf("is %d %d. And converted to Lua it is -\n", left.value.integerValue, right.value.integerValue);
557 convertAST2Lua(ast); 555 convertAST2Lua(ast);
558 printf("\n"); 556 printf("\n");
559 burnAST(ast); 557 burnAST(ast);
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h
index fe56c29..5d39b3b 100644
--- a/LuaSL/src/LuaSL_LSL_tree.h
+++ b/LuaSL/src/LuaSL_LSL_tree.h
@@ -20,8 +20,8 @@ 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 struct _LSL_Token LSL_Token;
23typedef struct _LSL_Leaf LSL_Leaf; 24typedef struct _LSL_Leaf LSL_Leaf;
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;
27typedef struct _LSL_Block LSL_Block; 27typedef struct _LSL_Block LSL_Block;
@@ -30,11 +30,14 @@ typedef struct _LSL_State LSL_State;
30typedef struct _LSL_Script LSL_Script; 30typedef struct _LSL_Script LSL_Script;
31typedef struct _LSL_AST LSL_AST; 31typedef struct _LSL_AST LSL_AST;
32 32
33extern LSL_Token **tokens;
34extern int lowestToken;
35
33typedef int LSL_Type; 36typedef int LSL_Type;
34 37
35typedef void (*convertToken2Lua) (LSL_Leaf *content); 38typedef void (*convertToken2Lua) (LSL_Leaf *content);
36typedef void (*outputToken) (LSL_Leaf *content); 39typedef void (*outputToken) (LSL_Leaf *content);
37typedef void (*evaluateToken) (LSL_Leaf *content, LSL_Value *left, LSL_Value *right); 40typedef void (*evaluateToken) (LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
38 41
39#ifndef FALSE 42#ifndef FALSE
40typedef enum 43typedef enum
@@ -55,7 +58,7 @@ typedef enum
55 LSL_CREATION = 32 58 LSL_CREATION = 32
56} LSL_Flags; 59} LSL_Flags;
57 60
58typedef struct 61struct _LSL_Token
59{ 62{
60 LSL_Type type; 63 LSL_Type type;
61 char *token; 64 char *token;
@@ -63,7 +66,7 @@ typedef struct
63 outputToken output; 66 outputToken output;
64 convertToken2Lua convert; 67 convertToken2Lua convert;
65 evaluateToken evaluate; 68 evaluateToken evaluate;
66} LSL_Token; 69};
67 70
68struct _LSL_Leaf 71struct _LSL_Leaf
69{ 72{
@@ -72,13 +75,13 @@ struct _LSL_Leaf
72 char *commentValue; 75 char *commentValue;
73 char *spaceValue; 76 char *spaceValue;
74 77
75 LSL_Type operationValue; 78 LSL_Type operationValue;
76 LSL_AST *expressionValue; 79 LSL_AST *expressionValue;
77 80
78 float floatValue; 81 float floatValue;
79 int integerValue; 82 int integerValue;
80 char *keyValue; 83 char *keyValue;
81 LSL_Leaf *listValue; 84 LSL_Leaf *listValue;
82 char *stringValue; 85 char *stringValue;
83 float vectorValue[3]; 86 float vectorValue[3];
84 float rotationValue[4]; 87 float rotationValue[4];
@@ -97,34 +100,29 @@ struct _LSL_Leaf
97 LSL_Statement *whileValue; 100 LSL_Statement *whileValue;
98 LSL_Statement *statementValue; 101 LSL_Statement *statementValue;
99 102
100 LSL_Block *blockValue; 103 LSL_Block *blockValue;
101 LSL_Identifier *parameterValue; 104 LSL_Identifier *parameterValue;
102 LSL_Function *functionValue; 105 LSL_Function *functionValue;
103 LSL_State *stateValue; 106 LSL_State *stateValue;
104 LSL_Script *scriptValue; 107 LSL_Script *scriptValue;
105 108
106 char *unknownValue; 109 char *unknownValue;
107 } value; 110 } value;
108 char *ignorableText; 111 char *ignorableText;
109 int line, column; 112 LSL_Token *token;
110};
111
112struct _LSL_Value
113{
114 LSL_Leaf content;
115 LSL_Type type; 113 LSL_Type type;
114 int line, column;
116}; 115};
117 116
118struct _LSL_Identifier // For variables and function parameters. 117struct _LSL_Identifier // For variables and function parameters.
119{ 118{
120 char *name; 119 char *name;
121 LSL_Value value; 120 LSL_Leaf value;
122}; 121};
123 122
124struct _LSL_Statement 123struct _LSL_Statement
125{ 124{
126 LSL_AST *expression; 125 LSL_AST *expressions; /// For things like a for statement, might hold three expressions.
127 LSL_Type type;
128}; 126};
129 127
130struct _LSL_Block 128struct _LSL_Block
@@ -137,7 +135,7 @@ struct _LSL_Function
137 char *name; 135 char *name;
138 LSL_Block block; 136 LSL_Block block;
139 LSL_Identifier *parameters; 137 LSL_Identifier *parameters;
140 LSL_Type type; 138 LSL_Type type; // Return type.
141}; 139};
142 140
143struct _LSL_State 141struct _LSL_State
@@ -158,10 +156,7 @@ struct _LSL_AST
158{ 156{
159 LSL_AST *left; 157 LSL_AST *left;
160 LSL_AST *right; 158 LSL_AST *right;
161 LSL_Token *token;
162 LSL_Leaf content; 159 LSL_Leaf content;
163 int line;
164 int character;
165}; 160};
166 161
167 162
diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l
index 08a68ef..30e0b6b 100644
--- a/LuaSL/src/LuaSL_lexer.l
+++ b/LuaSL/src/LuaSL_lexer.l
@@ -5,8 +5,7 @@
5#include <stdio.h> 5#include <stdio.h>
6 6
7void comment(yyscan_t yyscanner); 7void comment(yyscan_t yyscanner);
8void common(YYSTYPE *lval, char *text, boolean checkIgnorable); 8int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type);
9void ignorable(char *text);
10 9
11%} 10%}
12 11
@@ -22,90 +21,90 @@ EXPONANT [eE][+-]?{INTEGER}
22FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}? 21FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}?
23CHAR '(\\.|[^\\'\n])+' 22CHAR '(\\.|[^\\'\n])+'
24STRING \"(\\.|[^\\"\n])*\" 23STRING \"(\\.|[^\\"\n])*\"
25NAME [[:alpha:]](_|[[:alpha:]]|[[:digit:]])* 24IDENTIFIER [[:alpha:]](_|[[:alpha:]]|[[:digit:]])*
26 25
27%% 26%%
28 27
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. */ 28 /* 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. */
30 29
31 /* Ignorables. */ 30 /* Ignorables. */
32[[:space:]]+ %{ common(yylval, yytext, FALSE); ignorable(yytext); %} 31[[:space:]]+ %{ common(yylval, yytext, FALSE, LSL_SPACE); %}
33 /* Yes I know this will have problems with huge comments, just being simple to get it to work for now. */ 32 /* Yes I know this will have problems with huge comments, just being simple to get it to work for now. */
34"/*"([^"*/"]*)"*/" %{ common(yylval, yytext, FALSE); ignorable(yytext); %} 33"/*"([^"*/"]*)"*/" %{ common(yylval, yytext, FALSE, LSL_COMMENT); %}
35"//"[^\n]* %{ common(yylval, yytext, FALSE); ignorable(yytext); %} 34"//"[^\n]* %{ common(yylval, yytext, FALSE, LSL_COMMENT_LINE); %}
36 35
37 /* Operations. */ 36 /* Operations. */
38"&&" { common(yylval, yytext, TRUE); return LSL_BOOL_AND; } 37"&&" { return common(yylval, yytext, TRUE, LSL_BOOL_AND); }
39"||" { common(yylval, yytext, TRUE); return LSL_BOOL_OR; } 38"||" { return common(yylval, yytext, TRUE, LSL_BOOL_OR); }
40"|" { common(yylval, yytext, TRUE); return LSL_BIT_OR; } 39"|" { return common(yylval, yytext, TRUE, LSL_BIT_OR); }
41"^" { common(yylval, yytext, TRUE); return LSL_BIT_XOR; } 40"^" { return common(yylval, yytext, TRUE, LSL_BIT_XOR); }
42"&" { common(yylval, yytext, TRUE); return LSL_BIT_AND; } 41"&" { return common(yylval, yytext, TRUE, LSL_BIT_AND); }
43"!=" { common(yylval, yytext, TRUE); return LSL_NOT_EQUAL; } 42"!=" { return common(yylval, yytext, TRUE, LSL_NOT_EQUAL); }
44"==" { common(yylval, yytext, TRUE); return LSL_EQUAL; } 43"==" { return common(yylval, yytext, TRUE, LSL_EQUAL); }
45">=" { common(yylval, yytext, TRUE); return LSL_GREATER_EQUAL; } 44">=" { return common(yylval, yytext, TRUE, LSL_GREATER_EQUAL); }
46"<=" { common(yylval, yytext, TRUE); return LSL_LESS_EQUAL; } 45"<=" { return common(yylval, yytext, TRUE, LSL_LESS_EQUAL); }
47">" { common(yylval, yytext, TRUE); return LSL_GREATER_THAN; } 46">" { return common(yylval, yytext, TRUE, LSL_GREATER_THAN); }
48"<" { common(yylval, yytext, TRUE); return LSL_LESS_THAN; } 47"<" { return common(yylval, yytext, TRUE, LSL_LESS_THAN); }
49">>" { common(yylval, yytext, TRUE); return LSL_RIGHT_SHIFT; } 48">>" { return common(yylval, yytext, TRUE, LSL_RIGHT_SHIFT); }
50"<<" { common(yylval, yytext, TRUE); return LSL_LEFT_SHIFT; } 49"<<" { return common(yylval, yytext, TRUE, LSL_LEFT_SHIFT); }
51"+" { common(yylval, yytext, TRUE); return LSL_ADD; } 50"+" { return common(yylval, yytext, TRUE, LSL_ADD); }
52"-" { common(yylval, yytext, TRUE); return LSL_SUBTRACT; } 51"-" { return common(yylval, yytext, TRUE, LSL_SUBTRACT); }
53"*" { common(yylval, yytext, TRUE); return LSL_MULTIPLY; } 52"*" { return common(yylval, yytext, TRUE, LSL_MULTIPLY); }
54"%" { common(yylval, yytext, TRUE); return LSL_MODULO; } 53"%" { return common(yylval, yytext, TRUE, LSL_MODULO); }
55"/" { common(yylval, yytext, TRUE); return LSL_DIVIDE; } 54"/" { return common(yylval, yytext, TRUE, LSL_DIVIDE); }
56"!" { common(yylval, yytext, TRUE); return LSL_BOOL_NOT; } 55"!" { return common(yylval, yytext, TRUE, LSL_BOOL_NOT); }
57"~" { common(yylval, yytext, TRUE); return LSL_BIT_NOT; } 56"~" { return common(yylval, yytext, TRUE, LSL_BIT_NOT); }
58"[" { common(yylval, yytext, TRUE); return LSL_BRACKET_OPEN; } 57"[" { return common(yylval, yytext, TRUE, LSL_BRACKET_OPEN); }
59"]" { common(yylval, yytext, TRUE); return LSL_BRACKET_CLOSE; } 58"]" { return common(yylval, yytext, TRUE, LSL_BRACKET_CLOSE); }
60"(" { common(yylval, yytext, TRUE); return LSL_PARENTHESIS_OPEN; } 59"(" { return common(yylval, yytext, TRUE, LSL_PARENTHESIS_OPEN); }
61")" { common(yylval, yytext, TRUE); return LSL_PARENTHESIS_CLOSE; } 60")" { return common(yylval, yytext, TRUE, LSL_PARENTHESIS_CLOSE); }
62"+=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_ADD; } 61"+=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_ADD); }
63"-=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_SUBTRACT; } 62"-=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_SUBTRACT); }
64"*=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_MULTIPLY; } 63"*=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_MULTIPLY); }
65"%=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_MODULO; } 64"%=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_MODULO); }
66"/=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_DIVIDE; } 65"/=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_DIVIDE); }
67"=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_PLAIN; } 66"=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_PLAIN); }
68"." { common(yylval, yytext, TRUE); return LSL_DOT; } 67"." { return common(yylval, yytext, TRUE, LSL_DOT); }
69"--" { common(yylval, yytext, TRUE); return LSL_DECREMENT_PRE; } 68"--" { return common(yylval, yytext, TRUE, LSL_DECREMENT_PRE); }
70"++" { common(yylval, yytext, TRUE); return LSL_INCREMENT_PRE; } 69"++" { return common(yylval, yytext, TRUE, LSL_INCREMENT_PRE); }
71"," { common(yylval, yytext, TRUE); return LSL_COMMA; } 70"," { return common(yylval, yytext, TRUE, LSL_COMMA); }
72 71
73 /* Types. */ 72 /* Types. */
74{INTEGER} %{ common(yylval, yytext, TRUE); yylval->value.integerValue = atoi(yytext); return LSL_INTEGER; %} 73{INTEGER} %{ yylval->value.integerValue = atoi(yytext); return common(yylval, yytext, TRUE, LSL_INTEGER); %}
75{FLOAT} %{ common(yylval, yytext, TRUE); yylval->value.floatValue = atof(yytext); return LSL_FLOAT; %} 74{FLOAT} %{ yylval->value.floatValue = atof(yytext); return common(yylval, yytext, TRUE, LSL_FLOAT); %}
76 75
77 /* Type keywords. */ 76 /* Type keywords. */
78"float" %{ common(yylval, yytext, TRUE); return LSL_TYPE_FLOAT; %} 77"float" %{ return common(yylval, yytext, TRUE, LSL_TYPE_FLOAT); %}
79"integer" %{ common(yylval, yytext, TRUE); return LSL_TYPE_INTEGER; %} 78"integer" %{ return common(yylval, yytext, TRUE, LSL_TYPE_INTEGER); %}
80"key" %{ common(yylval, yytext, TRUE); return LSL_TYPE_KEY; %} 79"key" %{ return common(yylval, yytext, TRUE, LSL_TYPE_KEY); %}
81"list" %{ common(yylval, yytext, TRUE); return LSL_TYPE_LIST; %} 80"list" %{ return common(yylval, yytext, TRUE, LSL_TYPE_LIST); %}
82"quaternion" %{ common(yylval, yytext, TRUE); return LSL_TYPE_ROTATION; %} 81"quaternion" %{ return common(yylval, yytext, TRUE, LSL_TYPE_ROTATION); %}
83"rotation" %{ common(yylval, yytext, TRUE); return LSL_TYPE_ROTATION; %} 82"rotation" %{ return common(yylval, yytext, TRUE, LSL_TYPE_ROTATION); %}
84"string" %{ common(yylval, yytext, TRUE); return LSL_TYPE_STRING; %} 83"string" %{ return common(yylval, yytext, TRUE, LSL_TYPE_STRING); %}
85"vector" %{ common(yylval, yytext, TRUE); return LSL_TYPE_VECTOR; %} 84"vector" %{ return common(yylval, yytext, TRUE, LSL_TYPE_VECTOR); %}
86 85
87 /* Statement keywords. */ 86 /* Statement keywords. */
88"do" %{ common(yylval, yytext, TRUE); return LSL_DO; %} 87"do" %{ return common(yylval, yytext, TRUE, LSL_DO); %}
89"for" %{ common(yylval, yytext, TRUE); return LSL_FOR; %} 88"for" %{ return common(yylval, yytext, TRUE, LSL_FOR); %}
90"else" %{ common(yylval, yytext, TRUE); return LSL_ELSE; %} 89"else" %{ return common(yylval, yytext, TRUE, LSL_ELSE); %}
91"if" %{ common(yylval, yytext, TRUE); return LSL_IF; %} 90"if" %{ return common(yylval, yytext, TRUE, LSL_IF); %}
92"jump" %{ common(yylval, yytext, TRUE); return LSL_JUMP; %} 91"jump" %{ return common(yylval, yytext, TRUE, LSL_JUMP); %}
93"return" %{ common(yylval, yytext, TRUE); return LSL_RETURN; %} 92"return" %{ return common(yylval, yytext, TRUE, LSL_RETURN); %}
94"state" %{ common(yylval, yytext, TRUE); return LSL_STATE_CHANGE; %} 93"state" %{ return common(yylval, yytext, TRUE, LSL_STATE_CHANGE); %}
95"while" %{ common(yylval, yytext, TRUE); return LSL_WHILE; %} 94"while" %{ return common(yylval, yytext, TRUE, LSL_WHILE); %}
96 95
97{NAME} %{ common(yylval, yytext, TRUE); /* yylval->value.nameValue = strdup(yytext); return LSL_NAME; */ %} 96{IDENTIFIER} %{ /* yylval->value.nameValue = strdup(yytext); */ common(yylval, yytext, TRUE, LSL_IDENTIFIER); %}
98 97
99 /* Other symbols. */ 98 /* Other symbols. */
100"@" %{ common(yylval, yytext, TRUE); return LSL_LABEL; %} 99"@" %{ return common(yylval, yytext, TRUE, LSL_LABEL); %}
101"{" %{ common(yylval, yytext, TRUE); return LSL_BLOCK_OPEN; %} 100"{" %{ return common(yylval, yytext, TRUE, LSL_BLOCK_OPEN); %}
102"}" %{ common(yylval, yytext, TRUE); return LSL_BLOCK_CLOSE; %} 101"}" %{ return common(yylval, yytext, TRUE, LSL_BLOCK_CLOSE); %}
103";" %{ common(yylval, yytext, TRUE); return LSL_STATEMENT; %} 102";" %{ return common(yylval, yytext, TRUE, LSL_STATEMENT); %}
104 103
105<<EOF>> { yyterminate(); } 104<<EOF>> { yyterminate(); }
106 105
107 /* Everything else */ 106 /* Everything else */
108. %{ common(yylval, yytext, TRUE); printf(" unexpected character.\n"); %} 107. %{ printf(" unexpected character.\n"); yylval->value.unknownValue = strdup(yytext); common(yylval, yytext, TRUE, LSL_UNKNOWN); %}
109 108
110%% 109%%
111 110
@@ -126,7 +125,7 @@ static char *ignorableText = NULL;
126static int column = 0; 125static int column = 0;
127static int line = 0; 126static int line = 0;
128 127
129void common(YYSTYPE *lval, char *text, boolean checkIgnorable) 128int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type)
130{ 129{
131 int i; 130 int i;
132 131
@@ -141,6 +140,8 @@ void common(YYSTYPE *lval, char *text, boolean checkIgnorable)
141 else 140 else
142 column++; 141 column++;
143 142
143 lval->type = type;
144 lval->token = tokens[type - lowestToken];
144 lval->line = line; 145 lval->line = line;
145 lval->column = column; 146 lval->column = column;
146 147
@@ -149,24 +150,25 @@ void common(YYSTYPE *lval, char *text, boolean checkIgnorable)
149 lval->ignorableText = ignorableText; 150 lval->ignorableText = ignorableText;
150 ignorableText = NULL; 151 ignorableText = NULL;
151 } 152 }
153 else
154 {
155 if (ignorableText)
156 {
157 int lenI = strlen(ignorableText);
158 int lenT = strlen(text);
159
160 ignorableText = realloc(ignorableText, lenI + lenT + 1);
161 sprintf(&ignorableText[lenI], "%s", text);
162 }
163 else
164 ignorableText = strdup(text);
165 }
152 166
153#ifdef LUASL_DEBUG 167#ifdef LUASL_DEBUG
154 printf ("%04d, %04d [%s]\n", line, column, text); 168 printf ("%04d, %04d [%s]\n", line, column, text);
155#endif 169#endif
156}
157
158void ignorable(char *text)
159{
160 if (ignorableText)
161 {
162 int lenI = strlen(ignorableText);
163 int lenT = strlen(text);
164 170
165 ignorableText = realloc(ignorableText, lenI + lenT + 1); 171 return type;
166 sprintf(&ignorableText[lenI], "%s", text);
167 }
168 else
169 ignorableText = strdup(text);
170} 172}
171 173
172int yyerror(const char *msg) 174int yyerror(const char *msg)
diff --git a/LuaSL/src/LuaSL_yaccer.y b/LuaSL/src/LuaSL_yaccer.y
index ed69dd3..2176065 100644
--- a/LuaSL/src/LuaSL_yaccer.y
+++ b/LuaSL/src/LuaSL_yaccer.y
@@ -11,6 +11,11 @@
11 11
12 12
13%token <value.spaceValue> LSL_SPACE /* Never actually emitted, but we need it in the token table. */ 13%token <value.spaceValue> LSL_SPACE /* Never actually emitted, but we need it in the token table. */
14%token <value.commentValue> LSL_COMMENT /* Never actually emitted, but we need it in the token table. */
15%token <value.commentValue> LSL_COMMENT_LINE /* Never actually emitted, but we need it in the token table. */
16%token <value.unknownValue> LSL_UNKNOWN /* Never actually emitted, but we need it in the token table. */
17
18%token <value.variableValue> LSL_IDENTIFIER
14 19
15%type <value.expressionValue> expr 20%type <value.expressionValue> expr
16%left LSL_BOOL_AND 21%left LSL_BOOL_AND