From 2c2c78114f36a6b53ad3439fc1b2e541d73047bf Mon Sep 17 00:00:00 2001 From: Lilith Schier Date: Thu, 13 Aug 2026 12:22:41 +0200 Subject: [PATCH] added integer division --- src/dice.rs | 10 +++++++++- src/parsing.rs | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/dice.rs b/src/dice.rs index e4b2539..aef26a0 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -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, } } diff --git a/src/parsing.rs b/src/parsing.rs index ef0e51d..1a74101 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -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)),