added integer division

This commit is contained in:
Lilith Schier 2026-08-13 12:22:41 +02:00
parent 2ed3a82b79
commit 2c2c78114f
2 changed files with 11 additions and 1 deletions

View file

@ -16,6 +16,7 @@ pub enum Expression {
Sub(ExpBox, ExpBox),
Mul(ExpBox, ExpBox),
Div(ExpBox, ExpBox),
IntDiv(ExpBox, ExpBox),
}
#[derive(Debug, PartialEq, Clone)]
@ -246,6 +247,12 @@ impl Expression {
let rhs = rhs_node.evaluate(rng, w, precedence)?;
lhs / rhs
}
IntDiv(lhs_node, rhs_node) => {
let lhs = lhs_node.evaluate(rng, w, precedence)?;
write!(w, " // ")?;
let rhs = rhs_node.evaluate(rng, w, precedence)?;
lhs.div_euclid(rhs)
}
};
if needs_parens {
write!(w, ")")?;
@ -306,6 +313,7 @@ impl Expression {
Expression::Sub(lhs, rhs) => lhs.avg()? - rhs.avg()?,
Expression::Mul(lhs, rhs) => lhs.avg()? * rhs.avg()?,
Expression::Div(lhs, rhs) => lhs.avg()? / rhs.avg()?,
Expression::IntDiv(lhs, rhs) => lhs.avg()?.div_euclid(rhs.avg()?),
};
Ok(result)
}
@ -358,7 +366,7 @@ impl Expression {
Const(_) => 1,
Dice { .. } => 2,
Neg(_) => 5,
Mul(_, _) | Div(_, _) => 7,
Mul(_, _) | Div(_, _) | IntDiv(_, _) => 7,
Add(_, _) | Sub(_, _) => 8,
}
}

View file

@ -90,6 +90,7 @@ fn expr<'c, 'i>(
fail(),
complete(alt((
binary_op(7, Left, spaced_op("*")),
binary_op(7, Left, spaced_op("//")),
binary_op(7, Left, spaced_op("/")),
binary_op(8, Left, spaced_op("+")),
binary_op(8, Left, spaced_op("-")),
@ -101,6 +102,7 @@ fn expr<'c, 'i>(
Ok(match op {
Prefix("-", x) => Neg(Box::new(x)),
Binary(lhs, "*", rhs) => Mul(Box::new(lhs), Box::new(rhs)),
Binary(lhs, "//", rhs) => IntDiv(Box::new(lhs), Box::new(rhs)),
Binary(lhs, "/", rhs) => Div(Box::new(lhs), Box::new(rhs)),
Binary(lhs, "+", rhs) => Add(Box::new(lhs), Box::new(rhs)),
Binary(lhs, "-", rhs) => Sub(Box::new(lhs), Box::new(rhs)),