ergotree_ir/
type_check.rs1use crate::mir::expr::Expr;
4use crate::source_span::Spanned;
5
6#[derive(Debug, PartialEq, Eq)]
8pub struct TypeCheckError {
9 msg: String,
10}
11
12impl TypeCheckError {
13 pub fn new(msg: String) -> Self {
15 Self { msg }
16 }
17
18 pub fn pretty_desc(&self) -> String {
20 self.msg.clone()
21 }
22}
23
24pub fn type_check(e: Expr) -> Result<Expr, TypeCheckError> {
26 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}