1use std::collections::HashSet;
7
8#[derive(Debug, Clone, PartialEq)]
10pub enum BinOp {
11 Add, Subtract, Multiply, Divide, Modulo, Equal, NotEqual, Less, LessEqual, Greater, GreaterEqual, And, Or, BitAnd, BitOr, BitXor, Shl, Shr, }
37
38#[derive(Debug, Clone, PartialEq)]
40pub enum UnaryOp {
41 Minus, Not, }
44
45#[derive(Debug, Clone, PartialEq)]
47pub enum Expr {
48 Number(f64),
50 BigInteger(num_bigint::BigInt), String(String),
52 Boolean(bool),
53 Null,
54
55 Identifier(String),
57
58 Binary {
60 left: Box<Expr>,
61 op: BinOp,
62 right: Box<Expr>,
63 },
64
65 Unary {
67 op: UnaryOp,
68 expr: Box<Expr>,
69 },
70
71 Call {
73 func: Box<Expr>,
74 args: Vec<Expr>,
75 },
76
77 Array(Vec<Expr>),
79
80 Dict(Vec<(String, Expr)>),
82
83 Index {
85 object: Box<Expr>,
86 index: Box<Expr>,
87 },
88
89 If {
91 condition: Box<Expr>,
92 then_branch: Vec<Located>,
93 elif_branches: Vec<(Expr, Vec<Located>)>, else_branch: Option<Vec<Located>>,
95 },
96
97 Lambda {
99 params: Vec<String>,
100 body: Vec<Located>,
101 },
102}
103
104#[derive(Debug, Clone, PartialEq)]
106pub enum Stmt {
107 Set {
109 name: String,
110 value: Expr,
111 },
112
113 SetIndex {
115 object: Box<Expr>,
116 index: Box<Expr>,
117 value: Expr,
118 },
119
120 FuncDef {
122 name: String,
123 params: Vec<String>,
124 body: Vec<Located>,
125 },
126
127 GeneratorDef {
129 name: String,
130 params: Vec<String>,
131 body: Vec<Located>,
132 },
133
134 LazyDef {
136 name: String,
137 expr: Expr,
138 },
139
140 Return(Expr),
142
143 Yield(Expr),
145
146 Break,
148
149 Continue,
151
152 While {
154 condition: Expr,
155 body: Vec<Located>,
156 },
157
158 For {
160 var: String,
161 iterable: Expr,
162 body: Vec<Located>,
163 },
164
165 ForIndexed {
167 index_var: String,
168 value_var: String,
169 iterable: Expr,
170 body: Vec<Located>,
171 },
172
173 Switch {
175 expr: Expr,
176 cases: Vec<(Expr, Vec<Located>)>,
177 default: Option<Vec<Located>>,
178 },
179
180 Import {
185 names: Vec<String>,
186 path: String,
187 aliases: Vec<Option<String>>, namespace: Option<String>, },
190
191 Export(String),
193
194 Throw(Expr),
196
197 Expression(Expr),
199}
200
201#[derive(Debug, Clone)]
203pub struct Located {
204 pub line: usize,
205 pub stmt: Stmt,
206}
207
208impl PartialEq for Located {
211 fn eq(&self, other: &Self) -> bool {
212 self.stmt == other.stmt
213 }
214}
215impl Eq for Located {}
216
217pub fn located(line: usize, stmt: Stmt) -> Located {
219 Located { line, stmt }
220}
221
222pub type Program = Vec<Located>;
224
225pub fn collect_breakable_lines(program: &Program) -> HashSet<usize> {
227 let mut lines = HashSet::new();
228 for l in program {
229 lines.insert(l.line);
230 collect_stmt_lines(&l.stmt, &mut lines);
231 }
232 lines
233}
234
235fn collect_stmt_lines(stmt: &Stmt, lines: &mut HashSet<usize>) {
236 let body_of = |body: &Vec<Located>, lines: &mut HashSet<usize>| {
237 for l in body {
238 lines.insert(l.line);
239 collect_stmt_lines(&l.stmt, lines);
240 }
241 };
242 match stmt {
243 Stmt::FuncDef { body, .. } | Stmt::GeneratorDef { body, .. } => body_of(body, lines),
244 Stmt::While { body, .. } | Stmt::For { body, .. } | Stmt::ForIndexed { body, .. } => {
245 body_of(body, lines)
246 }
247 Stmt::Switch { cases, default, .. } => {
248 for (_, body) in cases {
249 body_of(body, lines);
250 }
251 if let Some(body) = default {
252 body_of(body, lines);
253 }
254 }
255 Stmt::Expression(Expr::If {
256 then_branch,
257 elif_branches,
258 else_branch,
259 ..
260 }) => {
261 body_of(then_branch, lines);
262 for (_, body) in elif_branches {
263 body_of(body, lines);
264 }
265 if let Some(body) = else_branch {
266 body_of(body, lines);
267 }
268 }
269 _ => {}
270 }
271}
272
273impl Expr {
274 pub fn binary(left: Expr, op: BinOp, right: Expr) -> Self {
276 Expr::Binary {
277 left: Box::new(left),
278 op,
279 right: Box::new(right),
280 }
281 }
282
283 pub fn unary(op: UnaryOp, expr: Expr) -> Self {
285 Expr::Unary {
286 op,
287 expr: Box::new(expr),
288 }
289 }
290
291 pub fn call(func: Expr, args: Vec<Expr>) -> Self {
293 Expr::Call {
294 func: Box::new(func),
295 args,
296 }
297 }
298
299 pub fn index(object: Expr, index: Expr) -> Self {
301 Expr::Index {
302 object: Box::new(object),
303 index: Box::new(index),
304 }
305 }
306}
307
308impl std::fmt::Display for BinOp {
309 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
310 match self {
311 BinOp::Add => write!(f, "+"),
312 BinOp::Subtract => write!(f, "-"),
313 BinOp::Multiply => write!(f, "*"),
314 BinOp::Divide => write!(f, "/"),
315 BinOp::Modulo => write!(f, "%"),
316 BinOp::BitAnd => write!(f, "&"),
317 BinOp::BitOr => write!(f, "|"),
318 BinOp::BitXor => write!(f, "^"),
319 BinOp::Shl => write!(f, "<<"),
320 BinOp::Shr => write!(f, ">>"),
321 BinOp::Equal => write!(f, "=="),
322 BinOp::NotEqual => write!(f, "!="),
323 BinOp::Less => write!(f, "<"),
324 BinOp::LessEqual => write!(f, "<="),
325 BinOp::Greater => write!(f, ">"),
326 BinOp::GreaterEqual => write!(f, ">="),
327 BinOp::And => write!(f, "&&"),
328 BinOp::Or => write!(f, "||"),
329 }
330 }
331}
332
333impl std::fmt::Display for UnaryOp {
334 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
335 match self {
336 UnaryOp::Minus => write!(f, "-"),
337 UnaryOp::Not => write!(f, "!"),
338 }
339 }
340}