diff options
Diffstat (limited to 'src/ast.rs')
-rw-r--r-- | src/ast.rs | 130 |
1 files changed, 112 insertions, 18 deletions
@@ -59,6 +59,16 @@ pub enum Pattern { | |||
59 | }, | 59 | }, |
60 | } | 60 | } |
61 | 61 | ||
62 | impl std::fmt::Display for Pattern { | ||
63 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
64 | match self { | ||
65 | Self::Begin => write!(f, "BEGIN"), | ||
66 | Self::End=> write!(f, "END"), | ||
67 | Self::Tree { modifier, matcher } => write!(f, "{modifier} {matcher}"), | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | |||
62 | impl Pattern { | 72 | impl Pattern { |
63 | pub fn matches(&self, node: tree_sitter::Node) -> bool { | 73 | pub fn matches(&self, node: tree_sitter::Node) -> bool { |
64 | match self { | 74 | match self { |
@@ -75,12 +85,39 @@ pub enum Modifier { | |||
75 | Leave, | 85 | Leave, |
76 | } | 86 | } |
77 | 87 | ||
88 | impl std::fmt::Display for Modifier { | ||
89 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
90 | match self { | ||
91 | Modifier::Enter => write!(f, "enter"), | ||
92 | Modifier::Leave => write!(f, "leave"), | ||
93 | } | ||
94 | } | ||
95 | } | ||
96 | |||
78 | #[derive(Debug, Eq, PartialEq, Clone)] | 97 | #[derive(Debug, Eq, PartialEq, Clone)] |
79 | pub enum TreePattern { | 98 | pub enum TreePattern { |
80 | Atom(String), | 99 | Atom(String), |
81 | List(Vec<TreePattern>), | 100 | List(Vec<TreePattern>), |
82 | } | 101 | } |
83 | 102 | ||
103 | impl std::fmt::Display for TreePattern { | ||
104 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
105 | match self { | ||
106 | Self::Atom(a) => write!(f, "{a}"), | ||
107 | Self::List(l) => { | ||
108 | write!( | ||
109 | f, | ||
110 | "({})", | ||
111 | l.iter() | ||
112 | .map(|i| i.to_string()) | ||
113 | .collect::<Vec<_>>() | ||
114 | .join(" ") | ||
115 | ) | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | |||
84 | impl TreePattern { | 121 | impl TreePattern { |
85 | pub fn matches(&self, node: tree_sitter::Node) -> bool { | 122 | pub fn matches(&self, node: tree_sitter::Node) -> bool { |
86 | match self { | 123 | match self { |
@@ -112,17 +149,6 @@ pub enum Statement { | |||
112 | Declaration(Declaration), | 149 | Declaration(Declaration), |
113 | } | 150 | } |
114 | 151 | ||
115 | impl Statement { | ||
116 | #[cfg(test)] | ||
117 | pub fn decl(ty: Type, ident: &str, init: Expr) -> Self { | ||
118 | Self::Declaration(Declaration { | ||
119 | ty, | ||
120 | name: ident.to_owned(), | ||
121 | init: Some(init.boxed()), | ||
122 | }) | ||
123 | } | ||
124 | } | ||
125 | |||
126 | #[derive(Debug, Eq, PartialEq, Clone)] | 152 | #[derive(Debug, Eq, PartialEq, Clone)] |
127 | pub enum Expr { | 153 | pub enum Expr { |
128 | Node, | 154 | Node, |
@@ -162,13 +188,6 @@ impl Expr { | |||
162 | parameters: params.to_vec(), | 188 | parameters: params.to_vec(), |
163 | }) | 189 | }) |
164 | } | 190 | } |
165 | |||
166 | #[cfg(test)] | ||
167 | pub fn list<const N: usize>(items: [Expr; N]) -> Expr { | ||
168 | Self::List(List { | ||
169 | items: items.to_vec(), | ||
170 | }) | ||
171 | } | ||
172 | } | 191 | } |
173 | 192 | ||
174 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] | 193 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
@@ -176,6 +195,14 @@ pub enum UnaryOp { | |||
176 | Not, | 195 | Not, |
177 | } | 196 | } |
178 | 197 | ||
198 | impl std::fmt::Display for UnaryOp { | ||
199 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
200 | match self { | ||
201 | Self::Not => write!(f, "!"), | ||
202 | } | ||
203 | } | ||
204 | } | ||
205 | |||
179 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] | 206 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
180 | pub enum BinOp { | 207 | pub enum BinOp { |
181 | Arith(ArithOp), | 208 | Arith(ArithOp), |
@@ -223,6 +250,17 @@ impl std::str::FromStr for BinOp { | |||
223 | } | 250 | } |
224 | } | 251 | } |
225 | 252 | ||
253 | impl std::fmt::Display for BinOp { | ||
254 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
255 | match self { | ||
256 | Self::Cmp(op) => write!(f, "{op}"), | ||
257 | Self::Arith(op) => write!(f, "{op}"), | ||
258 | Self::Logic(op) => write!(f, "{op}"), | ||
259 | Self::Assign(op) => write!(f, "{op}"), | ||
260 | } | ||
261 | } | ||
262 | } | ||
263 | |||
226 | // + - * / | 264 | // + - * / |
227 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] | 265 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
228 | pub enum ArithOp { | 266 | pub enum ArithOp { |
@@ -233,6 +271,18 @@ pub enum ArithOp { | |||
233 | Mod, | 271 | Mod, |
234 | } | 272 | } |
235 | 273 | ||
274 | impl std::fmt::Display for ArithOp { | ||
275 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
276 | match self { | ||
277 | Self::Add => write!(f, "+"), | ||
278 | Self::Sub => write!(f, "-"), | ||
279 | Self::Mul => write!(f, "*"), | ||
280 | Self::Div => write!(f, "/"), | ||
281 | Self::Mod => write!(f, "%"), | ||
282 | } | ||
283 | } | ||
284 | } | ||
285 | |||
236 | // && || | 286 | // && || |
237 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] | 287 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
238 | pub enum LogicOp { | 288 | pub enum LogicOp { |
@@ -240,6 +290,15 @@ pub enum LogicOp { | |||
240 | Or, | 290 | Or, |
241 | } | 291 | } |
242 | 292 | ||
293 | impl std::fmt::Display for LogicOp { | ||
294 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
295 | match self { | ||
296 | Self::And => write!(f, "&&"), | ||
297 | Self::Or => write!(f, "||"), | ||
298 | } | ||
299 | } | ||
300 | } | ||
301 | |||
243 | // == != > < >= <= | 302 | // == != > < >= <= |
244 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] | 303 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
245 | pub enum CmpOp { | 304 | pub enum CmpOp { |
@@ -251,12 +310,34 @@ pub enum CmpOp { | |||
251 | Lte, | 310 | Lte, |
252 | } | 311 | } |
253 | 312 | ||
313 | impl std::fmt::Display for CmpOp { | ||
314 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
315 | match self { | ||
316 | Self::Eq => write!(f, "=="), | ||
317 | Self::Neq => write!(f, "!="), | ||
318 | Self::Gt => write!(f, ">"), | ||
319 | Self::Lt => write!(f, "<"), | ||
320 | Self::Gte => write!(f, ">="), | ||
321 | Self::Lte => write!(f, "<="), | ||
322 | } | ||
323 | } | ||
324 | } | ||
325 | |||
254 | // =, +=, -=, *=, /= | 326 | // =, +=, -=, *=, /= |
255 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] | 327 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
256 | pub struct AssignOp { | 328 | pub struct AssignOp { |
257 | pub op: Option<ArithOp>, | 329 | pub op: Option<ArithOp>, |
258 | } | 330 | } |
259 | 331 | ||
332 | impl std::fmt::Display for AssignOp { | ||
333 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
334 | match self.op { | ||
335 | None => write!(f, "="), | ||
336 | Some(op) => write!(f, "{op}="), | ||
337 | } | ||
338 | } | ||
339 | } | ||
340 | |||
260 | pub type Identifier = String; | 341 | pub type Identifier = String; |
261 | 342 | ||
262 | #[derive(Debug, Eq, PartialEq, Clone)] | 343 | #[derive(Debug, Eq, PartialEq, Clone)] |
@@ -301,6 +382,19 @@ pub enum Type { | |||
301 | List, | 382 | List, |
302 | } | 383 | } |
303 | 384 | ||
385 | impl std::fmt::Display for Type { | ||
386 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
387 | match self { | ||
388 | Self::Unit => write!(f, "unit"), | ||
389 | Self::Integer => write!(f, "int"), | ||
390 | Self::String => write!(f, "string"), | ||
391 | Self::Boolean => write!(f, "bool"), | ||
392 | Self::Node => write!(f, "node"), | ||
393 | Self::List => write!(f, "list"), | ||
394 | } | ||
395 | } | ||
396 | } | ||
397 | |||
304 | #[derive(Debug, PartialEq, Eq, Clone)] | 398 | #[derive(Debug, PartialEq, Eq, Clone)] |
305 | pub struct Declaration { | 399 | pub struct Declaration { |
306 | pub ty: Type, | 400 | pub ty: Type, |