summaryrefslogtreecommitdiff
path: root/src/tokenizer.cpp
blob: 783803e65401f44477a9040389eb38fb9f44db72 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <string>
#include "include/tokenizer.hpp"
#include "exceptions/tokenizer_exception.hpp"

Tokenizer::Tokenizer() {
  tokens_head = 0;
  for(int i = 0; i < TOKENS_ARRAY_LEN; ++i)
    tokens[i] = nullptr;
}

Tokenizer::~Tokenizer() {
  clearTokens();
}

void Tokenizer::clearTokens() {
  tokens_head = 0;
  
  for(int i = 0; i < TOKENS_ARRAY_LEN; ++i) {
    if(tokens[i] != nullptr) {
      delete tokens[i];
      tokens[i] = nullptr;
    }
  }
}

void Tokenizer::insertToken(Type type, int value = 0) {
  Token *new_token = new Token { .type = type, .value = value};

  tokens[tokens_head] = new_token;
  tokens_head += 1;
}


Token **Tokenizer::tokenize(const std::string &operation) {
  clearTokens();
  to_tokenize = &operation;
  current_char_index = 0;
  
  while(current_char_index < operation.length()) {
    size_t unchanged_index = current_char_index;
    
    matchOperand(operation);
    matchParens(operation);
    matchOperation(operation);

    // No matches.
    if(unchanged_index == current_char_index)
      throw TokenizerException("Invalid character detected.");
  }

  // "Nil" terminated array. Intended to be the epsilon terminal in the grammar.
  tokens[tokens_head] = new Token { .type = nil, .value = 0 };
  return getTokens();
}

void Tokenizer::matchOperand(const std::string &operation) {
  std::smatch match;

  if(current_char_index > operation.length())
    return;

  std::string remaining(operation.substr(current_char_index)); 
  bool result = std::regex_search(remaining, match,
				  operand_regex);

  if(!result)
    return;

  insertToken(operand, std::stoi(match.str(0)));
  current_char_index += match.length(0);
}

void Tokenizer::matchParens(const std::string &operation) {
  std::smatch match;

  if(current_char_index > operation.length())
    return;

  std::string remaining(operation.substr(current_char_index)); 
  bool result = std::regex_search(remaining, match,
				  parens_regex);

  if(!result)
    return;

  switch(match.str(0)[0]) {
  case '(':
    insertToken(left_parens);
    break;
  case ')':
    insertToken(right_parens);
    break;
  }

  current_char_index += 1;
}

void Tokenizer::matchOperation(const std::string &operation) {
  std::smatch match;

  if(current_char_index > operation.length())
    return;

  std::string remaining(operation.substr(current_char_index)); 
  bool result = std::regex_search(remaining, match,
				  operation_regex);

  if(!result)
    return;

  switch(match.str(0)[0]) {
  case '+':
    insertToken(sum);
    break;
  case '-':
    insertToken(substraction);
    break;
  case '*':
    insertToken(multiplication);
    break;
  case '/':
    insertToken(division);
    break;
  }

  current_char_index += 1;
}

Token **Tokenizer::getTokens() {
  return tokens;
}