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.c112
1 files changed, 67 insertions, 45 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 */
12static SExpression* allocateExpression() 13static 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
27SExpression* createNumber(int value) 27SExpression* 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
39SExpression *createOperation( 32SExpression *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
37int 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
55void deleteExpression(SExpression *b) 53void 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
63SExpression *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
81int yyerror(const char *msg)
82{
83 fprintf(stderr,"Error:%s\n",msg); return 0;
63} 84}
85