Skip to main content

prune_lang/utils/
prim.rs

1use super::lit::LitType;
2
3#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
4pub enum Compare {
5    Lt,
6    Le,
7    Eq,
8    Ge,
9    Gt,
10    Ne,
11}
12
13#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
14pub enum Prim {
15    /// integer arithmetics
16    IAdd,
17    ISub,
18    IMul,
19    IDiv,
20    IRem,
21    INeg,
22
23    /// float-point arithmetics
24    // FAdd,
25    // FSub,
26    // FMul,
27    // FDiv,
28    // FNeg,
29
30    /// comparision
31    ICmp(Compare),
32
33    /// boolean operation
34    BAnd,
35    BOr,
36    BNot,
37}
38
39impl Prim {
40    pub fn get_typ(&self) -> Vec<LitType> {
41        match self {
42            Prim::IAdd | Prim::ISub | Prim::IMul | Prim::IDiv | Prim::IRem => {
43                vec![LitType::TyInt, LitType::TyInt, LitType::TyInt]
44            }
45            Prim::INeg => {
46                vec![LitType::TyInt, LitType::TyInt]
47            }
48            Prim::ICmp(_) => {
49                vec![LitType::TyInt, LitType::TyInt, LitType::TyBool]
50            }
51            Prim::BAnd | Prim::BOr => {
52                vec![LitType::TyBool, LitType::TyBool, LitType::TyBool]
53            }
54            Prim::BNot => {
55                vec![LitType::TyBool, LitType::TyBool]
56            }
57        }
58    }
59
60    pub fn get_prior(&self) -> u8 {
61        match self {
62            Prim::IAdd => 3,
63            Prim::ISub => 3,
64            Prim::IMul => 4,
65            Prim::IDiv => 4,
66            Prim::IRem => 4,
67            Prim::INeg => 0,
68            Prim::BAnd => 1,
69            Prim::BOr => 1,
70            Prim::BNot => 0,
71            Prim::ICmp(_) => 2,
72        }
73    }
74}