Skip to main content

lutra_compiler/pr/
utils.rs

1use lutra_bin::ir;
2
3impl From<ir::Ty> for super::Ty {
4    fn from(ty: ir::Ty) -> super::Ty {
5        let kind = match ty.kind {
6            ir::TyKind::Primitive(primitive) => {
7                let primitive = match primitive {
8                    ir::TyPrimitive::bool => super::TyPrimitive::bool,
9                    ir::TyPrimitive::int8 => super::TyPrimitive::int8,
10                    ir::TyPrimitive::int16 => super::TyPrimitive::int16,
11                    ir::TyPrimitive::int32 => super::TyPrimitive::int32,
12                    ir::TyPrimitive::int64 => super::TyPrimitive::int64,
13                    ir::TyPrimitive::uint8 => super::TyPrimitive::uint8,
14                    ir::TyPrimitive::uint16 => super::TyPrimitive::uint16,
15                    ir::TyPrimitive::uint32 => super::TyPrimitive::uint32,
16                    ir::TyPrimitive::uint64 => super::TyPrimitive::uint64,
17                    ir::TyPrimitive::float32 => super::TyPrimitive::float32,
18                    ir::TyPrimitive::float64 => super::TyPrimitive::float64,
19                    ir::TyPrimitive::text => super::TyPrimitive::text,
20                };
21                super::TyKind::Primitive(primitive)
22            }
23            ir::TyKind::Tuple(fields) => super::TyKind::Tuple(
24                fields
25                    .into_iter()
26                    .map(|f| {
27                        let name = f.name.clone();
28                        let ty = super::Ty::from(f.ty);
29                        super::TyTupleField {
30                            name,
31                            ty,
32                            unpack: false,
33                        }
34                    })
35                    .collect(),
36            ),
37            ir::TyKind::Array(items_ty) => {
38                super::TyKind::Array(Box::new(super::Ty::from(*items_ty)))
39            }
40            ir::TyKind::Enum(variants) => super::TyKind::Enum(
41                variants
42                    .into_iter()
43                    .map(super::TyEnumVariant::from)
44                    .collect(),
45            ),
46            ir::TyKind::Function(func) => super::TyKind::Func(super::TyFunc {
47                params: func
48                    .params
49                    .into_iter()
50                    .map(super::Ty::from)
51                    .map(|ty| super::TyFuncParam::simple(Some(ty)))
52                    .collect(),
53                body: Some(Box::new(super::Ty::from(func.body))),
54                ty_params: Vec::new(),
55            }),
56            ir::TyKind::Ident(path) => super::TyKind::Ident(super::Path::new(path.0)),
57        };
58
59        let mut r = super::Ty::new(kind);
60        r.name = ty.name;
61        r
62    }
63}
64
65impl From<ir::TyEnumVariant> for super::TyEnumVariant {
66    fn from(v: ir::TyEnumVariant) -> Self {
67        super::TyEnumVariant {
68            name: v.name,
69            ty: super::Ty::from(v.ty),
70        }
71    }
72}