summaryrefslogtreecommitdiff
path: root/parser.cpp
blob: e1595f30a9c1b6a7db063017bc3592a9b222f631 (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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <string>
#include "include/parser.hpp"
#include "include/expressions.hpp"
#include "include/lexer.hpp"
#include "include/exceptions.hpp"

using std::string, std::stoi;

// De haber habido un error vaciaremos la pila de expresiones.
void Parser::panic() {
  for(auto ptr = tree_stack.begin(); ptr != tree_stack.end(); ++ptr)
    delete *ptr;

  throw ParserException("Sintaxis incorrecta.");
}

Expression *Parser::popStack() {
  Expression *val = tree_stack.back();
  tree_stack.pop_back();

  return val;
}

Expression *Parser::newTree(token_type type) {
  switch(type) {
  case sum:
    return new AddExpression(popStack(), popStack());
  case substraction:
    return new SubExpression(popStack(), popStack());
  case multiplication:
    return new MultiplicationExpression(popStack(), popStack());
  case division:
    return new DivisionExpression(popStack(), popStack());
  default: // Potencia.
    return new PowerExpression(popStack(), popStack());
  }
}

void Parser::setText(string text) {
  tokenizer.setText(text); // Se prepara el lexer.
  var.value = "{}";
  var.type = nil;
}

bool Parser::checkToken() {
  return tokenizer.getCurrentToken().type == nil;
}

Expression *Parser::parse(string expr) {
  setText(expr);
  parseExpr();

  /* Si no se ha consumido toda la cadena, lanzamos
     error. */
  if(tokenizer.getCurrentToken().type != nil)
    panic();

  return popStack();
}

void Parser::parseExpr() {
  parseTerm();
  parsePrimeExpr();
}

void Parser::parsePrimeExpr() {
  token_type current;
  
  if(tokenizer.getCurrentToken().type == sum
     || tokenizer.getCurrentToken().type == substraction) {
    current = tokenizer.getCurrentToken().type;
    tokenizer.nextToken();

    if(checkToken())
      panic();

    parseTerm();
    tree_stack.push_back(newTree(current));
    parsePrimeExpr();
  }
}

void Parser::parseTerm() {
  parsePower();
  parsePrimeTerm();
}

void Parser::parsePrimeTerm() {
  token_type current;
  
  if(tokenizer.getCurrentToken().type == multiplication
     || tokenizer.getCurrentToken().type == division) {
    current = tokenizer.getCurrentToken().type;
    tokenizer.nextToken();

    if(checkToken())
      panic();

    parsePower();
    tree_stack.push_back(newTree(current));
    parsePrimeTerm();
  }
}

void Parser::parsePower() {
  parseFactor();
  parsePrimePower();
}

void Parser::parsePrimePower() {
  if(tokenizer.getCurrentToken().type == power) {
    tokenizer.nextToken();

    if(tokenizer.getCurrentToken().type != number)
      panic();

    tree_stack.push_back(new Literal(stoi(tokenizer.getCurrentToken().value)));
    tokenizer.nextToken();

    tree_stack.push_back(newTree(power));
    parsePrimePower();
  }
}

void Parser::parseFactor() {
  trig_functions name;
  
  switch(tokenizer.getCurrentToken().type) {
  case substraction:
    tokenizer.nextToken();
    
    parseExpr();
    tree_stack.push_back(new NegationExpression(popStack()));
    
    break;
  case left_parens:
    tokenizer.nextToken();
    parseExpr();

    // Consumimos paréntesis derecho.
    if(tokenizer.getCurrentToken().type != right_parens)
      panic();

    tokenizer.nextToken();
    
    break;
  case function:    
    if(tokenizer.getCurrentToken().value == "sin")
      name = i_sin;
    else if(tokenizer.getCurrentToken().value == "cos")
      name = i_cos;
    else if(tokenizer.getCurrentToken().value == "tan")
      name = i_tan;
    else if(tokenizer.getCurrentToken().value == "csc")
      name = i_csc;
    else if(tokenizer.getCurrentToken().value == "sec")
      name = i_sec;
    else
      name = i_ctg;
    
    tokenizer.nextToken();

    if(tokenizer.getCurrentToken().type != left_parens)
      panic();

    tokenizer.nextToken();
    parseExpr();
    
    if(tokenizer.getCurrentToken().type != right_parens)
      panic();
    
    tokenizer.nextToken();
    tree_stack.push_back(new Function(popStack(), name));
    
    break;
  case variable:
    /* Checamos si no se ha registrado ningún nombre de variable o si
       la variable leída por el lexer es la misma que la que se había leído. */
    if(tokenizer.getCurrentToken().value == var.value || var.type == nil)
      var = tokenizer.getCurrentToken();
    else
      panic(); // Se introdujeron dos variables en la expresión.
    
    tree_stack.push_back(new Variable(tokenizer.getCurrentToken().value[0]));
    tokenizer.nextToken();
  
    break;
  case number:
    tree_stack.push_back(new Literal(stoi(tokenizer.getCurrentToken().value)));
    tokenizer.nextToken();

    break;
  default:
    panic();
    
    break;
  }
}