blob: 534e4f422cc10c60c1ad5f367fd27895cdc15c71 (
plain)
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
|
/* PIA - Lenguajes Modernos de Programación
* FACULTAD DE CIENCIAS FÍSICO MATEMÁTICAS
* Luis Sebastián Martínez Vega - LCC */
#include <string>
#include "include/expressions.hpp"
#include "include/lexer.hpp"
using std::string;
Expression::Expression(Expression *left, Expression *right, token_type type)
: left(left), right(right), type(type) {}
Expression::~Expression() {
delTree();
}
const Expression *Expression::getLeft() {
return left;
}
const Expression *Expression::getRight() {
return right;
}
string Expression::getRepr() {
if(repr.empty())
setRepr();
return repr;
}
void Expression::setRepr() {
repr += '(';
if(getLeft() != NULL)
repr += left->getRepr();
switch(type) {
case sum:
repr += '+';
break;
case substraction:
repr += '-';
break;
case multiplication:
repr += '*';
break;
case division:
repr += '/';
break;
case power:
repr += '^';
break;
}
if(getRight() != NULL)
repr += right->getRepr();
repr += ')';
}
void Expression::delTree() {
if(getLeft() != NULL)
delete left;
if(getRight() != NULL)
delete right;
delete this;
}
|