diff options
Diffstat (limited to '')
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.c | 112 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.h | 38 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_parser.c | 92 |
3 files changed, 100 insertions, 142 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index 78da8e7..7ea5d67 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c | |||
@@ -1,63 +1,85 @@ | |||
1 | /* | 1 | /* |
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 | #include "LuaSL_LSL_tree.h" | 5 | #include "LuaSL_parser_param.h" |
6 | #include "LuaSL_yaccer.tab.h" | ||
6 | #include <stdlib.h> | 7 | #include <stdlib.h> |
7 | 8 | ||
8 | /** | 9 | /** |
9 | * @brief Allocates space for expression | 10 | * @brief Allocates space for expression |
10 | * @return The expression or NULL if not enough memory | 11 | * @return The expression or NULL if not enough memory |
11 | */ | 12 | */ |
12 | static SExpression* allocateExpression() | 13 | static SExpression* newExpression(EOperationType type, SExpression *left, SExpression *right, int value) |
13 | { | 14 | { |
14 | SExpression* b = malloc(sizeof *b); | 15 | SExpression* b = malloc(sizeof *b); |
15 | 16 | ||
16 | if( b == NULL ) return NULL; | 17 | if (b == NULL) return NULL; |
17 | 18 | ||
18 | b->type = eVALUE; | 19 | b->type = type; |
19 | b->value = 0; | 20 | b->value = value; |
20 | 21 | b->left = left; | |
21 | b->left = NULL; | 22 | b->right = right; |
22 | b->right = NULL; | 23 | |
23 | 24 | return b; | |
24 | return b; | ||
25 | } | 25 | } |
26 | 26 | ||
27 | SExpression* createNumber(int value) | 27 | SExpression* createNumber(int value) |
28 | { | 28 | { |
29 | SExpression* b = allocateExpression(); | 29 | return newExpression(eVALUE, NULL, NULL, value); |
30 | |||
31 | if( b == NULL ) return NULL; | ||
32 | |||
33 | b->type = eVALUE; | ||
34 | b->value = value; | ||
35 | |||
36 | return b; | ||
37 | } | 30 | } |
38 | 31 | ||
39 | SExpression *createOperation( | 32 | SExpression *createOperation(EOperationType type, SExpression *left, SExpression *right) |
40 | EOperationType type, | ||
41 | SExpression *left, | ||
42 | SExpression *right) | ||
43 | { | 33 | { |
44 | SExpression* b = allocateExpression(); | 34 | return newExpression(type, left, right, 0); |
45 | |||
46 | if( b == NULL ) return NULL; | ||
47 | |||
48 | b->type = type; | ||
49 | b->left = left; | ||
50 | b->right = right; | ||
51 | |||
52 | return b; | ||
53 | } | 35 | } |
54 | 36 | ||
37 | int evaluate(SExpression *e) | ||
38 | { | ||
39 | switch(e->type) | ||
40 | { | ||
41 | case eVALUE: | ||
42 | return e->value; | ||
43 | case eMULTIPLY: | ||
44 | return evaluate(e->left) * evaluate(e->right); | ||
45 | case ePLUS: | ||
46 | return evaluate(e->left) + evaluate(e->right); | ||
47 | default: | ||
48 | // shouldn't be here | ||
49 | return 0; | ||
50 | } | ||
51 | } | ||
52 | |||
55 | void deleteExpression(SExpression *b) | 53 | void deleteExpression(SExpression *b) |
56 | { | 54 | { |
57 | if (b == NULL) return; | 55 | if (b == NULL) return; |
58 | 56 | ||
59 | deleteExpression(b->left); | 57 | deleteExpression(b->left); |
60 | deleteExpression(b->right); | 58 | deleteExpression(b->right); |
61 | 59 | ||
62 | free(b); | 60 | free(b); |
61 | } | ||
62 | |||
63 | SExpression *getAST(const char *expr) | ||
64 | { | ||
65 | SParserParam p; | ||
66 | YY_BUFFER_STATE state; | ||
67 | |||
68 | p.expression = NULL; | ||
69 | if (yylex_init(&(p.scanner))) | ||
70 | return NULL; | ||
71 | |||
72 | state = yy_scan_string(expr, p.scanner); | ||
73 | if (yyparse(&p)) | ||
74 | return NULL; | ||
75 | |||
76 | yy_delete_buffer(state, p.scanner); | ||
77 | yylex_destroy(p.scanner); | ||
78 | return p.expression; | ||
79 | } | ||
80 | |||
81 | int yyerror(const char *msg) | ||
82 | { | ||
83 | fprintf(stderr,"Error:%s\n",msg); return 0; | ||
63 | } | 84 | } |
85 | |||
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 6d89672..71d372e 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h | |||
@@ -3,7 +3,7 @@ | |||
3 | */ | 3 | */ |
4 | #ifndef __EXPRESSION_H__ | 4 | #ifndef __EXPRESSION_H__ |
5 | #define __EXPRESSION_H__ | 5 | #define __EXPRESSION_H__ |
6 | 6 | ||
7 | /** | 7 | /** |
8 | * @brief The operation type | 8 | * @brief The operation type |
9 | */ | 9 | */ |
@@ -12,27 +12,27 @@ typedef enum tagEOperationType | |||
12 | eVALUE, | 12 | eVALUE, |
13 | eMULTIPLY, | 13 | eMULTIPLY, |
14 | ePLUS | 14 | ePLUS |
15 | }EOperationType; | 15 | } EOperationType; |
16 | 16 | ||
17 | /** | 17 | /** |
18 | * @brief The expression structure | 18 | * @brief The expression structure |
19 | */ | 19 | */ |
20 | typedef struct tagSExpression | 20 | typedef struct tagSExpression |
21 | { | 21 | { |
22 | EOperationType type;///< type of operation | 22 | EOperationType type;///< type of operation |
23 | 23 | ||
24 | int value;///< valid only when type is eVALUE | 24 | int value;///< valid only when type is eVALUE |
25 | struct tagSExpression* left; ///< left side of the tree | 25 | struct tagSExpression* left; ///< left side of the tree |
26 | struct tagSExpression* right;///< right side of the tree | 26 | struct tagSExpression* right;///< right side of the tree |
27 | }SExpression; | 27 | } SExpression; |
28 | 28 | ||
29 | /** | 29 | /** |
30 | * @brief It creates an identifier | 30 | * @brief It creates an identifier |
31 | * @param value The number value | 31 | * @param value The number value |
32 | * @return The expression or NULL in case of no memory | 32 | * @return The expression or NULL in case of no memory |
33 | */ | 33 | */ |
34 | SExpression* createNumber(int value); | 34 | SExpression* createNumber(int value); |
35 | 35 | ||
36 | /** | 36 | /** |
37 | * @brief It creates an operation | 37 | * @brief It creates an operation |
38 | * @param type The operation type | 38 | * @param type The operation type |
@@ -40,16 +40,20 @@ SExpression* createNumber(int value); | |||
40 | * @param right The right operand | 40 | * @param right The right operand |
41 | * @return The expression or NULL in case of no memory | 41 | * @return The expression or NULL in case of no memory |
42 | */ | 42 | */ |
43 | SExpression* createOperation( | 43 | SExpression* createOperation(EOperationType type, SExpression *left, SExpression *right); |
44 | EOperationType type, | 44 | |
45 | SExpression *left, | ||
46 | SExpression *right); | ||
47 | |||
48 | /** | 45 | /** |
49 | * @brief Deletes a expression | 46 | * @brief Deletes a expression |
50 | * @param b The expression | 47 | * @param b The expression |
51 | */ | 48 | */ |
52 | void deleteExpression(SExpression *b); | 49 | void deleteExpression(SExpression *b); |
53 | 50 | ||
51 | SExpression *getAST(const char *expr); | ||
52 | |||
53 | int evaluate(SExpression *e); | ||
54 | |||
55 | int yyerror(const char *msg); | ||
56 | int yyparse(void *param); | ||
57 | |||
54 | #endif // __EXPRESSION_H__ | 58 | #endif // __EXPRESSION_H__ |
55 | 59 | ||
diff --git a/LuaSL/src/LuaSL_parser.c b/LuaSL/src/LuaSL_parser.c index 9e1d3ca..c6026e2 100644 --- a/LuaSL/src/LuaSL_parser.c +++ b/LuaSL/src/LuaSL_parser.c | |||
@@ -1,86 +1,18 @@ | |||
1 | #include "LuaSL_parser_param.h" | 1 | #include "LuaSL_LSL_tree.h" |
2 | #include "LuaSL_yaccer.tab.h" | 2 | #include <stdio.h> |
3 | |||
4 | |||
5 | int yyparse(void *param); | ||
6 | |||
7 | static int initParserParam(SParserParam* param) | ||
8 | { | ||
9 | int ret = 0; | ||
10 | |||
11 | ret = yylex_init(¶m->scanner); | ||
12 | param->expression = NULL; | ||
13 | |||
14 | return ret; | ||
15 | } | ||
16 | |||
17 | static int destroyParserParam(SParserParam* param) | ||
18 | { | ||
19 | return yylex_destroy(param->scanner); | ||
20 | } | ||
21 | |||
22 | SExpression *getAST(const char *expr) | ||
23 | { | ||
24 | SParserParam p; | ||
25 | YY_BUFFER_STATE state; | ||
26 | |||
27 | if ( initParserParam(&p) ) | ||
28 | { | ||
29 | // couldn't initialize | ||
30 | return NULL; | ||
31 | } | ||
32 | |||
33 | state = yy_scan_string(expr, p.scanner); | ||
34 | |||
35 | if ( yyparse(&p) ) | ||
36 | { | ||
37 | // error parsing | ||
38 | return NULL; | ||
39 | } | ||
40 | |||
41 | yy_delete_buffer(state, p.scanner); | ||
42 | |||
43 | destroyParserParam(&p); | ||
44 | |||
45 | return p.expression; | ||
46 | } | ||
47 | |||
48 | int evaluate(SExpression *e) | ||
49 | { | ||
50 | switch(e->type) | ||
51 | { | ||
52 | case eVALUE: | ||
53 | return e->value; | ||
54 | case eMULTIPLY: | ||
55 | return evaluate(e->left) * evaluate(e->right); | ||
56 | case ePLUS: | ||
57 | return evaluate(e->left) + evaluate(e->right); | ||
58 | default: | ||
59 | // shouldn't be here | ||
60 | return 0; | ||
61 | } | ||
62 | } | ||
63 | 3 | ||
64 | int yyerror(const char *msg) | ||
65 | { | ||
66 | fprintf(stderr,"Error:%s\n",msg); return 0; | ||
67 | } | ||
68 | 4 | ||
69 | int main(void) | 5 | int main(void) |
70 | { | 6 | { |
71 | SExpression *e = NULL; | 7 | char test[]=" 4 + 2*10 + 3*( 5 + 1 )"; |
72 | char test[]=" 4 + 2*10 + 3*( 5 + 1 )"; | 8 | SExpression *e = NULL; |
73 | int result = 0; | 9 | int result = 0; |
74 | 10 | ||
75 | e = getAST(test); | 11 | e = getAST(test); |
76 | 12 | result = evaluate(e); | |
77 | result = evaluate(e); | 13 | printf("Result of '%s' is %d\n", test, result); |
78 | 14 | deleteExpression(e); | |
79 | printf("Result of '%s' is %d\n", test, result); | ||
80 | |||
81 | deleteExpression(e); | ||
82 | |||
83 | return 0; | ||
84 | } | ||
85 | 15 | ||
16 | return 0; | ||
17 | } | ||
86 | 18 | ||