diff options
author | David Walter Seikel | 2012-01-07 01:18:11 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-07 01:18:11 +1000 |
commit | 7cdcc9ee93757f8406094373d3faddaf62849e52 (patch) | |
tree | 9b66b2c8160f4c9e901b1eb07c3372fe62037c7c /LuaSL/src | |
parent | Flex performance tweaks, and other options. (diff) | |
download | SledjHamr-7cdcc9ee93757f8406094373d3faddaf62849e52.zip SledjHamr-7cdcc9ee93757f8406094373d3faddaf62849e52.tar.gz SledjHamr-7cdcc9ee93757f8406094373d3faddaf62849e52.tar.bz2 SledjHamr-7cdcc9ee93757f8406094373d3faddaf62849e52.tar.xz |
Convert the parser from dealing with just expressions, to dealing with the entire AST.
Diffstat (limited to 'LuaSL/src')
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.c | 306 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.h | 163 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_yaccer.y | 2 |
3 files changed, 237 insertions, 234 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 | /** | 9 | static 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 | /* | ||
19 | static 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; | 24 | void 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 | ||
35 | void burnLeaf(LSL_AST *leaf) | 33 | LSL_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 | ||
44 | static LSL_Expression *newLSLExpression(LSL_Type type, LSL_Expression *left, LSL_Expression *right) | 43 | static 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 | ||
87 | LSL_Expression *newTree(const char *expr) | 85 | int 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(¶m)) | 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 | ||
114 | int evaluateExpression(LSL_Expression *exp, int old) | 165 | int 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 | ||
231 | void outputExpression(LSL_Expression *exp) | 217 | void 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 | |||
238 | void 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 | ||
280 | void convertExpression2Lua(LSL_Expression *exp) | 279 | void 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 | ||
324 | LSL_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(¶m)) | ||
343 | return NULL; | ||
344 | |||
345 | yy_delete_buffer(state, param.scanner); | ||
346 | yylex_destroy(param.scanner); | ||
347 | |||
348 | return param.ast; | ||
349 | } | ||
350 | |||
323 | int main(void) | 351 | int 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; |
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 271f4a8..a392474 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h | |||
@@ -1,6 +1,4 @@ | |||
1 | /* | 1 | |
2 | * Definition of the structure used to build the abstract syntax tree. | ||
3 | */ | ||
4 | #ifndef __EXPRESSION_H__ | 2 | #ifndef __EXPRESSION_H__ |
5 | #define __EXPRESSION_H__ | 3 | #define __EXPRESSION_H__ |
6 | 4 | ||
@@ -215,110 +213,84 @@ char *LSL_Keywords[] = | |||
215 | }; | 213 | }; |
216 | #endif | 214 | #endif |
217 | 215 | ||
218 | typedef union | ||
219 | { | ||
220 | float floatValue; | ||
221 | int integerValue; | ||
222 | char *stringValue; | ||
223 | char *keyValue; | ||
224 | float vectorValue[3]; | ||
225 | float rotationValue[4]; | ||
226 | union LSL_Leaf *listValue; | ||
227 | } LSL_Value; | ||
228 | |||
229 | typedef struct | ||
230 | { | ||
231 | char *name; | ||
232 | LSL_Type type; | ||
233 | LSL_Value value; | ||
234 | } LSL_Identifier; | ||
235 | |||
236 | typedef struct LSL_Expression | ||
237 | { | ||
238 | struct LSL_Expression *left; | ||
239 | struct LSL_Expression *right; | ||
240 | LSL_Value value; | ||
241 | LSL_Operation expression; | ||
242 | LSL_Type type; | ||
243 | } LSL_Expression; | ||
244 | |||
245 | typedef struct | 216 | typedef struct |
246 | { | 217 | { |
247 | LSL_Type type; | 218 | LSL_Type type; |
248 | LSL_Expression *expressions; | 219 | struct LSL_Expression *expressions; |
249 | } LSL_Statement; | 220 | } LSL_Statement; |
250 | 221 | ||
251 | typedef struct | 222 | typedef struct |
252 | { | 223 | { |
253 | LSL_Statement *statements; | 224 | LSL_Statement *statements; |
254 | } LSL_Block; | 225 | } LSL_Block; |
255 | 226 | ||
256 | typedef struct | 227 | typedef struct |
257 | { | 228 | { |
258 | char *name; | 229 | char *name; |
259 | LSL_Identifier *parameters; | 230 | struct LSL_Identifier *parameters; |
260 | LSL_Block block; | 231 | LSL_Block block; |
261 | LSL_Type type; | 232 | LSL_Type type; |
262 | } LSL_Function; | 233 | } LSL_Function; |
263 | 234 | ||
264 | typedef struct | 235 | typedef struct |
265 | { | 236 | { |
266 | char *name; | 237 | char *name; |
267 | LSL_Function *handlers; | 238 | LSL_Function *handlers; |
268 | } LSL_State; | 239 | } LSL_State; |
269 | 240 | ||
270 | typedef struct | 241 | typedef struct |
271 | { | 242 | { |
272 | char *name; | 243 | char *name; |
273 | LSL_Identifier *variables; | 244 | struct LSL_Identifier *variables; |
274 | LSL_Function *functions; | 245 | LSL_Function *functions; |
275 | LSL_State *states; | 246 | LSL_State *states; |
276 | } LSL_Script; | 247 | } LSL_Script; |
277 | 248 | ||
278 | typedef union LSL_Leaf | 249 | typedef union LSL_Leaf |
279 | { | 250 | { |
280 | char *commentValue; | 251 | char *commentValue; |
281 | LSL_Type typeValue; | 252 | LSL_Type typeValue; |
282 | char *nameValue; | 253 | char *nameValue; |
283 | LSL_Identifier *identifierValue; | 254 | struct LSL_Identifier *identifierValue; |
284 | float floatValue; | 255 | float floatValue; |
285 | int integerValue; | 256 | int integerValue; |
286 | char *stringValue; | 257 | char *stringValue; |
287 | char *keyValue; | 258 | char *keyValue; |
288 | float vectorValue[3]; | 259 | float vectorValue[3]; |
289 | float rotationValue[4]; | 260 | float rotationValue[4]; |
290 | union LSL_Leaf *listValue; | 261 | union LSL_Leaf *listValue; |
291 | char *labelValue; | 262 | char *labelValue; |
292 | // LSL_Operation expressionValue; | 263 | LSL_Operation operationValue; |
293 | LSL_Expression *expressionValue; | 264 | struct LSL_Expression *expressionValue; |
294 | LSL_Statement *doValue; | 265 | LSL_Statement *doValue; |
295 | LSL_Statement *forValue; | 266 | LSL_Statement *forValue; |
296 | LSL_Statement *ifValue; | 267 | LSL_Statement *ifValue; |
297 | LSL_Statement *elseValue; | 268 | LSL_Statement *elseValue; |
298 | LSL_Statement *elseIfValue; | 269 | LSL_Statement *elseIfValue; |
299 | char *jumpValue; | 270 | char *jumpValue; |
300 | char *stateChangeValue; | 271 | char *stateChangeValue; |
301 | LSL_Statement *statementValue; | 272 | LSL_Statement *statementValue; |
302 | LSL_Identifier *parameterValue; | 273 | struct LSL_Identifier *parameterValue; |
303 | LSL_Function *functionValue; | 274 | LSL_Function *functionValue; |
304 | LSL_State *stateValue; | 275 | LSL_State *stateValue; |
305 | LSL_Script *scriptValue; | 276 | LSL_Script *scriptValue; |
306 | // class LLScriptType *type; | ||
307 | // class LLScriptConstant *constant; | ||
308 | // class LLScriptIdentifier *identifier; | ||
309 | // class LLScriptSimpleAssignable *assignable; | ||
310 | // class LLScriptGlobalVariable *global; | ||
311 | // class LLScriptEvent *event; | ||
312 | // class LLScriptEventHandler *handler; | ||
313 | // class LLScriptExpression *expression; | ||
314 | // class LLScriptStatement *statement; | ||
315 | // class LLScriptGlobalFunctions *global_funcs; | ||
316 | // class LLScriptFunctionDec *global_decl; | ||
317 | // class LLScriptState *state; | ||
318 | // class LLScritpGlobalStorage *global_store; | ||
319 | // class LLScriptScript *script; | ||
320 | } LSL_Leaf; | 277 | } LSL_Leaf; |
321 | 278 | ||
279 | typedef struct | ||
280 | { | ||
281 | char *name; | ||
282 | LSL_Type type; | ||
283 | LSL_Leaf content; | ||
284 | } LSL_Identifier; | ||
285 | |||
286 | typedef struct LSL_Expression | ||
287 | { | ||
288 | struct LSL_Expression *left; | ||
289 | struct LSL_Expression *right; | ||
290 | LSL_Type type; | ||
291 | LSL_Leaf content; | ||
292 | } LSL_Expression; | ||
293 | |||
322 | typedef struct LSL_AST | 294 | typedef struct LSL_AST |
323 | { | 295 | { |
324 | struct LSL_AST *left; | 296 | struct LSL_AST *left; |
@@ -342,19 +314,9 @@ typedef struct LSL_AST | |||
342 | typedef struct | 314 | typedef struct |
343 | { | 315 | { |
344 | yyscan_t scanner; | 316 | yyscan_t scanner; |
345 | LSL_Expression *expression; | 317 | LSL_AST *ast; |
346 | } LuaSL_yyparseParam; | 318 | } LuaSL_yyparseParam; |
347 | 319 | ||
348 | |||
349 | void burnLeaf(LSL_AST *leaf); | ||
350 | void burnLSLExpression(LSL_Expression *exp); | ||
351 | LSL_Expression *addInteger(int value); | ||
352 | LSL_Expression *addOperation(LSL_Operation type, LSL_Expression *left, LSL_Expression *right); | ||
353 | LSL_Expression *newTree(const char *expr); | ||
354 | int evaluateExpression(LSL_Expression *exp, int old); | ||
355 | void outputExpression(LSL_Expression *exp); | ||
356 | void convertExpression2Lua(LSL_Expression *exp); | ||
357 | |||
358 | // the parameter name (of the reentrant 'yyparse' function) | 320 | // the parameter name (of the reentrant 'yyparse' function) |
359 | // data is a pointer to a 'SParserParam' structure | 321 | // data is a pointer to a 'SParserParam' structure |
360 | #define YYPARSE_PARAM data | 322 | #define YYPARSE_PARAM data |
@@ -362,6 +324,19 @@ void convertExpression2Lua(LSL_Expression *exp); | |||
362 | // the argument for the 'yylex' function | 324 | // the argument for the 'yylex' function |
363 | #define YYLEX_PARAM ((LuaSL_yyparseParam*)data)->scanner | 325 | #define YYLEX_PARAM ((LuaSL_yyparseParam*)data)->scanner |
364 | 326 | ||
327 | |||
328 | void burnLSLExpression(LSL_Expression *exp); | ||
329 | void burnAST(LSL_AST *ast); | ||
330 | LSL_AST *addExpression(LSL_Expression *exp); | ||
331 | LSL_Expression *addInteger(int value); | ||
332 | LSL_Expression *addOperation(LSL_Operation type, LSL_Expression *left, LSL_Expression *right); | ||
333 | int evaluateExpression(LSL_Expression *exp, int old); | ||
334 | int evaluateAST(LSL_AST *ast, int old); | ||
335 | void outputExpression(LSL_Expression *exp); | ||
336 | void outputAST(LSL_AST *ast); | ||
337 | void convertAST2Lua(LSL_AST *ast); | ||
338 | LSL_AST *newTree(const char *expr); | ||
339 | |||
365 | int yyerror(const char *msg); | 340 | int yyerror(const char *msg); |
366 | int yyparse(void *param); | 341 | int yyparse(void *param); |
367 | 342 | ||
diff --git a/LuaSL/src/LuaSL_yaccer.y b/LuaSL/src/LuaSL_yaccer.y index fd5a461..e208cce 100644 --- a/LuaSL/src/LuaSL_yaccer.y +++ b/LuaSL/src/LuaSL_yaccer.y | |||
@@ -28,7 +28,7 @@ | |||
28 | %% | 28 | %% |
29 | 29 | ||
30 | input : | 30 | input : |
31 | expr { ((LuaSL_yyparseParam*)data)->expression = $1; } | 31 | expr { ((LuaSL_yyparseParam*)data)->ast = addExpression($1); } |
32 | ; | 32 | ; |
33 | 33 | ||
34 | expr : | 34 | expr : |