aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/src/LuaSL_LSL_tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'LuaSL/src/LuaSL_LSL_tree.c')
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.c306
1 files changed, 167 insertions, 139 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c
index 88768e0..8462112 100644
--- a/LuaSL/src/LuaSL_LSL_tree.c
+++ b/LuaSL/src/LuaSL_LSL_tree.c
@@ -1,7 +1,3 @@
1/*
2 * Implementation of functions used to build the abstract syntax tree.
3 */
4
5 1
6#define LSL_Keywords_define 2#define LSL_Keywords_define
7#define LSL_Tokens_define 3#define LSL_Tokens_define
@@ -10,35 +6,38 @@
10#include <stdio.h> 6#include <stdio.h>
11 7
12 8
13/** 9static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right)
14 * @brief Allocates space for an AST leaf
15 * @return The expression or NULL if not enough memory
16 */
17
18/*
19static LSL_AST *newLeaf(LSL_Type type, LSL_AST *left, LSL_AST *right)
20{ 10{
21 LSL_AST *leaf = malloc(sizeof(LSL_AST)); 11 LSL_AST *ast = malloc(sizeof(LSL_AST));
12
13 if (ast == NULL) return NULL;
22 14
23 if (leaf == NULL) return NULL; 15 ast->type = type;
16 ast->left = left;
17 ast->right = right;
18 ast->line = -1;
19 ast->character = -1;
24 20
25 leaf->type = type; 21 return ast;
26 leaf->left = left; 22}
27 leaf->right = right; 23
28 leaf->line = -1; 24void burnAST(LSL_AST *ast)
29 leaf->character = -1; 25{
26 if (ast == NULL) return;
30 27
31 return leaf; 28 burnAST(ast->left);
29 burnAST(ast->right);
30 free(ast);
32} 31}
33*/
34 32
35void burnLeaf(LSL_AST *leaf) 33LSL_AST *addExpression(LSL_Expression *exp)
36{ 34{
37 if (leaf == NULL) return; 35 LSL_AST *ast = newAST(LSL_EXPRESSION, NULL, NULL);
38 36
39 burnLeaf(leaf->left); 37 if (ast)
40 burnLeaf(leaf->right); 38 ast->content.expressionValue = exp;
41 free(leaf); 39
40 return ast;
42} 41}
43 42
44static LSL_Expression *newLSLExpression(LSL_Type type, LSL_Expression *left, LSL_Expression *right) 43static LSL_Expression *newLSLExpression(LSL_Type type, LSL_Expression *left, LSL_Expression *right)
@@ -50,7 +49,6 @@ static LSL_Expression *newLSLExpression(LSL_Type type, LSL_Expression *left, LSL
50 exp->type = type; 49 exp->type = type;
51 exp->left = left; 50 exp->left = left;
52 exp->right = right; 51 exp->right = right;
53 exp->expression=0;
54 52
55 return exp; 53 return exp;
56} 54}
@@ -69,7 +67,7 @@ LSL_Expression *addInteger(int value)
69 LSL_Expression *exp = newLSLExpression(LSL_INTEGER, NULL, NULL); 67 LSL_Expression *exp = newLSLExpression(LSL_INTEGER, NULL, NULL);
70 68
71 if (exp) 69 if (exp)
72 exp->value.integerValue = value; 70 exp->content.integerValue = value;
73 71
74 return exp; 72 return exp;
75} 73}
@@ -79,41 +77,96 @@ LSL_Expression *addOperation(LSL_Operation type, LSL_Expression *left, LSL_Expre
79 LSL_Expression *exp = newLSLExpression(LSL_EXPRESSION, left, right); 77 LSL_Expression *exp = newLSLExpression(LSL_EXPRESSION, left, right);
80 78
81 if (exp) 79 if (exp)
82 exp->expression = type; 80 exp->content.operationValue = type;
83 81
84 return exp; 82 return exp;
85} 83}
86 84
87LSL_Expression *newTree(const char *expr) 85int evaluateExpression(LSL_Expression *exp, int old)
88{ 86{
89 LuaSL_yyparseParam param; 87 if (NULL == exp)
90 YY_BUFFER_STATE state; 88 return old;
91
92#ifdef LUASL_DEBUG 89#ifdef LUASL_DEBUG
93 yydebug= 5; 90 #ifdef LUASL_USE_ENUM
91 printf(" %s ", LSL_Tokens[exp->content.operationValue - LSL_COMMA].token);
92 #else
93 printf(" # ");
94 #endif
94#endif 95#endif
95 96
96 param.expression = NULL; 97 if (LSL_INTEGER == exp->type)
97 if (yylex_init(&(param.scanner))) 98 {
98 return NULL;
99
100#ifdef LUASL_DEBUG 99#ifdef LUASL_DEBUG
101 yyset_debug(1, param.scanner); 100 printf("%d", exp->content.integerValue);
102#endif 101#endif
102 return exp->content.integerValue;
103 }
103 104
104 state = yy_scan_string(expr, param.scanner); 105 switch (exp->content.operationValue)
105 if (yyparse(&param)) 106 {
106 return NULL; 107#ifdef LUASL_USE_ENUM
107 108 case LSL_COMMA :
108 yy_delete_buffer(state, param.scanner); 109 case LSL_INCREMENT_PRE :
109 yylex_destroy(param.scanner); 110 case LSL_INCREMENT_POST :
111 case LSL_DECREMENT_PRE :
112 case LSL_DECREMENT_POST :
113 case LSL_DOT :
114 case LSL_ASSIGNMENT_PLAIN :
115 case LSL_ASSIGNMENT_DIVIDE :
116 case LSL_ASSIGNMENT_MODULO :
117 case LSL_ASSIGNMENT_MULTIPLY :
118 case LSL_ASSIGNMENT_SUBTRACT :
119 case LSL_ASSIGNMENT_ADD :
120 case LSL_ASSIGNMENT_CONCATENATE :
121 case LSL_PARENTHESIS_OPEN :
122 case LSL_PARENTHESIS_CLOSE :
123 case LSL_BRACKET_OPEN :
124 case LSL_BRACKET_CLOSE :
125 case LSL_ANGLE_OPEN :
126 case LSL_ANGLE_CLOSE :
127 case LSL_TYPECAST :
128 break;
129#endif
130 case LSL_BIT_NOT : return ~ evaluateExpression(exp->right, old);
131 case LSL_BOOL_NOT : return ! evaluateExpression(exp->right, old);
132 case LSL_NEGATION : return 0 - evaluateExpression(exp->right, old);
133 case LSL_DIVIDE : return evaluateExpression(exp->left, old) / evaluateExpression(exp->right, old);
134#ifdef LUASL_USE_ENUM
135 case LSL_MODULO : return evaluateExpression(exp->left, old) % evaluateExpression(exp->right, old);
136#endif
137 case LSL_MULTIPLY : return evaluateExpression(exp->left, old) * evaluateExpression(exp->right, old);
138#ifdef LUASL_USE_ENUM
139 case LSL_DOT_PRODUCT : break;
140 case LSL_CROSS_PRODUCT : break;
141#endif
142 case LSL_SUBTRACT : return evaluateExpression(exp->left, old) - evaluateExpression(exp->right, old);
143 case LSL_ADD : return evaluateExpression(exp->left, old) + evaluateExpression(exp->right, old);
144#ifdef LUASL_USE_ENUM
145 case LSL_CONCATENATE : break;
146#endif
147 case LSL_LEFT_SHIFT : return evaluateExpression(exp->left, old) << evaluateExpression(exp->right, old);
148 case LSL_RIGHT_SHIFT : return evaluateExpression(exp->left, old) >> evaluateExpression(exp->right, old);
149 case LSL_LESS_THAN : return evaluateExpression(exp->left, old) < evaluateExpression(exp->right, old);
150 case LSL_GREATER_THAN : return evaluateExpression(exp->left, old) > evaluateExpression(exp->right, old);
151 case LSL_LESS_EQUAL : return evaluateExpression(exp->left, old) <= evaluateExpression(exp->right, old);
152 case LSL_GREATER_EQUAL : return evaluateExpression(exp->left, old) >= evaluateExpression(exp->right, old);
153 case LSL_EQUAL : return evaluateExpression(exp->left, old) == evaluateExpression(exp->right, old);
154 case LSL_NOT_EQUAL : return evaluateExpression(exp->left, old) != evaluateExpression(exp->right, old);
155 case LSL_BIT_AND : return evaluateExpression(exp->left, old) & evaluateExpression(exp->right, old);
156 case LSL_BIT_XOR : return evaluateExpression(exp->left, old) ^ evaluateExpression(exp->right, old);
157 case LSL_BIT_OR : return evaluateExpression(exp->left, old) | evaluateExpression(exp->right, old);
158 case LSL_BOOL_OR : return evaluateExpression(exp->left, old) || evaluateExpression(exp->right, old);
159 case LSL_BOOL_AND : return evaluateExpression(exp->left, old) && evaluateExpression(exp->right, old);
160 }
110 161
111 return param.expression; 162 return old;
112} 163}
113 164
114int evaluateExpression(LSL_Expression *exp, int old) 165int evaluateAST(LSL_AST *ast, int old)
115{ 166{
116 switch(exp->type) 167 if (NULL == ast)
168 return old;
169 switch(ast->type)
117 { 170 {
118#ifdef LUASL_USE_ENUM 171#ifdef LUASL_USE_ENUM
119 case LSL_COMMENT : 172 case LSL_COMMENT :
@@ -121,13 +174,13 @@ int evaluateExpression(LSL_Expression *exp, int old)
121 case LSL_NAME : 174 case LSL_NAME :
122 case LSL_IDENTIFIER : 175 case LSL_IDENTIFIER :
123 break; 176 break;
124 case LSL_FLOAT : return (int) exp->value.floatValue; 177 case LSL_FLOAT : return (int) ast->content.floatValue;
125#endif 178#endif
126 case LSL_INTEGER : 179 case LSL_INTEGER :
127#ifdef LUASL_DEBUG 180#ifdef LUASL_DEBUG
128 printf("%d", exp->value.integerValue); 181 printf("%d", ast->content.integerValue);
129#endif 182#endif
130 return exp->value.integerValue; 183 return ast->content.integerValue;
131#ifdef LUASL_USE_ENUM 184#ifdef LUASL_USE_ENUM
132 case LSL_STRING : 185 case LSL_STRING :
133 case LSL_KEY : 186 case LSL_KEY :
@@ -137,73 +190,7 @@ int evaluateExpression(LSL_Expression *exp, int old)
137 case LSL_LABEL : 190 case LSL_LABEL :
138 break; 191 break;
139#endif 192#endif
140 case LSL_EXPRESSION : 193 case LSL_EXPRESSION : return evaluateExpression(ast->content.expressionValue, old);
141 {
142#ifdef LUASL_DEBUG
143 #ifdef LUASL_USE_ENUM
144 printf(" %s ", LSL_Tokens[exp->expression - LSL_COMMA].token);
145 #else
146 printf(" # ");
147 #endif
148#endif
149 switch (exp->expression)
150 {
151#ifdef LUASL_USE_ENUM
152 case LSL_COMMA :
153 case LSL_INCREMENT_PRE :
154 case LSL_INCREMENT_POST :
155 case LSL_DECREMENT_PRE :
156 case LSL_DECREMENT_POST :
157 case LSL_DOT :
158 case LSL_ASSIGNMENT_PLAIN :
159 case LSL_ASSIGNMENT_DIVIDE :
160 case LSL_ASSIGNMENT_MODULO :
161 case LSL_ASSIGNMENT_MULTIPLY :
162 case LSL_ASSIGNMENT_SUBTRACT :
163 case LSL_ASSIGNMENT_ADD :
164 case LSL_ASSIGNMENT_CONCATENATE :
165 case LSL_PARENTHESIS_OPEN :
166 case LSL_PARENTHESIS_CLOSE :
167 case LSL_BRACKET_OPEN :
168 case LSL_BRACKET_CLOSE :
169 case LSL_ANGLE_OPEN :
170 case LSL_ANGLE_CLOSE :
171 case LSL_TYPECAST :
172 break;
173#endif
174 case LSL_BIT_NOT : return ~ evaluateExpression(exp->right, old);
175 case LSL_BOOL_NOT : return ! evaluateExpression(exp->right, old);
176 case LSL_NEGATION : return 0 - evaluateExpression(exp->right, old);
177 case LSL_DIVIDE : return evaluateExpression(exp->left, old) / evaluateExpression(exp->right, old);
178#ifdef LUASL_USE_ENUM
179 case LSL_MODULO : return evaluateExpression(exp->left, old) % evaluateExpression(exp->right, old);
180#endif
181 case LSL_MULTIPLY : return evaluateExpression(exp->left, old) * evaluateExpression(exp->right, old);
182#ifdef LUASL_USE_ENUM
183 case LSL_DOT_PRODUCT : break;
184 case LSL_CROSS_PRODUCT : break;
185#endif
186 case LSL_SUBTRACT : return evaluateExpression(exp->left, old) - evaluateExpression(exp->right, old);
187 case LSL_ADD : return evaluateExpression(exp->left, old) + evaluateExpression(exp->right, old);
188#ifdef LUASL_USE_ENUM
189 case LSL_CONCATENATE : break;
190#endif
191 case LSL_LEFT_SHIFT : return evaluateExpression(exp->left, old) << evaluateExpression(exp->right, old);
192 case LSL_RIGHT_SHIFT : return evaluateExpression(exp->left, old) >> evaluateExpression(exp->right, old);
193 case LSL_LESS_THAN : return evaluateExpression(exp->left, old) < evaluateExpression(exp->right, old);
194 case LSL_GREATER_THAN : return evaluateExpression(exp->left, old) > evaluateExpression(exp->right, old);
195 case LSL_LESS_EQUAL : return evaluateExpression(exp->left, old) <= evaluateExpression(exp->right, old);
196 case LSL_GREATER_EQUAL : return evaluateExpression(exp->left, old) >= evaluateExpression(exp->right, old);
197 case LSL_EQUAL : return evaluateExpression(exp->left, old) == evaluateExpression(exp->right, old);
198 case LSL_NOT_EQUAL : return evaluateExpression(exp->left, old) != evaluateExpression(exp->right, old);
199 case LSL_BIT_AND : return evaluateExpression(exp->left, old) & evaluateExpression(exp->right, old);
200 case LSL_BIT_XOR : return evaluateExpression(exp->left, old) ^ evaluateExpression(exp->right, old);
201 case LSL_BIT_OR : return evaluateExpression(exp->left, old) | evaluateExpression(exp->right, old);
202 case LSL_BOOL_OR : return evaluateExpression(exp->left, old) || evaluateExpression(exp->right, old);
203 case LSL_BOOL_AND : return evaluateExpression(exp->left, old) && evaluateExpression(exp->right, old);
204 }
205 break;
206 }
207#ifdef LUASL_USE_ENUM 194#ifdef LUASL_USE_ENUM
208 case LSL_DO : 195 case LSL_DO :
209 case LSL_FOR : 196 case LSL_FOR :
@@ -225,22 +212,42 @@ int evaluateExpression(LSL_Expression *exp, int old)
225 } 212 }
226 213
227 return old; 214 return old;
228
229} 215}
230 216
231void outputExpression(LSL_Expression *exp) 217void outputExpression(LSL_Expression *exp)
232{ 218{
233 switch(exp->type) 219 if (NULL == exp)
220 return;
221
222 if (LSL_INTEGER == exp->type)
223 {
224 printf("%d", exp->content.integerValue);
225 }
226 else
227 {
228 outputExpression(exp->left);
229#ifdef LUASL_USE_ENUM
230 printf(" %s ", LSL_Tokens[exp->content.operationValue - LSL_COMMA].token);
231#else
232 printf(" # ");
233#endif
234 outputExpression(exp->right);
235 }
236}
237
238void outputAST(LSL_AST *ast)
239{
240 if (NULL == ast)
241 return;
242 switch(ast->type)
234 { 243 {
235#ifdef LUASL_USE_ENUM 244#ifdef LUASL_USE_ENUM
236 case LSL_COMMENT : return; 245 case LSL_COMMENT : return;
237 case LSL_TYPE : return; 246 case LSL_TYPE : return;
238 case LSL_NAME : return; 247 case LSL_NAME : return;
239 case LSL_IDENTIFIER : return; 248 case LSL_IDENTIFIER : return;
240 case LSL_FLOAT : printf("%f", exp->value.floatValue); break; 249 case LSL_FLOAT : printf("%f", ast->content.floatValue); break;
241#endif 250 case LSL_INTEGER : printf("%d", ast->content.integerValue); break;
242 case LSL_INTEGER : printf("%d", exp->value.integerValue); break;
243#ifdef LUASL_USE_ENUM
244 case LSL_STRING : return; 251 case LSL_STRING : return;
245 case LSL_KEY : return; 252 case LSL_KEY : return;
246 case LSL_VECTOR : return; 253 case LSL_VECTOR : return;
@@ -248,15 +255,7 @@ void outputExpression(LSL_Expression *exp)
248 case LSL_LIST : return; 255 case LSL_LIST : return;
249 case LSL_LABEL : return; 256 case LSL_LABEL : return;
250#endif 257#endif
251 case LSL_EXPRESSION : 258 case LSL_EXPRESSION : outputExpression(ast->content.expressionValue); break;
252 outputExpression(exp->left);
253#ifdef LUASL_USE_ENUM
254 printf(" %s ", LSL_Tokens[exp->expression - LSL_COMMA].token);
255#else
256 printf(" # ");
257#endif
258 outputExpression(exp->right);
259 break;
260#ifdef LUASL_USE_ENUM 259#ifdef LUASL_USE_ENUM
261 case LSL_DO : return; 260 case LSL_DO : return;
262 case LSL_FOR : return; 261 case LSL_FOR : return;
@@ -277,10 +276,12 @@ void outputExpression(LSL_Expression *exp)
277 } 276 }
278} 277}
279 278
280void convertExpression2Lua(LSL_Expression *exp) 279void convertAST2Lua(LSL_AST *ast)
281{ 280{
282#ifdef LUASL_USE_ENUM 281#ifdef LUASL_USE_ENUM
283 switch(exp->type) 282 if (NULL == ast)
283 return;
284 switch(ast->type)
284 { 285 {
285 case LSL_COMMENT : return; 286 case LSL_COMMENT : return;
286 case LSL_TYPE : return; 287 case LSL_TYPE : return;
@@ -320,22 +321,49 @@ int yyerror(const char *msg)
320 return 0; 321 return 0;
321} 322}
322 323
324LSL_AST *newTree(const char *expr)
325{
326 LuaSL_yyparseParam param;
327 YY_BUFFER_STATE state;
328
329#ifdef LUASL_DEBUG
330 yydebug= 5;
331#endif
332
333 param.ast = NULL;
334 if (yylex_init(&(param.scanner)))
335 return NULL;
336
337#ifdef LUASL_DEBUG
338 yyset_debug(1, param.scanner);
339#endif
340
341 state = yy_scan_string(expr, param.scanner);
342 if (yyparse(&param))
343 return NULL;
344
345 yy_delete_buffer(state, param.scanner);
346 yylex_destroy(param.scanner);
347
348 return param.ast;
349}
350
323int main(void) 351int main(void)
324{ 352{
325 const char test[] = " 4 + 2 * 10 + 3 * ( 5 + 1 )"; 353 const char test[] = " 4 + 2 * 10 + 3 * ( 5 + 1 )";
326 LSL_Expression *exp; 354 LSL_AST *ast;
327 355
328 if ((exp = newTree(test))) 356 if ((ast = newTree(test)))
329 { 357 {
330 int result = evaluateExpression(exp, 0); 358 int result = evaluateAST(ast, 0);
331 359
332#ifdef LUASL_DEBUG 360#ifdef LUASL_DEBUG
333 printf("\n"); 361 printf("\n");
334#endif 362#endif
335 printf("Result of '%s' is %d\n", test, result); 363 printf("Result of '%s' is %d\n", test, result);
336 outputExpression(exp); 364 outputAST(ast);
337 printf("\n"); 365 printf("\n");
338 burnLSLExpression(exp); 366 burnAST(ast);
339 } 367 }
340 368
341 return 0; 369 return 0;