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
use crate::core::Function;
use crate::lang::parser::{Located, SourceLocation};
use crate::runtime::TypeName;
use serde::{Serialize, Serializer};
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::sync::Arc;

pub mod builder;
pub mod hir;
pub mod lir;
pub mod mir;
pub mod parser;

#[derive(Debug, Clone, Serialize)]
pub enum SyntacticSugar {
    None,
    And,
    Or,
    Refine,
    Traverse,
    Chain,
    Not,
}

impl From<TypeName> for SyntacticSugar {
    fn from(name: TypeName) -> Self {
        match name.as_type_str().as_str() {
            "lang::And" => Self::And,
            "lang::Or" => Self::Or,
            "lang::Refine" => Self::Refine,
            "lang::Traverse" => Self::Traverse,
            "lang::Chain" => Self::Chain,
            "lang::Not" => Self::Not,
            _ => Self::None,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub enum PrimordialType {
    Integer,
    Decimal,
    Boolean,
    String,
    Function(SyntacticSugar, TypeName, #[serde(skip)] Arc<dyn Function>),
}

impl Hash for PrimordialType {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            PrimordialType::Integer => "integer".hash(state),
            PrimordialType::Decimal => "decimal".hash(state),
            PrimordialType::Boolean => "boolean".hash(state),
            PrimordialType::String => "string".hash(state),
            PrimordialType::Function(_, name, _) => name.hash(state),
        }
    }
}

impl PartialEq for PrimordialType {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Integer, Self::Integer) => true,
            (Self::Decimal, Self::Decimal) => true,
            (Self::Boolean, Self::Boolean) => true,
            (Self::String, Self::String) => true,
            (Self::Function(_, lhs, _), Self::Function(_, rhs, _)) => lhs.eq(rhs),
            _ => false,
        }
    }
}