aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-01-10 03:30:17 +1000
committerDavid Walter Seikel2012-01-10 03:30:17 +1000
commiteb8dda3811a849ef633faa225cf1427bf93a4494 (patch)
treee360394cc64c99ca94238c2442bd0cf7b29617d1 /LuaSL
parentMove more things into the LSL_Leaf structure, and some clean up related to that. (diff)
downloadSledjHamr-eb8dda3811a849ef633faa225cf1427bf93a4494.zip
SledjHamr-eb8dda3811a849ef633faa225cf1427bf93a4494.tar.gz
SledjHamr-eb8dda3811a849ef633faa225cf1427bf93a4494.tar.bz2
SledjHamr-eb8dda3811a849ef633faa225cf1427bf93a4494.tar.xz
Get rid of the LSL_AST structure, it's all in LSL_Leaf now.
Diffstat (limited to 'LuaSL')
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.c184
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.h29
-rw-r--r--LuaSL/src/LuaSL_lexer.l1
3 files changed, 95 insertions, 119 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c
index 016dbc9..cc6ed3f 100644
--- a/LuaSL/src/LuaSL_LSL_tree.c
+++ b/LuaSL/src/LuaSL_LSL_tree.c
@@ -128,118 +128,102 @@ LSL_Token **tokens = NULL;
128int lowestToken = 999999; 128int lowestToken = 999999;
129 129
130 130
131static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right) 131static LSL_Leaf *newLeaf(LSL_Type type, LSL_Leaf *left, LSL_Leaf *right)
132{ 132{
133 LSL_AST *ast = malloc(sizeof(LSL_AST)); 133 LSL_Leaf *leaf = malloc(sizeof(LSL_Leaf));
134 134
135 if (ast == NULL) return NULL; 135 if (leaf)
136 136 {
137 ast->left = left; 137 leaf->left = left;
138 ast->right = right; 138 leaf->right = right;
139 ast->content.token = tokens[type - lowestToken]; 139 leaf->token = tokens[type - lowestToken];
140 140 }
141 return ast;
142}
143
144static void burnAST(LSL_AST *ast)
145{
146 if (ast == NULL) return;
147 141
148 burnAST(ast->left); 142 return leaf;
149 burnAST(ast->right);
150 free(ast->content.ignorableText);
151 free(ast);
152} 143}
153 144
154static LSL_AST *checkAndAddIgnorable(LSL_AST *root) 145static void burnLeaf(LSL_Leaf *leaf)
155{ 146{
156 if (root) 147 if (leaf)
157 { 148 {
158 if (root->content.ignorableText) 149 burnLeaf(leaf->left);
159 { 150 burnLeaf(leaf->right);
160 LSL_AST *ast = newAST(LSL_SPACE, NULL, root); 151 // TODO - Should free up the value to.
161 152 free(leaf->ignorableText);
162 if (ast) 153 free(leaf);
163 {
164 ast->content.value.spaceValue = root->content.ignorableText;
165 return ast;
166 }
167 }
168 } 154 }
169 return root;
170} 155}
171 156
172LSL_AST *addInteger(LSL_Leaf *lval, int value) 157LSL_Leaf *addInteger(LSL_Leaf *lval, int value)
173{ 158{
174 LSL_AST *ast = newAST(LSL_INTEGER, NULL, NULL); 159 LSL_Leaf *leaf = newLeaf(LSL_INTEGER, NULL, NULL);
175 160
176 if (ast) 161 if (leaf)
177 { 162 {
178 ast->content.value.integerValue = value; 163 leaf->value.integerValue = value;
179 ast->content.ignorableText = lval->ignorableText; 164 leaf->ignorableText = lval->ignorableText;
180 } 165 }
181 166
182 return checkAndAddIgnorable(ast); 167 return leaf;
183} 168}
184 169
185LSL_AST *addOperation(LSL_Leaf *lval, LSL_Type type, LSL_AST *left, LSL_AST *right) 170LSL_Leaf *addOperation(LSL_Leaf *lval, LSL_Type type, LSL_Leaf *left, LSL_Leaf *right)
186{ 171{
187 LSL_AST *ast = newAST(type, left, right); 172 LSL_Leaf *leaf = newLeaf(type, left, right);
188 173
189 if (ast) 174 if (leaf)
190 { 175 {
191 if (LSL_EXPRESSION == type) 176 if (LSL_EXPRESSION == type)
192 { 177 {
193 ast->content.value.expressionValue = right; 178 leaf->value.expressionValue = right;
194 ast->left = NULL; 179 leaf->left = NULL;
195 } 180 }
196 else 181 else
197 { 182 {
198 ast->content.value.operationValue = type; 183 leaf->value.operationValue = type;
199 ast->content.ignorableText = lval->ignorableText; 184 leaf->ignorableText = lval->ignorableText;
200 } 185 }
201 } 186 }
202 187
203 return checkAndAddIgnorable(ast); 188 return leaf;
204} 189}
205 190
206LSL_AST *addParenthesis(LSL_Leaf *lval, LSL_AST *expr) 191LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr)
207{ 192{
208 LSL_AST *ast = newAST(LSL_PARENTHESIS_OPEN, NULL, expr); 193 LSL_Leaf *leaf = newLeaf(LSL_PARENTHESIS_OPEN, NULL, expr);
209 194
210 if (ast) 195 if (leaf)
211 { 196 {
212 ast = newAST(LSL_PARENTHESIS_CLOSE, ast, NULL); 197 leaf->ignorableText = lval->ignorableText;
213 ast->content.ignorableText = lval->ignorableText; 198 leaf = newLeaf(LSL_PARENTHESIS_CLOSE, leaf, NULL);
214 } 199 }
215 200
216 return checkAndAddIgnorable(ast); 201 return leaf;
217} 202}
218 203
219LSL_Statement *createStatement(LSL_Type type, LSL_AST *expr) 204LSL_Statement *createStatement(LSL_Type type, LSL_Leaf *expr)
220{ 205{
221 LSL_Statement *stat = malloc(sizeof(LSL_Statement)); 206 LSL_Statement *stat = malloc(sizeof(LSL_Statement));
222 207
223 if (stat == NULL) return NULL; 208 if (stat)
224 209 stat->expressions = expr;
225 stat->expressions = expr;
226 210
227 return stat; 211 return stat;
228} 212}
229 213
230LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root) 214LSL_Leaf *addStatement(LSL_Statement *statement, LSL_Leaf *root)
231{ 215{
232 LSL_AST *ast = newAST(LSL_STATEMENT, root, NULL); 216 LSL_Leaf *leaf = newLeaf(LSL_STATEMENT, root, NULL);
233 217
234 if (ast) 218 if (leaf)
235 ast->content.value.statementValue = statement; 219 leaf->value.statementValue = statement;
236 220
237 return checkAndAddIgnorable(ast); 221 return leaf;
238} 222}
239 223
240static void evaluateAST(LSL_AST *ast, LSL_Leaf *left, LSL_Leaf *right) 224static void evaluateLeaf(LSL_Leaf *leaf, LSL_Leaf *left, LSL_Leaf *right)
241{ 225{
242 if (ast) 226 if (leaf)
243 { 227 {
244 LSL_Leaf lresult; 228 LSL_Leaf lresult;
245 LSL_Leaf rresult; 229 LSL_Leaf rresult;
@@ -247,34 +231,34 @@ static void evaluateAST(LSL_AST *ast, LSL_Leaf *left, LSL_Leaf *right)
247 memcpy(&lresult, left, sizeof(LSL_Leaf)); 231 memcpy(&lresult, left, sizeof(LSL_Leaf));
248 memcpy(&rresult, right, sizeof(LSL_Leaf)); 232 memcpy(&rresult, right, sizeof(LSL_Leaf));
249 233
250 if (LSL_RIGHT2LEFT & ast->content.token->flags) 234 if (LSL_RIGHT2LEFT & leaf->token->flags)
251 { 235 {
252 memcpy(&rresult, left, sizeof(LSL_Leaf)); 236 memcpy(&rresult, left, sizeof(LSL_Leaf));
253 evaluateAST(ast->right, &rresult, right); 237 evaluateLeaf(leaf->right, &rresult, right);
254 if (!(LSL_UNARY & ast->content.token->flags)) 238 if (!(LSL_UNARY & leaf->token->flags))
255 { 239 {
256 evaluateAST(ast->left, &lresult, right); 240 evaluateLeaf(leaf->left, &lresult, right);
257 } 241 }
258 } 242 }
259 else // Assume left to right. 243 else // Assume left to right.
260 { 244 {
261 evaluateAST(ast->left, &lresult, right); 245 evaluateLeaf(leaf->left, &lresult, right);
262 if (!(LSL_UNARY & ast->content.token->flags)) 246 if (!(LSL_UNARY & leaf->token->flags))
263 { 247 {
264 memcpy(&rresult, left, sizeof(LSL_Leaf)); 248 memcpy(&rresult, left, sizeof(LSL_Leaf));
265 evaluateAST(ast->right, &rresult, right); 249 evaluateLeaf(leaf->right, &rresult, right);
266 } 250 }
267 } 251 }
268 252
269 if (ast->content.token->evaluate) 253 if (leaf->token->evaluate)
270 { 254 {
271 ast->content.token->evaluate(&(ast->content), &lresult, &rresult); 255 leaf->token->evaluate(leaf, &lresult, &rresult);
272 memcpy(left, &lresult, sizeof(LSL_Leaf)); 256 memcpy(left, &lresult, sizeof(LSL_Leaf));
273 } 257 }
274 else 258 else
275 { 259 {
276#ifdef LUASL_DEBUG 260#ifdef LUASL_DEBUG
277 printf(" eval <%s %d %d %d %d> ", ast->content.token->token, left->value.integerValue, right->value.integerValue, lresult.value.integerValue, rresult.value.integerValue); 261 printf(" eval <%s %d %d %d %d> ", leaf->token->token, left->value.integerValue, right->value.integerValue, lresult.value.integerValue, rresult.value.integerValue);
278#endif 262#endif
279 memcpy(left, &rresult, sizeof(LSL_Leaf)); 263 memcpy(left, &rresult, sizeof(LSL_Leaf));
280 } 264 }
@@ -289,7 +273,7 @@ static void evaluateIntegerToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *ri
289 printf(" <%d> ", content->value.integerValue); 273 printf(" <%d> ", content->value.integerValue);
290#endif 274#endif
291 left->value.integerValue = content->value.integerValue; 275 left->value.integerValue = content->value.integerValue;
292 left->type = LSL_INTEGER; 276 left->token = tokens[LSL_INTEGER - lowestToken];
293 } 277 }
294} 278}
295 279
@@ -364,19 +348,21 @@ static void evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *
364static void evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) 348static void evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
365{ 349{
366 if (content) 350 if (content)
367 evaluateAST(content->value.statementValue->expressions, left, right); 351 evaluateLeaf(content->value.statementValue->expressions, left, right);
368} 352}
369 353
370static void outputAST(LSL_AST *ast) 354static void outputLeaf(LSL_Leaf *leaf)
371{ 355{
372 if (ast) 356 if (leaf)
373 { 357 {
374 outputAST(ast->left); 358 outputLeaf(leaf->left);
375 if (ast->content.token->output) 359 if (leaf->ignorableText)
376 ast->content.token->output(&(ast->content)); 360 printf("%s", leaf->ignorableText);
361 if (leaf->token->output)
362 leaf->token->output(leaf);
377 else 363 else
378 printf("%s", ast->content.token->token); 364 printf("%s", leaf->token->token);
379 outputAST(ast->right); 365 outputLeaf(leaf->right);
380 } 366 }
381} 367}
382 368
@@ -395,7 +381,7 @@ static void outputOperationToken(LSL_Leaf *content)
395static void outputStatementToken(LSL_Leaf *content) 381static void outputStatementToken(LSL_Leaf *content)
396{ 382{
397 if (content) 383 if (content)
398 outputAST(content->value.statementValue->expressions); 384 outputLeaf(content->value.statementValue->expressions);
399 printf(";"); 385 printf(";");
400} 386}
401 387
@@ -405,18 +391,18 @@ static void outputSpaceToken(LSL_Leaf *content)
405 printf("%s", content->value.spaceValue); 391 printf("%s", content->value.spaceValue);
406} 392}
407 393
408static void convertAST2Lua(LSL_AST *ast) 394static void convertLeaf2Lua(LSL_Leaf *leaf)
409{ 395{
410 if (ast) 396 if (leaf)
411 { 397 {
412 convertAST2Lua(ast->left); 398 convertLeaf2Lua(leaf->left);
413 if (ast->content.token->convert) 399 if (leaf->token->convert)
414 ast->content.token->convert(&(ast->content)); 400 leaf->token->convert(leaf);
415 else if (ast->content.token->output) 401 else if (leaf->token->output)
416 ast->content.token->output(&(ast->content)); 402 leaf->token->output(leaf);
417 else 403 else
418 printf("%s", ast->content.token->token); 404 printf("%s", leaf->token->token);
419 convertAST2Lua(ast->right); 405 convertLeaf2Lua(leaf->right);
420 } 406 }
421} 407}
422 408
@@ -435,7 +421,6 @@ int main(int argc, char **argv)
435 if (tokens) 421 if (tokens)
436 { 422 {
437 char buffer[4096]; 423 char buffer[4096];
438 LSL_AST *ast;
439 LuaSL_yyparseParam param; 424 LuaSL_yyparseParam param;
440 int count; 425 int count;
441 FILE *file; 426 FILE *file;
@@ -533,28 +518,27 @@ int main(int argc, char **argv)
533 if (!yyparse(&param)) 518 if (!yyparse(&param))
534 { 519 {
535 yylex_destroy(param.scanner); 520 yylex_destroy(param.scanner);
536 ast = param.ast;
537 521
538 if (ast) 522 if (param.ast)
539 { 523 {
540 LSL_Leaf left, right; 524 LSL_Leaf left, right;
541 525
542 left.value.integerValue = 0; 526 left.value.integerValue = 0;
543 left.type = LSL_INTEGER; 527 left.token = tokens[LSL_INTEGER - lowestToken];
544 right.value.integerValue = 0; 528 right.value.integerValue = 0;
545 right.type = LSL_INTEGER; 529 right.token = tokens[LSL_INTEGER - lowestToken];
546 evaluateAST(ast, &left, &right); 530 evaluateLeaf(param.ast, &left, &right);
547 531
548#ifdef LUASL_DEBUG 532#ifdef LUASL_DEBUG
549 printf("\n"); 533 printf("\n");
550#endif 534#endif
551 printf("Result of -\n"); 535 printf("Result of -\n");
552 outputAST(ast); 536 outputLeaf(param.ast);
553 printf("\n"); 537 printf("\n");
554 printf("is %d %d. And converted to Lua it is -\n", left.value.integerValue, right.value.integerValue); 538 printf("is %d %d. And converted to Lua it is -\n", left.value.integerValue, right.value.integerValue);
555 convertAST2Lua(ast); 539 convertLeaf2Lua(param.ast);
556 printf("\n"); 540 printf("\n");
557 burnAST(ast); 541 burnLeaf(param.ast);
558 } 542 }
559 } 543 }
560 } 544 }
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h
index 5d39b3b..4676171 100644
--- a/LuaSL/src/LuaSL_LSL_tree.h
+++ b/LuaSL/src/LuaSL_LSL_tree.h
@@ -28,7 +28,6 @@ typedef struct _LSL_Block LSL_Block;
28typedef struct _LSL_Function LSL_Function; 28typedef struct _LSL_Function LSL_Function;
29typedef struct _LSL_State LSL_State; 29typedef struct _LSL_State LSL_State;
30typedef struct _LSL_Script LSL_Script; 30typedef struct _LSL_Script LSL_Script;
31typedef struct _LSL_AST LSL_AST;
32 31
33extern LSL_Token **tokens; 32extern LSL_Token **tokens;
34extern int lowestToken; 33extern int lowestToken;
@@ -70,13 +69,15 @@ struct _LSL_Token
70 69
71struct _LSL_Leaf 70struct _LSL_Leaf
72{ 71{
72 LSL_Leaf *left;
73 LSL_Leaf *right;
73 union 74 union
74 { 75 {
75 char *commentValue; 76 char *commentValue;
76 char *spaceValue; 77 char *spaceValue;
77 78
78 LSL_Type operationValue; 79 LSL_Type operationValue;
79 LSL_AST *expressionValue; 80 LSL_Leaf *expressionValue;
80 81
81 float floatValue; 82 float floatValue;
82 int integerValue; 83 int integerValue;
@@ -110,7 +111,6 @@ struct _LSL_Leaf
110 } value; 111 } value;
111 char *ignorableText; 112 char *ignorableText;
112 LSL_Token *token; 113 LSL_Token *token;
113 LSL_Type type;
114 int line, column; 114 int line, column;
115}; 115};
116 116
@@ -122,7 +122,7 @@ struct _LSL_Identifier // For variables and function parameters.
122 122
123struct _LSL_Statement 123struct _LSL_Statement
124{ 124{
125 LSL_AST *expressions; /// For things like a for statement, might hold three expressions. 125 LSL_Leaf *expressions; /// For things like a for statement, might hold three expressions.
126}; 126};
127 127
128struct _LSL_Block 128struct _LSL_Block
@@ -152,13 +152,6 @@ struct _LSL_Script
152 LSL_Identifier *variables; 152 LSL_Identifier *variables;
153}; 153};
154 154
155struct _LSL_AST
156{
157 LSL_AST *left;
158 LSL_AST *right;
159 LSL_Leaf content;
160};
161
162 155
163// define the type for flex and bison 156// define the type for flex and bison
164#define YYSTYPE LSL_Leaf 157#define YYSTYPE LSL_Leaf
@@ -172,7 +165,7 @@ struct _LSL_AST
172typedef struct 165typedef struct
173{ 166{
174 yyscan_t scanner; 167 yyscan_t scanner;
175 LSL_AST *ast; 168 LSL_Leaf *ast;
176} LuaSL_yyparseParam; 169} LuaSL_yyparseParam;
177 170
178// the parameter name (of the reentrant 'yyparse' function) 171// the parameter name (of the reentrant 'yyparse' function)
@@ -183,12 +176,12 @@ typedef struct
183#define YYLEX_PARAM ((LuaSL_yyparseParam*)data)->scanner 176#define YYLEX_PARAM ((LuaSL_yyparseParam*)data)->scanner
184 177
185 178
186LSL_AST *addExpression(LSL_AST *exp); 179LSL_Leaf *addExpression(LSL_Leaf *exp);
187LSL_AST *addInteger(LSL_Leaf *lval, int value); 180LSL_Leaf *addInteger(LSL_Leaf *lval, int value);
188LSL_AST *addOperation(LSL_Leaf *lval, LSL_Type type, LSL_AST *left, LSL_AST *right); 181LSL_Leaf *addOperation(LSL_Leaf *lval, LSL_Type type, LSL_Leaf *left, LSL_Leaf *right);
189LSL_AST *addParenthesis(LSL_Leaf *lval, LSL_AST *expr); 182LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr);
190LSL_Statement *createStatement(LSL_Type type, LSL_AST *root); 183LSL_Statement *createStatement(LSL_Type type, LSL_Leaf *root);
191LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root); 184LSL_Leaf *addStatement(LSL_Statement *statement, LSL_Leaf *root);
192 185
193int yyerror(const char *msg); 186int yyerror(const char *msg);
194int yyparse(void *param); 187int yyparse(void *param);
diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l
index 30e0b6b..aaba74a 100644
--- a/LuaSL/src/LuaSL_lexer.l
+++ b/LuaSL/src/LuaSL_lexer.l
@@ -140,7 +140,6 @@ int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type)
140 else 140 else
141 column++; 141 column++;
142 142
143 lval->type = type;
144 lval->token = tokens[type - lowestToken]; 143 lval->token = tokens[type - lowestToken];
145 lval->line = line; 144 lval->line = line;
146 lval->column = column; 145 lval->column = column;