Repository navigation
Expand file tree
/
Copy pathToken.cpp
More file actions
74 lines (68 loc) · 2.33 KB
/
Copy pathToken.cpp
File metadata and controls
74 lines (68 loc) · 2.33 KB
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
#include "Token.h"
Token::Token(const TokenType type, const std::string &lexeme, const ObjPtr& literal, const unsigned int line, const unsigned int column): type(type), lexeme(lexeme), literal(literal), line(line), column(column) {}
Token::~Token() {}
std::string Token::toString() const {
return std::visit(
[]<typename T>(T&& arg) -> std::string {
using U = std::decay_t<T>;
if constexpr (std::is_same_v<U, std::monostate>) {
return "nil";
} else if constexpr (std::is_same_v<U, Number>) {
return arg.asString();
} else if constexpr (std::is_same_v<U, std::string>) {
return arg;
} else if constexpr (std::is_same_v<U, std::shared_ptr<LoxCallable>>) {
return "<Function>";
} else { // std::is_same_v<T, bool>
return arg ? "true" : "false";
}
},
*literal
);
}
std::ostream& operator<<(std::ostream& os, const Token &t) {
os << "type: " << t.type << " lexeme: " << t.lexeme << " internal value: " << t.toString() << " pos: " << t.line << ":" << t.column;
return os;
}
std::ostream &operator<<(std::ostream &os, const Object &obj) {
os << std::visit(
[]<typename T>(T&& arg) -> std::string {
using U = std::decay_t<T>;
if constexpr (std::is_same_v<U, std::monostate>) {
return "nil";
} else if constexpr (std::is_same_v<U, Number>) {
return arg.asString();
}
else if constexpr (std::is_same_v<U, std::string>) {
return arg;
} else if constexpr (std::is_same_v<U, std::shared_ptr<LoxCallable>>) {
return arg->asString();
} else { // std::is_same_v<T, bool>
return arg ? "true" : "false";
}
},
obj
);
return os;
}
std::ostream &operator<<(std::ostream &os, const ObjPtr &obj) {
os << std::visit(
[]<typename T>(T&& arg) -> std::string {
using U = std::decay_t<T>;
if constexpr (std::is_same_v<U, std::monostate>) {
return "nil";
} else if constexpr (std::is_same_v<U, Number>) {
return arg.asString();
}
else if constexpr (std::is_same_v<U, std::string>) {
return arg;
} else if constexpr (std::is_same_v<U, std::shared_ptr<LoxCallable>>) {
return arg->asString();
} else { // std::is_same_v<T, bool>
return arg ? "true" : "false";
}
},
*obj
);
return os;
}