summaryrefslogtreecommitdiff
path: root/src/include/tokenizer.hpp
blob: 7d0005554feb24f581d363e19090279befa277ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once
#include <regex>
#include <string>
#define TOKENS_ARRAY_LEN 16

enum Type {
  operand,
  sum,
  substraction,
  multiplication,
  division,
  left_parens,
  right_parens,
  equals,
  nil
};

struct Token {
  Type type;
  int value;
};

class Tokenizer {
private:
  // Private attributes
  Token *tokens[17];
  const std::string *to_tokenize;
  size_t current_char_index;
  short tokens_head;
  // Regexes
  std::regex operand_regex = std::regex("^0{1}|^\\d+");
  std::regex operation_regex = std::regex("^\\+|^-|/|^\\*");
  std::regex parens_regex = std::regex("^\\(|^\\)");
  // End of private attributes
  // Private methods
  void clearTokens();
  void insertToken(Type type, int value);
  void matchOperation(const std::string &operation);
  void matchParens(const std::string &operation);
  void matchOperand(const std::string &operation);
  std::string buildNumber(char *current);
  // End of private methods 
public:
  Tokenizer();
  ~Tokenizer();
  Token **tokenize(const std::string &operation);
  Token **getTokens();
};