1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (C) 2024 Ethan Uppal. All rights reserved.
use lazy_static::lazy_static;
use pulsar_utils::{id::Id, mutcell::MutCell};
use std::{fmt::Display, hash::Hash};

lazy_static! {
    pub static ref UNIT_TYPE_CELL: TypeCell = TypeCell::new(Type::Unit);
    pub static ref INT64_TYPE_CELL: TypeCell = TypeCell::new(Type::Int64);
}

pub const ARRAY_TYPE_UNKNOWN_SIZE: isize = -1;

#[derive(Clone, Hash, PartialEq, Eq, Debug)]
pub enum Type {
    Unknown,
    Unit,
    Var(Id),
    Name(String),
    Int64,

    /// A negative size indicates that the size is not yet known.
    Array(TypeCell, isize),

    Function {
        is_pure: bool,
        args: Vec<Type>,
        ret: Box<Type>
    }
}

impl Type {
    pub fn unwrap(self) -> Self {
        match self {
            Self::Unknown => panic!("Type::unwrap failed"),
            other => other
        }
    }

    pub fn is_known(self) -> bool {
        match self {
            Self::Unknown => false,
            _ => true
        }
    }

    pub fn make_unknown() -> TypeCell {
        TypeCell::new(Self::Unknown)
    }

    pub fn int64_singleton() -> TypeCell {
        INT64_TYPE_CELL.to_owned()
    }

    pub fn unit_singleton() -> TypeCell {
        UNIT_TYPE_CELL.to_owned()
    }

    /// The number of bytes to store one instance of a value of the current
    /// type.
    pub fn size(&self) -> usize {
        match &self {
            Type::Unknown => panic!("Type::Unknown does not have a size"),
            Type::Unit => 0,
            Type::Var(_) => {
                panic!("Type::Var should have been resolved by type inference")
            }
            Type::Name(_) => todo!("Need to figure out user-defined types"),
            Type::Int64 => 8,
            Type::Array(element_type, element_count) => {
                element_type.as_ref().size() * (*element_count as usize)
            }
            Type::Function {
                is_pure: _,
                args: _,
                ret: _
            } => 8
        }
    }

    pub fn as_array_type(&self) -> (TypeCell, isize) {
        match &self {
            Self::Array(element_type, size) => (element_type.clone(), *size),
            _ => panic!(
                "{}",
                format!(
                    "Type::as_array_type called on non-array type `{}`",
                    &self
                )
            )
        }
    }

    pub fn mangle(&self) -> String {
        match &self {
            Type::Unknown | Type::Var(_) => panic!(),
            Type::Unit => "u".into(),
            Type::Name(name) => format!("{}{}", name.len(), name),
            Type::Int64 => "q".into(),
            Type::Array(element_type, count) => {
                format!("A{}{}", count, element_type)
            }
            Type::Function {
                is_pure: _,
                args: _,
                ret: _
            } => todo!()
        }
    }
}

impl Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unknown => write!(f, "?"),
            Self::Unit => write!(f, "Unit"),
            Self::Var(var) => write!(f, "'t{}", var),
            Self::Name(name) => write!(f, "{}", name),
            Self::Int64 => write!(f, "Int64"),
            Self::Array(tcell, size) => write!(
                f,
                "{}[{}]",
                tcell,
                if *size == ARRAY_TYPE_UNKNOWN_SIZE {
                    "?".into()
                } else {
                    size.to_string()
                }
            ),
            Self::Function { is_pure, args, ret } => write!(
                f,
                "{}({}) -> {}",
                if *is_pure { "pure " } else { "" },
                args.iter()
                    .map(|ty| ty.to_string())
                    .collect::<Vec<_>>()
                    .join(", "),
                ret,
            )
        }
    }
}

#[derive(Clone, PartialEq, Eq)]
pub enum StmtTermination {
    Terminal,
    Nonterminal
}

impl Display for StmtTermination {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Terminal => "Terminal",
            Self::Nonterminal => "Nonterminal"
        }
        .fmt(f)
    }
}

#[derive(Clone, PartialEq, Eq)]
pub struct StmtType {
    pub termination: StmtTermination,
    pub is_pure: bool,
    is_unknown: bool
}

impl StmtType {
    pub fn from(termination: StmtTermination, is_pure: bool) -> StmtType {
        StmtType {
            termination,
            is_pure,
            is_unknown: false
        }
    }

    pub fn make_unknown() -> StmtTypeCell {
        StmtTypeCell::new(StmtType {
            termination: StmtTermination::Nonterminal,
            is_pure: false,
            is_unknown: true
        })
    }
}

impl Display for StmtType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", if self.is_pure { "Pure" } else { "Impure" })?;
        write!(f, "{}", self.termination)
    }
}

pub type TypeCell = MutCell<Type>;
pub type StmtTypeCell = MutCell<StmtType>;