From d76b91a4412c4939d7ef84ea57e3775e112d88c7 Mon Sep 17 00:00:00 2001 From: Riey Date: Tue, 27 Jun 2023 22:33:43 +0900 Subject: [PATCH] Parse Nor, Nand operators --- crates/erars-ast/src/operator.rs | 8 +++++++- crates/erars-compiler/src/parser.rs | 2 ++ crates/erars-compiler/src/parser/expr.rs | 2 ++ crates/erars-vm/src/terminal_vm/executor.rs | 2 ++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/erars-ast/src/operator.rs b/crates/erars-ast/src/operator.rs index 6f36d79..7822068 100644 --- a/crates/erars-ast/src/operator.rs +++ b/crates/erars-ast/src/operator.rs @@ -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, @@ -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, diff --git a/crates/erars-compiler/src/parser.rs b/crates/erars-compiler/src/parser.rs index d20b720..7779704 100644 --- a/crates/erars-compiler/src/parser.rs +++ b/crates/erars-compiler/src/parser.rs @@ -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()?), diff --git a/crates/erars-compiler/src/parser/expr.rs b/crates/erars-compiler/src/parser/expr.rs index 977d9bc..1b114b3 100644 --- a/crates/erars-compiler/src/parser/expr.rs +++ b/crates/erars-compiler/src/parser/expr.rs @@ -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('&')), diff --git a/crates/erars-vm/src/terminal_vm/executor.rs b/crates/erars-vm/src/terminal_vm/executor.rs index 1f0bd98..85c8b4b 100644 --- a/crates/erars-vm/src/terminal_vm/executor.rs +++ b/crates/erars-vm/src/terminal_vm/executor.rs @@ -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()?),