Skip to main content

lisette_syntax/
display.rs

1use std::fmt;
2
3use crate::types::Type;
4
5impl Type {
6    pub fn stringify(&self) -> String {
7        match self {
8            Type::Nominal {
9                id, params: args, ..
10            } => {
11                let args_formatted = args
12                    .iter()
13                    .map(|a| a.stringify())
14                    .collect::<Vec<_>>()
15                    .join(", ");
16
17                let name = id.last_segment();
18
19                if name == "Unit" {
20                    return "()".to_string();
21                }
22
23                if name == "bool" {
24                    return "bool".to_string();
25                }
26
27                if name.starts_with("Tuple") {
28                    return format!("({})", args_formatted);
29                }
30
31                if name == "Ref" {
32                    return format!("Ref<{}>", args_formatted);
33                }
34
35                if args.is_empty() {
36                    return name.to_string();
37                }
38
39                format!("{}<{}>", name, args_formatted)
40            }
41
42            Type::Var { id, hint } => match hint {
43                Some(name) => format!("?{}", name),
44                None => format!("?{}", id.index()),
45            },
46
47            Type::Uninferred => "?uninferred".to_string(),
48
49            Type::Ignored => "?ignored".to_string(),
50
51            Type::Function(f) => {
52                let args_formatted = f
53                    .params
54                    .iter()
55                    .map(|param| {
56                        if param.mutable {
57                            format!("mut {}", param.ty.stringify())
58                        } else {
59                            param.ty.stringify()
60                        }
61                    })
62                    .collect::<Vec<_>>()
63                    .join(", ");
64
65                let ret_formatted = f.return_type.stringify();
66
67                format!("fn ({}) -> {}", args_formatted, ret_formatted)
68            }
69
70            Type::Forall { .. } => {
71                unreachable!("Forall types are always instantiated before display")
72            }
73
74            Type::Parameter(name) => name.to_string(),
75
76            Type::Never => "Never".to_string(),
77
78            Type::Tuple(elements) => {
79                let formatted = elements
80                    .iter()
81                    .map(|e| e.stringify())
82                    .collect::<Vec<_>>()
83                    .join(", ");
84                format!("({})", formatted)
85            }
86
87            Type::Array { length, element } => {
88                format!("Array<{}, {}>", element.stringify(), length)
89            }
90
91            Type::Error => "<error>".to_string(),
92
93            Type::ImportNamespace(module_id) if module_id.as_str() == crate::ENTRY_MODULE_ID => {
94                crate::ROOT_IMPORT.to_string()
95            }
96
97            Type::ImportNamespace(module_id) => {
98                let path = module_id.strip_prefix("go:").unwrap_or(module_id);
99                path.rsplit('/').next().unwrap_or(module_id).to_string()
100            }
101
102            Type::ReceiverPlaceholder => "self".to_string(),
103
104            Type::Simple(kind) => match kind {
105                crate::types::SimpleKind::Unit => "()".to_string(),
106                _ => kind.leaf_name().to_string(),
107            },
108
109            Type::Compound { kind, args } => {
110                let args_formatted = args
111                    .iter()
112                    .map(|a| a.stringify())
113                    .collect::<Vec<_>>()
114                    .join(", ");
115                if args.is_empty() {
116                    kind.leaf_name().to_string()
117                } else {
118                    format!("{}<{}>", kind.leaf_name(), args_formatted)
119                }
120            }
121        }
122    }
123}
124
125impl fmt::Display for Type {
126    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
127        let (types, _generics) = Self::remove_vars(&[self]);
128        write!(f, "{}", types[0].stringify())
129    }
130}