-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokens.cpp
76 lines (62 loc) · 1.48 KB
/
tokens.cpp
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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include "tokens.h"
namespace Math_Interpreter {
std::string Token::str() const {
std::string result;
switch (type) {
case TokenType::NUMBER:
result += "NUMBER:" + std::to_string(value);
break;
case TokenType::PLUS:
result += "PLUS";
break;
case TokenType::MINUS:
result += "MINUS";
break;
case TokenType::MULTIPLY:
result += "MULTIPLY";
break;
case TokenType::DIVIDE:
result += "DIVIDE";
break;
case TokenType::LPAREN:
result += "LPAREN";
break;
case TokenType::RPAREN:
result += "RPAREN";
break;
case TokenType::EOF_:
result += "EOF";
break;
}
return result;
}
Token::operator bool() const {
if (type != TokenType::EOF_) {
return true;
} else {
return false;
}
}
Token::operator std::string() const {
return str();
}
void print_tokens(const std::vector<Token>& tokens) {
std::ostringstream oss;
bool empty = true;
oss << "[";
for (const Token& token : tokens) {
if (empty) {
empty = false;
oss << token.str();
} else {
oss << ", " << token.str();
}
}
oss << "]";
std::cout << oss.str() << '\n';
}
} // namespace Math_Interpreter