Skip to content

Commit

Permalink
Merge pull request #36 from JayXon/more_operators
Browse files Browse the repository at this point in the history
More operators
  • Loading branch information
JayXon authored Jan 6, 2024
2 parents 7a28079 + 8a2d9fb commit fcbc942
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn find_binary_expressions_left(
k: usize,
er: &Expr,
) {
seq!(op_len in 1..=3 {
seq!(op_len in 1..=5 {
if n <= k + op_len {
return;
};
Expand All @@ -175,7 +175,7 @@ fn find_binary_expressions_right(
k: usize,
el: &Expr,
) {
seq!(op_len in 1..=3 {
seq!(op_len in 1..=5 {
if n <= k + op_len {
return;
};
Expand Down
96 changes: 74 additions & 22 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ impl BinaryOp {
pub fn apply_or(l: Num, r: Num) -> Option<Num> {
Some(if l != 0 { l } else { r })
}
pub fn apply_or_logical(l: Num, r: Num) -> Option<Num> {
Some((l != 0 || r != 0) as Num)
}
pub fn apply_and(l: Num, r: Num) -> Option<Num> {
Some(if l != 0 { r } else { l })
}
pub fn apply_and_logical(l: Num, r: Num) -> Option<Num> {
Some((l != 0 && r != 0) as Num)
}
pub fn apply_lt(l: Num, r: Num) -> Option<Num> {
Some((l < r) as Num)
}
Expand Down Expand Up @@ -206,6 +215,9 @@ pub fn apply_bit_neg(x: Num) -> Num {
pub fn apply_neg(x: Num) -> Num {
0 - x
}
pub fn apply_not(x: Num) -> Num {
(x == 0) as Num
}

#[inline(always)]
pub fn can_apply_unary_always(_: &Expr) -> bool {
Expand All @@ -216,48 +228,87 @@ pub fn can_apply_binary_always(_: &Expr, _: &Expr) -> bool {
true
}
#[inline(always)]
pub fn can_apply_or(el: &Expr, er: &Expr) -> bool {
pub fn can_apply_keyword(el: &Expr, er: &Expr) -> bool {
ok_before_keyword(el) && ok_after_keyword(er)
}
#[inline(always)]
pub fn can_apply_space_or(el: &Expr, er: &Expr) -> bool {
pub fn can_apply_space_keyword(el: &Expr, er: &Expr) -> bool {
!ok_before_keyword(el) && ok_after_keyword(er)
}
#[inline(always)]
pub fn can_apply_or_space(el: &Expr, er: &Expr) -> bool {
pub fn can_apply_keyword_space(el: &Expr, er: &Expr) -> bool {
ok_before_keyword(el) && !ok_after_keyword(er)
}
#[inline(always)]
pub fn can_apply_space_or_space(el: &Expr, er: &Expr) -> bool {
pub fn can_apply_space_keyword_space(el: &Expr, er: &Expr) -> bool {
!ok_before_keyword(el) && !ok_after_keyword(er)
}

pub const OP_OR: BinaryOp = BinaryOp {
name: "or",
prec: 3,
apply: apply_or,
can_apply: can_apply_or,
can_apply: can_apply_keyword,
..BinaryOp::EMPTY
};
pub const OP_SPACE_OR: BinaryOp = BinaryOp {
name: " or",
prec: 3,
apply: apply_or,
can_apply: can_apply_space_or,
..BinaryOp::EMPTY
can_apply: can_apply_space_keyword,
..OP_OR
};
pub const OP_OR_SPACE: BinaryOp = BinaryOp {
name: "or ",
prec: 3,
apply: apply_or,
can_apply: can_apply_or_space,
..BinaryOp::EMPTY
can_apply: can_apply_keyword_space,
..OP_OR
};
pub const OP_SPACE_OR_SPACE: BinaryOp = BinaryOp {
name: " or ",
can_apply: can_apply_space_keyword_space,
..OP_OR
};
pub const OP_OR_SYMBOL: BinaryOp = BinaryOp {
name: "||",
can_apply: can_apply_binary_always,
..OP_OR
};
pub const OP_OR_LOGICAL: BinaryOp = BinaryOp {
name: "||",
prec: 3,
apply: apply_or,
can_apply: can_apply_space_or_space,
apply: apply_or_logical,
commutative: true,
..BinaryOp::EMPTY
};
pub const OP_AND: BinaryOp = BinaryOp {
name: "and",
prec: 4,
apply: apply_and,
..BinaryOp::EMPTY
};
pub const OP_SPACE_AND: BinaryOp = BinaryOp {
name: " and",
can_apply: can_apply_space_keyword,
..OP_AND
};
pub const OP_AND_SPACE: BinaryOp = BinaryOp {
name: "and ",
can_apply: can_apply_keyword_space,
..OP_AND
};
pub const OP_SPACE_AND_SPACE: BinaryOp = BinaryOp {
name: " and ",
can_apply: can_apply_space_keyword_space,
..OP_AND
};
pub const OP_AND_SYMBOL: BinaryOp = BinaryOp {
name: "&&",
can_apply: can_apply_binary_always,
..OP_AND
};
pub const OP_AND_LOGICAL: BinaryOp = BinaryOp {
name: "&&",
prec: 4,
apply: apply_and_logical,
commutative: true,
..BinaryOp::EMPTY
};
pub const OP_LT: BinaryOp = BinaryOp {
Expand Down Expand Up @@ -323,14 +374,11 @@ pub const OP_BIT_SHL: BinaryOp = BinaryOp {
name: "<<",
prec: 9,
apply: apply_bit_shl,
commutative: false,
..BinaryOp::EMPTY
};
pub const OP_BIT_SHL_WRAP: BinaryOp = BinaryOp {
name: "<<",
prec: 9,
apply: apply_bit_shl_wrap,
..BinaryOp::EMPTY
..OP_BIT_SHL
};
pub const OP_BIT_SHR: BinaryOp = BinaryOp {
name: ">>",
Expand All @@ -339,10 +387,8 @@ pub const OP_BIT_SHR: BinaryOp = BinaryOp {
..BinaryOp::EMPTY
};
pub const OP_BIT_SHR_WRAP: BinaryOp = BinaryOp {
name: ">>",
prec: 9,
apply: apply_bit_shr_wrap,
..BinaryOp::EMPTY
..OP_BIT_SHR
};
pub const OP_ADD: BinaryOp = BinaryOp {
name: "+",
Expand Down Expand Up @@ -414,6 +460,12 @@ pub const OP_NEG: UnaryOp = UnaryOp {
apply: apply_neg,
can_apply: can_apply_unary_always,
};
pub const OP_NOT: UnaryOp = UnaryOp {
name: "!",
prec: 12,
apply: apply_not,
can_apply: can_apply_unary_always,
};

// All operators: [..Binary, ..Unary, Parens, Literal, Variable]
pub const NUM_OPERATORS: usize = UNARY_OPERATORS.len() + BINARY_OPERATORS.len() + 3;
Expand Down
9 changes: 9 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ pub const BINARY_OPERATORS: &[BinaryOp] = &[
OP_SPACE_OR,
OP_OR_SPACE,
// OP_SPACE_OR_SPACE,
// OP_OR_SYMBOL,
// OP_OR_LOGICAL,
// OP_AND,
// OP_SPACE_AND,
// OP_AND_SPACE,
// OP_SPACE_AND_SPACE,
// OP_AND_SYMBOL,
// OP_AND_LOGICAL,
OP_LT,
OP_LE,
// OP_GT,
Expand Down Expand Up @@ -80,6 +88,7 @@ pub const BINARY_OPERATORS: &[BinaryOp] = &[
pub const UNARY_OPERATORS: &[UnaryOp] = &[
OP_BIT_NEG,
OP_NEG,
// OP_NOT,
];

/// Match leaf expressions 1 output at a time to avoid unnecessary precalculations
Expand Down

0 comments on commit fcbc942

Please sign in to comment.