aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.c278
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.h368
-rw-r--r--LuaSL/src/LuaSL_lexer.l2
-rw-r--r--LuaSL/src/LuaSL_parser.c10
-rw-r--r--LuaSL/src/LuaSL_yaccer.y12
5 files changed, 556 insertions, 114 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c
index 7cebecc..9137692 100644
--- a/LuaSL/src/LuaSL_LSL_tree.c
+++ b/LuaSL/src/LuaSL_LSL_tree.c
@@ -2,83 +2,275 @@
2 * Implementation of functions used to build the abstract syntax tree. 2 * Implementation of functions used to build the abstract syntax tree.
3 */ 3 */
4 4
5
6#define LSL_Keywords_define
7#define LSL_Tokens_define
5#include "LuaSL_LSL_tree.h" 8#include "LuaSL_LSL_tree.h"
6#include <stdlib.h> 9#include <stdlib.h>
7 10
11
8/** 12/**
9 * @brief Allocates space for expression 13 * @brief Allocates space for an AST leaf
10 * @return The expression or NULL if not enough memory 14 * @return The expression or NULL if not enough memory
11 */ 15 */
12static SExpression* newExpression(EOperationType type, SExpression *left, SExpression *right, int value) 16
17/*
18static LSL_AST *newLeaf(LSL_Type type, LSL_AST *left, LSL_AST *right)
13{ 19{
14 SExpression* b = malloc(sizeof *b); 20 LSL_AST *leaf = malloc(sizeof(LSL_AST));
15 21
16 if (b == NULL) return NULL; 22 if (leaf == NULL) return NULL;
17 23
18 b->type = type; 24 leaf->type = type;
19 b->value = value; 25 leaf->left = left;
20 b->left = left; 26 leaf->right = right;
21 b->right = right; 27 leaf->line = -1;
28 leaf->character = -1;
22 29
23 return b; 30 return leaf;
24} 31}
32*/
25 33
26SExpression* createNumber(int value) 34void burnLeaf(LSL_AST *leaf)
27{ 35{
28 return newExpression(eVALUE, NULL, NULL, value); 36 if (leaf == NULL) return;
37
38 burnLeaf(leaf->left);
39 burnLeaf(leaf->right);
40 free(leaf);
29} 41}
30 42
31SExpression *createOperation(EOperationType type, SExpression *left, SExpression *right) 43static LSL_Expression *newLSLExpression(LSL_Type type, LSL_Expression *left, LSL_Expression *right)
32{ 44{
33 return newExpression(type, left, right, 0); 45 LSL_Expression *exp = malloc(sizeof(LSL_Expression));
46
47 if (exp == NULL) return NULL;
48
49 exp->type = type;
50 exp->left = left;
51 exp->right = right;
52 exp->expression=0;
53
54 return exp;
34} 55}
35 56
36int evaluate(SExpression *e) 57void burnLSLExpression(LSL_Expression *exp)
37{ 58{
38 switch(e->type) 59 if (exp == NULL) return;
39 { 60
40 case eVALUE: 61 burnLSLExpression(exp->left);
41 return e->value; 62 burnLSLExpression(exp->right);
42 case eMULTIPLY: 63 free(exp);
43 return evaluate(e->left) * evaluate(e->right); 64}
44 case ePLUS: 65
45 return evaluate(e->left) + evaluate(e->right); 66LSL_Expression *addInteger(int value)
46 default: 67{
47 // shouldn't be here 68 LSL_Expression *exp = newLSLExpression(LSL_INTEGER, NULL, NULL);
48 return 0; 69
49 } 70 if (exp)
71 exp->value.integerValue = value;
72
73 return exp;
50} 74}
51 75
52void deleteExpression(SExpression *b) 76LSL_Expression *addOperation(LSL_Operation type, LSL_Expression *left, LSL_Expression *right)
53{ 77{
54 if (b == NULL) return; 78 LSL_Expression *exp = newLSLExpression(LSL_EXPRESSION, left, right);
55 79
56 deleteExpression(b->left); 80 if (exp)
57 deleteExpression(b->right); 81 exp->expression = type;
58 82
59 free(b); 83 return exp;
60} 84}
61 85
62SExpression *getAST(const char *expr) 86LSL_Expression *newTree(const char *expr)
63{ 87{
64 SParserParam p; 88 LuaSL_yyparseParam param;
65 YY_BUFFER_STATE state; 89 YY_BUFFER_STATE state;
66 90
67 p.expression = NULL; 91 param.expression = NULL;
68 if (yylex_init(&(p.scanner))) 92 if (yylex_init(&(param.scanner)))
69 return NULL; 93 return NULL;
94
95 state = yy_scan_string(expr, param.scanner);
96 if (yyparse(&param))
97 return NULL;
70 98
71 state = yy_scan_string(expr, p.scanner); 99 yy_delete_buffer(state, param.scanner);
72 if (yyparse(&p)) 100 yylex_destroy(param.scanner);
73 return NULL;
74 101
75 yy_delete_buffer(state, p.scanner); 102 return param.expression;
76 yylex_destroy(p.scanner); 103}
77 return p.expression; 104
105int evaluateExpression(LSL_Expression *exp, int old)
106{
107 switch(exp->type)
108 {
109 case LSL_COMMENT :
110 case LSL_TYPE :
111 case LSL_NAME :
112 case LSL_IDENTIFIER :
113 break;
114 case LSL_FLOAT : return (int) exp->value.floatValue;
115 case LSL_INTEGER : return exp->value.integerValue;
116 case LSL_STRING :
117 case LSL_KEY :
118 case LSL_VECTOR :
119 case LSL_ROTATION :
120 case LSL_LIST :
121 case LSL_LABEL :
122 break;
123 case LSL_EXPRESSION :
124 {
125 switch (exp->expression)
126 {
127 case LSL_COMMA :
128 case LSL_INCREMENT_PRE :
129 case LSL_INCREMENT_POST :
130 case LSL_DECREMENT_PRE :
131 case LSL_DECREMENT_POST :
132 case LSL_DOT :
133 case LSL_ASSIGNMENT_PLAIN :
134 case LSL_ASSIGNMENT_DIVIDE :
135 case LSL_ASSIGNMENT_MODULO :
136 case LSL_ASSIGNMENT_MULTIPLY :
137 case LSL_ASSIGNMENT_SUBTRACT :
138 case LSL_ASSIGNMENT_ADD :
139 case LSL_ASSIGNMENT_CONCATENATE :
140 case LSL_PARENTHESIS_OPEN :
141 case LSL_PARENTHESIS_CLOSE :
142 case LSL_BRACKET_OPEN :
143 case LSL_BRACKET_CLOSE :
144 case LSL_ANGLE_OPEN :
145 case LSL_ANGLE_CLOSE :
146 case LSL_TYPECAST :
147 case LSL_BIT_NOT :
148 case LSL_BOOL_NOT :
149 case LSL_NEGATION :
150 break;
151 case LSL_DIVIDE : return evaluateExpression(exp->left, old) / evaluateExpression(exp->right, old);
152 case LSL_MODULO : return evaluateExpression(exp->left, old) % evaluateExpression(exp->right, old);
153 case LSL_MULTIPLY : return evaluateExpression(exp->left, old) * evaluateExpression(exp->right, old);
154 case LSL_DOT_PRODUCT : break;
155 case LSL_CROSS_PRODUCT : break;
156 case LSL_SUBTRACT : return evaluateExpression(exp->left, old) - evaluateExpression(exp->right, old);
157 case LSL_ADD : return evaluateExpression(exp->left, old) + evaluateExpression(exp->right, old);
158 case LSL_CONCATENATE : break;
159 case LSL_LEFT_SHIFT : return evaluateExpression(exp->left, old) << evaluateExpression(exp->right, old);
160 case LSL_RIGHT_SHIFT : return evaluateExpression(exp->left, old) >> evaluateExpression(exp->right, old);
161 case LSL_LESS_THAN : return evaluateExpression(exp->left, old) < evaluateExpression(exp->right, old);
162 case LSL_GREATER_THAN : return evaluateExpression(exp->left, old) > evaluateExpression(exp->right, old);
163 case LSL_LESS_EQUAL : return evaluateExpression(exp->left, old) <= evaluateExpression(exp->right, old);
164 case LSL_GREATER_EQUAL : return evaluateExpression(exp->left, old) >= evaluateExpression(exp->right, old);
165 case LSL_EQUAL : return evaluateExpression(exp->left, old) == evaluateExpression(exp->right, old);
166 case LSL_NOT_EQUAL : return evaluateExpression(exp->left, old) != evaluateExpression(exp->right, old);
167 case LSL_BIT_AND : return evaluateExpression(exp->left, old) & evaluateExpression(exp->right, old);
168 case LSL_BIT_XOR : return evaluateExpression(exp->left, old) ^ evaluateExpression(exp->right, old);
169 case LSL_BIT_OR : return evaluateExpression(exp->left, old) | evaluateExpression(exp->right, old);
170 case LSL_BOOL_OR : return evaluateExpression(exp->left, old) || evaluateExpression(exp->right, old);
171 case LSL_BOOL_AND : return evaluateExpression(exp->left, old) && evaluateExpression(exp->right, old);
172 }
173 break;
174 }
175 case LSL_DO :
176 case LSL_FOR :
177 case LSL_IF :
178 case LSL_ELSE :
179 case LSL_ELSEIF :
180 case LSL_JUMP :
181 case LSL_STATE_CHANGE :
182 case LSL_WHILE :
183 case LSL_RETURN :
184 case LSL_STATEMENT :
185 case LSL_BLOCK :
186 case LSL_PARAMETER :
187 case LSL_FUNCTION :
188 case LSL_STATE :
189 case LSL_SCRIPT :
190 break;
191 }
192
193 return old;
194
195}
196
197void outputExpression(LSL_Expression *exp)
198{
199 switch(exp->type)
200 {
201 case LSL_COMMENT : return;
202 case LSL_TYPE : return;
203 case LSL_NAME : return;
204 case LSL_IDENTIFIER : return;
205 case LSL_FLOAT : printf("%f", exp->value.floatValue); break;
206 case LSL_INTEGER : printf("%d", exp->value.integerValue); break;
207 case LSL_STRING : return;
208 case LSL_KEY : return;
209 case LSL_VECTOR : return;
210 case LSL_ROTATION : return;
211 case LSL_LIST : return;
212 case LSL_LABEL : return;
213 case LSL_EXPRESSION :
214 outputExpression(exp->left);
215 printf(" %s ", LSL_Tokens[exp->expression].token);
216 outputExpression(exp->right);
217 break;
218 case LSL_DO : return;
219 case LSL_FOR : return;
220 case LSL_IF : return;
221 case LSL_ELSE : return;
222 case LSL_ELSEIF : return;
223 case LSL_JUMP : return;
224 case LSL_STATE_CHANGE : return;
225 case LSL_WHILE : return;
226 case LSL_RETURN : return;
227 case LSL_STATEMENT : return;
228 case LSL_BLOCK : return;
229 case LSL_PARAMETER : return;
230 case LSL_FUNCTION : return;
231 case LSL_STATE : return;
232 case LSL_SCRIPT : return;
233 }
234}
235
236void convertExpression2Lua(LSL_Expression *exp)
237{
238 switch(exp->type)
239 {
240 case LSL_COMMENT : return;
241 case LSL_TYPE : return;
242 case LSL_NAME : return;
243 case LSL_IDENTIFIER : return;
244 case LSL_FLOAT : return;
245 case LSL_INTEGER : return;
246 case LSL_STRING : return;
247 case LSL_KEY : return;
248 case LSL_VECTOR : return;
249 case LSL_ROTATION : return;
250 case LSL_LIST : return;
251 case LSL_LABEL : return;
252 case LSL_EXPRESSION : return;
253 case LSL_DO : return;
254 case LSL_FOR : return;
255 case LSL_IF : return;
256 case LSL_ELSE : return;
257 case LSL_ELSEIF : return;
258 case LSL_JUMP : return;
259 case LSL_STATE_CHANGE : return;
260 case LSL_WHILE : return;
261 case LSL_RETURN : return;
262 case LSL_STATEMENT : return;
263 case LSL_BLOCK : return;
264 case LSL_PARAMETER : return;
265 case LSL_FUNCTION : return;
266 case LSL_STATE : return;
267 case LSL_SCRIPT : return;
268 }
78} 269}
79 270
80int yyerror(const char *msg) 271int yyerror(const char *msg)
81{ 272{
82 fprintf(stderr,"Error:%s\n",msg); return 0; 273 fprintf(stderr, "Parser error: %s\n", msg);
274 return 0;
83} 275}
84 276
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h
index 9c27a8f..a5d090e 100644
--- a/LuaSL/src/LuaSL_LSL_tree.h
+++ b/LuaSL/src/LuaSL_LSL_tree.h
@@ -8,39 +8,306 @@
8#define YY_NO_UNISTD_H 1 8#define YY_NO_UNISTD_H 1
9#endif // YY_NO_UNISTD_H 9#endif // YY_NO_UNISTD_H
10 10
11// http://w-hat.com/stackdepth is a useful discussion about some aspects of the LL parser.
11 12
12/** 13typedef enum
13 * @brief The operation type
14 */
15typedef enum tagEOperationType
16{ 14{
17 eVALUE, 15 LSL_LEFT2RIGHT = 0,
18 eMULTIPLY, 16 LSL_RIGHT2LEFT = 1,
19 ePLUS 17 LSL_INNER2OUTER = 2,
20} EOperationType; 18 LSL_UNARY = 4,
19 LSL_ASSIGNMENT = 8,
20 LSL_CREATION = 16
21} LSL_Flags;
21 22
22/** 23typedef enum // In order of precedence, high to low.
23 * @brief The expression structure 24 // Left to right, unless oterwise stated.
24 */ 25 // According to http://wiki.secondlife.com/wiki/Category:LSL_Operators
25typedef struct tagSExpression 26{
27 LSL_COMMA,
28 LSL_INCREMENT_PRE, // Right to left.
29 LSL_INCREMENT_POST, // Right to left.
30 LSL_DECREMENT_PRE, // Right to left.
31 LSL_DECREMENT_POST, // Right to left.
32 LSL_DOT, // Right to left.
33 LSL_ASSIGNMENT_PLAIN, // Right to left.
34 LSL_ASSIGNMENT_DIVIDE, // Right to left.
35 LSL_ASSIGNMENT_MODULO, // Right to left.
36 LSL_ASSIGNMENT_MULTIPLY, // Right to left.
37 LSL_ASSIGNMENT_SUBTRACT, // Right to left.
38 LSL_ASSIGNMENT_ADD, // Right to left.
39 LSL_ASSIGNMENT_CONCATENATE, // Right to left.
40 LSL_PARENTHESIS_OPEN, // Inner to outer.
41 LSL_PARENTHESIS_CLOSE, // Inner to outer.
42 LSL_BRACKET_OPEN, // Inner to outer.
43 LSL_BRACKET_CLOSE, // Inner to outer.
44 LSL_ANGLE_OPEN,
45 LSL_ANGLE_CLOSE,
46 LSL_TYPECAST, // Right to left.
47 LSL_BIT_NOT, // Right to left.
48 LSL_BOOL_NOT, // Right to left.
49 LSL_NEGATION, // Right to left.
50 LSL_DIVIDE,
51 LSL_MODULO,
52 LSL_MULTIPLY,
53 LSL_DOT_PRODUCT,
54 LSL_CROSS_PRODUCT,
55 LSL_SUBTRACT,
56 LSL_ADD,
57 LSL_CONCATENATE,
58 LSL_LEFT_SHIFT,
59 LSL_RIGHT_SHIFT,
60 LSL_LESS_THAN,
61 LSL_GREATER_THAN,
62 LSL_LESS_EQUAL,
63 LSL_GREATER_EQUAL,
64 LSL_EQUAL,
65 LSL_NOT_EQUAL,
66 LSL_BIT_AND,
67 LSL_BIT_XOR,
68 LSL_BIT_OR,
69 LSL_BOOL_OR,
70 LSL_BOOL_AND
71} LSL_Operation;
72
73typedef struct
74{
75// LSL_Operation operation,
76 char *token;
77 LSL_Flags flags;
78} LSL_Operator;
79
80// QUIRK - Seems to be some disagreement about BOOL_AND/BOOL_OR precedence. Either they are equal, or OR is higher.
81// QUIRK - Conditionals are executed right to left. Or left to right, depending on who you ask. lol
82// QUIRK - No boolean short circuiting.
83
84#ifdef LSL_Tokens_define
85LSL_Operator LSL_Tokens[] =
26{ 86{
27 EOperationType type;///< type of operation 87 {",", LSL_LEFT2RIGHT},
88 {"++", LSL_RIGHT2LEFT | LSL_UNARY},
89 {"++", LSL_RIGHT2LEFT | LSL_UNARY},
90 {"--", LSL_RIGHT2LEFT | LSL_UNARY},
91 {"--", LSL_RIGHT2LEFT | LSL_UNARY},
92 {".", LSL_RIGHT2LEFT},
93 {"=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT},
94 {"/=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT},
95 {"%=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT},
96 {"*=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT},
97 {"-=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT},
98 {"+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT},
99 {"+=", LSL_RIGHT2LEFT | LSL_ASSIGNMENT},
100 {"(", LSL_INNER2OUTER},
101 {")", LSL_INNER2OUTER},
102 {"[", LSL_INNER2OUTER | LSL_CREATION},
103 {"]", LSL_INNER2OUTER | LSL_CREATION},
104 {"<", LSL_LEFT2RIGHT | LSL_CREATION},
105 {">", LSL_LEFT2RIGHT | LSL_CREATION},
106 {"()", LSL_RIGHT2LEFT | LSL_UNARY},
107 {"~", LSL_RIGHT2LEFT | LSL_UNARY},
108 {"!", LSL_RIGHT2LEFT | LSL_UNARY},
109 {"-", LSL_RIGHT2LEFT | LSL_UNARY},
110 {"/", LSL_LEFT2RIGHT},
111 {"%", LSL_LEFT2RIGHT},
112 {"*", LSL_LEFT2RIGHT},
113 {"*", LSL_LEFT2RIGHT},
114 {"%", LSL_LEFT2RIGHT},
115 {"-", LSL_LEFT2RIGHT},
116 {"+", LSL_LEFT2RIGHT},
117 {"+", LSL_LEFT2RIGHT},
118 {"<<", LSL_LEFT2RIGHT},
119 {">>", LSL_LEFT2RIGHT},
120 {"<", LSL_LEFT2RIGHT},
121 {">", LSL_LEFT2RIGHT},
122 {"<=", LSL_LEFT2RIGHT},
123 {">=", LSL_LEFT2RIGHT},
124 {"==", LSL_LEFT2RIGHT},
125 {"!=", LSL_LEFT2RIGHT},
126 {"&", LSL_LEFT2RIGHT},
127 {"^", LSL_LEFT2RIGHT},
128 {"|", LSL_LEFT2RIGHT},
129 {"||", LSL_LEFT2RIGHT},
130 {"&&", LSL_LEFT2RIGHT}
131};
132#endif
133
134typedef enum
135{
136 LSL_COMMENT,
137 LSL_TYPE,
138 LSL_NAME,
139 LSL_IDENTIFIER,
140 LSL_FLOAT,
141 LSL_INTEGER,
142 LSL_STRING,
143 LSL_KEY,
144 LSL_VECTOR,
145 LSL_ROTATION,
146 LSL_LIST,
147 LSL_LABEL,
148 LSL_EXPRESSION,
149 LSL_DO,
150 LSL_FOR,
151 LSL_IF,
152 LSL_ELSE,
153 LSL_ELSEIF,
154 LSL_JUMP,
155 LSL_STATE_CHANGE,
156 LSL_WHILE,
157 LSL_RETURN,
158 LSL_STATEMENT,
159 LSL_BLOCK,
160 LSL_PARAMETER,
161 LSL_FUNCTION,
162 LSL_STATE,
163 LSL_SCRIPT
164} LSL_Type;
165
166#ifdef LSL_Keywords_define
167char *LSL_Keywords[] =
168{
169 "//", // Also "/*",
170 "",
171 "",
172 "",
173 "float",
174 "integer",
175 "string",
176 "key",
177 "vector",
178 "rotation",
179 "list",
180 "@",
181 "",
182 "do",
183 "for",
184 "if",
185 "else",
186 "else if",
187 "jump",
188 "state",
189 "while",
190 "return",
191 ";",
192 "{}",
193 "",
194 "",
195 "",
196 ""
197};
198#endif
199
200typedef union
201{
202 float floatValue;
203 int integerValue;
204 char *stringValue;
205 char *keyValue;
206 float vectorValue[3];
207 float rotationValue[4];
208 union LSL_Leaf *listValue;
209} LSL_Value;
210
211typedef struct
212{
213 char *name;
214 LSL_Type type;
215 LSL_Value value;
216} LSL_Identifier;
217
218typedef struct LSL_Expression
219{
220 struct LSL_Expression *left;
221 struct LSL_Expression *right;
222 LSL_Value value;
223 LSL_Operation expression;
224 LSL_Type type;
225} LSL_Expression;
226
227typedef struct
228{
229 LSL_Type type;
230 LSL_Expression *expressions;
231} LSL_Statement;
232
233typedef struct
234{
235 LSL_Statement *statements;
236} LSL_Block;
237
238typedef struct
239{
240 char *name;
241 LSL_Identifier *parameters;
242 LSL_Block block;
243 LSL_Type type;
244} LSL_Function;
245
246typedef struct
247{
248 char *name;
249 LSL_Function *handlers;
250} LSL_State;
251
252typedef struct
253{
254 char *name;
255 LSL_Identifier *variables;
256 LSL_Function *functions;
257 LSL_State *states;
258} LSL_Script;
259
260typedef union LSL_Leaf
261{
262 char *commentValue;
263 LSL_Type typeValue;
264 char *nameValue;
265 LSL_Identifier *identifierValue;
266 float floatValue;
267 int integerValue;
268 char *stringValue;
269 char *keyValue;
270 float vectorValue[3];
271 float rotationValue[4];
272 union LSL_Leaf *listValue;
273 char *labelValue;
274// LSL_Operation expressionValue;
275 LSL_Expression *expressionValue;
276 LSL_Statement *doValue;
277 LSL_Statement *forValue;
278 LSL_Statement *ifValue;
279 LSL_Statement *elseValue;
280 LSL_Statement *elseIfValue;
281 char *jumpValue;
282 char *stateChangeValue;
283 LSL_Statement *statementValue;
284 LSL_Identifier *parameterValue;
285 LSL_Function *functionValue;
286 LSL_State *stateValue;
287 LSL_Script *scriptValue;
288} LSL_Leaf;
289
290typedef struct LSL_AST
291{
292 struct LSL_AST *left;
293 struct LSL_AST *right;
294 int line;
295 int character;
296 LSL_Type type;
297 LSL_Leaf content;
298} LSL_AST;
28 299
29 int value;///< valid only when type is eVALUE
30 struct tagSExpression* left; ///< left side of the tree
31 struct tagSExpression* right;///< right side of the tree
32} SExpression;
33 300
34/** 301/**
35 * @brief The structure used by flex and bison 302 * @brief The structure used by flex and bison
36 */ 303 */
37typedef union tagTypeParser 304//typedef union tagTypeParser
38{ 305//{
39 SExpression *expression; 306// SExpression *expression;
40 int value; 307// int value;
41 int ival; 308// int ival;
42 float fval; 309// float fval;
43 char *sval; 310// char *sval;
44// class LLScriptType *type; 311// class LLScriptType *type;
45// class LLScriptConstant *constant; 312// class LLScriptConstant *constant;
46// class LLScriptIdentifier *identifier; 313// class LLScriptIdentifier *identifier;
@@ -55,10 +322,10 @@ typedef union tagTypeParser
55// class LLScriptState *state; 322// class LLScriptState *state;
56// class LLScritpGlobalStorage *global_store; 323// class LLScritpGlobalStorage *global_store;
57// class LLScriptScript *script; 324// class LLScriptScript *script;
58}STypeParser; 325//}STypeParser;
59 326
60// define the type for flex and bison 327// define the type for flex and bison
61#define YYSTYPE STypeParser 328#define YYSTYPE LSL_Leaf
62 329
63 330
64#ifndef excludeLexer 331#ifndef excludeLexer
@@ -66,47 +333,28 @@ typedef union tagTypeParser
66#endif 333#endif
67 334
68 335
69/** 336typedef struct
70 * @brief structure given as argument to the reentrant 'yyparse' function.
71 */
72typedef struct tagSParserParam
73{ 337{
74 yyscan_t scanner; 338 yyscan_t scanner;
75 SExpression *expression; 339 LSL_Expression *expression;
76}SParserParam; 340} LuaSL_yyparseParam;
77 341
342
343void burnLeaf(LSL_AST *leaf);
344void burnLSLExpression(LSL_Expression *exp);
345LSL_Expression *addInteger(int value);
346LSL_Expression *addOperation(LSL_Operation type, LSL_Expression *left, LSL_Expression *right);
347LSL_Expression *newTree(const char *expr);
348int evaluateExpression(LSL_Expression *exp, int old);
349void outputExpression(LSL_Expression *exp);
350void convertExpression2Lua(LSL_Expression *exp);
351
78// the parameter name (of the reentrant 'yyparse' function) 352// the parameter name (of the reentrant 'yyparse' function)
79// data is a pointer to a 'SParserParam' structure 353// data is a pointer to a 'SParserParam' structure
80#define YYPARSE_PARAM data 354#define YYPARSE_PARAM data
81 355
82// the argument for the 'yylex' function 356// the argument for the 'yylex' function
83#define YYLEX_PARAM ((SParserParam*)data)->scanner 357#define YYLEX_PARAM ((LuaSL_yyparseParam*)data)->scanner
84
85/**
86 * @brief It creates an identifier
87 * @param value The number value
88 * @return The expression or NULL in case of no memory
89 */
90SExpression* createNumber(int value);
91
92/**
93 * @brief It creates an operation
94 * @param type The operation type
95 * @param left The left operand
96 * @param right The right operand
97 * @return The expression or NULL in case of no memory
98 */
99SExpression* createOperation(EOperationType type, SExpression *left, SExpression *right);
100
101/**
102 * @brief Deletes a expression
103 * @param b The expression
104 */
105void deleteExpression(SExpression *b);
106
107SExpression *getAST(const char *expr);
108
109int evaluate(SExpression *e);
110 358
111int yyerror(const char *msg); 359int yyerror(const char *msg);
112int yyparse(void *param); 360int yyparse(void *param);
diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l
index 93dd39c..d204916 100644
--- a/LuaSL/src/LuaSL_lexer.l
+++ b/LuaSL/src/LuaSL_lexer.l
@@ -19,7 +19,7 @@ WS [ \r\n\t]*
19%% 19%%
20 20
21{WS} { /* Skip blanks. */ } 21{WS} { /* Skip blanks. */ }
22{NUMBER} { sscanf(yytext,"%d",&yylval->value); return TOKEN_NUMBER; } 22{NUMBER} { sscanf(yytext,"%d",&yylval->integerValue); return TOKEN_NUMBER; }
23 23
24{MULTIPLY} { return TOKEN_MULTIPLY; } 24{MULTIPLY} { return TOKEN_MULTIPLY; }
25{PLUS} { return TOKEN_PLUS; } 25{PLUS} { return TOKEN_PLUS; }
diff --git a/LuaSL/src/LuaSL_parser.c b/LuaSL/src/LuaSL_parser.c
index f5a56d3..35cb782 100644
--- a/LuaSL/src/LuaSL_parser.c
+++ b/LuaSL/src/LuaSL_parser.c
@@ -5,14 +5,16 @@
5int main(void) 5int main(void)
6{ 6{
7 const char test[] = " 4 + 2*10 + 3*( 5 + 1 )"; 7 const char test[] = " 4 + 2*10 + 3*( 5 + 1 )";
8 SExpression *e = NULL; 8 LSL_Expression *exp;
9 9
10 if ((e = getAST(test))) 10 if ((exp = newTree(test)))
11 { 11 {
12 int result = evaluate(e); 12 int result = evaluateExpression(exp, 0);
13 13
14 printf("Result of '%s' is %d\n", test, result); 14 printf("Result of '%s' is %d\n", test, result);
15 deleteExpression(e); 15 outputExpression(exp);
16 printf("\n");
17 burnLSLExpression(exp);
16 } 18 }
17 19
18 return 0; 20 return 0;
diff --git a/LuaSL/src/LuaSL_yaccer.y b/LuaSL/src/LuaSL_yaccer.y
index 19009bf..820f7da 100644
--- a/LuaSL/src/LuaSL_yaccer.y
+++ b/LuaSL/src/LuaSL_yaccer.y
@@ -14,21 +14,21 @@
14%token TOKEN_PLUS 14%token TOKEN_PLUS
15%token TOKEN_MULTIPLY 15%token TOKEN_MULTIPLY
16 16
17%token <value> TOKEN_NUMBER 17%token <integerValue> TOKEN_NUMBER
18 18
19%type <expression> expr 19%type <expressionValue> expr
20 20
21%% 21%%
22 22
23input: 23input:
24 expr { ((SParserParam*)data)->expression = $1; } 24 expr { ((LuaSL_yyparseParam*)data)->expression = $1; }
25 ; 25 ;
26 26
27expr: 27expr:
28 expr TOKEN_PLUS expr { $$ = createOperation( ePLUS, $1, $3 ); } 28 expr TOKEN_PLUS expr { $$ = addOperation( LSL_ADD, $1, $3 ); }
29 | expr TOKEN_MULTIPLY expr { $$ = createOperation( eMULTIPLY, $1, $3 ); } 29 | expr TOKEN_MULTIPLY expr { $$ = addOperation( LSL_MULTIPLY, $1, $3 ); }
30 | TOKEN_LPAREN expr TOKEN_RPAREN { $$ = $2; } 30 | TOKEN_LPAREN expr TOKEN_RPAREN { $$ = $2; }
31 | TOKEN_NUMBER { $$ = createNumber($1); } 31 | TOKEN_NUMBER { $$ = addInteger($1); }
32; 32;
33 33
34%% 34%%