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

Configure Feed

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

Begin translation of lists to mir

author
Danielle Maywood
committer
nandi
date (Jul 26, 2026, 12:11 PM -0700) commit 3cdc483f parent e00d1f1c change-id lvnpnsst
+95 -19
+1
compiler-core/src/cranelift.rs
··· 187 187 mir::Expression::FloatMul { lhs: _, rhs: _ } => todo!(), 188 188 mir::Expression::FloatDiv { lhs: _, rhs: _ } => todo!(), 189 189 mir::Expression::StringConcat { lhs: _, rhs: _ } => todo!(), 190 + mir::Expression::List { items: _, tail: _ } => todo!(), 190 191 mir::Expression::Struct { tag: _, items: _ } => todo!(), 191 192 mir::Expression::StructAccess { value: _, index: _ } => todo!(), 192 193 mir::Expression::Set { name, value } => {
+49 -5
compiler-core/src/cranelift/mir.rs
··· 37 37 Bool, 38 38 String, 39 39 Struct { elements: Vec<Type> }, 40 + List(Box<Type>), 40 41 Generic, 41 42 } 42 43 ··· 173 174 rhs: Box<Expression>, 174 175 }, 175 176 177 + List { 178 + items: Vec<Expression>, 179 + tail: Option<Box<Expression>>, 180 + }, 181 + 176 182 Struct { 177 183 tag: Option<u32>, 178 184 items: Vec<Expression>, ··· 268 274 ("gleam", "Float") => Type::Float, 269 275 ("gleam", "Bool") => Type::Bool, 270 276 ("gleam", "String") => Type::String, 277 + ("gleam", "List") => { 278 + let list_type = Self::translate_type(&arguments[0]); 279 + 280 + Type::List(Box::new(list_type)) 281 + } 271 282 (_, _) => Type::Struct { 272 283 elements: arguments 273 284 .iter() ··· 325 336 | Expression::FloatMul { .. } 326 337 | Expression::FloatDiv { .. } 327 338 | Expression::StringConcat { .. } 339 + | Expression::List { .. } 328 340 | Expression::Struct { .. } 329 341 | Expression::StructAccess { .. } 330 342 | Expression::Set { .. } ··· 432 444 lhs: Box::new(Self::flatten_expression(*lhs)), 433 445 rhs: Box::new(Self::flatten_expression(*rhs)), 434 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 + }, 435 451 Expression::Struct { tag, items } => Expression::Struct { 436 452 tag, 437 453 items: items.into_iter().map(Self::flatten_expression).collect(), ··· 502 518 | Expression::FloatMul { .. } 503 519 | Expression::FloatDiv { .. } 504 520 | Expression::StringConcat { .. } 521 + | Expression::List { .. } 505 522 | Expression::Struct { .. } 506 523 | Expression::StructAccess { .. } 507 524 | Expression::Set { .. } ··· 608 625 Expression::StringConcat { lhs, rhs } => Expression::StringConcat { 609 626 lhs: Box::new(Self::remove_unused_code(*lhs)), 610 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))), 611 632 }, 612 633 Expression::Struct { tag, items } => Expression::Struct { 613 634 tag, ··· 817 838 ast::TypedExpr::List { 818 839 location: _, 819 840 type_: _, 820 - elements: _, 821 - tail: _, 822 - } => todo!(), 841 + elements, 842 + tail, 843 + } => { 844 + let items = elements 845 + .iter() 846 + .map(|element| self.translate_expression(element)) 847 + .collect(); 848 + 849 + let tail = tail 850 + .as_ref() 851 + .map(|tail| Box::new(self.translate_expression(tail))); 852 + 853 + Expression::List { items, tail } 854 + } 823 855 ast::TypedExpr::Call { 824 856 location: _, 825 857 type_: _, ··· 1365 1397 }, 1366 1398 ast::Constant::List { 1367 1399 location: _, 1368 - elements: _, 1400 + elements, 1369 1401 type_: _, 1370 - } => todo!(), 1402 + } => Expression::List { 1403 + items: elements 1404 + .iter() 1405 + .map(|element| self.translate_constant(element)) 1406 + .collect(), 1407 + tail: None, 1408 + }, 1371 1409 ast::Constant::Record { 1372 1410 location: _, 1373 1411 module: _, ··· 1652 1690 1653 1691 pub fn pipe() { 1654 1692 10 |> add(20) |> add(30) 1693 + }"# 1694 + )); 1695 + 1696 + insta::assert_debug_snapshot!(translate( 1697 + r#"pub fn pipe() { 1698 + [1, 2, 3, ..[4, 5, 6]] 1655 1699 }"# 1656 1700 )); 1657 1701 }
+44
compiler-core/src/cranelift/snapshots/gleam_core__cranelift__mir__tests__translates_samples-12.snap
··· 1 + --- 2 + source: compiler-core/src/cranelift/mir.rs 3 + expression: "translate(r#\"pub fn pipe() {\n [1, 2, 3, ..[4, 5, 6]]\n }\"#)" 4 + --- 5 + Module { 6 + functions: [ 7 + Function { 8 + name: "pipe", 9 + return_type: List( 10 + Int, 11 + ), 12 + parameters: [], 13 + body: List { 14 + items: [ 15 + Int { 16 + value: 1, 17 + }, 18 + Int { 19 + value: 2, 20 + }, 21 + Int { 22 + value: 3, 23 + }, 24 + ], 25 + tail: Some( 26 + List { 27 + items: [ 28 + Int { 29 + value: 4, 30 + }, 31 + Int { 32 + value: 5, 33 + }, 34 + Int { 35 + value: 6, 36 + }, 37 + ], 38 + tail: None, 39 + }, 40 + ), 41 + }, 42 + }, 43 + ], 44 + }
+1 -14
test/project_cranelift/src/project_cranelift.gleam
··· 1 - pub fn fib(n: Int) -> Int { 2 - case n { 3 - 0 -> 0 4 - 1 -> 1 5 - _ -> fib(n - 1) + fib(n - 2) 6 - } 7 - } 8 - 9 - pub fn add(lhs: Int, rhs: Int) -> Int { 10 - lhs + rhs 11 - } 12 - 13 1 pub fn main() { 14 - fib(40) 15 - |> add(fib(40)) 2 + let _ = [1, 2, 3] 16 3 }