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

Configure Feed

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

Perform translations of Bool expression

author
Danielle Maywood
committer
nandi
date (Jul 26, 2026, 12:11 PM -0700) commit 5694377e parent 46c18ca2 change-id zsztunly
+133 -9
+40 -6
compiler-core/src/cranelift/mir.rs
··· 62 62 value: EcoString, 63 63 }, 64 64 65 + Bool { 66 + value: bool, 67 + }, 68 + 65 69 Equals { 66 70 lhs: Box<Expression>, 67 71 rhs: Box<Expression>, ··· 217 221 | Expression::FunctionRef { .. } 218 222 | Expression::Int { .. } 219 223 | Expression::Float { .. } 224 + | Expression::Bool { .. } 220 225 | Expression::Equals { .. } 221 226 | Expression::IntAdd { .. } 222 227 | Expression::IntSub { .. } ··· 241 246 Expression::Var(var) => Expression::Var(var), 242 247 Expression::Int { value } => Expression::Int { value }, 243 248 Expression::Float { value } => Expression::Float { value }, 249 + Expression::Bool { value } => Expression::Bool { value }, 244 250 Expression::Equals { lhs, rhs } => Expression::Equals { 245 251 lhs: Box::new(Self::flatten_expression(*lhs)), 246 252 rhs: Box::new(Self::flatten_expression(*rhs)), ··· 304 310 let expression = Self::remove_unused_code(expression); 305 311 306 312 match expression { 307 - Expression::Var { .. } | Expression::FunctionRef { .. } 313 + Expression::Var { .. } 314 + | Expression::FunctionRef { .. } 315 + | Expression::Int { .. } 316 + | Expression::Float { .. } 317 + | Expression::Bool { .. } 308 318 if index != last_expression_index => {} 309 319 310 320 Expression::Block(_) ··· 312 322 | Expression::Var { .. } 313 323 | Expression::Int { .. } 314 324 | Expression::Float { .. } 325 + | Expression::Bool { .. } 315 326 | Expression::Equals { .. } 316 327 | Expression::IntAdd { .. } 317 328 | Expression::IntSub { .. } ··· 336 347 Expression::Var(var) => Expression::Var(var), 337 348 Expression::Int { value } => Expression::Int { value }, 338 349 Expression::Float { value } => Expression::Float { value }, 350 + Expression::Bool { value } => Expression::Bool { value }, 339 351 Expression::Equals { lhs, rhs } => Expression::Equals { 340 352 lhs: Box::new(Self::remove_unused_code(*lhs)), 341 353 rhs: Box::new(Self::remove_unused_code(*rhs)), ··· 519 531 name: name.clone(), 520 532 }, 521 533 type_::ValueConstructorVariant::Record { 522 - name: _, 534 + name, 523 535 arity: _, 524 536 field_map: _, 525 537 location: _, 526 - module: _, 538 + module, 527 539 variants_count: _, 528 540 variant_index: _, 529 541 documentation: _, 530 - } => todo!(), 542 + } => match (module.as_str(), name.as_str()) { 543 + ("gleam", "True") => Expression::Bool { value: true }, 544 + ("gleam", "False") => Expression::Bool { value: false }, 545 + (_, _) => todo!(), 546 + }, 531 547 }, 532 548 ast::TypedExpr::Fn { 533 549 location: _, ··· 573 589 let rhs = Box::new(self.translate_expression(right)); 574 590 575 591 match name { 576 - ast::BinOp::And => todo!(), 577 - ast::BinOp::Or => todo!(), 592 + ast::BinOp::And => Expression::If { 593 + cond: lhs, 594 + then: rhs, 595 + else_: Box::new(Expression::Bool { value: false }), 596 + }, 597 + ast::BinOp::Or => Expression::If { 598 + cond: lhs, 599 + then: Box::new(Expression::Bool { value: true }), 600 + else_: rhs, 601 + }, 578 602 ast::BinOp::Eq => Expression::Equals { lhs, rhs }, 579 603 ast::BinOp::NotEq => todo!(), 580 604 ast::BinOp::LtInt => todo!(), ··· 1212 1236 let _ = 2.0 -. 1.0 1213 1237 let _ = 3.0 *. 2.0 1214 1238 let _ = 4.0 /. 3.0 1239 + 1240 + let _ = 1 == 2 && 2 == 4 1241 + let _ = 1 == 2 || 2 == 4 1215 1242 }"# 1216 1243 )); 1244 + 1245 + insta::assert_debug_snapshot!(translate( 1246 + r#"pub fn booleans() { 1247 + let _ = True 1248 + let _ = False 1249 + }"# 1250 + )) 1217 1251 } 1218 1252 }
+55 -3
compiler-core/src/cranelift/snapshots/gleam_core__cranelift__mir__tests__translates_samples-5.snap
··· 1 1 --- 2 2 source: compiler-core/src/cranelift/mir.rs 3 - expression: "translate(r#\"pub fn maths() {\n let _ = 1 + 0\n let _ = 2 - 1\n let _ = 3 * 2\n let _ = 4 / 3\n\n let _ = 1.0 +. 0.0\n let _ = 2.0 -. 1.0\n let _ = 3.0 *. 2.0\n let _ = 4.0 /. 3.0\n }\"#)" 3 + expression: "translate(r#\"pub fn maths() {\n let _ = 1 + 0\n let _ = 2 - 1\n let _ = 3 * 2\n let _ = 4 / 3\n\n let _ = 1.0 +. 0.0\n let _ = 2.0 -. 1.0\n let _ = 3.0 *. 2.0\n let _ = 4.0 /. 3.0\n\n let _ = 1 == 2 && 2 == 4\n let _ = 1 == 2 || 2 == 4\n }\"#)" 4 4 --- 5 5 Module { 6 6 functions: [ 7 7 Function { 8 8 name: "maths", 9 - return_type: Float, 9 + return_type: Bool, 10 10 parameters: [], 11 11 body: Block( 12 12 [ ··· 114 114 }, 115 115 }, 116 116 }, 117 + Set { 118 + name: Var { 119 + name: "_tmp$9", 120 + }, 121 + value: If { 122 + cond: Equals { 123 + lhs: Int { 124 + value: 1, 125 + }, 126 + rhs: Int { 127 + value: 2, 128 + }, 129 + }, 130 + then: Equals { 131 + lhs: Int { 132 + value: 2, 133 + }, 134 + rhs: Int { 135 + value: 4, 136 + }, 137 + }, 138 + else_: Bool { 139 + value: false, 140 + }, 141 + }, 142 + }, 143 + Set { 144 + name: Var { 145 + name: "_tmp$10", 146 + }, 147 + value: If { 148 + cond: Equals { 149 + lhs: Int { 150 + value: 1, 151 + }, 152 + rhs: Int { 153 + value: 2, 154 + }, 155 + }, 156 + then: Bool { 157 + value: true, 158 + }, 159 + else_: Equals { 160 + lhs: Int { 161 + value: 2, 162 + }, 163 + rhs: Int { 164 + value: 4, 165 + }, 166 + }, 167 + }, 168 + }, 117 169 Var( 118 170 Var { 119 - name: "_tmp$8", 171 + name: "_tmp$10", 120 172 }, 121 173 ), 122 174 ],
+38
compiler-core/src/cranelift/snapshots/gleam_core__cranelift__mir__tests__translates_samples-6.snap
··· 1 + --- 2 + source: compiler-core/src/cranelift/mir.rs 3 + expression: "translate(r#\"pub fn booleans() {\n let _ = True\n let _ = False\n }\"#)" 4 + --- 5 + Module { 6 + functions: [ 7 + Function { 8 + name: "booleans", 9 + return_type: Bool, 10 + parameters: [], 11 + body: Block( 12 + [ 13 + Set { 14 + name: Var { 15 + name: "_tmp$1", 16 + }, 17 + value: Bool { 18 + value: true, 19 + }, 20 + }, 21 + Set { 22 + name: Var { 23 + name: "_tmp$2", 24 + }, 25 + value: Bool { 26 + value: false, 27 + }, 28 + }, 29 + Var( 30 + Var { 31 + name: "_tmp$2", 32 + }, 33 + ), 34 + ], 35 + ), 36 + }, 37 + ], 38 + }