about summary refs log tree commit diff
path: root/src/interpreter/value.rs
blob: 69e4d4ffeb96439ac609ea2e78c5c46142a85612 (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::convert::TryFrom;
use std::fmt::{self, Display};
use std::ops::{Add, Div, Mul, Neg, Sub};
use std::rc::Rc;

use derive_more::{Deref, From, TryInto};

use super::{Error, Result};
use crate::ast::Type;

#[derive(Debug, PartialEq, From, TryInto)]
#[try_into(owned, ref)]
pub enum Val {
    Int(i64),
    Float(f64),
    Bool(bool),
}

impl From<u64> for Val {
    fn from(i: u64) -> Self {
        Self::from(i as i64)
    }
}

impl Display for Val {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Val::Int(x) => x.fmt(f),
            Val::Float(x) => x.fmt(f),
            Val::Bool(x) => x.fmt(f),
        }
    }
}

impl Val {
    pub fn type_(&self) -> Type {
        match self {
            Val::Int(_) => Type::Int,
            Val::Float(_) => Type::Float,
            Val::Bool(_) => Type::Bool,
        }
    }

    pub fn into_type<'a, T>(&'a self) -> Result<&'a T>
    where
        T: TypeOf + 'a + Clone,
        &'a T: TryFrom<&'a Self>,
    {
        <&T>::try_from(self).map_err(|_| Error::InvalidType {
            actual: self.type_(),
            expected: <T as TypeOf>::type_of(),
        })
    }
}

#[derive(Debug, PartialEq, Clone, Deref)]
pub struct Value(Rc<Val>);

impl Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<T> From<T> for Value
where
    Val: From<T>,
{
    fn from(x: T) -> Self {
        Self(Rc::new(x.into()))
    }
}

impl Neg for Value {
    type Output = Result<Value>;

    fn neg(self) -> Self::Output {
        Ok((-self.into_type::<i64>()?).into())
    }
}

impl Add for Value {
    type Output = Result<Value>;

    fn add(self, rhs: Self) -> Self::Output {
        Ok((self.into_type::<i64>()? + rhs.into_type::<i64>()?).into())
    }
}

impl Sub for Value {
    type Output = Result<Value>;

    fn sub(self, rhs: Self) -> Self::Output {
        Ok((self.into_type::<i64>()? - rhs.into_type::<i64>()?).into())
    }
}

impl Mul for Value {
    type Output = Result<Value>;

    fn mul(self, rhs: Self) -> Self::Output {
        Ok((self.into_type::<i64>()? * rhs.into_type::<i64>()?).into())
    }
}

impl Div for Value {
    type Output = Result<Value>;

    fn div(self, rhs: Self) -> Self::Output {
        Ok((self.into_type::<f64>()? / rhs.into_type::<f64>()?).into())
    }
}

pub trait TypeOf {
    fn type_of() -> Type;
}

impl TypeOf for i64 {
    fn type_of() -> Type {
        Type::Int
    }
}

impl TypeOf for bool {
    fn type_of() -> Type {
        Type::Bool
    }
}

impl TypeOf for f64 {
    fn type_of() -> Type {
        Type::Float
    }
}