-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokens.cpp
38 lines (30 loc) · 960 Bytes
/
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
#include <iostream>
#include <string>
#include "tokens.h"
namespace Tokens {
Token::Token(TokenType type, double value) {
this->type = type;
this->value = value;
}
Token::operator std::string const() {
std::string s;
switch (type) {
case NUMBER: s += "NUMBER"; break;
case PLUS: s += "PLUS"; break;
case MINUS: s += "MINUS"; break;
case MULTIPLY: s += "MULTIPLY"; break;
case DIVIDE: s += "DIVIDE"; break;
case LPAREN: s += "LPAREN"; break;
case RPAREN: s += "RPAREN"; break;
case POWER: s += "POWER"; break;
case EOF_: s += "EOF"; break;
}
if (type == NUMBER)
s += ":" + std::to_string(value);
return s;
}
void print_token(Token& token, bool newline) {
std::cout << static_cast<std::string>(token);
if (newline) std::cout << '\n';
}
};