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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
pub mod types;
pub mod value;

pub trait IRValue {
    type AssignValue: serde::Serialize + for<'a> serde::Deserialize<'a> + std::fmt::Debug + Clone;
    type VarDefineType: serde::Serialize + for<'a> serde::Deserialize<'a> + std::fmt::Debug + Clone;
    type FnDefineType: serde::Serialize + for<'a> serde::Deserialize<'a> + std::fmt::Debug + Clone;
    type ParameterType: serde::Serialize + for<'a> serde::Deserialize<'a> + std::fmt::Debug + Clone;
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub enum Item<Var: IRValue = crate::value::Value> {
    FnDefine(FnDefine<Var>),
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct Statements<Var: IRValue> {
    pub stmts: Vec<Statement<Var>>,
    pub returned: bool,
}

impl<Var: IRValue> Statements<Var> {
    pub fn new() -> Self {
        Self {
            stmts: vec![],
            returned: false,
        }
    }

    pub fn push(&mut self, stmt: impl Into<Statement<Var>>) {
        let stmt = stmt.into();
        self.returned = self.returned || stmt.returned();
        self.stmts.push(stmt);
    }
}

impl<Var: IRValue> Default for Statements<Var> {
    fn default() -> Self {
        Self::new()
    }
}

impl<Var: IRValue> From<Vec<Statement<Var>>> for Statements<Var> {
    fn from(stmts: Vec<Statement<Var>>) -> Self {
        Statements {
            returned: stmts.iter().any(|stmt| stmt.returned()),
            stmts,
        }
    }
}

/// just make code generation easier
impl<Var: IRValue> std::ops::Deref for Statements<Var> {
    type Target = Vec<Statement<Var>>;

    fn deref(&self) -> &Self::Target {
        &self.stmts
    }
}

/// rename vardefine support
impl<Var: IRValue> std::ops::DerefMut for Statements<Var> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.stmts
    }
}

pub trait ControlFlow {
    fn returned(&self) -> bool;
}

impl<Var: IRValue> ControlFlow for Statements<Var> {
    fn returned(&self) -> bool {
        self.returned
    }
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub enum Statement<Var: IRValue> {
    VarDefine(VarDefine<Var>),
    VarStore(VarStore<Var>),
    Block(Statements<Var>),
    If(If<Var>),
    While(While<Var>),
    Return(Return<Var>),
}

impl<Var: IRValue> ControlFlow for Statement<Var> {
    fn returned(&self) -> bool {
        match self {
            Statement::Block(v) => v.returned(),
            Statement::If(v) => v.returned(),
            Statement::While(v) => v.returned(),
            Statement::Return(v) => v.returned(),
            _ => false,
        }
    }
}

mod from_impls {
    use super::*;
    impl<Var: IRValue> From<FnDefine<Var>> for Item<Var> {
        fn from(v: FnDefine<Var>) -> Self {
            Self::FnDefine(v)
        }
    }

    impl<Var: IRValue> From<VarDefine<Var>> for Statement<Var> {
        fn from(v: VarDefine<Var>) -> Self {
            Self::VarDefine(v)
        }
    }

    impl<Var: IRValue> From<VarStore<Var>> for Statement<Var> {
        fn from(v: VarStore<Var>) -> Self {
            Self::VarStore(v)
        }
    }

    impl<Var: IRValue> From<Statements<Var>> for Statement<Var> {
        fn from(v: Statements<Var>) -> Self {
            Self::Block(v)
        }
    }

    impl<Var: IRValue> From<If<Var>> for Statement<Var> {
        fn from(v: If<Var>) -> Self {
            Self::If(v)
        }
    }

    impl<Var: IRValue> From<While<Var>> for Statement<Var> {
        fn from(v: While<Var>) -> Self {
            Self::While(v)
        }
    }

    impl<Var: IRValue> From<Return<Var>> for Statement<Var> {
        fn from(v: Return<Var>) -> Self {
            Self::Return(v)
        }
    }
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct Parameter<Pty> {
    #[serde(rename = "type")]
    pub ty: Pty,
    /// using string because its the name of parameter, not a value
    pub name: String,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct FnDefine<Var: IRValue> {
    #[serde(rename = "type")]
    pub ty: Var::FnDefineType,
    pub name: String,
    pub params: Vec<Parameter<Var::ParameterType>>,
    pub body: Statements<Var>,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct VarDefine<Var: IRValue> {
    /// the type of the variable
    #[serde(rename = "type")]
    pub ty: Var::VarDefineType,
    /// the name of the variable, either a named variable or a temp variable from computing
    pub name: String,
    /// the initial value of the variable
    ///
    /// [`None`] represents the variable is not initialized when it was created
    pub init: Option<Var::AssignValue>,
    /// a `temp value` will only be immediately used once in expressions
    ///
    /// this is a hint for code generation backend optimization
    ///
    /// code generation backend can directly inline `init` only when `is_temp` is true
    pub is_temp: bool,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct VarStore<Var> {
    pub name: String,
    pub val: Var,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct Condition<Var: IRValue> {
    // the final value of the condition
    pub val: Var,
    pub compute: Statements<Var>,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct IfBranch<Var: IRValue> {
    pub cond: Condition<Var>,
    pub body: Statements<Var>,
}

impl<Var: IRValue> ControlFlow for IfBranch<Var> {
    fn returned(&self) -> bool {
        self.body.returned()
    }
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct If<Var: IRValue> {
    pub branches: Vec<IfBranch<Var>>,
    #[serde(rename = "else")]
    pub else_: Option<Statements<Var>>,
}

impl<Var: IRValue> ControlFlow for If<Var> {
    fn returned(&self) -> bool {
        self.else_.as_ref().is_some_and(|else_| else_.returned())
            && self.branches.iter().all(|branch| branch.returned())
    }
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct While<Var: IRValue> {
    pub cond: Condition<Var>,
    pub body: Statements<Var>,
}

impl<Var: IRValue> ControlFlow for While<Var> {
    fn returned(&self) -> bool {
        false
    }
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct Return<Var> {
    pub val: Option<Var>,
}

impl<Var: IRValue> ControlFlow for Return<Var> {
    fn returned(&self) -> bool {
        true
    }
}

#[macro_export]
macro_rules! custom_ir_variable {
    ($vis:vis IR<$variable:ty>) => {
        $vis type Item        = $crate::Item        <$variable>;
        $vis type FnDefine    = $crate::FnDefine    <$variable>;
        $vis type Statements  = $crate::Statements  <$variable>;
        $vis type Statement   = $crate::Statement   <$variable>;
        $vis type VarDefine   = $crate::VarDefine   <$variable>;
        $vis type VarStore    = $crate::VarStore    <$variable>;
        $vis type Condition   = $crate::Condition   <$variable>;
        $vis type If          = $crate::If          <$variable>;
        $vis type IfBranch    = $crate::IfBranch    <$variable>;
        $vis type While       = $crate::While       <$variable>;
        $vis type Return      = $crate::Return      <$variable>;
        $vis type Parameter   = $crate::Parameter   <<$variable as $crate::IRValue>::ParameterType>;
    };
}