44 lines
1.1 KiB
Rust
44 lines
1.1 KiB
Rust
use std::ops::{Add, Mul, Neg, Not, Sub};
|
|
|
|
use crate::binary_repr::BinaryRepr;
|
|
|
|
pub enum BinOp {
|
|
Plus,
|
|
Sub,
|
|
Mul,
|
|
Div,
|
|
}
|
|
|
|
pub enum UnaryOp {
|
|
Not,
|
|
Neg,
|
|
}
|
|
|
|
pub enum Expr {
|
|
Constant(BinaryRepr),
|
|
BinaryOp(Box<Self>, BinOp, Box<Self>),
|
|
UnaryOp(UnaryOp, Box<Self>),
|
|
Group(Box<Self>),
|
|
}
|
|
|
|
impl Expr {
|
|
pub fn into_binary_repr(self) -> BinaryRepr {
|
|
match self {
|
|
Self::Constant(binary_repr) => binary_repr,
|
|
Self::BinaryOp(expr, bin_op, expr1) => {
|
|
(match bin_op {
|
|
BinOp::Plus => <BinaryRepr as Add>::add,
|
|
BinOp::Sub => <BinaryRepr as Sub>::sub,
|
|
BinOp::Mul => <BinaryRepr as Mul>::mul,
|
|
BinOp::Div => todo!(),
|
|
})(expr.into_binary_repr(), expr1.into_binary_repr())
|
|
}
|
|
Self::UnaryOp(unary_op, expr) => (match unary_op {
|
|
UnaryOp::Neg => <BinaryRepr as Neg>::neg,
|
|
UnaryOp::Not => <BinaryRepr as Not>::not,
|
|
})(expr.into_binary_repr()),
|
|
Self::Group(group) => group.into_binary_repr(),
|
|
}
|
|
}
|
|
}
|