This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.h
100 lines (77 loc) · 1.89 KB
/
token.h
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
#ifndef TOKEN_H
#define TOKEN_H
#include <memory>
#include <optional>
#include <ostream>
#include <string>
enum class TokenType {
LEFT_PAREN,
RIGHT_PAREN,
QUOTE,
QUASIQUOTE,
UNQUOTE,
DOT,
BOOLEAN_LITERAL,
NUMERIC_LITERAL,
STRING_LITERAL,
IDENTIFIER,
};
class Token;
using TokenPtr = std::unique_ptr<Token>;
class Token {
private:
TokenType type;
protected:
Token(TokenType type) : type{type} {}
public:
virtual ~Token() = default;
static TokenPtr fromChar(char c);
static TokenPtr dot();
TokenType getType() const {
return type;
}
virtual std::string toString() const;
};
class BooleanLiteralToken : public Token {
private:
bool value;
public:
BooleanLiteralToken(bool value) : Token(TokenType::BOOLEAN_LITERAL), value{value} {}
static std::unique_ptr<BooleanLiteralToken> fromChar(char c);
bool getValue() const {
return value;
}
std::string toString() const override;
};
class NumericLiteralToken : public Token {
private:
double value;
public:
NumericLiteralToken(double value) : Token(TokenType::NUMERIC_LITERAL), value{value} {}
double getValue() const {
return value;
}
std::string toString() const override;
};
class StringLiteralToken : public Token {
private:
std::string value;
public:
StringLiteralToken(const std::string& value) : Token(TokenType::STRING_LITERAL), value{value} {}
const std::string& getValue() const {
return value;
}
std::string toString() const override;
};
class IdentifierToken : public Token {
private:
std::string name;
public:
IdentifierToken(const std::string& name) : Token(TokenType::IDENTIFIER), name{name} {}
const std::string& getName() const {
return name;
}
std::string toString() const override;
};
std::ostream& operator<<(std::ostream& os, const Token& token);
#endif