summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--expression_base.cpp76
-rw-r--r--expressions.cpp132
-rw-r--r--include/expressions.hpp41
3 files changed, 232 insertions, 17 deletions
diff --git a/expression_base.cpp b/expression_base.cpp
new file mode 100644
index 0000000..534e4f4
--- /dev/null
+++ b/expression_base.cpp
@@ -0,0 +1,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;
+}
diff --git a/expressions.cpp b/expressions.cpp
new file mode 100644
index 0000000..e402020
--- /dev/null
+++ b/expressions.cpp
@@ -0,0 +1,132 @@
+#include <cstddef>
+#include <string>
+#include "include/lexer.hpp"
+#include "include/expressions.hpp"
+
+using std::string, std::to_string;
+
+// Literal
+Literal::Literal(int value) : Expression(NULL, NULL, number) {
+ this->value = value;
+}
+
+Literal::~Literal() {
+ delTree();
+}
+
+void Literal::setRepr() {
+ repr = to_string(value);
+}
+
+// Function.
+Function::Function(Expression *arg, trig_functions name) : Expression(NULL, NULL, function) {
+ this->arg = arg;
+ function_name = name;
+}
+
+Function::~Function() {
+ delTree();
+}
+
+trig_functions Function::getFunctionName() { return function_name; }
+
+void Function::delTree() {
+ delete arg;
+ delete this;
+}
+
+void Function::setRepr() {
+ switch(function_name) {
+ case sin:
+ repr += "sin";
+
+ break;
+ case cos:
+ repr += "cos";
+
+ break;
+ case tan:
+ repr += "tan";
+
+ break;
+ case csc:
+ repr += "csc";
+
+ break;
+ case sec:
+ repr += "sec";
+
+ break;
+ case ctg:
+ repr += "ctg";
+
+ break;
+ }
+
+ repr += '(';
+ repr += arg->getRepr();
+ repr += ')';
+}
+
+// Variable.
+Variable::Variable(char name) : Expression(NULL, NULL, variable) {
+ this->name = name;
+ setRepr();
+}
+
+Variable::~Variable() {
+ delTree();
+}
+
+void Variable::setRepr() {
+ repr += name;
+}
+
+// Sum.
+AddExpression::AddExpression(Expression *left, Expression *right)
+ : Expression(left, right, sum) {}
+
+AddExpression::~AddExpression() {
+ delTree();
+}
+
+// Substraction.
+SubExpression::SubExpression(Expression *left, Expression *right)
+ : Expression(left, right, substraction) {}
+
+SubExpression::~SubExpression() {
+ delTree();
+}
+
+// Negation.
+NegationExpression::NegationExpression(Expression *right)
+ : Expression(NULL, right, substraction) {}
+
+NegationExpression::~NegationExpression() {
+ delTree();
+}
+
+// División.
+DivisionExpression::DivisionExpression(Expression *left, Expression *right)
+ : Expression(left, right, division) {}
+
+DivisionExpression::~DivisionExpression() {
+ delTree();
+}
+
+// Multiplication.
+MultiplicationExpression::MultiplicationExpression(Expression *left,
+ Expression *right)
+ : Expression(left, right, multiplication) {}
+
+MultiplicationExpression::~MultiplicationExpression() {
+ delTree();
+}
+
+// Power.
+PowerExpression::PowerExpression(Expression *left, Expression *right)
+ : Expression(left, right, power) {}
+
+PowerExpression::~PowerExpression() {
+ delTree();
+}
diff --git a/include/expressions.hpp b/include/expressions.hpp
index 5d8ed03..908272f 100644
--- a/include/expressions.hpp
+++ b/include/expressions.hpp
@@ -3,6 +3,7 @@
* Luis Sebastián Martínez Vega - LCC */
#include <string>
+#include "lexer.hpp"
#ifndef EXPRESSIONS_H
#define EXPRESSIONS_H
@@ -19,79 +20,85 @@ class Expression {
private:
Expression *left;
Expression *right;
- std::string repr;
- void setRepr();
- void delTree();
+ token_type type;
public:
- Expression(Expression *left, Expression *right);
+ Expression(Expression *left, Expression *right, token_type type);
virtual ~Expression() = 0;
std::string getRepr();
- Expression *getLeft();
- Expression *getRight();
+ const Expression *getLeft();
+ const Expression *getRight();
+protected:
+ virtual void setRepr();
+ void delTree();
+ std::string repr{};
};
class Literal : public Expression {
private:
int value;
public:
- Literal();
+ Literal(int value);
~Literal();
int getValue();
+ void setRepr() override;
};
class Function : public Expression {
private:
trig_functions function_name;
+ Expression *arg;
public:
- Function();
+ Function(Expression *arg, trig_functions name);
~Function();
trig_functions getFunctionName();
+ void setRepr() override;
+protected:
+ void delTree();
};
class Variable : public Expression {
private:
char name;
public:
- Variable();
+ Variable(char name);
~Variable();
- char getName();
+ void setRepr() override;
};
class AddExpression : public Expression {
public:
- AddExpression();
+ AddExpression(Expression *left, Expression *right);
~AddExpression();
};
class SubExpression : public Expression {
public:
- SubExpression();
+ SubExpression(Expression *left, Expression *right);
~SubExpression();
};
class NegationExpression : public Expression {
public:
- NegationExpression();
+ NegationExpression(Expression *right);
~NegationExpression();
};
class DivisionExpression : public Expression {
public:
- DivisionExpression();
+ DivisionExpression(Expression *left, Expression *right);
~DivisionExpression();
};
class MultiplicationExpression : public Expression {
public:
- MultiplicationExpression();
+ MultiplicationExpression(Expression *left, Expression *right);
~MultiplicationExpression();
};
class PowerExpression : public Expression {
public:
- PowerExpression();
+ PowerExpression(Expression *left, Expression *right);
~PowerExpression();
};
-
#endif /* EXPRESSIONS_H */