Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1. #include <iostream>
  2. #include "algebra.hpp"
  3.  
  4. dero::algebra::token::token(const std::string & syntagm)
  5. {
  6.     switch (syntagm[0])
  7.     {
  8.         case('{'):
  9.         value = syntagm.substr(1,syntagm.size()-2);
  10.         type = dero::algebra::token::tokentype::var;
  11.         break;
  12.  
  13.         case('#'):
  14.         value = syntagm.substr(1,syntagm.size()-1);
  15.         type = dero::algebra::token::tokentype::function;
  16.         break;
  17.  
  18.         case('+'):
  19.         type = dero::algebra::token::tokentype::sum;
  20.         break;
  21.  
  22.         case('-'):
  23.         type = dero::algebra::token::tokentype::minus;
  24.         break;
  25.  
  26.         case('*'):
  27.         type = dero::algebra::token::tokentype::mul;
  28.         break;
  29.  
  30.         case('/'):
  31.         type = dero::algebra::token::tokentype::div;
  32.         break;
  33.  
  34.         case('^'):
  35.         type = dero::algebra::token::tokentype::pow;
  36.         break;
  37.  
  38.         case('('):
  39.         type = dero::algebra::token::tokentype::openparen;
  40.         break;
  41.  
  42.         case(')'):
  43.         type = dero::algebra::token::tokentype::closedparen;
  44.         break;
  45.  
  46.         case(','):
  47.         type = dero::algebra::token::tokentype::comma;
  48.         break;
  49.  
  50.         default:
  51.         value = syntagm;
  52.         type = dero::algebra::token::tokentype::var;
  53.         break;
  54.     }    
  55. }
  56.  
  57. dero::algebra::token::token(const double & number)
  58. {
  59.     value = number;
  60.     type = dero::algebra::token::tokentype::number;
  61. }
  62.  
  63. auto dero::algebra::tokenization(const std::string & source)
  64. -> std::list<dero::algebra::token>
  65. {
  66.     auto tokenvec = std::list<dero::algebra::token>();
  67.     for (int i = 0 ; i < source.size() ; i++)
  68.     {
  69.         if (source[i] == '.' || isdigit(source[i]))
  70.         {
  71.             auto number = std::string();
  72.             while (i < source.size() && ( source[i] == '.' || isdigit(source[i])))
  73.             {
  74.                 number.push_back(source[i]);
  75.                 i++;
  76.             }
  77.             tokenvec.emplace_back(dero::algebra::token(std::stod(number)));
  78.             i--; continue;
  79.         }
  80.  
  81.         if (source[i] == '#')
  82.         {
  83.             auto function = std::string();
  84.             do
  85.             {
  86.                 function.push_back(source[i]);
  87.                 i++;
  88.             }
  89.             while (i < source.size() && ( source[i] != '#' && source[i] != '(' && source[i] != ' '));
  90.             tokenvec.emplace_back(dero::algebra::token(function));
  91.             i--; continue;
  92.         }
  93.  
  94.         if (source[i] == '{')
  95.         {
  96.             auto var = std::string();
  97.             do
  98.             {
  99.                 var.push_back(source[i]);
  100.                 i++;
  101.             }
  102.             while (i < source.size() && ( source[i] != '}'));
  103.             var += "}";
  104.             tokenvec.emplace_back(dero::algebra::token(var));
  105.             continue;
  106.         }
  107.  
  108.         if (source[i] != ' ')
  109.         {  
  110.             auto tmp = std::string(); tmp.push_back(source[i]);
  111.             tokenvec.push_back(dero::algebra::token(tmp));
  112.         }
  113.     }
  114.     return tokenvec;
  115. }
  116.  
  117. // from debug.hpp [ optional file ]
  118. // you can safeley remove lines between 117 and 179
  119.  
  120. #include <iostream>
  121.  
  122. void token_dump(const dero::algebra::token & t)
  123. {
  124.     switch (t.type)
  125.     {
  126.         case (dero::algebra::token::tokentype::number):
  127.         std::cout << "[number: " << std::get<double>(t.value) << "]";
  128.         break;
  129.  
  130.         case (dero::algebra::token::tokentype::function):
  131.         std::cout << "[function: " << std::get<std::string>(t.value) << "]";
  132.         break;
  133.        
  134.         case (dero::algebra::token::tokentype::var):
  135.         std::cout << "[var: " << std::get<std::string>(t.value) << "]";
  136.         break;
  137.  
  138.         case (dero::algebra::token::tokentype::openparen):
  139.         std::cout << "(";
  140.         break;
  141.  
  142.         case (dero::algebra::token::tokentype::closedparen):
  143.         std::cout << ")";
  144.         break;
  145.  
  146.         case (dero::algebra::token::tokentype::sum):
  147.         std::cout << "+";
  148.         break;
  149.  
  150.         case (dero::algebra::token::tokentype::minus):
  151.         std::cout << "-";
  152.         break;
  153.  
  154.         case (dero::algebra::token::tokentype::mul):
  155.         std::cout << "*";
  156.         break;
  157.  
  158.         case (dero::algebra::token::tokentype::div):
  159.         std::cout << "/";
  160.         break;
  161.  
  162.         case (dero::algebra::token::tokentype::pow):
  163.         std::cout << "^";
  164.         break;
  165.  
  166.         case (dero::algebra::token::tokentype::comma):
  167.         std::cout << ",";
  168.         break;
  169.     }
  170. }
  171.  
  172. void tokenlist_dump(const std::list<dero::algebra::token> & tokenvec)
  173. {
  174.     for ( auto t : tokenvec )
  175.     {
  176.         token_dump(t);
  177.         std::cout << " ";
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement