-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree.c
80 lines (66 loc) · 2.11 KB
/
tree.c
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
/***************************************************************************
This file is a part of Interpretor.
Interpretor is a simple interpretor of a basic language.
Copyright (C) 2011 DURET Simon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "global.h"
void free_tree(struct tree *tree)
{
if(tree != NULL)
{
free_tree(tree->left);
free_tree(tree->right);
free(tree);
}
}
struct tree* create_node(struct tree *left, struct token *token, struct tree *right)
{
struct tree *node;
if((node = malloc(sizeof(struct tree))) == NULL)
{
perror("malloc ");
exit(EXIT_FAILURE);
}
node->token = *token;
node->left = left;
node->right = right;
return node;
}
double calc_tree(struct tree *tree)
{
assert(tree != NULL);
switch(tree->token.type)
{
case OPERATOR:
return exec_operator(tree->token.u_type.op_id, tree->left, tree->right);
case VALUE:
return tree->token.u_type.val;
case FUNCTION:
return exec_function(tree->token.u_type.fun_id, tree->left);
case VARIABLE:
return get_variable_value(tree->token.u_type.var_id);
case AFFECTATION:
return affect_variable(tree->left->token.u_type.var_id, calc_tree(tree->right));
case WHILE:
case IF:
return calc_tree(tree->left);
case END:
case UNDEF:
return 0.0;
}
return 0.0;
}