Skip to content

Commit

Permalink
Parse Nor, Nand operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Riey committed Jun 27, 2023
1 parent 9da356b commit d76b91a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/erars-ast/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ pub enum BinaryOperator {
/// &&
#[strum(to_string = "&&")]
And,
/// !&
#[strum(to_string = "!&")]
Nand,
/// |
#[strum(to_string = "|")]
BitOr,
/// ||
#[strum(to_string = "||")]
Or,
/// ||
#[strum(to_string = "!|")]
Nor,
/// ^
#[strum(to_string = "^")]
Xor,
Expand Down Expand Up @@ -88,7 +94,7 @@ impl BinaryOperator {

match self {
BitAnd | BitOr | BitXor => 2,
And | Or | Xor => 3,
And | Nand | Or | Nor | Xor => 3,
Equal | NotEqual => 4,
Less | LessOrEqual | Greater | GreaterOrEqual => 5,
Lhs | Rhs => 6,
Expand Down
2 changes: 2 additions & 0 deletions crates/erars-compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ impl HeaderInfo {
BinaryOperator::NotEqual => Value::Int(i64::from(lhs != rhs)),
BinaryOperator::And => Value::Int(i64::from(lhs.as_bool() && rhs.as_bool())),
BinaryOperator::Or => Value::Int(i64::from(lhs.as_bool() || rhs.as_bool())),
BinaryOperator::Nand => Value::Int(i64::from(!(lhs.as_bool() && rhs.as_bool()))),
BinaryOperator::Nor => Value::Int(i64::from(!(lhs.as_bool() || rhs.as_bool()))),
BinaryOperator::Xor => Value::Int(i64::from(lhs.as_bool() ^ rhs.as_bool())),
BinaryOperator::BitAnd => Value::Int(lhs.try_into_int()? & rhs.try_into_int()?),
BinaryOperator::BitOr => Value::Int(lhs.try_into_int()? | rhs.try_into_int()?),
Expand Down
2 changes: 2 additions & 0 deletions crates/erars-compiler/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@ fn binop(i: &str) -> IResult<'_, BinaryOperator> {
value(BinaryOperator::Xor, tag("^^")),
value(BinaryOperator::Or, tag("||")),
value(BinaryOperator::And, tag("&&")),
value(BinaryOperator::Nor, tag("!|")),
value(BinaryOperator::Nand, tag("!&")),
value(BinaryOperator::BitXor, char('^')),
value(BinaryOperator::BitOr, char('|')),
value(BinaryOperator::BitAnd, char('&')),
Expand Down
2 changes: 2 additions & 0 deletions crates/erars-vm/src/terminal_vm/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ pub(super) fn run_instruction(
BinaryOperator::NotEqual => Value::Int(i64::from(lhs != rhs)),
BinaryOperator::And => Value::Int(i64::from(lhs.as_bool() && rhs.as_bool())),
BinaryOperator::Or => Value::Int(i64::from(lhs.as_bool() || rhs.as_bool())),
BinaryOperator::Nand => Value::Int(i64::from(!(lhs.as_bool() && rhs.as_bool()))),
BinaryOperator::Nor => Value::Int(i64::from(!(lhs.as_bool() || rhs.as_bool()))),
BinaryOperator::Xor => Value::Int(i64::from(lhs.as_bool() ^ rhs.as_bool())),
BinaryOperator::BitAnd => Value::Int(lhs.try_into_int()? & rhs.try_into_int()?),
BinaryOperator::BitOr => Value::Int(lhs.try_into_int()? | rhs.try_into_int()?),
Expand Down

0 comments on commit d76b91a

Please sign in to comment.