Skip to content

Commit

Permalink
feat: extend more grammar (#3)
Browse files Browse the repository at this point in the history
* feat: support procedural roll string

* chore: bump version

* feat: support `+|-` prefix in roll string paring

* feat: support for some match functions

* fix(lint): fix code lint
  • Loading branch information
fu050409 authored Oct 29, 2024
1 parent 26c853b commit 3262824
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changes/math.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"noctisroll": patch:feat
---

Support for some math functions in roll string.

For example, `max(1, 2)` will return `2`.
5 changes: 5 additions & 0 deletions .changes/roll-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"noctisroll": patch:feat
---

Support for procedural roll string.
5 changes: 5 additions & 0 deletions .changes/sign.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"noctisroll": patch:feat
---

Support for `+|-` prefix in roll string parsing.
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ mod tests {
#[test]
fn test_roll_inline() {
assert_eq!(roll_inline("6d1").unwrap(), 6);
assert_eq!(roll_inline("-6d1").unwrap(), -6);
assert_eq!(roll_inline("+6d1").unwrap(), 6);
assert_eq!(roll_inline("3d1+3d1").unwrap(), 6);
}

Expand All @@ -95,4 +97,14 @@ mod tests {

assert_eq!(roll("3d1+3d1").unwrap().1, "[1, 1, 1] + [1, 1, 1]");
}

#[test]
fn test_math() {
assert_eq!(roll_inline("min(6d1, 3d1)").unwrap(), 3);
assert_eq!(roll_inline("max(6d1, 3d1)").unwrap(), 6);
assert_eq!(roll_inline("abs(-6d1)").unwrap(), 6);

// TODO: support high precision math functions
// assert_eq!(roll_inline("cos(6d1)").unwrap(), -0.5403023058681398);
}
}
51 changes: 48 additions & 3 deletions src/roll.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,56 @@ Factor: (i32, String) = {
<l:Factor> "^" <r:Term> => (l.0.pow(r.0 as u32), format!("{} ^ {}", l.1, r.1)),
<l:Factor> "**" <r:Term> => (l.0.pow(r.0 as u32), format!("{} ** {}", l.1, r.1)),
<l:Factor> "%" <r:Term> => (l.0 % r.0, format!("{} % {}", l.1, r.1)),
"+" <Term> => (<>.0, format!("+{}", <>.1)),
"-" <Term> => (-<>.0, format!("-{}", <>.1)),
Term,
};

Term: (i32, String) = {
Dice,
Num,
BasicDice,
"(" <Expr> ")",
"abs(" <Expr> ")" => {
(<>.0.abs(), format!("abs({})", <>.1))
},
"cos(" <Expr> ")" => {
let x = <>.0 as f64;
let result = x.cos();
(result as i32, format!("cos({})", <>.1))
},
"exp(" <Expr> ")" => {
let x = <>.0 as f64;
let result = x.exp();
(result as i32, format!("exp({})", <>.1))
},
"floor(" <Expr> ")" => {
let x = <>.0 as f64;
let result = x.floor();
(result as i32, format!("floor({})", <>.1))
},
"max(" <l:Expr> "," <r:Expr> ")" => {
(l.0.max(r.0), format!("max({}, {})", l.1, r.1))
},
"min(" <l:Expr> "," <r:Expr> ")" => {
(l.0.min(r.0), format!("min({}, {})", l.1, r.1))
},
"sin(" <Expr> ")" => {
let x = <>.0 as f64;
let result = x.sin();
(result as i32, format!("sin({})", <>.1))
},
"sqrt(" <Expr> ")" => {
let x = <>.0 as f64;
let result = x.sqrt();
(result as i32, format!("sqrt({})", <>.1))
},
"tan(" <Expr> ")" => {
let x = <>.0 as f64;
let result = x.tan();
(result as i32, format!("tan({})", <>.1))
},
};

Dice: (i32, String) = {
BasicDice: (i32, String) = {
<l:Num> "d" <r:Num> => {
let dice = Dice::new(l.0 as usize, r.0).roll();
(dice.result, dice.roll_str())
Expand All @@ -35,6 +75,11 @@ Dice: (i32, String) = {
let dice = Dice::new(1, r.0).roll();
(dice.result, dice.roll_str())
},
"d" => {
let dice = Dice::new(1, 100).roll();
return (dice.result, dice.roll_str());
},
Num,
}

Num: (i32, String) = {
Expand Down

0 comments on commit 3262824

Please sign in to comment.