21 this->_input = ogn._input;
22 this->_pos = ogn._pos;
31 switch (this->_input[this->_pos]) {
36 case '*':
case '/': this->_pos++;
return next();
38 double number = this->read_number();
40 switch (this->_input[this->_pos]) {
41 case '*': this->_pos++; number *= this->read_number();
break;
42 case '/': this->_pos++; number /= this->read_number();
break;
43 case '^': this->_pos++; number = std::pow(number, this->read_number());
break;
53 if (this->_pos <= 0) {
return;}
54 if (emthp::Lexer::is_digit(this->_input[this->_pos-1])) {
while (emthp::Lexer::is_digit(this->_input[this->_pos-1])) {this->_pos--;}
return;}
62std::string emthp::Lexer::clean(
const std::string& input)
noexcept {
63 long unsigned int pos = 0; std::string string;
64 while (pos < input.size()) {
66 case '+':
string +=
'+';
break;
67 case '-':
string +=
'-';
break;
68 case '*':
string +=
'*';
break;
69 case '/':
string +=
'/';
break;
70 case '^':
string +=
'^';
break;
71 case 'x':
string +=
'x';
break;
72 default:
if (emthp::Lexer::is_digit(input[pos])) {
string += input[pos];}
break;
81double emthp::Lexer::read_number() noexcept {
83 while (this->_pos < this->_input.size() && emthp::Lexer::is_digit(this->_input[this->_pos])) {
84 snum += this->_input[this->_pos];
87 if (snum.empty()) {
return 1;}
88 return std::stod(snum);
107 while (
true) {
if(!polynomial.
push_monomial(std::move(this->parse_monomial()))) {
return polynomial;}}
115 monomial.
set_coeff(this->parse_number());
116 bool v =
false;
bool e =
false;
119 switch (token.
type) {
126 monomial.
set_degree((
int) this->parse_number());
133double emthp::Parser::parse_number() noexcept {
134 bool positive =
true;
Class for representing and operating monomials.
void set_degree(int dgr) noexcept
Setter function for setting the degree of the Monomial.
void set_coeff(double cf) noexcept
Setter function for setting the coefficient of the Monomial.
Class for representing and operating polynomials.
bool push_monomial(const Monomial &m) noexcept
Function for appending a lvalue monomial to the Polynomial.
Class that works as a token supplier for the Parser class.
void back() noexcept
Backtrack function that moves the cursor's position one position backwards.
Lexer & operator=(const Lexer &ogn) noexcept
Assigment operator for when called with an lvalue.
Token next() noexcept
Function that returns the next Token in the input provided in the constructor.
Lexer(const std::string &input) noexcept
Standalone constructor for the Lexer class.
Class that wraps a std::string and parses it as a Monomial or as a Polynomial.
Parser(const std::string &input) noexcept
Standalone constructor for the Parser class.
emth::Monomial parse_monomial() noexcept
Function that parses the next Monomial in the input.
emth::Polynomial parse_polynomial() noexcept
Function that parses the input as a Polynomial.
Parser & operator=(const Parser &ogn) noexcept
Assigment operator for when called with an lvalue.
This is the main header file for the equation parser.
@ T_MINUS
Minus sign type.
@ T_EXPONENT
Exponent sign type.
Struct for representing a token as a type and a value.
double value
The token's numerical value as a double.
TokenType type
The token's type as a TokenType.