summaryrefslogtreecommitdiff
path: root/expression_base.cpp
blob: 2457571ab49378da6354e18ed9e5a6b3df82ab85 (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
/* 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) {}

const Expression *Expression::getLeft() const {
  return left;
}

const Expression *Expression::getRight() const {
  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;
}