Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,10 @@ mod tests {
#[case("1 + 2 / 2 + 1", "3")]
#[case("3.12 * 2", "6.24")]
#[case("\"3.12\" + \".2\"", "\"3.12.2\"")]
#[case("22 - 18", "4")]
#[case("22 - 18 - 1", "3")]
#[case("16 / 2", "8")]
#[case("16 / 2 / 2", "4")]
#[case("true and true", "true")]
#[case("true or false", "true")]
#[case("false or false", "false")]
Expand Down
26 changes: 18 additions & 8 deletions src/transform/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,20 @@ pub struct OperatorExprTransformer {

macro_rules! impl_operator_parse {
($ident:ident, $op:ident) => {
if let Some((index, _)) = $ident
.iter()
.enumerate()
.find(|(_, pair)| matches!(pair.as_rule(), Rule::$op))
{
impl_operator_parse!($ident, $op, false)
};
($ident:ident, $op:ident, $invert:literal) => {
let found_rule = {
let matcher = |(_, pair): &(_, &Pair<Rule>)| matches!(pair.as_rule(), Rule::$op);

if $invert {
$ident.iter().enumerate().rev().find(matcher)
} else {
$ident.iter().enumerate().find(matcher)
}
};

if let Some((index, _)) = found_rule {
let mut right = $ident.split_off(index).split_off(1);

let lhs = if $ident.len() == 1 {
Expand Down Expand Up @@ -168,10 +177,11 @@ impl OperatorExprTransformer {
impl_operator_parse!(pairs, Lte);
impl_operator_parse!(pairs, Equal);
impl_operator_parse!(pairs, NotEqual);
impl_operator_parse!(pairs, Add);
impl_operator_parse!(pairs, Sub);

impl_operator_parse!(pairs, Mul);
impl_operator_parse!(pairs, Div);
impl_operator_parse!(pairs, Add);
impl_operator_parse!(pairs, Div, true);
impl_operator_parse!(pairs, Sub, true);

Err(JsltError::InvalidInput(format!(
"Could not evaluate the expession {pairs:#?}",
Expand Down
Loading