From 643e33711781fb5a2a3ffc1b8b4b0031bf9509a1 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sun, 3 Apr 2022 16:42:07 -0500 Subject: Añadido código de las expresiones. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- expression_base.cpp | 76 ++++++++++++++++++++++++++++ expressions.cpp | 132 ++++++++++++++++++++++++++++++++++++++++++++++++ include/expressions.hpp | 41 ++++++++------- 3 files changed, 232 insertions(+), 17 deletions(-) create mode 100644 expression_base.cpp create mode 100644 expressions.cpp 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 +#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 +#include +#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 +#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 */ -- cgit v1.2.3