Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "algebra.hpp"
- dero::algebra::token::token(const std::string & syntagm)
- {
- switch (syntagm[0])
- {
- case('{'):
- value = syntagm.substr(1,syntagm.size()-2);
- type = dero::algebra::token::tokentype::var;
- break;
- case('#'):
- value = syntagm.substr(1,syntagm.size()-1);
- type = dero::algebra::token::tokentype::function;
- break;
- case('+'):
- type = dero::algebra::token::tokentype::sum;
- break;
- case('-'):
- type = dero::algebra::token::tokentype::minus;
- break;
- case('*'):
- type = dero::algebra::token::tokentype::mul;
- break;
- case('/'):
- type = dero::algebra::token::tokentype::div;
- break;
- case('^'):
- type = dero::algebra::token::tokentype::pow;
- break;
- case('('):
- type = dero::algebra::token::tokentype::openparen;
- break;
- case(')'):
- type = dero::algebra::token::tokentype::closedparen;
- break;
- case(','):
- type = dero::algebra::token::tokentype::comma;
- break;
- default:
- value = syntagm;
- type = dero::algebra::token::tokentype::var;
- break;
- }
- }
- dero::algebra::token::token(const double & number)
- {
- value = number;
- type = dero::algebra::token::tokentype::number;
- }
- auto dero::algebra::tokenization(const std::string & source)
- -> std::list<dero::algebra::token>
- {
- auto tokenvec = std::list<dero::algebra::token>();
- for (int i = 0 ; i < source.size() ; i++)
- {
- if (source[i] == '.' || isdigit(source[i]))
- {
- auto number = std::string();
- while (i < source.size() && ( source[i] == '.' || isdigit(source[i])))
- {
- number.push_back(source[i]);
- i++;
- }
- tokenvec.emplace_back(dero::algebra::token(std::stod(number)));
- i--; continue;
- }
- if (source[i] == '#')
- {
- auto function = std::string();
- do
- {
- function.push_back(source[i]);
- i++;
- }
- while (i < source.size() && ( source[i] != '#' && source[i] != '(' && source[i] != ' '));
- tokenvec.emplace_back(dero::algebra::token(function));
- i--; continue;
- }
- if (source[i] == '{')
- {
- auto var = std::string();
- do
- {
- var.push_back(source[i]);
- i++;
- }
- while (i < source.size() && ( source[i] != '}'));
- var += "}";
- tokenvec.emplace_back(dero::algebra::token(var));
- continue;
- }
- if (source[i] != ' ')
- {
- auto tmp = std::string(); tmp.push_back(source[i]);
- tokenvec.push_back(dero::algebra::token(tmp));
- }
- }
- return tokenvec;
- }
- // from debug.hpp [ optional file ]
- // you can safeley remove lines between 117 and 179
- #include <iostream>
- void token_dump(const dero::algebra::token & t)
- {
- switch (t.type)
- {
- case (dero::algebra::token::tokentype::number):
- std::cout << "[number: " << std::get<double>(t.value) << "]";
- break;
- case (dero::algebra::token::tokentype::function):
- std::cout << "[function: " << std::get<std::string>(t.value) << "]";
- break;
- case (dero::algebra::token::tokentype::var):
- std::cout << "[var: " << std::get<std::string>(t.value) << "]";
- break;
- case (dero::algebra::token::tokentype::openparen):
- std::cout << "(";
- break;
- case (dero::algebra::token::tokentype::closedparen):
- std::cout << ")";
- break;
- case (dero::algebra::token::tokentype::sum):
- std::cout << "+";
- break;
- case (dero::algebra::token::tokentype::minus):
- std::cout << "-";
- break;
- case (dero::algebra::token::tokentype::mul):
- std::cout << "*";
- break;
- case (dero::algebra::token::tokentype::div):
- std::cout << "/";
- break;
- case (dero::algebra::token::tokentype::pow):
- std::cout << "^";
- break;
- case (dero::algebra::token::tokentype::comma):
- std::cout << ",";
- break;
- }
- }
- void tokenlist_dump(const std::list<dero::algebra::token> & tokenvec)
- {
- for ( auto t : tokenvec )
- {
- token_dump(t);
- std::cout << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement