From 3a62e01d86dc15344bafc855b6917f30b65b1041 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Wed, 5 Jun 2024 17:44:05 -0300 Subject: [PATCH] Implement le and ge operators --- src/fun/display.rs | 2 ++ src/fun/mod.rs | 4 ++++ src/fun/parser.rs | 8 ++++++++ src/fun/term_to_net.rs | 25 +++++++++++++++++++++---- src/imp/parser.rs | 2 ++ tests/golden_tests/run_file/ops.bend | 3 +++ tests/snapshots/run_file__ops.bend.snap | 4 ++-- 7 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/fun/display.rs b/src/fun/display.rs index d08b3c9f..5ca805f7 100644 --- a/src/fun/display.rs +++ b/src/fun/display.rs @@ -248,6 +248,8 @@ impl fmt::Display for Op { Op::SHL => write!(f, "<<"), Op::LOG => todo!(), Op::ATN => todo!(), + Op::LE => write!(f, "<="), + Op::GE => write!(f, ">="), } } } diff --git a/src/fun/mod.rs b/src/fun/mod.rs index 164295d0..045bd0f4 100644 --- a/src/fun/mod.rs +++ b/src/fun/mod.rs @@ -202,6 +202,10 @@ pub enum Op { LOG, // a^b POW, + /// Less than or equal + LE, + /// Greater than or equal + GE, } #[derive(Debug, Clone, Copy)] diff --git a/src/fun/parser.rs b/src/fun/parser.rs index eb036438..8624d643 100644 --- a/src/fun/parser.rs +++ b/src/fun/parser.rs @@ -1057,6 +1057,10 @@ pub trait ParserCommons<'a>: Parser<'a> { Op::SHL } else if self.try_consume_exactly(">>") { Op::SHR + } else if self.try_consume_exactly("<=") { + Op::LE + } else if self.try_consume_exactly(">=") { + Op::GE } else if self.try_consume_exactly("<") { Op::LT } else if self.try_consume_exactly(">") { @@ -1094,6 +1098,10 @@ pub trait ParserCommons<'a>: Parser<'a> { Op::SHL } else if self.starts_with(">>") { Op::SHR + } else if self.starts_with("<=") { + Op::LE + } else if self.starts_with(">=") { + Op::GE } else if self.starts_with("<") { Op::LT } else if self.starts_with(">") { diff --git a/src/fun/term_to_net.rs b/src/fun/term_to_net.rs index 171c3a77..af06cd0d 100644 --- a/src/fun/term_to_net.rs +++ b/src/fun/term_to_net.rs @@ -180,7 +180,7 @@ impl<'t, 'l> EncodeTermState<'t, 'l> { let node = self.new_opr(); self.link(fst, node.0); self.encode_term(snd, node.1); - self.link(up, node.2); + self.encode_le_ge_oprs(opr, up, node.2); } // Partially apply with snd, flip (fst, Term::Num { val }) => { @@ -194,7 +194,7 @@ impl<'t, 'l> EncodeTermState<'t, 'l> { let node2 = self.new_opr(); self.link(node1.2, node2.0); self.encode_term(snd, node2.1); - self.link(up, node2.2); + self.encode_le_ge_oprs(opr, up, node2.2); } else { // flip let val = val.to_bits(); @@ -203,7 +203,7 @@ impl<'t, 'l> EncodeTermState<'t, 'l> { let node = self.new_opr(); self.encode_term(fst, node.0); self.link(snd, node.1); - self.link(up, node.2); + self.encode_le_ge_oprs(opr, up, node.2); } } // Don't partially apply @@ -216,7 +216,7 @@ impl<'t, 'l> EncodeTermState<'t, 'l> { let node2 = self.new_opr(); self.link(node1.2, node2.0); self.encode_term(snd, node2.1); - self.link(up, node2.2); + self.encode_le_ge_oprs(opr, up, node2.2); } } } @@ -240,6 +240,20 @@ impl<'t, 'l> EncodeTermState<'t, 'l> { }) } + fn encode_le_ge_oprs(&mut self, opr: &Op, up: Place<'t>, node: Place<'t>) { + match opr { + Op::LE | Op::GE => { + let node_eq = self.new_opr(); + let eq_val = + Place::Tree(LoanedMut::new(Tree::Num { val: hvm::ast::Numb(Op::EQ.to_native_tag() as u32) })); + self.link(eq_val, node_eq.0); + self.link(node_eq.1, node); + self.link(up, node_eq.2); + } + _ => self.link(up, node), + } + } + fn encode_pat(&mut self, pat: &Pattern, up: Place<'t>) { maybe_grow(|| match pat { Pattern::Var(None) => self.link(up, Place::Tree(LoanedMut::new(Tree::Era))), @@ -452,6 +466,9 @@ impl Op { Op::ATN => hvm::hvm::OP_AND, Op::LOG => hvm::hvm::OP_OR, Op::POW => hvm::hvm::OP_XOR, + + Op::LE => hvm::hvm::OP_GT, + Op::GE => hvm::hvm::OP_LT, } } } diff --git a/src/imp/parser.rs b/src/imp/parser.rs index 35f133dc..45ee1a7a 100644 --- a/src/imp/parser.rs +++ b/src/imp/parser.rs @@ -1176,6 +1176,8 @@ impl Op { Op::NEQ => 3, Op::LT => 4, Op::GT => 4, + Op::LE => 4, + Op::GE => 4, Op::SHL => 5, Op::SHR => 5, Op::ADD => 6, diff --git a/tests/golden_tests/run_file/ops.bend b/tests/golden_tests/run_file/ops.bend index 002585da..a329342e 100644 --- a/tests/golden_tests/run_file/ops.bend +++ b/tests/golden_tests/run_file/ops.bend @@ -5,4 +5,7 @@ def main: -5 + -1 == -6, -3 * -9 == +27, 0.250 + 0.125 == 0.375, + 1 >= 1 == 1, + 1 <= 0 == 0, + 42 >= 43 == 0, ] diff --git a/tests/snapshots/run_file__ops.bend.snap b/tests/snapshots/run_file__ops.bend.snap index 961c54e6..8e2347a5 100644 --- a/tests/snapshots/run_file__ops.bend.snap +++ b/tests/snapshots/run_file__ops.bend.snap @@ -3,7 +3,7 @@ source: tests/golden_tests.rs input_file: tests/golden_tests/run_file/ops.bend --- NumScott: -[1, 1, 1, 1, 1] +[1, 1, 1, 1, 1, 1, 1, 1] Scott: -[1, 1, 1, 1, 1] +[1, 1, 1, 1, 1, 1, 1, 1]