-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.rs
205 lines (192 loc) · 5.97 KB
/
token.rs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#[cfg(test)]
use serde::{Deserialize, Serialize};
use std::{
fmt,
hash::{Hash, Hasher},
};
use crate::expr::Expr;
#[cfg_attr(test, derive(Serialize, Deserialize))]
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TokenType {
// Single character tokens
LeftParen,
RightParen,
LeftBrace,
RightBrace,
Comma,
Dot,
Minus,
Plus,
Semicolon,
Colon,
Slash,
Star,
LeftBracket,
RightBracket,
// One or two character tokens
Bang,
BangEqual,
Equal,
EqualEqual,
Greater,
GreaterEqual,
Less,
LessEqual,
// Literals
Identifier,
String,
Integer,
Float,
// Keywords
And,
Class,
Else,
False,
Fun,
For,
If,
Nil,
Or,
Print,
Return,
Super,
This,
True,
Var,
While,
IntType,
FloatType,
StrType,
BoolType,
#[default]
Eof,
}
#[cfg_attr(test, derive(Serialize, Deserialize))]
#[derive(Default, Debug, Clone, PartialEq)]
pub struct Token {
pub r#type: TokenType,
pub lexeme: String,
pub literal: Option<Object>,
pub line: usize,
}
impl Hash for Token {
fn hash<H: Hasher>(&self, state: &mut H) {
self.lexeme.hash(state);
self.line.hash(state);
}
}
impl Eq for Token {}
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let val = match (&self.r#type, &self.literal) {
(TokenType::LeftParen, _) => "(".to_string(),
(TokenType::RightParen, _) => ")".to_string(),
(TokenType::LeftBrace, _) => "{".to_string(),
(TokenType::RightBrace, _) => "}".to_string(),
(TokenType::Comma, _) => ",".to_string(),
(TokenType::Dot, _) => ".".to_string(),
(TokenType::Minus, _) => "-".to_string(),
(TokenType::Plus, _) => "+".to_string(),
(TokenType::Semicolon, _) => ";".to_string(),
(TokenType::Slash, _) => "/".to_string(),
(TokenType::Star, _) => "*".to_string(),
(TokenType::Bang, _) => "!".to_string(),
(TokenType::BangEqual, _) => "!=".to_string(),
(TokenType::Equal, _) => "=".to_string(),
(TokenType::EqualEqual, _) => "==".to_string(),
(TokenType::Greater, _) => ">".to_string(),
(TokenType::GreaterEqual, _) => ">=".to_string(),
(TokenType::Less, _) => "<".to_string(),
(TokenType::LessEqual, _) => "<=".to_string(),
(TokenType::Identifier, Some(val))
| (TokenType::String, Some(val))
| (TokenType::Float, Some(val))
| (TokenType::Integer, Some(val)) => val.to_string(),
(TokenType::And, _) => "and".to_string(),
(TokenType::Class, _) => "class".to_string(),
(TokenType::Else, _) => "else".to_string(),
(TokenType::False, _) => "false".to_string(),
(TokenType::Fun, _) => "fun".to_string(),
(TokenType::For, _) => "for".to_string(),
(TokenType::If, _) => "if".to_string(),
(TokenType::Nil, _) => "nil".to_string(),
(TokenType::Or, _) => "or".to_string(),
(TokenType::Print, _) => "print".to_string(),
(TokenType::Return, _) => "return".to_string(),
(TokenType::Super, _) => "super".to_string(),
(TokenType::This, _) => "this".to_string(),
(TokenType::True, _) => "true".to_string(),
(TokenType::Var, _) => "var".to_string(),
(TokenType::While, _) => "while".to_string(),
(TokenType::Eof, _) => "eof".to_string(),
(TokenType::Identifier, None)
| (TokenType::String, None)
| (TokenType::Float, None)
| (TokenType::Integer, None) => panic!("Invalid token"),
(TokenType::StrType, _) => "str".to_string(),
(TokenType::BoolType, _) => "bool".to_string(),
(TokenType::IntType, _) => "i64".to_string(),
(TokenType::FloatType, _) => "f64".to_string(),
(TokenType::Colon, _) => ":".to_string(),
(TokenType::LeftBracket, _) => "[".to_string(),
(TokenType::RightBracket, _) => "]".to_string(),
};
f.write_str(&val)
}
}
#[cfg_attr(test, derive(Serialize, Deserialize))]
#[non_exhaustive]
#[derive(Default, Debug, Clone)]
pub enum Object {
String(String),
Integer(i64),
Float(f64),
Identifier(String),
Bool(bool),
Array(Vec<Expr>),
#[default]
Nil,
}
#[cfg_attr(test, derive(Serialize, Deserialize))]
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ObjType {
String,
Integer,
Array,
Float,
Bool,
Nil,
}
impl PartialEq for Object {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Object::Nil, Object::Nil) => true,
(_, Object::Nil) | (Object::Nil, _) => false,
(Object::Bool(left), Object::Bool(right)) => left == right,
(Object::Float(left), Object::Float(right)) => left == right,
(Object::Integer(left), Object::Integer(right)) => left == right,
(Object::String(left), Object::String(right)) => left == right,
(Object::Array(left), Object::Array(right)) => left == right,
_ => false,
}
}
}
impl Object {
pub fn is_truthy(&self) -> bool {
!matches!(self, Object::Bool(false) | Object::Nil)
}
}
impl fmt::Display for Object {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Object::String(str) => f.write_str(str),
Object::Integer(num) => f.write_str(&num.to_string()),
Object::Float(num) => f.write_str(&num.to_string()),
Object::Identifier(ident) => f.write_str(ident),
Object::Bool(b) => f.write_str(&b.to_string()),
Object::Nil => f.write_str("nil"),
Object::Array(_) => f.write_str("[Array]"),
}
}
}