forked from ColorsWind/algorimatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToken.cpp
115 lines (97 loc) · 2.9 KB
/
Token.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
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
//
// Created by colors_wind on 2020/8/24.
//
#include "Token.h"
#include <sstream>
using std::to_string;
string Token::toString() const {
switch(m_type) {
case OPERATOR:
return string("Operator{'") + asChar() + "'}";
case DELIMITER:
return string("Delimiter{'") + asChar() + "'}";
case NUMBER:
return string("Number{") + to_string(asNumber()) + "}";
case VARIABLE:
return string("Variable{\"") + asString() + "\"}";
case FUNCTION:
return string("Function{") + getRawText() + "}";
case END:
return "END{}";
default:
return "Unknown";
}
}
Token::Token(TokenType type, void *p, const string &origin): m_type(type), m_value(p), m_origin(origin) {}
Token::Token(TokenType type, Func p, const string &origin): m_type(type), m_origin(origin) {
m_value = p;
}
Token::~Token() {
if (m_value != NULL) {
switch(m_type) {
case OPERATOR:
case DELIMITER:
delete (char*)m_value;
break;
case NUMBER:
delete (double *)m_value;
break;
case VARIABLE:
delete (string*)m_value;
break;
}
m_value = nullptr;
}
}
Token &Token::operator=(const Token &token) {
this -> m_type = token.m_type;
this -> m_value = token.copyValue();
return *this;
}
Token::Token(const Token & token): m_type(token.m_type), m_value(token.copyValue()) {}
string Token::asString() const {
return *(string*)m_value;
}
char Token::asChar() const {
return *(char*)m_value;
}
double Token::asNumber() const {
return *(double*)m_value;
}
Func Token::asFunction() const {
return (Func)m_value;
}
Token::Token(TokenType type, double d, const string &origin) : m_type(type), m_value(new double(d)), m_origin(origin){}
Token::Token(TokenType type, char c, const string &origin) : m_type(type), m_value(new char(c)), m_origin(origin){}
Token::Token(TokenType type, string str, const string &origin) : m_type(type), m_value(new string(str)), m_origin(origin){}
Token::Token(TokenType type, const string &origin) : m_type(type), m_value(NULL), m_origin(origin) {}
bool Token::isEquls(char c) const {
switch (m_type) {
case OPERATOR:
case DELIMITER:
if (asChar() == c) return true;
}
return false;
}
TokenType Token::getType() const {
return m_type;
}
void *Token::copyValue() const {
switch(m_type) {
case OPERATOR:
case DELIMITER:
return new char(asChar());
case NUMBER:
return new double(asNumber());
case VARIABLE:
return new string(asString());
case FUNCTION:
return m_value;
case END:
default:
return nullptr;
}
}
const string &Token::getRawText() const {
return m_origin;
}