Fork of daniellemaywood.uk/gleam — Wasm codegen work
1

Configure Feed

Select the types of activity you want to include in your feed.

Introduce visitor pattern for flatten + remove unused pass

author
Danielle Maywood
committer
nandi
date (Jul 26, 2026, 12:11 PM -0700) commit daa1750c parent 3cdc483f change-id mrwwnyor
+983 -682
+35 -35
compiler-core/src/cranelift.rs
··· 8 8 9 9 pub mod mir; 10 10 11 - pub fn compile(m: mir::Module) { 11 + pub fn compile(m: mir::ast::Module) { 12 12 let mut flag_builder = settings::builder(); 13 13 14 14 flag_builder ··· 113 113 self.builder.ins().iconst(types::I64, value) 114 114 } 115 115 116 - fn translate_expression(&mut self, expression: mir::Expression) -> Value { 116 + fn translate_expression(&mut self, expression: mir::ast::Expression) -> Value { 117 117 _ = self.module; 118 118 119 119 match expression { 120 - mir::Expression::Block(expressions) => expressions 120 + mir::ast::Expression::Block(expressions) => expressions 121 121 .into_iter() 122 122 .map(|expression| self.translate_expression(expression)) 123 123 .last() 124 124 .expect("block shouldn't be empty"), 125 - mir::Expression::FunctionRef { 125 + mir::ast::Expression::FunctionRef { 126 126 module: _, 127 127 name, 128 128 arity, ··· 143 143 144 144 self.builder.ins().func_addr(types::I64, func) 145 145 } 146 - mir::Expression::Var(var) => { 146 + mir::ast::Expression::Var(var) => { 147 147 let var = self.get_var(&var.name); 148 148 149 149 self.builder.use_var(var) 150 150 } 151 - mir::Expression::Int { value } => self.make_int(value), 152 - mir::Expression::Float { value: _ } => todo!(), 153 - mir::Expression::Bool { value: _ } => todo!(), 154 - mir::Expression::String { value: _ } => todo!(), 155 - mir::Expression::Equals { lhs, rhs } => { 151 + mir::ast::Expression::Int { value } => self.make_int(value), 152 + mir::ast::Expression::Float { value: _ } => todo!(), 153 + mir::ast::Expression::Bool { value: _ } => todo!(), 154 + mir::ast::Expression::String { value: _ } => todo!(), 155 + mir::ast::Expression::Equals { lhs, rhs } => { 156 156 let lhs = self.translate_expression(*lhs); 157 157 let rhs = self.translate_expression(*rhs); 158 158 159 159 self.builder.ins().icmp(IntCC::Equal, lhs, rhs) 160 160 } 161 - mir::Expression::NotEquals { lhs: _, rhs: _ } => todo!(), 162 - mir::Expression::IntGt { lhs: _, rhs: _ } => todo!(), 163 - mir::Expression::IntGtEq { lhs: _, rhs: _ } => todo!(), 164 - mir::Expression::IntLt { lhs: _, rhs: _ } => todo!(), 165 - mir::Expression::IntLtEq { lhs: _, rhs: _ } => todo!(), 166 - mir::Expression::IntAdd { lhs, rhs } => { 161 + mir::ast::Expression::NotEquals { lhs: _, rhs: _ } => todo!(), 162 + mir::ast::Expression::IntGt { lhs: _, rhs: _ } => todo!(), 163 + mir::ast::Expression::IntGtEq { lhs: _, rhs: _ } => todo!(), 164 + mir::ast::Expression::IntLt { lhs: _, rhs: _ } => todo!(), 165 + mir::ast::Expression::IntLtEq { lhs: _, rhs: _ } => todo!(), 166 + mir::ast::Expression::IntAdd { lhs, rhs } => { 167 167 let lhs = self.translate_expression(*lhs); 168 168 let rhs = self.translate_expression(*rhs); 169 169 170 170 self.builder.ins().iadd(lhs, rhs) 171 171 } 172 - mir::Expression::IntSub { lhs, rhs } => { 172 + mir::ast::Expression::IntSub { lhs, rhs } => { 173 173 let lhs = self.translate_expression(*lhs); 174 174 let rhs = self.translate_expression(*rhs); 175 175 176 176 self.builder.ins().isub(lhs, rhs) 177 177 } 178 - mir::Expression::IntMul { lhs: _, rhs: _ } => todo!(), 179 - mir::Expression::IntDiv { lhs: _, rhs: _ } => todo!(), 180 - mir::Expression::IntRem { lhs: _, rhs: _ } => todo!(), 181 - mir::Expression::FloatGt { lhs: _, rhs: _ } => todo!(), 182 - mir::Expression::FloatGtEq { lhs: _, rhs: _ } => todo!(), 183 - mir::Expression::FloatLt { lhs: _, rhs: _ } => todo!(), 184 - mir::Expression::FloatLtEq { lhs: _, rhs: _ } => todo!(), 185 - mir::Expression::FloatAdd { lhs: _, rhs: _ } => todo!(), 186 - mir::Expression::FloatSub { lhs: _, rhs: _ } => todo!(), 187 - mir::Expression::FloatMul { lhs: _, rhs: _ } => todo!(), 188 - mir::Expression::FloatDiv { lhs: _, rhs: _ } => todo!(), 189 - mir::Expression::StringConcat { lhs: _, rhs: _ } => todo!(), 190 - mir::Expression::List { items: _, tail: _ } => todo!(), 191 - mir::Expression::Struct { tag: _, items: _ } => todo!(), 192 - mir::Expression::StructAccess { value: _, index: _ } => todo!(), 193 - mir::Expression::Set { name, value } => { 178 + mir::ast::Expression::IntMul { lhs: _, rhs: _ } => todo!(), 179 + mir::ast::Expression::IntDiv { lhs: _, rhs: _ } => todo!(), 180 + mir::ast::Expression::IntRem { lhs: _, rhs: _ } => todo!(), 181 + mir::ast::Expression::FloatGt { lhs: _, rhs: _ } => todo!(), 182 + mir::ast::Expression::FloatGtEq { lhs: _, rhs: _ } => todo!(), 183 + mir::ast::Expression::FloatLt { lhs: _, rhs: _ } => todo!(), 184 + mir::ast::Expression::FloatLtEq { lhs: _, rhs: _ } => todo!(), 185 + mir::ast::Expression::FloatAdd { lhs: _, rhs: _ } => todo!(), 186 + mir::ast::Expression::FloatSub { lhs: _, rhs: _ } => todo!(), 187 + mir::ast::Expression::FloatMul { lhs: _, rhs: _ } => todo!(), 188 + mir::ast::Expression::FloatDiv { lhs: _, rhs: _ } => todo!(), 189 + mir::ast::Expression::StringConcat { lhs: _, rhs: _ } => todo!(), 190 + mir::ast::Expression::List { items: _, tail: _ } => todo!(), 191 + mir::ast::Expression::Struct { tag: _, items: _ } => todo!(), 192 + mir::ast::Expression::StructAccess { value: _, index: _ } => todo!(), 193 + mir::ast::Expression::Set { name, value } => { 194 194 let var = self.make_var(name.name); 195 195 let val = self.translate_expression(*value); 196 196 197 197 self.builder.def_var(var, val); 198 198 self.builder.use_var(var) 199 199 } 200 - mir::Expression::If { cond, then, else_ } => { 200 + mir::ast::Expression::If { cond, then, else_ } => { 201 201 let cond = self.translate_expression(*cond); 202 202 203 203 let then_block = self.builder.create_block(); ··· 234 234 235 235 self.builder.block_params(merge_block)[0] 236 236 } 237 - mir::Expression::Call { target, args } => { 237 + mir::ast::Expression::Call { target, args } => { 238 238 let mut signature = self.module.make_signature(); 239 239 240 240 for _ in 0..args.len() {
+159 -647
compiler-core/src/cranelift/mir.rs
··· 4 4 use num_bigint::BigInt; 5 5 use vec1::Vec1; 6 6 7 - use crate::{ast, exhaustiveness, parse, type_}; 7 + use crate::{ 8 + cranelift::mir::{ast::*, visit::Visit}, 9 + exhaustiveness, parse, type_, 10 + }; 8 11 9 - #[derive(Debug, PartialEq, Eq, Default)] 10 - pub struct Module { 11 - pub functions: Vec<Function>, 12 - } 12 + pub mod ast; 13 + pub mod visit; 13 14 14 - #[derive(Debug, PartialEq, Eq)] 15 - pub struct Function { 16 - pub name: EcoString, 17 - pub return_type: Type, 18 - pub parameters: Vec<FunctionParameter>, 19 - pub body: Expression, 20 - } 15 + struct FlattenBlockPass; 21 16 22 - #[derive(Debug, PartialEq, Eq)] 23 - pub struct FunctionParameter { 24 - pub type_: Type, 25 - pub name: Option<EcoString>, 26 - } 17 + impl<'mir> Visit<'mir> for FlattenBlockPass { 18 + fn visit_expression(&mut self, expression: &'mir mut Expression) { 19 + visit::visit_expression(self, expression); 27 20 28 - #[derive(Debug, Default, PartialEq, Eq)] 29 - pub struct Translator { 30 - module: Module, 31 - } 21 + if let Expression::Block(inner) = expression 22 + && inner.len() == 1 23 + { 24 + *expression = inner.pop().expect("block should've been poppable"); 25 + } 26 + } 32 27 33 - #[derive(Debug, Clone, PartialEq, Eq)] 34 - pub enum Type { 35 - Int, 36 - Float, 37 - Bool, 38 - String, 39 - Struct { elements: Vec<Type> }, 40 - List(Box<Type>), 41 - Generic, 42 - } 28 + fn visit_expression_block(&mut self, expressions: &'mir mut Vec<Expression>) { 29 + visit::visit_expression_block(self, expressions); 43 30 44 - #[derive(Debug, PartialEq, Eq, Clone)] 45 - pub struct Var { 46 - pub name: EcoString, 31 + *expressions = std::mem::take(expressions) 32 + .into_iter() 33 + .flat_map(|expr| match expr { 34 + Expression::Block(inner) => inner, 35 + _ => vec![expr], 36 + }) 37 + .collect(); 38 + } 47 39 } 48 40 49 - #[derive(Debug, PartialEq, Eq, Clone)] 50 - pub enum Expression { 51 - Block(Vec<Expression>), 52 - 53 - FunctionRef { 54 - module: EcoString, 55 - name: EcoString, 56 - arity: usize, 57 - }, 58 - 59 - Var(Var), 60 - 61 - Int { 62 - value: BigInt, 63 - }, 64 - 65 - Float { 66 - value: EcoString, 67 - }, 68 - 69 - Bool { 70 - value: bool, 71 - }, 72 - 73 - String { 74 - value: EcoString, 75 - }, 76 - 77 - Equals { 78 - lhs: Box<Expression>, 79 - rhs: Box<Expression>, 80 - }, 81 - 82 - NotEquals { 83 - lhs: Box<Expression>, 84 - rhs: Box<Expression>, 85 - }, 86 - 87 - IntGt { 88 - lhs: Box<Expression>, 89 - rhs: Box<Expression>, 90 - }, 41 + struct RemoveUnusedPass; 91 42 92 - IntGtEq { 93 - lhs: Box<Expression>, 94 - rhs: Box<Expression>, 95 - }, 43 + impl<'mir> Visit<'mir> for RemoveUnusedPass { 44 + fn visit_expression_block(&mut self, expressions: &'mir mut Vec<Expression>) { 45 + visit::visit_expression_block(self, expressions); 96 46 97 - IntLt { 98 - lhs: Box<Expression>, 99 - rhs: Box<Expression>, 100 - }, 47 + let last_index = expressions.len() - 1; 101 48 102 - IntLtEq { 103 - lhs: Box<Expression>, 104 - rhs: Box<Expression>, 105 - }, 49 + *expressions = std::mem::take(expressions) 50 + .into_iter() 51 + .enumerate() 52 + .filter_map(|(index, expr)| match expr { 53 + Expression::Var { .. } 54 + | Expression::FunctionRef { .. } 55 + | Expression::Int { .. } 56 + | Expression::Float { .. } 57 + | Expression::Bool { .. } 58 + | Expression::String { .. } 59 + if index != last_index => 60 + { 61 + None 62 + } 106 63 107 - IntAdd { 108 - lhs: Box<Expression>, 109 - rhs: Box<Expression>, 110 - }, 64 + _ => Some(expr), 65 + }) 66 + .collect(); 67 + } 68 + } 111 69 112 - IntSub { 113 - lhs: Box<Expression>, 114 - rhs: Box<Expression>, 115 - }, 116 - 117 - IntMul { 118 - lhs: Box<Expression>, 119 - rhs: Box<Expression>, 120 - }, 121 - 122 - IntDiv { 123 - lhs: Box<Expression>, 124 - rhs: Box<Expression>, 125 - }, 126 - 127 - IntRem { 128 - lhs: Box<Expression>, 129 - rhs: Box<Expression>, 130 - }, 131 - 132 - FloatGt { 133 - lhs: Box<Expression>, 134 - rhs: Box<Expression>, 135 - }, 136 - 137 - FloatGtEq { 138 - lhs: Box<Expression>, 139 - rhs: Box<Expression>, 140 - }, 141 - 142 - FloatLt { 143 - lhs: Box<Expression>, 144 - rhs: Box<Expression>, 145 - }, 146 - 147 - FloatLtEq { 148 - lhs: Box<Expression>, 149 - rhs: Box<Expression>, 150 - }, 151 - 152 - FloatAdd { 153 - lhs: Box<Expression>, 154 - rhs: Box<Expression>, 155 - }, 156 - 157 - FloatSub { 158 - lhs: Box<Expression>, 159 - rhs: Box<Expression>, 160 - }, 161 - 162 - FloatMul { 163 - lhs: Box<Expression>, 164 - rhs: Box<Expression>, 165 - }, 166 - 167 - FloatDiv { 168 - lhs: Box<Expression>, 169 - rhs: Box<Expression>, 170 - }, 171 - 172 - StringConcat { 173 - lhs: Box<Expression>, 174 - rhs: Box<Expression>, 175 - }, 176 - 177 - List { 178 - items: Vec<Expression>, 179 - tail: Option<Box<Expression>>, 180 - }, 181 - 182 - Struct { 183 - tag: Option<u32>, 184 - items: Vec<Expression>, 185 - }, 186 - 187 - StructAccess { 188 - value: Box<Expression>, 189 - index: u64, 190 - }, 191 - 192 - Set { 193 - name: Var, 194 - value: Box<Expression>, 195 - }, 196 - 197 - If { 198 - cond: Box<Expression>, 199 - then: Box<Expression>, 200 - else_: Box<Expression>, 201 - }, 202 - 203 - Call { 204 - target: Box<Expression>, 205 - args: Vec<Expression>, 206 - }, 70 + #[derive(Debug, Default, PartialEq, Eq)] 71 + pub struct Translator { 72 + module: Module, 207 73 } 208 74 209 75 impl Translator { 210 - pub fn translate(mut self, module: &ast::TypedModule) -> Module { 76 + pub fn translate(mut self, module: &crate::ast::TypedModule) -> Module { 211 77 for definition in &module.definitions { 212 78 match definition { 213 - ast::Definition::Function(function) => { 79 + crate::ast::Definition::Function(function) => { 214 80 self.module 215 81 .functions 216 82 .push(Self::translate_function(function)); 217 83 } 218 - ast::Definition::TypeAlias(_type_alias) => todo!(), 219 - ast::Definition::CustomType(_custom_type) => {} 220 - ast::Definition::Import(_import) => todo!(), 221 - ast::Definition::ModuleConstant(_module_constant) => todo!(), 84 + crate::ast::Definition::TypeAlias(_type_alias) => todo!(), 85 + crate::ast::Definition::CustomType(_custom_type) => {} 86 + crate::ast::Definition::Import(_import) => todo!(), 87 + crate::ast::Definition::ModuleConstant(_module_constant) => todo!(), 222 88 } 223 89 } 224 90 225 91 self.module 226 92 } 227 93 228 - fn translate_function(function: &ast::TypedFunction) -> Function { 94 + fn translate_function(function: &crate::ast::TypedFunction) -> Function { 229 95 let (_, name) = function.name.clone().expect("function should have a name"); 230 96 231 97 let return_type = Self::translate_type(&function.return_type); ··· 248 114 }, 249 115 )); 250 116 251 - let body = translator.translate(&function.body); 252 - let body = Self::flatten_expression(body); 253 - let body = Self::remove_unused_code(body); 117 + let mut body = translator.translate(&function.body); 118 + FlattenBlockPass.visit_expression(&mut body); 119 + RemoveUnusedPass.visit_expression(&mut body); 254 120 255 121 Function { 256 122 name, ··· 299 165 type_::Type::Tuple { elements: _ } => todo!(), 300 166 } 301 167 } 302 - 303 - fn flatten_expression(expression: Expression) -> Expression { 304 - match expression { 305 - Expression::Block(expressions) => { 306 - let mut block = vec![]; 307 - 308 - for expression in expressions { 309 - let expression = Self::flatten_expression(expression); 310 - 311 - match expression { 312 - Expression::Block(mut expressions) => block.append(&mut expressions), 313 - Expression::Var { .. } 314 - | Expression::FunctionRef { .. } 315 - | Expression::Int { .. } 316 - | Expression::Float { .. } 317 - | Expression::Bool { .. } 318 - | Expression::String { .. } 319 - | Expression::Equals { .. } 320 - | Expression::NotEquals { .. } 321 - | Expression::IntGt { .. } 322 - | Expression::IntGtEq { .. } 323 - | Expression::IntLt { .. } 324 - | Expression::IntLtEq { .. } 325 - | Expression::IntAdd { .. } 326 - | Expression::IntSub { .. } 327 - | Expression::IntMul { .. } 328 - | Expression::IntDiv { .. } 329 - | Expression::IntRem { .. } 330 - | Expression::FloatGt { .. } 331 - | Expression::FloatGtEq { .. } 332 - | Expression::FloatLt { .. } 333 - | Expression::FloatLtEq { .. } 334 - | Expression::FloatAdd { .. } 335 - | Expression::FloatSub { .. } 336 - | Expression::FloatMul { .. } 337 - | Expression::FloatDiv { .. } 338 - | Expression::StringConcat { .. } 339 - | Expression::List { .. } 340 - | Expression::Struct { .. } 341 - | Expression::StructAccess { .. } 342 - | Expression::Set { .. } 343 - | Expression::If { .. } 344 - | Expression::Call { .. } => block.push(expression), 345 - }; 346 - } 347 - 348 - match block.as_slice() { 349 - [single] => single.clone(), 350 - _ => Expression::Block(block), 351 - } 352 - } 353 - Expression::FunctionRef { 354 - module, 355 - name, 356 - arity, 357 - } => Expression::FunctionRef { 358 - module, 359 - name, 360 - arity, 361 - }, 362 - Expression::Var(var) => Expression::Var(var), 363 - Expression::Int { value } => Expression::Int { value }, 364 - Expression::Float { value } => Expression::Float { value }, 365 - Expression::Bool { value } => Expression::Bool { value }, 366 - Expression::String { value } => Expression::String { value }, 367 - Expression::Equals { lhs, rhs } => Expression::Equals { 368 - lhs: Box::new(Self::flatten_expression(*lhs)), 369 - rhs: Box::new(Self::flatten_expression(*rhs)), 370 - }, 371 - Expression::NotEquals { lhs, rhs } => Expression::NotEquals { 372 - lhs: Box::new(Self::flatten_expression(*lhs)), 373 - rhs: Box::new(Self::flatten_expression(*rhs)), 374 - }, 375 - Expression::IntGt { lhs, rhs } => Expression::IntGt { 376 - lhs: Box::new(Self::flatten_expression(*lhs)), 377 - rhs: Box::new(Self::flatten_expression(*rhs)), 378 - }, 379 - Expression::IntGtEq { lhs, rhs } => Expression::IntGtEq { 380 - lhs: Box::new(Self::flatten_expression(*lhs)), 381 - rhs: Box::new(Self::flatten_expression(*rhs)), 382 - }, 383 - Expression::IntLt { lhs, rhs } => Expression::IntLt { 384 - lhs: Box::new(Self::flatten_expression(*lhs)), 385 - rhs: Box::new(Self::flatten_expression(*rhs)), 386 - }, 387 - Expression::IntLtEq { lhs, rhs } => Expression::IntLtEq { 388 - lhs: Box::new(Self::flatten_expression(*lhs)), 389 - rhs: Box::new(Self::flatten_expression(*rhs)), 390 - }, 391 - Expression::IntAdd { lhs, rhs } => Expression::IntAdd { 392 - lhs: Box::new(Self::flatten_expression(*lhs)), 393 - rhs: Box::new(Self::flatten_expression(*rhs)), 394 - }, 395 - Expression::IntSub { lhs, rhs } => Expression::IntSub { 396 - lhs: Box::new(Self::flatten_expression(*lhs)), 397 - rhs: Box::new(Self::flatten_expression(*rhs)), 398 - }, 399 - Expression::IntMul { lhs, rhs } => Expression::IntMul { 400 - lhs: Box::new(Self::flatten_expression(*lhs)), 401 - rhs: Box::new(Self::flatten_expression(*rhs)), 402 - }, 403 - Expression::IntDiv { lhs, rhs } => Expression::IntDiv { 404 - lhs: Box::new(Self::flatten_expression(*lhs)), 405 - rhs: Box::new(Self::flatten_expression(*rhs)), 406 - }, 407 - Expression::IntRem { lhs, rhs } => Expression::IntRem { 408 - lhs: Box::new(Self::flatten_expression(*lhs)), 409 - rhs: Box::new(Self::flatten_expression(*rhs)), 410 - }, 411 - Expression::FloatGt { lhs, rhs } => Expression::FloatGt { 412 - lhs: Box::new(Self::flatten_expression(*lhs)), 413 - rhs: Box::new(Self::flatten_expression(*rhs)), 414 - }, 415 - Expression::FloatGtEq { lhs, rhs } => Expression::FloatGtEq { 416 - lhs: Box::new(Self::flatten_expression(*lhs)), 417 - rhs: Box::new(Self::flatten_expression(*rhs)), 418 - }, 419 - Expression::FloatLt { lhs, rhs } => Expression::FloatLt { 420 - lhs: Box::new(Self::flatten_expression(*lhs)), 421 - rhs: Box::new(Self::flatten_expression(*rhs)), 422 - }, 423 - Expression::FloatLtEq { lhs, rhs } => Expression::FloatLtEq { 424 - lhs: Box::new(Self::flatten_expression(*lhs)), 425 - rhs: Box::new(Self::flatten_expression(*rhs)), 426 - }, 427 - Expression::FloatAdd { lhs, rhs } => Expression::FloatAdd { 428 - lhs: Box::new(Self::flatten_expression(*lhs)), 429 - rhs: Box::new(Self::flatten_expression(*rhs)), 430 - }, 431 - Expression::FloatSub { lhs, rhs } => Expression::FloatSub { 432 - lhs: Box::new(Self::flatten_expression(*lhs)), 433 - rhs: Box::new(Self::flatten_expression(*rhs)), 434 - }, 435 - Expression::FloatMul { lhs, rhs } => Expression::FloatMul { 436 - lhs: Box::new(Self::flatten_expression(*lhs)), 437 - rhs: Box::new(Self::flatten_expression(*rhs)), 438 - }, 439 - Expression::FloatDiv { lhs, rhs } => Expression::FloatDiv { 440 - lhs: Box::new(Self::flatten_expression(*lhs)), 441 - rhs: Box::new(Self::flatten_expression(*rhs)), 442 - }, 443 - Expression::StringConcat { lhs, rhs } => Expression::StringConcat { 444 - lhs: Box::new(Self::flatten_expression(*lhs)), 445 - rhs: Box::new(Self::flatten_expression(*rhs)), 446 - }, 447 - Expression::List { items, tail } => Expression::List { 448 - items: items.into_iter().map(Self::flatten_expression).collect(), 449 - tail: tail.map(|tail| Box::new(Self::flatten_expression(*tail))), 450 - }, 451 - Expression::Struct { tag, items } => Expression::Struct { 452 - tag, 453 - items: items.into_iter().map(Self::flatten_expression).collect(), 454 - }, 455 - Expression::StructAccess { value, index } => Expression::StructAccess { 456 - value: Box::new(Self::flatten_expression(*value)), 457 - index: index, 458 - }, 459 - Expression::Set { name, value } => Expression::Set { 460 - name, 461 - value: Box::new(Self::flatten_expression(*value)), 462 - }, 463 - Expression::If { cond, then, else_ } => Expression::If { 464 - cond: Box::new(Self::flatten_expression(*cond)), 465 - then: Box::new(Self::flatten_expression(*then)), 466 - else_: Box::new(Self::flatten_expression(*else_)), 467 - }, 468 - Expression::Call { target, args } => Expression::Call { 469 - target: Box::new(Self::flatten_expression(*target)), 470 - args: args.into_iter().map(Self::flatten_expression).collect(), 471 - }, 472 - } 473 - } 474 - 475 - fn remove_unused_code(expression: Expression) -> Expression { 476 - match expression { 477 - Expression::Block(expressions) => { 478 - let mut block = vec![]; 479 - 480 - let last_expression_index = expressions.len() - 1; 481 - 482 - for (index, expression) in expressions.into_iter().enumerate() { 483 - let expression = Self::remove_unused_code(expression); 484 - 485 - match expression { 486 - Expression::Var { .. } 487 - | Expression::FunctionRef { .. } 488 - | Expression::Int { .. } 489 - | Expression::Float { .. } 490 - | Expression::Bool { .. } 491 - | Expression::String { .. } 492 - if index != last_expression_index => {} 493 - 494 - Expression::Block(_) 495 - | Expression::FunctionRef { .. } 496 - | Expression::Var { .. } 497 - | Expression::Int { .. } 498 - | Expression::Float { .. } 499 - | Expression::Bool { .. } 500 - | Expression::String { .. } 501 - | Expression::Equals { .. } 502 - | Expression::NotEquals { .. } 503 - | Expression::IntGt { .. } 504 - | Expression::IntGtEq { .. } 505 - | Expression::IntLt { .. } 506 - | Expression::IntLtEq { .. } 507 - | Expression::IntAdd { .. } 508 - | Expression::IntSub { .. } 509 - | Expression::IntMul { .. } 510 - | Expression::IntDiv { .. } 511 - | Expression::IntRem { .. } 512 - | Expression::FloatGt { .. } 513 - | Expression::FloatGtEq { .. } 514 - | Expression::FloatLt { .. } 515 - | Expression::FloatLtEq { .. } 516 - | Expression::FloatAdd { .. } 517 - | Expression::FloatSub { .. } 518 - | Expression::FloatMul { .. } 519 - | Expression::FloatDiv { .. } 520 - | Expression::StringConcat { .. } 521 - | Expression::List { .. } 522 - | Expression::Struct { .. } 523 - | Expression::StructAccess { .. } 524 - | Expression::Set { .. } 525 - | Expression::If { .. } 526 - | Expression::Call { .. } => block.push(expression), 527 - } 528 - } 529 - 530 - match block.as_slice() { 531 - [single] => single.clone(), 532 - _ => Expression::Block(block), 533 - } 534 - } 535 - Expression::FunctionRef { 536 - module, 537 - name, 538 - arity, 539 - } => Expression::FunctionRef { 540 - module, 541 - name, 542 - arity, 543 - }, 544 - Expression::Var(var) => Expression::Var(var), 545 - Expression::Int { value } => Expression::Int { value }, 546 - Expression::Float { value } => Expression::Float { value }, 547 - Expression::Bool { value } => Expression::Bool { value }, 548 - Expression::String { value } => Expression::String { value }, 549 - Expression::Equals { lhs, rhs } => Expression::Equals { 550 - lhs: Box::new(Self::remove_unused_code(*lhs)), 551 - rhs: Box::new(Self::remove_unused_code(*rhs)), 552 - }, 553 - Expression::NotEquals { lhs, rhs } => Expression::NotEquals { 554 - lhs: Box::new(Self::remove_unused_code(*lhs)), 555 - rhs: Box::new(Self::remove_unused_code(*rhs)), 556 - }, 557 - Expression::IntGt { lhs, rhs } => Expression::IntGt { 558 - lhs: Box::new(Self::remove_unused_code(*lhs)), 559 - rhs: Box::new(Self::remove_unused_code(*rhs)), 560 - }, 561 - Expression::IntGtEq { lhs, rhs } => Expression::IntGtEq { 562 - lhs: Box::new(Self::remove_unused_code(*lhs)), 563 - rhs: Box::new(Self::remove_unused_code(*rhs)), 564 - }, 565 - Expression::IntLt { lhs, rhs } => Expression::IntLt { 566 - lhs: Box::new(Self::remove_unused_code(*lhs)), 567 - rhs: Box::new(Self::remove_unused_code(*rhs)), 568 - }, 569 - Expression::IntLtEq { lhs, rhs } => Expression::IntLtEq { 570 - lhs: Box::new(Self::remove_unused_code(*lhs)), 571 - rhs: Box::new(Self::remove_unused_code(*rhs)), 572 - }, 573 - Expression::IntAdd { lhs, rhs } => Expression::IntAdd { 574 - lhs: Box::new(Self::remove_unused_code(*lhs)), 575 - rhs: Box::new(Self::remove_unused_code(*rhs)), 576 - }, 577 - Expression::IntSub { lhs, rhs } => Expression::IntSub { 578 - lhs: Box::new(Self::remove_unused_code(*lhs)), 579 - rhs: Box::new(Self::remove_unused_code(*rhs)), 580 - }, 581 - Expression::IntMul { lhs, rhs } => Expression::IntMul { 582 - lhs: Box::new(Self::remove_unused_code(*lhs)), 583 - rhs: Box::new(Self::remove_unused_code(*rhs)), 584 - }, 585 - Expression::IntDiv { lhs, rhs } => Expression::IntDiv { 586 - lhs: Box::new(Self::remove_unused_code(*lhs)), 587 - rhs: Box::new(Self::remove_unused_code(*rhs)), 588 - }, 589 - Expression::IntRem { lhs, rhs } => Expression::IntRem { 590 - lhs: Box::new(Self::flatten_expression(*lhs)), 591 - rhs: Box::new(Self::flatten_expression(*rhs)), 592 - }, 593 - Expression::FloatGt { lhs, rhs } => Expression::FloatGt { 594 - lhs: Box::new(Self::remove_unused_code(*lhs)), 595 - rhs: Box::new(Self::remove_unused_code(*rhs)), 596 - }, 597 - Expression::FloatGtEq { lhs, rhs } => Expression::FloatGtEq { 598 - lhs: Box::new(Self::remove_unused_code(*lhs)), 599 - rhs: Box::new(Self::remove_unused_code(*rhs)), 600 - }, 601 - Expression::FloatLt { lhs, rhs } => Expression::FloatLt { 602 - lhs: Box::new(Self::remove_unused_code(*lhs)), 603 - rhs: Box::new(Self::remove_unused_code(*rhs)), 604 - }, 605 - Expression::FloatLtEq { lhs, rhs } => Expression::FloatLtEq { 606 - lhs: Box::new(Self::remove_unused_code(*lhs)), 607 - rhs: Box::new(Self::remove_unused_code(*rhs)), 608 - }, 609 - Expression::FloatAdd { lhs, rhs } => Expression::FloatAdd { 610 - lhs: Box::new(Self::remove_unused_code(*lhs)), 611 - rhs: Box::new(Self::remove_unused_code(*rhs)), 612 - }, 613 - Expression::FloatSub { lhs, rhs } => Expression::FloatSub { 614 - lhs: Box::new(Self::remove_unused_code(*lhs)), 615 - rhs: Box::new(Self::remove_unused_code(*rhs)), 616 - }, 617 - Expression::FloatMul { lhs, rhs } => Expression::FloatMul { 618 - lhs: Box::new(Self::remove_unused_code(*lhs)), 619 - rhs: Box::new(Self::remove_unused_code(*rhs)), 620 - }, 621 - Expression::FloatDiv { lhs, rhs } => Expression::FloatDiv { 622 - lhs: Box::new(Self::remove_unused_code(*lhs)), 623 - rhs: Box::new(Self::remove_unused_code(*rhs)), 624 - }, 625 - Expression::StringConcat { lhs, rhs } => Expression::StringConcat { 626 - lhs: Box::new(Self::remove_unused_code(*lhs)), 627 - rhs: Box::new(Self::remove_unused_code(*rhs)), 628 - }, 629 - Expression::List { items, tail } => Expression::List { 630 - items: items.into_iter().map(Self::remove_unused_code).collect(), 631 - tail: tail.map(|tail| Box::new(Self::remove_unused_code(*tail))), 632 - }, 633 - Expression::Struct { tag, items } => Expression::Struct { 634 - tag, 635 - items: items.into_iter().map(Self::remove_unused_code).collect(), 636 - }, 637 - Expression::StructAccess { value, index } => Expression::StructAccess { 638 - value: Box::new(Self::remove_unused_code(*value)), 639 - index: index, 640 - }, 641 - Expression::Set { name, value } => Expression::Set { 642 - name, 643 - value: Box::new(Self::remove_unused_code(*value)), 644 - }, 645 - Expression::If { cond, then, else_ } => Expression::If { 646 - cond: Box::new(Self::remove_unused_code(*cond)), 647 - then: Box::new(Self::remove_unused_code(*then)), 648 - else_: Box::new(Self::remove_unused_code(*else_)), 649 - }, 650 - Expression::Call { target, args } => Expression::Call { 651 - target: Box::new(Self::remove_unused_code(*target)), 652 - args: args.into_iter().map(Self::remove_unused_code).collect(), 653 - }, 654 - } 655 - } 656 168 } 657 169 658 170 #[derive(Debug, Default)] ··· 698 210 result 699 211 } 700 212 701 - fn translate(mut self, body: &Vec1<ast::TypedStatement>) -> Expression { 213 + fn translate(mut self, body: &Vec1<crate::ast::TypedStatement>) -> Expression { 702 214 self.translate_statements(body) 703 215 } 704 216 705 - fn translate_statements(&mut self, statements: &[ast::TypedStatement]) -> Expression { 217 + fn translate_statements(&mut self, statements: &[crate::ast::TypedStatement]) -> Expression { 706 218 Expression::Block(Iterator::collect( 707 219 statements.iter().map(|stmt| self.translate_statement(stmt)), 708 220 )) 709 221 } 710 222 711 - fn translate_statement(&mut self, statement: &ast::TypedStatement) -> Expression { 223 + fn translate_statement(&mut self, statement: &crate::ast::TypedStatement) -> Expression { 712 224 match statement { 713 - ast::Statement::Expression(expression) => self.translate_expression(expression), 714 - ast::Statement::Assignment(assignment) => self.translate_assignment(assignment), 715 - ast::Statement::Use(_) => todo!(), 716 - ast::Statement::Assert(_) => todo!(), 225 + crate::ast::Statement::Expression(expression) => self.translate_expression(expression), 226 + crate::ast::Statement::Assignment(assignment) => self.translate_assignment(assignment), 227 + crate::ast::Statement::Use(_) => todo!(), 228 + crate::ast::Statement::Assert(_) => todo!(), 717 229 } 718 230 } 719 231 720 - fn translate_expression(&mut self, expression: &ast::TypedExpr) -> Expression { 232 + fn translate_expression(&mut self, expression: &crate::ast::TypedExpr) -> Expression { 721 233 match expression { 722 - ast::TypedExpr::Int { 234 + crate::ast::TypedExpr::Int { 723 235 location: _, 724 236 type_: _, 725 237 value: _, ··· 727 239 } => Expression::Int { 728 240 value: int_value.clone(), 729 241 }, 730 - ast::TypedExpr::Float { 242 + crate::ast::TypedExpr::Float { 731 243 location: _, 732 244 type_: _, 733 245 value, 734 246 } => Expression::Float { 735 247 value: value.clone(), 736 248 }, 737 - ast::TypedExpr::String { 249 + crate::ast::TypedExpr::String { 738 250 location: _, 739 251 type_: _, 740 252 value, 741 253 } => Expression::String { 742 254 value: value.clone(), 743 255 }, 744 - ast::TypedExpr::Block { 256 + crate::ast::TypedExpr::Block { 745 257 location: _, 746 258 statements, 747 259 } => self.in_var_scope(|this| this.translate_statements(statements)), 748 - ast::TypedExpr::Pipeline { 260 + crate::ast::TypedExpr::Pipeline { 749 261 location: _, 750 262 first_value, 751 263 assignments, ··· 772 284 773 285 Expression::Block(block) 774 286 } 775 - ast::TypedExpr::Var { 287 + crate::ast::TypedExpr::Var { 776 288 location: _, 777 289 constructor, 778 290 name, ··· 826 338 }, 827 339 }, 828 340 }, 829 - ast::TypedExpr::Fn { 341 + crate::ast::TypedExpr::Fn { 830 342 location: _, 831 343 type_: _, 832 344 kind: _, ··· 835 347 return_annotation: _, 836 348 purity: _, 837 349 } => todo!(), 838 - ast::TypedExpr::List { 350 + crate::ast::TypedExpr::List { 839 351 location: _, 840 352 type_: _, 841 353 elements, ··· 852 364 853 365 Expression::List { items, tail } 854 366 } 855 - ast::TypedExpr::Call { 367 + crate::ast::TypedExpr::Call { 856 368 location: _, 857 369 type_: _, 858 370 fun, ··· 869 381 args, 870 382 } 871 383 } 872 - ast::TypedExpr::BinOp { 384 + crate::ast::TypedExpr::BinOp { 873 385 location: _, 874 386 type_: _, 875 387 name, ··· 881 393 let rhs = Box::new(self.translate_expression(right)); 882 394 883 395 match name { 884 - ast::BinOp::And => Expression::If { 396 + crate::ast::BinOp::And => Expression::If { 885 397 cond: lhs, 886 398 then: rhs, 887 399 else_: Box::new(Expression::Bool { value: false }), 888 400 }, 889 - ast::BinOp::Or => Expression::If { 401 + crate::ast::BinOp::Or => Expression::If { 890 402 cond: lhs, 891 403 then: Box::new(Expression::Bool { value: true }), 892 404 else_: rhs, 893 405 }, 894 - ast::BinOp::Eq => Expression::Equals { lhs, rhs }, 895 - ast::BinOp::NotEq => Expression::NotEquals { lhs, rhs }, 896 - ast::BinOp::LtInt => Expression::IntLt { lhs, rhs }, 897 - ast::BinOp::LtEqInt => Expression::IntLtEq { lhs, rhs }, 898 - ast::BinOp::LtFloat => Expression::FloatLt { lhs, rhs }, 899 - ast::BinOp::LtEqFloat => Expression::FloatLtEq { lhs, rhs }, 900 - ast::BinOp::GtEqInt => Expression::IntGtEq { lhs, rhs }, 901 - ast::BinOp::GtInt => Expression::IntGt { lhs, rhs }, 902 - ast::BinOp::GtEqFloat => Expression::FloatGtEq { lhs, rhs }, 903 - ast::BinOp::GtFloat => Expression::FloatGt { lhs, rhs }, 904 - ast::BinOp::AddInt => Expression::IntAdd { lhs, rhs }, 905 - ast::BinOp::AddFloat => Expression::FloatAdd { lhs, rhs }, 906 - ast::BinOp::SubInt => Expression::IntSub { lhs, rhs }, 907 - ast::BinOp::SubFloat => Expression::FloatSub { lhs, rhs }, 908 - ast::BinOp::MultInt => Expression::IntMul { lhs, rhs }, 909 - ast::BinOp::MultFloat => Expression::FloatMul { lhs, rhs }, 910 - ast::BinOp::DivInt => Expression::IntDiv { lhs, rhs }, 911 - ast::BinOp::DivFloat => Expression::FloatDiv { lhs, rhs }, 912 - ast::BinOp::RemainderInt => Expression::IntRem { lhs, rhs }, 913 - ast::BinOp::Concatenate => Expression::StringConcat { lhs, rhs }, 406 + crate::ast::BinOp::Eq => Expression::Equals { lhs, rhs }, 407 + crate::ast::BinOp::NotEq => Expression::NotEquals { lhs, rhs }, 408 + crate::ast::BinOp::LtInt => Expression::IntLt { lhs, rhs }, 409 + crate::ast::BinOp::LtEqInt => Expression::IntLtEq { lhs, rhs }, 410 + crate::ast::BinOp::LtFloat => Expression::FloatLt { lhs, rhs }, 411 + crate::ast::BinOp::LtEqFloat => Expression::FloatLtEq { lhs, rhs }, 412 + crate::ast::BinOp::GtEqInt => Expression::IntGtEq { lhs, rhs }, 413 + crate::ast::BinOp::GtInt => Expression::IntGt { lhs, rhs }, 414 + crate::ast::BinOp::GtEqFloat => Expression::FloatGtEq { lhs, rhs }, 415 + crate::ast::BinOp::GtFloat => Expression::FloatGt { lhs, rhs }, 416 + crate::ast::BinOp::AddInt => Expression::IntAdd { lhs, rhs }, 417 + crate::ast::BinOp::AddFloat => Expression::FloatAdd { lhs, rhs }, 418 + crate::ast::BinOp::SubInt => Expression::IntSub { lhs, rhs }, 419 + crate::ast::BinOp::SubFloat => Expression::FloatSub { lhs, rhs }, 420 + crate::ast::BinOp::MultInt => Expression::IntMul { lhs, rhs }, 421 + crate::ast::BinOp::MultFloat => Expression::FloatMul { lhs, rhs }, 422 + crate::ast::BinOp::DivInt => Expression::IntDiv { lhs, rhs }, 423 + crate::ast::BinOp::DivFloat => Expression::FloatDiv { lhs, rhs }, 424 + crate::ast::BinOp::RemainderInt => Expression::IntRem { lhs, rhs }, 425 + crate::ast::BinOp::Concatenate => Expression::StringConcat { lhs, rhs }, 914 426 } 915 427 } 916 - ast::TypedExpr::Case { 428 + crate::ast::TypedExpr::Case { 917 429 location: _, 918 430 type_: _, 919 431 subjects, ··· 943 455 944 456 Expression::Block(block) 945 457 } 946 - ast::TypedExpr::RecordAccess { 458 + crate::ast::TypedExpr::RecordAccess { 947 459 location: _, 948 460 field_start: _, 949 461 type_: _, ··· 954 466 value: Box::new(self.translate_expression(record)), 955 467 index: *index, 956 468 }, 957 - ast::TypedExpr::ModuleSelect { 469 + crate::ast::TypedExpr::ModuleSelect { 958 470 location: _, 959 471 field_start: _, 960 472 type_: _, ··· 963 475 module_alias: _, 964 476 constructor: _, 965 477 } => todo!(), 966 - ast::TypedExpr::Tuple { 478 + crate::ast::TypedExpr::Tuple { 967 479 location: _, 968 480 type_: _, 969 481 elements, ··· 974 486 .map(|element| self.translate_expression(element)) 975 487 .collect(), 976 488 }, 977 - ast::TypedExpr::TupleIndex { 489 + crate::ast::TypedExpr::TupleIndex { 978 490 location: _, 979 491 type_: _, 980 492 index, ··· 983 495 value: Box::new(self.translate_expression(tuple)), 984 496 index: *index, 985 497 }, 986 - ast::TypedExpr::Todo { 498 + crate::ast::TypedExpr::Todo { 987 499 location: _, 988 500 message: _, 989 501 kind: _, 990 502 type_: _, 991 503 } => todo!(), 992 - ast::TypedExpr::Panic { 504 + crate::ast::TypedExpr::Panic { 993 505 location: _, 994 506 message: _, 995 507 type_: _, 996 508 } => todo!(), 997 - ast::TypedExpr::Echo { 509 + crate::ast::TypedExpr::Echo { 998 510 location: _, 999 511 type_: _, 1000 512 expression: _, 1001 513 message: _, 1002 514 } => todo!(), 1003 - ast::TypedExpr::BitArray { 515 + crate::ast::TypedExpr::BitArray { 1004 516 location: _, 1005 517 type_: _, 1006 518 segments: _, 1007 519 } => todo!(), 1008 - ast::TypedExpr::RecordUpdate { 520 + crate::ast::TypedExpr::RecordUpdate { 1009 521 location: _, 1010 522 type_: _, 1011 523 record_assignment: _, 1012 524 constructor: _, 1013 525 arguments: _, 1014 526 } => todo!(), 1015 - ast::TypedExpr::NegateBool { location: _, value } => Expression::Equals { 527 + crate::ast::TypedExpr::NegateBool { location: _, value } => Expression::Equals { 1016 528 lhs: Box::new(self.translate_expression(value)), 1017 529 rhs: Box::new(Expression::Bool { value: false }), 1018 530 }, 1019 - ast::TypedExpr::NegateInt { location: _, value } => Expression::IntSub { 531 + crate::ast::TypedExpr::NegateInt { location: _, value } => Expression::IntSub { 1020 532 lhs: Box::new(Expression::Int { 1021 533 value: BigInt::from(0), 1022 534 }), 1023 535 rhs: Box::new(self.translate_expression(value)), 1024 536 }, 1025 - ast::TypedExpr::Invalid { 537 + crate::ast::TypedExpr::Invalid { 1026 538 location: _, 1027 539 type_: _, 1028 540 } => todo!(), 1029 541 } 1030 542 } 1031 543 1032 - fn translate_assignment(&mut self, assignment: &ast::TypedAssignment) -> Expression { 544 + fn translate_assignment(&mut self, assignment: &crate::ast::TypedAssignment) -> Expression { 1033 545 let mut block = vec![]; 1034 546 1035 547 let value_var = self.make_tmp_var(); ··· 1056 568 &mut self, 1057 569 kind: DecisionKind, 1058 570 subjects: &[Var], 1059 - clauses: &[ast::TypedClause], 571 + clauses: &[crate::ast::TypedClause], 1060 572 decision: &exhaustiveness::Decision, 1061 573 ) -> Expression { 1062 574 match decision { ··· 1148 660 } 1149 661 } 1150 662 1151 - fn translate_guard(&mut self, guard: &ast::TypedClauseGuard) -> Expression { 663 + fn translate_guard(&mut self, guard: &crate::ast::TypedClauseGuard) -> Expression { 1152 664 match guard { 1153 - ast::ClauseGuard::Block { location: _, value } => self.translate_guard(value), 1154 - ast::ClauseGuard::Equals { 665 + crate::ast::ClauseGuard::Block { location: _, value } => self.translate_guard(value), 666 + crate::ast::ClauseGuard::Equals { 1155 667 location: _, 1156 668 left, 1157 669 right, ··· 1164 676 rhs: Box::new(rhs), 1165 677 } 1166 678 } 1167 - ast::ClauseGuard::NotEquals { 679 + crate::ast::ClauseGuard::NotEquals { 1168 680 location: _, 1169 681 left, 1170 682 right, ··· 1172 684 lhs: Box::new(self.translate_guard(left)), 1173 685 rhs: Box::new(self.translate_guard(right)), 1174 686 }, 1175 - ast::ClauseGuard::GtInt { 687 + crate::ast::ClauseGuard::GtInt { 1176 688 location: _, 1177 689 left, 1178 690 right, ··· 1180 692 lhs: Box::new(self.translate_guard(left)), 1181 693 rhs: Box::new(self.translate_guard(right)), 1182 694 }, 1183 - ast::ClauseGuard::GtEqInt { 695 + crate::ast::ClauseGuard::GtEqInt { 1184 696 location: _, 1185 697 left, 1186 698 right, ··· 1188 700 lhs: Box::new(self.translate_guard(left)), 1189 701 rhs: Box::new(self.translate_guard(right)), 1190 702 }, 1191 - ast::ClauseGuard::LtInt { 703 + crate::ast::ClauseGuard::LtInt { 1192 704 location: _, 1193 705 left, 1194 706 right, ··· 1196 708 lhs: Box::new(self.translate_guard(left)), 1197 709 rhs: Box::new(self.translate_guard(right)), 1198 710 }, 1199 - ast::ClauseGuard::LtEqInt { 711 + crate::ast::ClauseGuard::LtEqInt { 1200 712 location: _, 1201 713 left, 1202 714 right, ··· 1204 716 lhs: Box::new(self.translate_guard(left)), 1205 717 rhs: Box::new(self.translate_guard(right)), 1206 718 }, 1207 - ast::ClauseGuard::GtFloat { 719 + crate::ast::ClauseGuard::GtFloat { 1208 720 location: _, 1209 721 left, 1210 722 right, ··· 1212 724 lhs: Box::new(self.translate_guard(left)), 1213 725 rhs: Box::new(self.translate_guard(right)), 1214 726 }, 1215 - ast::ClauseGuard::GtEqFloat { 727 + crate::ast::ClauseGuard::GtEqFloat { 1216 728 location: _, 1217 729 left, 1218 730 right, ··· 1220 732 lhs: Box::new(self.translate_guard(left)), 1221 733 rhs: Box::new(self.translate_guard(right)), 1222 734 }, 1223 - ast::ClauseGuard::LtFloat { 735 + crate::ast::ClauseGuard::LtFloat { 1224 736 location: _, 1225 737 left, 1226 738 right, ··· 1228 740 lhs: Box::new(self.translate_guard(left)), 1229 741 rhs: Box::new(self.translate_guard(right)), 1230 742 }, 1231 - ast::ClauseGuard::LtEqFloat { 743 + crate::ast::ClauseGuard::LtEqFloat { 1232 744 location: _, 1233 745 left, 1234 746 right, ··· 1236 748 lhs: Box::new(self.translate_guard(left)), 1237 749 rhs: Box::new(self.translate_guard(right)), 1238 750 }, 1239 - ast::ClauseGuard::AddInt { 751 + crate::ast::ClauseGuard::AddInt { 1240 752 location: _, 1241 753 left, 1242 754 right, ··· 1244 756 lhs: Box::new(self.translate_guard(left)), 1245 757 rhs: Box::new(self.translate_guard(right)), 1246 758 }, 1247 - ast::ClauseGuard::AddFloat { 759 + crate::ast::ClauseGuard::AddFloat { 1248 760 location: _, 1249 761 left, 1250 762 right, ··· 1252 764 lhs: Box::new(self.translate_guard(left)), 1253 765 rhs: Box::new(self.translate_guard(right)), 1254 766 }, 1255 - ast::ClauseGuard::SubInt { 767 + crate::ast::ClauseGuard::SubInt { 1256 768 location: _, 1257 769 left, 1258 770 right, ··· 1260 772 lhs: Box::new(self.translate_guard(left)), 1261 773 rhs: Box::new(self.translate_guard(right)), 1262 774 }, 1263 - ast::ClauseGuard::SubFloat { 775 + crate::ast::ClauseGuard::SubFloat { 1264 776 location: _, 1265 777 left, 1266 778 right, ··· 1268 780 lhs: Box::new(self.translate_guard(left)), 1269 781 rhs: Box::new(self.translate_guard(right)), 1270 782 }, 1271 - ast::ClauseGuard::MultInt { 783 + crate::ast::ClauseGuard::MultInt { 1272 784 location: _, 1273 785 left, 1274 786 right, ··· 1276 788 lhs: Box::new(self.translate_guard(left)), 1277 789 rhs: Box::new(self.translate_guard(right)), 1278 790 }, 1279 - ast::ClauseGuard::MultFloat { 791 + crate::ast::ClauseGuard::MultFloat { 1280 792 location: _, 1281 793 left, 1282 794 right, ··· 1284 796 lhs: Box::new(self.translate_guard(left)), 1285 797 rhs: Box::new(self.translate_guard(right)), 1286 798 }, 1287 - ast::ClauseGuard::DivInt { 799 + crate::ast::ClauseGuard::DivInt { 1288 800 location: _, 1289 801 left, 1290 802 right, ··· 1292 804 lhs: Box::new(self.translate_guard(left)), 1293 805 rhs: Box::new(self.translate_guard(right)), 1294 806 }, 1295 - ast::ClauseGuard::DivFloat { 807 + crate::ast::ClauseGuard::DivFloat { 1296 808 location: _, 1297 809 left, 1298 810 right, ··· 1300 812 lhs: Box::new(self.translate_guard(left)), 1301 813 rhs: Box::new(self.translate_guard(right)), 1302 814 }, 1303 - ast::ClauseGuard::RemainderInt { 815 + crate::ast::ClauseGuard::RemainderInt { 1304 816 location: _, 1305 817 left, 1306 818 right, ··· 1308 820 lhs: Box::new(self.translate_guard(left)), 1309 821 rhs: Box::new(self.translate_guard(right)), 1310 822 }, 1311 - ast::ClauseGuard::Or { 823 + crate::ast::ClauseGuard::Or { 1312 824 location: _, 1313 825 left, 1314 826 right, ··· 1317 829 then: Box::new(Expression::Bool { value: true }), 1318 830 else_: Box::new(self.translate_guard(right)), 1319 831 }, 1320 - ast::ClauseGuard::And { 832 + crate::ast::ClauseGuard::And { 1321 833 location: _, 1322 834 left, 1323 835 right, ··· 1326 838 then: Box::new(self.translate_guard(right)), 1327 839 else_: Box::new(Expression::Bool { value: false }), 1328 840 }, 1329 - ast::ClauseGuard::Not { 841 + crate::ast::ClauseGuard::Not { 1330 842 location: _, 1331 843 expression, 1332 844 } => Expression::Equals { 1333 845 lhs: Box::new(self.translate_guard(expression)), 1334 846 rhs: Box::new(Expression::Bool { value: false }), 1335 847 }, 1336 - ast::ClauseGuard::Var { 848 + crate::ast::ClauseGuard::Var { 1337 849 location: _, 1338 850 type_: _, 1339 851 name, 1340 852 definition_location: _, 1341 853 } => Expression::Var(self.get_var(name)), 1342 - ast::ClauseGuard::TupleIndex { 854 + crate::ast::ClauseGuard::TupleIndex { 1343 855 location: _, 1344 856 index, 1345 857 type_: _, ··· 1348 860 value: Box::new(self.translate_guard(tuple)), 1349 861 index: *index, 1350 862 }, 1351 - ast::ClauseGuard::FieldAccess { 863 + crate::ast::ClauseGuard::FieldAccess { 1352 864 label_location: _, 1353 865 index, 1354 866 label: _, ··· 1358 870 value: Box::new(self.translate_guard(container)), 1359 871 index: index.expect("field access expected an index"), 1360 872 }, 1361 - ast::ClauseGuard::ModuleSelect { 873 + crate::ast::ClauseGuard::ModuleSelect { 1362 874 location: _, 1363 875 type_: _, 1364 876 label: _, ··· 1366 878 module_alias: _, 1367 879 literal: _, 1368 880 } => todo!(), 1369 - ast::ClauseGuard::Constant(constant) => self.translate_constant(constant), 881 + crate::ast::ClauseGuard::Constant(constant) => self.translate_constant(constant), 1370 882 } 1371 883 } 1372 884 1373 - fn translate_constant(&mut self, constant: &ast::TypedConstant) -> Expression { 885 + fn translate_constant(&mut self, constant: &crate::ast::TypedConstant) -> Expression { 1374 886 match constant { 1375 - ast::Constant::Int { 887 + crate::ast::Constant::Int { 1376 888 location: _, 1377 889 value: _, 1378 890 int_value, 1379 891 } => Expression::Int { 1380 892 value: int_value.clone(), 1381 893 }, 1382 - ast::Constant::Float { location: _, value } => Expression::Float { 894 + crate::ast::Constant::Float { location: _, value } => Expression::Float { 1383 895 value: value.clone(), 1384 896 }, 1385 - ast::Constant::String { location: _, value } => Expression::String { 897 + crate::ast::Constant::String { location: _, value } => Expression::String { 1386 898 value: value.clone(), 1387 899 }, 1388 - ast::Constant::Tuple { 900 + crate::ast::Constant::Tuple { 1389 901 location: _, 1390 902 elements, 1391 903 } => Expression::Struct { ··· 1395 907 .map(|element| self.translate_constant(element)) 1396 908 .collect(), 1397 909 }, 1398 - ast::Constant::List { 910 + crate::ast::Constant::List { 1399 911 location: _, 1400 912 elements, 1401 913 type_: _, ··· 1406 918 .collect(), 1407 919 tail: None, 1408 920 }, 1409 - ast::Constant::Record { 921 + crate::ast::Constant::Record { 1410 922 location: _, 1411 923 module: _, 1412 924 name: _, ··· 1416 928 field_map: _, 1417 929 record_constructor: _, 1418 930 } => todo!(), 1419 - ast::Constant::BitArray { 931 + crate::ast::Constant::BitArray { 1420 932 location: _, 1421 933 segments: _, 1422 934 } => todo!(), 1423 - ast::Constant::Var { 935 + crate::ast::Constant::Var { 1424 936 location: _, 1425 937 module: _, 1426 938 name: _, 1427 939 constructor: _, 1428 940 type_: _, 1429 941 } => todo!(), 1430 - ast::Constant::StringConcatenation { 942 + crate::ast::Constant::StringConcatenation { 1431 943 location: _, 1432 944 left, 1433 945 right, ··· 1435 947 lhs: Box::new(self.translate_constant(left)), 1436 948 rhs: Box::new(self.translate_constant(right)), 1437 949 }, 1438 - ast::Constant::Invalid { 950 + crate::ast::Constant::Invalid { 1439 951 location: _, 1440 952 type_: _, 1441 953 } => todo!(), ··· 1518 1030 use crate::warning::{TypeWarningEmitter, WarningEmitter}; 1519 1031 use camino::Utf8PathBuf; 1520 1032 1521 - fn compile_module(src: &str) -> ast::TypedModule { 1033 + fn compile_module(src: &str) -> crate::ast::TypedModule { 1522 1034 use crate::type_::build_prelude; 1523 1035 let parsed = parse_module(Utf8PathBuf::from("test/path"), src, &WarningEmitter::null()) 1524 1036 .expect("syntax error");
+197
compiler-core/src/cranelift/mir/ast.rs
··· 1 + use ecow::EcoString; 2 + use num_bigint::BigInt; 3 + 4 + #[derive(Debug, PartialEq, Eq, Default)] 5 + pub struct Module { 6 + pub functions: Vec<Function>, 7 + } 8 + 9 + #[derive(Debug, PartialEq, Eq)] 10 + pub struct Function { 11 + pub name: EcoString, 12 + pub return_type: Type, 13 + pub parameters: Vec<FunctionParameter>, 14 + pub body: Expression, 15 + } 16 + 17 + #[derive(Debug, PartialEq, Eq)] 18 + pub struct FunctionParameter { 19 + pub type_: Type, 20 + pub name: Option<EcoString>, 21 + } 22 + 23 + #[derive(Debug, Clone, PartialEq, Eq)] 24 + pub enum Type { 25 + Int, 26 + Float, 27 + Bool, 28 + String, 29 + Struct { elements: Vec<Type> }, 30 + List(Box<Type>), 31 + Generic, 32 + } 33 + 34 + #[derive(Debug, PartialEq, Eq, Clone)] 35 + pub struct Var { 36 + pub name: EcoString, 37 + } 38 + 39 + #[derive(Debug, PartialEq, Eq, Clone)] 40 + pub enum Expression { 41 + Block(Vec<Expression>), 42 + 43 + FunctionRef { 44 + module: EcoString, 45 + name: EcoString, 46 + arity: usize, 47 + }, 48 + 49 + Var(Var), 50 + 51 + Int { 52 + value: BigInt, 53 + }, 54 + 55 + Float { 56 + value: EcoString, 57 + }, 58 + 59 + Bool { 60 + value: bool, 61 + }, 62 + 63 + String { 64 + value: EcoString, 65 + }, 66 + 67 + Equals { 68 + lhs: Box<Expression>, 69 + rhs: Box<Expression>, 70 + }, 71 + 72 + NotEquals { 73 + lhs: Box<Expression>, 74 + rhs: Box<Expression>, 75 + }, 76 + 77 + IntGt { 78 + lhs: Box<Expression>, 79 + rhs: Box<Expression>, 80 + }, 81 + 82 + IntGtEq { 83 + lhs: Box<Expression>, 84 + rhs: Box<Expression>, 85 + }, 86 + 87 + IntLt { 88 + lhs: Box<Expression>, 89 + rhs: Box<Expression>, 90 + }, 91 + 92 + IntLtEq { 93 + lhs: Box<Expression>, 94 + rhs: Box<Expression>, 95 + }, 96 + 97 + IntAdd { 98 + lhs: Box<Expression>, 99 + rhs: Box<Expression>, 100 + }, 101 + 102 + IntSub { 103 + lhs: Box<Expression>, 104 + rhs: Box<Expression>, 105 + }, 106 + 107 + IntMul { 108 + lhs: Box<Expression>, 109 + rhs: Box<Expression>, 110 + }, 111 + 112 + IntDiv { 113 + lhs: Box<Expression>, 114 + rhs: Box<Expression>, 115 + }, 116 + 117 + IntRem { 118 + lhs: Box<Expression>, 119 + rhs: Box<Expression>, 120 + }, 121 + 122 + FloatGt { 123 + lhs: Box<Expression>, 124 + rhs: Box<Expression>, 125 + }, 126 + 127 + FloatGtEq { 128 + lhs: Box<Expression>, 129 + rhs: Box<Expression>, 130 + }, 131 + 132 + FloatLt { 133 + lhs: Box<Expression>, 134 + rhs: Box<Expression>, 135 + }, 136 + 137 + FloatLtEq { 138 + lhs: Box<Expression>, 139 + rhs: Box<Expression>, 140 + }, 141 + 142 + FloatAdd { 143 + lhs: Box<Expression>, 144 + rhs: Box<Expression>, 145 + }, 146 + 147 + FloatSub { 148 + lhs: Box<Expression>, 149 + rhs: Box<Expression>, 150 + }, 151 + 152 + FloatMul { 153 + lhs: Box<Expression>, 154 + rhs: Box<Expression>, 155 + }, 156 + 157 + FloatDiv { 158 + lhs: Box<Expression>, 159 + rhs: Box<Expression>, 160 + }, 161 + 162 + StringConcat { 163 + lhs: Box<Expression>, 164 + rhs: Box<Expression>, 165 + }, 166 + 167 + List { 168 + items: Vec<Expression>, 169 + tail: Option<Box<Expression>>, 170 + }, 171 + 172 + Struct { 173 + tag: Option<u32>, 174 + items: Vec<Expression>, 175 + }, 176 + 177 + StructAccess { 178 + value: Box<Expression>, 179 + index: u64, 180 + }, 181 + 182 + Set { 183 + name: Var, 184 + value: Box<Expression>, 185 + }, 186 + 187 + If { 188 + cond: Box<Expression>, 189 + then: Box<Expression>, 190 + else_: Box<Expression>, 191 + }, 192 + 193 + Call { 194 + target: Box<Expression>, 195 + args: Vec<Expression>, 196 + }, 197 + }
+592
compiler-core/src/cranelift/mir/visit.rs
··· 1 + use ecow::EcoString; 2 + use num_bigint::BigInt; 3 + 4 + use crate::cranelift::mir::ast::{Expression, Function, Module, Var}; 5 + 6 + pub trait Visit<'mir> { 7 + fn visit_module(&mut self, module: &'mir mut Module) { 8 + visit_module(self, module); 9 + } 10 + 11 + fn visit_function(&mut self, function: &'mir mut Function) { 12 + visit_function(self, function); 13 + } 14 + 15 + fn visit_expression(&mut self, expression: &'mir mut Expression) { 16 + visit_expression(self, expression); 17 + } 18 + 19 + fn visit_expression_block(&mut self, expressions: &'mir mut Vec<Expression>) { 20 + visit_expression_block(self, expressions); 21 + } 22 + 23 + fn visit_expression_function_ref( 24 + &mut self, 25 + module: &mut EcoString, 26 + name: &mut EcoString, 27 + arity: &mut usize, 28 + ) { 29 + visit_expression_function_ref(self, module, name, arity); 30 + } 31 + 32 + fn visit_expression_var(&mut self, var: &'mir mut Var) { 33 + visit_expression_var(self, var); 34 + } 35 + 36 + fn visit_expression_int(&mut self, value: &'mir mut BigInt) { 37 + visit_expression_int(self, value); 38 + } 39 + 40 + fn visit_expression_float(&mut self, value: &'mir mut EcoString) { 41 + visit_expression_float(self, value); 42 + } 43 + 44 + fn visit_expression_bool(&mut self, value: &'mir mut bool) { 45 + visit_expression_bool(self, value); 46 + } 47 + 48 + fn visit_expression_string(&mut self, value: &'mir mut EcoString) { 49 + visit_expression_string(self, value); 50 + } 51 + 52 + fn visit_expression_equals(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 53 + visit_expression_equals(self, lhs, rhs); 54 + } 55 + 56 + fn visit_expression_not_equals( 57 + &mut self, 58 + lhs: &'mir mut Expression, 59 + rhs: &'mir mut Expression, 60 + ) { 61 + visit_expression_not_equals(self, lhs, rhs); 62 + } 63 + 64 + fn visit_expression_int_gt(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 65 + visit_expression_int_gt(self, lhs, rhs); 66 + } 67 + 68 + fn visit_expression_int_gt_eq(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 69 + visit_expression_int_gt_eq(self, lhs, rhs); 70 + } 71 + 72 + fn visit_expression_int_lt(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 73 + visit_expression_int_lt(self, lhs, rhs); 74 + } 75 + 76 + fn visit_expression_int_lt_eq(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 77 + visit_expression_int_lt_eq(self, lhs, rhs); 78 + } 79 + 80 + fn visit_expression_int_add(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 81 + visit_expression_int_add(self, lhs, rhs); 82 + } 83 + 84 + fn visit_expression_int_sub(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 85 + visit_expression_int_sub(self, lhs, rhs); 86 + } 87 + 88 + fn visit_expression_int_mul(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 89 + visit_expression_int_mul(self, lhs, rhs); 90 + } 91 + 92 + fn visit_expression_int_div(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 93 + visit_expression_int_div(self, lhs, rhs); 94 + } 95 + 96 + fn visit_expression_int_rem(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 97 + visit_expression_int_rem(self, lhs, rhs); 98 + } 99 + 100 + fn visit_expression_float_gt(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 101 + visit_expression_float_gt(self, lhs, rhs); 102 + } 103 + 104 + fn visit_expression_float_gt_eq( 105 + &mut self, 106 + lhs: &'mir mut Expression, 107 + rhs: &'mir mut Expression, 108 + ) { 109 + visit_expression_float_gt_eq(self, lhs, rhs); 110 + } 111 + 112 + fn visit_expression_float_lt(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 113 + visit_expression_float_lt(self, lhs, rhs); 114 + } 115 + 116 + fn visit_expression_float_lt_eq( 117 + &mut self, 118 + lhs: &'mir mut Expression, 119 + rhs: &'mir mut Expression, 120 + ) { 121 + visit_expression_float_lt_eq(self, lhs, rhs); 122 + } 123 + 124 + fn visit_expression_float_add(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 125 + visit_expression_float_add(self, lhs, rhs); 126 + } 127 + 128 + fn visit_expression_float_sub(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 129 + visit_expression_float_sub(self, lhs, rhs); 130 + } 131 + 132 + fn visit_expression_float_mul(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 133 + visit_expression_float_mul(self, lhs, rhs); 134 + } 135 + 136 + fn visit_expression_float_div(&mut self, lhs: &'mir mut Expression, rhs: &'mir mut Expression) { 137 + visit_expression_float_div(self, lhs, rhs); 138 + } 139 + 140 + fn visit_expression_string_concat( 141 + &mut self, 142 + lhs: &'mir mut Expression, 143 + rhs: &'mir mut Expression, 144 + ) { 145 + visit_expression_string_concat(self, lhs, rhs); 146 + } 147 + 148 + fn visit_expression_list( 149 + &mut self, 150 + items: &'mir mut Vec<Expression>, 151 + tail: &'mir mut Option<Box<Expression>>, 152 + ) { 153 + visit_expression_list(self, items, tail); 154 + } 155 + 156 + fn visit_expression_struct( 157 + &mut self, 158 + tag: &'mir mut Option<u32>, 159 + items: &'mir mut Vec<Expression>, 160 + ) { 161 + visit_expression_struct(self, tag, items); 162 + } 163 + 164 + fn visit_expression_struct_access( 165 + &mut self, 166 + value: &'mir mut Expression, 167 + index: &'mir mut u64, 168 + ) { 169 + visit_expression_struct_access(self, value, index); 170 + } 171 + 172 + fn visit_expression_set(&mut self, name: &'mir mut Var, value: &'mir mut Expression) { 173 + visit_expression_set(self, name, value); 174 + } 175 + 176 + fn visit_expression_if( 177 + &mut self, 178 + cond: &'mir mut Expression, 179 + then: &'mir mut Expression, 180 + else_: &'mir mut Expression, 181 + ) { 182 + visit_expression_if(self, cond, then, else_); 183 + } 184 + 185 + fn visit_expression_call( 186 + &mut self, 187 + target: &'mir mut Expression, 188 + args: &'mir mut Vec<Expression>, 189 + ) { 190 + visit_expression_call(self, target, args); 191 + } 192 + } 193 + 194 + pub fn visit_module<'mir, V>(v: &mut V, module: &'mir mut Module) 195 + where 196 + V: Visit<'mir> + ?Sized, 197 + { 198 + for function in &mut module.functions { 199 + v.visit_function(function); 200 + } 201 + } 202 + 203 + pub fn visit_function<'mir, V>(v: &mut V, function: &'mir mut Function) 204 + where 205 + V: Visit<'mir> + ?Sized, 206 + { 207 + v.visit_expression(&mut function.body); 208 + } 209 + 210 + pub fn visit_expression<'mir, V>(v: &mut V, expression: &'mir mut Expression) 211 + where 212 + V: Visit<'mir> + ?Sized, 213 + { 214 + match expression { 215 + Expression::Block(expressions) => v.visit_expression_block(expressions), 216 + Expression::FunctionRef { 217 + module, 218 + name, 219 + arity, 220 + } => v.visit_expression_function_ref(module, name, arity), 221 + Expression::Var(var) => v.visit_expression_var(var), 222 + Expression::Int { value } => v.visit_expression_int(value), 223 + Expression::Float { value } => v.visit_expression_float(value), 224 + Expression::Bool { value } => v.visit_expression_bool(value), 225 + Expression::String { value } => v.visit_expression_string(value), 226 + Expression::Equals { lhs, rhs } => v.visit_expression_equals(lhs, rhs), 227 + Expression::NotEquals { lhs, rhs } => v.visit_expression_not_equals(lhs, rhs), 228 + Expression::IntGt { lhs, rhs } => v.visit_expression_int_gt(lhs, rhs), 229 + Expression::IntGtEq { lhs, rhs } => v.visit_expression_int_gt_eq(lhs, rhs), 230 + Expression::IntLt { lhs, rhs } => v.visit_expression_int_lt(lhs, rhs), 231 + Expression::IntLtEq { lhs, rhs } => v.visit_expression_int_lt_eq(lhs, rhs), 232 + Expression::IntAdd { lhs, rhs } => v.visit_expression_int_add(lhs, rhs), 233 + Expression::IntSub { lhs, rhs } => v.visit_expression_int_sub(lhs, rhs), 234 + Expression::IntMul { lhs, rhs } => v.visit_expression_int_mul(lhs, rhs), 235 + Expression::IntDiv { lhs, rhs } => v.visit_expression_int_div(lhs, rhs), 236 + Expression::IntRem { lhs, rhs } => v.visit_expression_int_rem(lhs, rhs), 237 + Expression::FloatGt { lhs, rhs } => v.visit_expression_float_gt(lhs, rhs), 238 + Expression::FloatGtEq { lhs, rhs } => v.visit_expression_float_gt_eq(lhs, rhs), 239 + Expression::FloatLt { lhs, rhs } => v.visit_expression_float_lt(lhs, rhs), 240 + Expression::FloatLtEq { lhs, rhs } => v.visit_expression_float_lt_eq(lhs, rhs), 241 + Expression::FloatAdd { lhs, rhs } => v.visit_expression_float_add(lhs, rhs), 242 + Expression::FloatSub { lhs, rhs } => v.visit_expression_float_sub(lhs, rhs), 243 + Expression::FloatMul { lhs, rhs } => v.visit_expression_float_mul(lhs, rhs), 244 + Expression::FloatDiv { lhs, rhs } => v.visit_expression_float_div(lhs, rhs), 245 + Expression::StringConcat { lhs, rhs } => v.visit_expression_string_concat(lhs, rhs), 246 + Expression::List { items, tail } => v.visit_expression_list(items, tail), 247 + Expression::Struct { tag, items } => v.visit_expression_struct(tag, items), 248 + Expression::StructAccess { value, index } => v.visit_expression_struct_access(value, index), 249 + Expression::Set { name, value } => v.visit_expression_set(name, value), 250 + Expression::If { cond, then, else_ } => v.visit_expression_if(cond, then, else_), 251 + Expression::Call { target, args } => v.visit_expression_call(target, args), 252 + } 253 + } 254 + 255 + pub fn visit_expression_block<'mir, V>(v: &mut V, expressions: &'mir mut Vec<Expression>) 256 + where 257 + V: Visit<'mir> + ?Sized, 258 + { 259 + for expression in expressions { 260 + v.visit_expression(expression); 261 + } 262 + } 263 + 264 + pub fn visit_expression_function_ref<'mir, V>( 265 + _v: &mut V, 266 + _module: &mut EcoString, 267 + _name: &mut EcoString, 268 + _arity: &mut usize, 269 + ) where 270 + V: Visit<'mir> + ?Sized, 271 + { 272 + } 273 + 274 + pub fn visit_expression_var<'mir, V>(_v: &mut V, _var: &mut Var) 275 + where 276 + V: Visit<'mir> + ?Sized, 277 + { 278 + } 279 + 280 + pub fn visit_expression_int<'mir, V>(_v: &mut V, _value: &mut BigInt) 281 + where 282 + V: Visit<'mir> + ?Sized, 283 + { 284 + } 285 + 286 + pub fn visit_expression_float<'mir, V>(_v: &mut V, _value: &mut EcoString) 287 + where 288 + V: Visit<'mir> + ?Sized, 289 + { 290 + } 291 + 292 + pub fn visit_expression_bool<'mir, V>(_v: &mut V, _value: &mut bool) 293 + where 294 + V: Visit<'mir> + ?Sized, 295 + { 296 + } 297 + 298 + pub fn visit_expression_string<'mir, V>(_v: &mut V, _value: &mut EcoString) 299 + where 300 + V: Visit<'mir> + ?Sized, 301 + { 302 + } 303 + 304 + pub fn visit_expression_equals<'mir, V>( 305 + v: &mut V, 306 + lhs: &'mir mut Expression, 307 + rhs: &'mir mut Expression, 308 + ) where 309 + V: Visit<'mir> + ?Sized, 310 + { 311 + v.visit_expression(lhs); 312 + v.visit_expression(rhs); 313 + } 314 + 315 + pub fn visit_expression_not_equals<'mir, V>( 316 + v: &mut V, 317 + lhs: &'mir mut Expression, 318 + rhs: &'mir mut Expression, 319 + ) where 320 + V: Visit<'mir> + ?Sized, 321 + { 322 + v.visit_expression(lhs); 323 + v.visit_expression(rhs); 324 + } 325 + 326 + pub fn visit_expression_int_gt<'mir, V>( 327 + v: &mut V, 328 + lhs: &'mir mut Expression, 329 + rhs: &'mir mut Expression, 330 + ) where 331 + V: Visit<'mir> + ?Sized, 332 + { 333 + v.visit_expression(lhs); 334 + v.visit_expression(rhs); 335 + } 336 + 337 + pub fn visit_expression_int_gt_eq<'mir, V>( 338 + v: &mut V, 339 + lhs: &'mir mut Expression, 340 + rhs: &'mir mut Expression, 341 + ) where 342 + V: Visit<'mir> + ?Sized, 343 + { 344 + v.visit_expression(lhs); 345 + v.visit_expression(rhs); 346 + } 347 + 348 + pub fn visit_expression_int_lt<'mir, V>( 349 + v: &mut V, 350 + lhs: &'mir mut Expression, 351 + rhs: &'mir mut Expression, 352 + ) where 353 + V: Visit<'mir> + ?Sized, 354 + { 355 + v.visit_expression(lhs); 356 + v.visit_expression(rhs); 357 + } 358 + 359 + pub fn visit_expression_int_lt_eq<'mir, V>( 360 + v: &mut V, 361 + lhs: &'mir mut Expression, 362 + rhs: &'mir mut Expression, 363 + ) where 364 + V: Visit<'mir> + ?Sized, 365 + { 366 + v.visit_expression(lhs); 367 + v.visit_expression(rhs); 368 + } 369 + 370 + pub fn visit_expression_int_add<'mir, V>( 371 + v: &mut V, 372 + lhs: &'mir mut Expression, 373 + rhs: &'mir mut Expression, 374 + ) where 375 + V: Visit<'mir> + ?Sized, 376 + { 377 + v.visit_expression(lhs); 378 + v.visit_expression(rhs); 379 + } 380 + 381 + pub fn visit_expression_int_sub<'mir, V>( 382 + v: &mut V, 383 + lhs: &'mir mut Expression, 384 + rhs: &'mir mut Expression, 385 + ) where 386 + V: Visit<'mir> + ?Sized, 387 + { 388 + v.visit_expression(lhs); 389 + v.visit_expression(rhs); 390 + } 391 + 392 + pub fn visit_expression_int_mul<'mir, V>( 393 + v: &mut V, 394 + lhs: &'mir mut Expression, 395 + rhs: &'mir mut Expression, 396 + ) where 397 + V: Visit<'mir> + ?Sized, 398 + { 399 + v.visit_expression(lhs); 400 + v.visit_expression(rhs); 401 + } 402 + 403 + pub fn visit_expression_int_div<'mir, V>( 404 + v: &mut V, 405 + lhs: &'mir mut Expression, 406 + rhs: &'mir mut Expression, 407 + ) where 408 + V: Visit<'mir> + ?Sized, 409 + { 410 + v.visit_expression(lhs); 411 + v.visit_expression(rhs); 412 + } 413 + 414 + pub fn visit_expression_int_rem<'mir, V>( 415 + v: &mut V, 416 + lhs: &'mir mut Expression, 417 + rhs: &'mir mut Expression, 418 + ) where 419 + V: Visit<'mir> + ?Sized, 420 + { 421 + v.visit_expression(lhs); 422 + v.visit_expression(rhs); 423 + } 424 + 425 + pub fn visit_expression_float_gt<'mir, V>( 426 + v: &mut V, 427 + lhs: &'mir mut Expression, 428 + rhs: &'mir mut Expression, 429 + ) where 430 + V: Visit<'mir> + ?Sized, 431 + { 432 + v.visit_expression(lhs); 433 + v.visit_expression(rhs); 434 + } 435 + 436 + pub fn visit_expression_float_gt_eq<'mir, V>( 437 + v: &mut V, 438 + lhs: &'mir mut Expression, 439 + rhs: &'mir mut Expression, 440 + ) where 441 + V: Visit<'mir> + ?Sized, 442 + { 443 + v.visit_expression(lhs); 444 + v.visit_expression(rhs); 445 + } 446 + 447 + pub fn visit_expression_float_lt<'mir, V>( 448 + v: &mut V, 449 + lhs: &'mir mut Expression, 450 + rhs: &'mir mut Expression, 451 + ) where 452 + V: Visit<'mir> + ?Sized, 453 + { 454 + v.visit_expression(lhs); 455 + v.visit_expression(rhs); 456 + } 457 + 458 + pub fn visit_expression_float_lt_eq<'mir, V>( 459 + v: &mut V, 460 + lhs: &'mir mut Expression, 461 + rhs: &'mir mut Expression, 462 + ) where 463 + V: Visit<'mir> + ?Sized, 464 + { 465 + v.visit_expression(lhs); 466 + v.visit_expression(rhs); 467 + } 468 + 469 + pub fn visit_expression_float_add<'mir, V>( 470 + v: &mut V, 471 + lhs: &'mir mut Expression, 472 + rhs: &'mir mut Expression, 473 + ) where 474 + V: Visit<'mir> + ?Sized, 475 + { 476 + v.visit_expression(lhs); 477 + v.visit_expression(rhs); 478 + } 479 + 480 + pub fn visit_expression_float_sub<'mir, V>( 481 + v: &mut V, 482 + lhs: &'mir mut Expression, 483 + rhs: &'mir mut Expression, 484 + ) where 485 + V: Visit<'mir> + ?Sized, 486 + { 487 + v.visit_expression(lhs); 488 + v.visit_expression(rhs); 489 + } 490 + 491 + pub fn visit_expression_float_mul<'mir, V>( 492 + v: &mut V, 493 + lhs: &'mir mut Expression, 494 + rhs: &'mir mut Expression, 495 + ) where 496 + V: Visit<'mir> + ?Sized, 497 + { 498 + v.visit_expression(lhs); 499 + v.visit_expression(rhs); 500 + } 501 + 502 + pub fn visit_expression_float_div<'mir, V>( 503 + v: &mut V, 504 + lhs: &'mir mut Expression, 505 + rhs: &'mir mut Expression, 506 + ) where 507 + V: Visit<'mir> + ?Sized, 508 + { 509 + v.visit_expression(lhs); 510 + v.visit_expression(rhs); 511 + } 512 + 513 + pub fn visit_expression_string_concat<'mir, V>( 514 + v: &mut V, 515 + lhs: &'mir mut Expression, 516 + rhs: &'mir mut Expression, 517 + ) where 518 + V: Visit<'mir> + ?Sized, 519 + { 520 + v.visit_expression(lhs); 521 + v.visit_expression(rhs); 522 + } 523 + 524 + pub fn visit_expression_list<'mir, V>( 525 + v: &mut V, 526 + items: &'mir mut Vec<Expression>, 527 + tail: &'mir mut Option<Box<Expression>>, 528 + ) where 529 + V: Visit<'mir> + ?Sized, 530 + { 531 + for item in items { 532 + v.visit_expression(item); 533 + } 534 + if let Some(tail) = tail { 535 + v.visit_expression(tail); 536 + } 537 + } 538 + 539 + pub fn visit_expression_struct<'mir, V>( 540 + v: &mut V, 541 + _tag: &'mir mut Option<u32>, 542 + items: &'mir mut Vec<Expression>, 543 + ) where 544 + V: Visit<'mir> + ?Sized, 545 + { 546 + for item in items { 547 + v.visit_expression(item); 548 + } 549 + } 550 + 551 + pub fn visit_expression_struct_access<'mir, V>( 552 + v: &mut V, 553 + value: &'mir mut Expression, 554 + _index: &'mir mut u64, 555 + ) where 556 + V: Visit<'mir> + ?Sized, 557 + { 558 + v.visit_expression(value); 559 + } 560 + 561 + pub fn visit_expression_set<'mir, V>(v: &mut V, _name: &'mir mut Var, value: &'mir mut Expression) 562 + where 563 + V: Visit<'mir> + ?Sized, 564 + { 565 + v.visit_expression(value); 566 + } 567 + 568 + pub fn visit_expression_if<'mir, V>( 569 + v: &mut V, 570 + cond: &'mir mut Expression, 571 + then: &'mir mut Expression, 572 + else_: &'mir mut Expression, 573 + ) where 574 + V: Visit<'mir> + ?Sized, 575 + { 576 + v.visit_expression(cond); 577 + v.visit_expression(then); 578 + v.visit_expression(else_); 579 + } 580 + 581 + pub fn visit_expression_call<'mir, V>( 582 + v: &mut V, 583 + target: &'mir mut Expression, 584 + args: &'mir mut Vec<Expression>, 585 + ) where 586 + V: Visit<'mir> + ?Sized, 587 + { 588 + v.visit_expression(target); 589 + for arg in args { 590 + v.visit_expression(arg); 591 + } 592 + }