ergotree_ir/
type_check.rs

1//! Type checking
2
3use crate::mir::expr::Expr;
4use crate::source_span::Spanned;
5
6/// Typecheck error
7#[derive(Debug, PartialEq, Eq)]
8pub struct TypeCheckError {
9    msg: String,
10}
11
12impl TypeCheckError {
13    /// Create new
14    pub fn new(msg: String) -> Self {
15        Self { msg }
16    }
17
18    /// Get error description
19    pub fn pretty_desc(&self) -> String {
20        self.msg.clone()
21    }
22}
23
24/// Type checks the given expression
25pub fn type_check(e: Expr) -> Result<Expr, TypeCheckError> {
26    // not really a relevant check, since such kind of check should be in BinOp::new()
27    match &e {
28        Expr::BinOp(Spanned {
29            source_span: _,
30            expr: bin,
31        }) => {
32            if bin.left.tpe() == bin.right.tpe() {
33                Ok(e)
34            } else {
35                Err(TypeCheckError::new(format!(
36                    "Type check error: binary op operands types do not match: {0:?}",
37                    bin
38                )))
39            }
40        }
41        _ => Ok(e),
42    }
43}