about summary refs log tree commit diff
path: root/src/interpret/walker.rs
blob: 1602cd2d4f99ff28a111070ddf7f75f47f1a9b58 (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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::{
    fmt::Display,
    ops::{Add, Div, Mul, Neg, Sub},
};

use crate::{lex::token::TokenVariant::*, parse::ast::Expression};
use anyhow::Result;

// No state for now.
pub struct Walker {}

impl Walker {
    pub fn new() -> Self {
        Walker {}
    }

    pub fn walk(&self, node: &Expression) -> Result<WalkValue> {
        match node {
            Expression::Binary { left, op, right } => {
                let left_value = self.walk(left)?;
                let right_value = self.walk(right)?;

                let new_value = match op.variant {
                    OpPlus => left_value + right_value,
                    OpMinus => left_value - right_value,
                    OpStar => left_value * right_value,
                    OpSlash => left_value / right_value,
                    _ => unreachable!(),
                };

                Ok(new_value)
            }
            Expression::Unary { op, right } => {
                let value = self.walk(right)?;

                let new_value = match op.variant {
                    OpPlus => value,
                    OpMinus => -value,
                    OpNot => todo!("Implement boolean arithmetic."),
                    _ => unreachable!(),
                };

                Ok(new_value)
            }
            Expression::Group(node) => self.walk(node),
            Expression::Literal(token) => {
                let value = match token.variant {
                    Int(int) => WalkValue::Int(int as i64),
                    Float(float) => WalkValue::Float(float as f64),
                    _ => unreachable!(),
                };

                Ok(value)
            }
        }
    }
}

pub enum WalkValue {
    Float(f64),
    Int(i64),
}

impl Add for WalkValue {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        match self {
            Self::Float(float) => match rhs {
                Self::Float(other_float) => Self::Float(float + other_float),
                Self::Int(other_int) => Self::Float(float + other_int as f64),
            },
            Self::Int(int) => match rhs {
                Self::Float(other_float) => Self::Float(int as f64 + other_float),
                Self::Int(other_int) => Self::Int(int + other_int),
            },
        }
    }
}

impl Sub for WalkValue {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        match self {
            Self::Float(float) => match rhs {
                Self::Float(other_float) => Self::Float(float - other_float),
                Self::Int(other_int) => Self::Float(float - other_int as f64),
            },
            Self::Int(int) => match rhs {
                Self::Float(other_float) => Self::Float(int as f64 - other_float),
                Self::Int(other_int) => Self::Int(int - other_int),
            },
        }
    }
}

impl Mul for WalkValue {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        match self {
            Self::Float(float) => match rhs {
                Self::Float(other_float) => Self::Float(float * other_float),
                Self::Int(other_int) => Self::Float(float * other_int as f64),
            },
            Self::Int(int) => match rhs {
                Self::Float(other_float) => Self::Float(int as f64 * other_float),
                Self::Int(other_int) => Self::Int(int * other_int),
            },
        }
    }
}

impl Div for WalkValue {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        match self {
            Self::Float(float) => match rhs {
                Self::Float(other_float) => Self::Float(float / other_float),
                Self::Int(other_int) => Self::Float(float / other_int as f64),
            },
            Self::Int(int) => match rhs {
                Self::Float(other_float) => Self::Float(int as f64 / other_float),
                Self::Int(other_int) => Self::Int(int / other_int),
            },
        }
    }
}

impl Neg for WalkValue {
    type Output = Self;

    fn neg(self) -> Self::Output {
        match self {
            Self::Float(float) => Self::Float(-float),
            Self::Int(int) => Self::Int(-int),
        }
    }
}

impl Display for WalkValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Float(float) => write!(f, "{}", float),
            Self::Int(int) => write!(f, "{}", int),
        }
    }
}