-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
106 lines (97 loc) · 2.04 KB
/
code.c
File metadata and controls
106 lines (97 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "code.h"
#include <stdlib.h>
#include <stdio.h>
void freeAST(AstNode *root)
{
if (root != NULL)
{
if (root->left != NULL)
{
free(root->left);
}
if (root->right != NULL)
{
free(root->right);
}
free(root);
}
}
// opr
AstNode *buildAST(AstNode *left, AstNode *right, DATATYPE value)
{
AstNode *node = (AstNode *)malloc(sizeof(AstNode));
if (!node)
{
printf("error building ast\n");
exit(-1);
}
node->value = value;
node->left = left;
node->right = right;
return node;
}
// valori
AstNode *addNode(int valoare)
{
info *obj = (info *)malloc(sizeof(info));
if (!obj)
{
printf("error adding number ast\n");
exit(-1);
}
obj->type = NUMBER;
obj->val = valoare;
return (AstNode *)obj;
}
int evalAST(AstNode *node)
{
int resultEval = 0;
if (node->value == NUMBER)
{
resultEval = ((info *)node)->val;
}
else if (node->value == ADD)
{
resultEval = evalAST(node->left) + evalAST(node->right);
}
else if (node->value == SUB)
{
resultEval = evalAST(node->left) - evalAST(node->right);
}
else if (node->value == MUL)
{
resultEval = evalAST(node->left) * evalAST(node->right);
}
else if (node->value == DIV)
{
int nod = evalAST(node->right);
if(nod!= 0){
resultEval = evalAST(node->left) / evalAST(node->right);
}
else{
printf("Eroare: impartire la 0\n");
exit(-1);
}
}
else if (node->value == POW)
{
resultEval = evalAST(node->left) ^ evalAST(node->right);
}
else if (node->value == MOD)
{
int nod = evalAST(node->right);
if(nod!= 0){
resultEval = evalAST(node->left) % evalAST(node->right);
}
else{
printf("Eroare: impartire la 0\n");
exit(-1);
}
}
else
{
printf("0\n");
exit(-1);
}
return resultEval;
}