Skip to content

Commit

Permalink
Merge pull request #1 from vic1707/pemdas/pejmdas
Browse files Browse the repository at this point in the history
Pemdas/pejmdas
  • Loading branch information
vic1707 authored Sep 29, 2023
2 parents 9685311 + 582e32f commit 39e1e82
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/fmt&lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ jobs:
- uses: actions/checkout@v3
- name: Install Rust
uses: IronCoreLabs/rust-toolchain@v1
- name: Rust clippy
run: cargo clippy --all --all-targets
- name: Rust clippy (default features, currently `pemdas`)
run: cargo clippy
- name: Rust clippy (`pejmdas` feature)
run: cargo clippy --no-default-features --features pejmdas
6 changes: 4 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ jobs:
- uses: actions/checkout@v3
- name: Install Rust
uses: IronCoreLabs/rust-toolchain@v1
- name: Run tests
run: cargo test --workspace
- name: Run tests (default features, currently `pemdas`)
run: cargo test
- name: Run tests (`pejmdas` feature)
run: cargo test --no-default-features --features pejmdas
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ thiserror = "1.0.48"
[[bin]]
name = "repl"
path = "src/bin/repl.rs"

[features]
default = ["pemdas"]
pemdas = []
pejmdas = []
9 changes: 8 additions & 1 deletion src/element/binop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ impl<'a> BinOp<'a> {
Self { op, lhs, rhs }
}

#[cfg(feature = "pejmdas")]
pub const IMPLICIT_MULTIPLICATION_PRECEDENCE: usize = 3;
#[cfg(feature = "pemdas")]
pub const IMPLICIT_MULTIPLICATION_PRECEDENCE: usize =
Self::precedence(&Operator::Times);

pub const fn precedence(op: &Operator) -> usize {
match *op {
Operator::Plus | Operator::Minus => 1,
Operator::Times | Operator::Divide | Operator::Modulo => 2,
Operator::Power => 3,
// uses `4` because `pejmdas` feature uses `3`
Operator::Power => 4,
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ mod token;
mod tests;
/* Exports */
pub use parser::{Error, ErrorKind, Parser};

/* Feature safety */
#[cfg(all(feature = "pemdas", feature = "pejmdas"))]
compile_error!(
"You can't enable both features `pemdas` and `pejmdas` at the same time."
);
#[cfg(not(any(feature = "pemdas", feature = "pejmdas")))]
compile_error!("You must enable either feature `pemdas` or `pejmdas`.");
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl ParserImpl<'_> {

#[allow(clippy::items_after_statements)]
const TIMES_INFOS: (Operator, usize) =
(Operator::Times, BinOp::precedence(&Operator::Times));
(Operator::Times, BinOp::IMPLICIT_MULTIPLICATION_PRECEDENCE);
match current_byte {
// if multiplication precedence is lower than current precedence
// we now we don't need implicit multiplication
Expand Down
19 changes: 18 additions & 1 deletion src/tests/parser/implicit_multiplication.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
/* Crate imports */
use crate::Parser;

const IMPLICIT_MULTIPLICATIONS: [(&str, &str); 8] = [
#[cfg(feature = "pemdas")]
const IMPLICIT_MULTIPLICATIONS: [(&str, &str); 9] = [
("2(3)", "2*(3)"),
("2(3+4)", "2*(3+4)"),
("2 (3+4)5", "2*(3+4)*5"),
("2 (3+4) 5", "2*(3+4)*5"),
("2(3+4)(5+6)", "2*(3+4)*(5+6)"),
("2x + 3 x y", "2*x + 3*x*y"),
("12 + 3-1x+3y x", "12 + 3-1*x+3*y*x"),
// These ones are different for the `pejmdas` feature
("6/2(2+1)", "6/2*(2+1)"),
("1/2x", "(1/2)*x"),
];

#[cfg(feature = "pejmdas")]
const IMPLICIT_MULTIPLICATIONS: [(&str, &str); 9] = [
("2(3)", "2*(3)"),
("2(3+4)", "2*(3+4)"),
("2 (3+4)5", "2*(3+4)*5"),
("2 (3+4) 5", "2*(3+4)*5"),
("2(3+4)(5+6)", "2*(3+4)*(5+6)"),
("2x + 3 x y", "2*x + 3*x*y"),
("12 + 3-1x+3y x", "12 + 3-1*x+3*y*x"),
// These ones are different for the `pemdas` feature
("6/2(2+1)", "6/(2*(2+1))"),
("1/2x", "1/(2*x)"),
];

#[test]
Expand Down

0 comments on commit 39e1e82

Please sign in to comment.