Advertisement
NLinker

std::variant matching and string representation

May 13th, 2019
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <variant>
  3. #include <vector>
  4.  
  5. enum Brace { Round, Square };
  6. enum Arity { Unary, Binary };
  7. enum Assoc { Left, Right };
  8. struct Op {
  9.     char symbol;
  10.     Arity ary;
  11.     Assoc assoc;
  12.     int prec;
  13. };
  14.  
  15. struct Number { float n; };
  16. struct RealVar { std::string v; };
  17. struct BoolVar { std::string v; };
  18. struct Operator { Op op; };
  19. struct LeftParen { Brace b; };
  20. struct RightParen { Brace b; };
  21. struct Function { std::string f; };
  22.  
  23. using Token = std::variant<Number, RealVar, BoolVar, Operator, LeftParen, RightParen, Function>;
  24.  
  25. bool is_number(const Token& token) { return token.index() == 0; }
  26. bool is_real_var(const Token& token) { return token.index() == 1; }
  27. bool is_bool_var(const Token& token) { return token.index() == 2; }
  28. bool is_operator(const Token& token) { return token.index() == 3; }
  29. bool is_left_paren(const Token& token) { return token.index() == 4; }
  30. bool is_right_paren(const Token& token) { return token.index() == 5; }
  31. bool is_function(const Token& token) { return token.index() == 6; }
  32.  
  33. std::ostream& operator<< (std::ostream &os, const Op &op) {
  34.     return os << "Op(" << op.symbol << "," << op.ary << "," << op.assoc << "," << op.prec << ")";
  35. }
  36.  
  37. std::ostream& operator<< (std::ostream &os, const Token &token) {
  38.     if (is_number(token)) return os << "Number(" << std::get<Number>(token).n << ")";
  39.     else if (is_real_var(token)) return os << "RealVar(" << std::get<RealVar>(token).v << ")";
  40.     else if (is_bool_var(token)) return os << "BoolVar(" << std::get<BoolVar>(token).v << ")";
  41.     else if (is_operator(token)) return os << "Operator(" << std::get<Operator>(token).op << ")";
  42.     else if (is_left_paren(token)) return os << "LeftParen(" << std::get<LeftParen>(token).b << ")";
  43.     else if (is_right_paren(token)) return os << "RightParen(" << std::get<RightParen>(token).b << ")";
  44.     else if (is_function(token)) return os << "Function(" << std::get<Function>(token).f << ")";
  45.     else return os;
  46. }
  47.  
  48. int main() {
  49.     std::vector<Token> tokens = {
  50.         Number { 0.123456 },
  51.         RealVar { "Rsomething" },
  52.         BoolVar { "Bflag" },
  53.         Operator { Op { '+', Binary, Left, 5 } },
  54.         LeftParen { Square },
  55.         RightParen { Square },
  56.         Function { "exp" }
  57.     };
  58.     // print all
  59.     std::cout << "my tokens: \n";
  60.     for (const auto& token : tokens) {
  61.         std::cout << token << "\n";
  62.     }
  63.  
  64.     std::cout << "\nHello, World!\n";
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement