EMath++
Classes for mathematical concepts
Loading...
Searching...
No Matches
eparser.cpp
Go to the documentation of this file.
1
9#include "eparser.h"
14emthp::Lexer::Lexer(const std::string& input) noexcept : _input(this->clean(input)), _pos(0) {}
21 this->_input = ogn._input;
22 this->_pos = ogn._pos;
23 return *this;
24};
30 if (this->_pos >= this->_input.size()) {return {emthp::TokenType::T_EOF, {0}};} //Returs EOL on end of _input
31 switch (this->_input[this->_pos]) {
32 case '+': this->_pos++; return {emthp::TokenType::T_PLUS, {0}};
33 case '-': this->_pos++; return {emthp::TokenType::T_MINUS, {0}};
34 case '^': this->_pos++; return {emthp::TokenType::T_EXPONENT, {0}};
35 case 'x': this->_pos++; return {emthp::TokenType::T_VARIABLE, {0}};
36 case '*': case '/': this->_pos++; return next();
37 default:
38 double number = this->read_number();
39 while(true) {
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;
44 default: return {emthp::TokenType::T_NUMBER, {number}};
45 }
46 }
47 }
48}
52void emthp::Lexer::back() noexcept {
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;}
55 this->_pos--; return;
56}
62std::string emthp::Lexer::clean(const std::string& input) noexcept {
63 long unsigned int pos = 0; std::string string;
64 while (pos < input.size()) {
65 switch (input[pos]) {
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;
73 } pos++;
74 }
75 return string;
76}
81double emthp::Lexer::read_number() noexcept {
82 std::string snum;
83 while (this->_pos < this->_input.size() && emthp::Lexer::is_digit(this->_input[this->_pos])) {
84 snum += this->_input[this->_pos];
85 this->_pos++;
86 }
87 if (snum.empty()) {return 1;}
88 return std::stod(snum);
89}
94emthp::Parser::Parser(const std::string& input) noexcept : _lexer(input) {};
100emthp::Parser& emthp::Parser::operator =(const emthp::Parser& ogn) noexcept {this->_lexer = ogn._lexer; return *this;};
106 emth::Polynomial polynomial;
107 while (true) {if(!polynomial.push_monomial(std::move(this->parse_monomial()))) {return polynomial;}}
108}
114 emth::Monomial monomial(0, 0);
115 monomial.set_coeff(this->parse_number());
116 bool v = false; bool e = false;
117 while (!v || !e) {
118 emthp::Token token = this->_lexer.next();
119 switch (token.type) {
120 case emthp::TokenType::T_VARIABLE: v = true; break;
121 case emthp::TokenType::T_EXPONENT: if (v) {e = true;} break;
122 case emthp::TokenType::T_PLUS: case emthp::TokenType::T_MINUS: case emthp::TokenType::T_NUMBER: if (v) {monomial.set_degree(1); this->_lexer.back();} return monomial;
123 case emthp::TokenType::T_EOF: if (v) {monomial.set_degree(1);} return monomial;
124 }
125 }
126 monomial.set_degree((int) this->parse_number());
127 return monomial;
128}
133double emthp::Parser::parse_number() noexcept {
134 bool positive = true;
135 while(true) {
136 emthp::Token token = this->_lexer.next();
137 switch(token.type) {
138 case emthp::TokenType::T_MINUS: positive = !positive; break;
139 case emthp::TokenType::T_NUMBER: return positive ? token.value : -token.value;
140 case emthp::TokenType::T_VARIABLE: this->_lexer.back(); return positive ? 1 : -1;
141 case emthp::TokenType::T_EOF: return 0;
142 }
143 }
144}
Class for representing and operating monomials.
Definition: emath.h:187
void set_degree(int dgr) noexcept
Setter function for setting the degree of the Monomial.
Definition: emath.cpp:236
void set_coeff(double cf) noexcept
Setter function for setting the coefficient of the Monomial.
Definition: emath.cpp:231
Class for representing and operating polynomials.
Definition: emath.h:392
bool push_monomial(const Monomial &m) noexcept
Function for appending a lvalue monomial to the Polynomial.
Definition: emath.cpp:484
Class that works as a token supplier for the Parser class.
Definition: eparser.h:70
void back() noexcept
Backtrack function that moves the cursor's position one position backwards.
Definition: eparser.cpp:52
Lexer & operator=(const Lexer &ogn) noexcept
Assigment operator for when called with an lvalue.
Definition: eparser.cpp:20
Token next() noexcept
Function that returns the next Token in the input provided in the constructor.
Definition: eparser.cpp:29
Lexer(const std::string &input) noexcept
Standalone constructor for the Lexer class.
Definition: eparser.cpp:14
Class that wraps a std::string and parses it as a Monomial or as a Polynomial.
Definition: eparser.h:125
Parser(const std::string &input) noexcept
Standalone constructor for the Parser class.
Definition: eparser.cpp:94
emth::Monomial parse_monomial() noexcept
Function that parses the next Monomial in the input.
Definition: eparser.cpp:113
emth::Polynomial parse_polynomial() noexcept
Function that parses the input as a Polynomial.
Definition: eparser.cpp:105
Parser & operator=(const Parser &ogn) noexcept
Assigment operator for when called with an lvalue.
Definition: eparser.cpp:100
This is the main header file for the equation parser.
@ T_EOF
EOF type.
Definition: eparser.h:48
@ T_MINUS
Minus sign type.
Definition: eparser.h:32
@ T_VARIABLE
X type.
Definition: eparser.h:40
@ T_EXPONENT
Exponent sign type.
Definition: eparser.h:44
@ T_PLUS
Plus sign type.
Definition: eparser.h:28
@ T_NUMBER
Number type.
Definition: eparser.h:36
Struct for representing a token as a type and a value.
Definition: eparser.h:54
double value
The token's numerical value as a double.
Definition: eparser.h:63
TokenType type
The token's type as a TokenType.
Definition: eparser.h:59