summaryrefslogtreecommitdiff
path: root/src/lib/eval/arith.rs
blob: fe3a06b7d38d4ea7ff83c2c4a13e9b96b8e1ccee (plain)
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
use std::ops::{Add, Sub, Mul, Div};
use super::super::types::Type;
use super::super::types::Number;

fn apply_arithmetic(
    func_i: fn(isize, isize) -> isize,
    func_f: fn(f32, f32) -> f32,
    operand_a: &Type,
    operand_b: &Type) -> Result<Type, String> {

    match (operand_a, operand_b) {
        (Type::Bool(_), _) | (_, Type::Bool(_)) |
        (Type::Str(_), _) | (_, Type::Str(_)) |
        (Type::Operator(_), _) | (_, Type::Operator(_)) => {
            Err("Incompatible types".to_string())
        },

        (Type::Number(Number::Int(i)), Type::Number(Number::Float(f))) |
        (Type::Number(Number::Float(f)), Type::Number(Number::Int(i))) => {
            Ok(Type::Number(Number::Float(func_f(*f, *i as f32))))
        },

        (Type::Number(Number::Int(a)), Type::Number(Number::Int(b))) => {
            Ok(Type::Number(Number::Int(func_i(*a, *b))))
        },

        (Type::Number(Number::Float(a)), Type::Number(Number::Float(b))) => {
            Ok(Type::Number(Number::Float(func_f(*a, *b))))
        },

        (Type::Symbol(_), Type::Number(_)) |
        (Type::Number(_), Type::Symbol(_)) |
        (Type::Symbol(_), Type::Symbol(_)) => Err("Not yet implemented".to_string())
    }
}

impl Add for Type {
    type Output = Result<Type, String>;

    fn add(self, other: Type) -> Result<Type, String> {
        apply_arithmetic(|a, b| a + b, |a, b| a + b, &self, &other)
    }
}

impl Sub for Type {
    type Output = Result<Type, String>;

    fn sub(self, other: Type) -> Result<Type, String> {
        apply_arithmetic(|a, b| a - b, |a, b| a - b, &self, &other)
    }
}

impl Mul for Type {
    type Output = Result<Type, String>;

    fn mul(self, other: Type) -> Result<Type, String> {
        apply_arithmetic(|a, b| a * b, |a, b| a * b, &self, &other)
    }
}

impl Div for Type {
    type Output = Result<Type, String>;

    fn div(self, other: Type) -> Result<Type, String> {
        apply_arithmetic(|a, b| a / b, |a, b| a / b, &self, &other)
    }
}