Implement le and ge operators

This commit is contained in:
imaqtkatt 2024-06-05 17:44:05 -03:00
parent d04aa4642d
commit 3a62e01d86
7 changed files with 42 additions and 6 deletions

View File

@ -248,6 +248,8 @@ impl fmt::Display for Op {
Op::SHL => write!(f, "<<"), Op::SHL => write!(f, "<<"),
Op::LOG => todo!(), Op::LOG => todo!(),
Op::ATN => todo!(), Op::ATN => todo!(),
Op::LE => write!(f, "<="),
Op::GE => write!(f, ">="),
} }
} }
} }

View File

@ -202,6 +202,10 @@ pub enum Op {
LOG, LOG,
// a^b // a^b
POW, POW,
/// Less than or equal
LE,
/// Greater than or equal
GE,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]

View File

@ -1057,6 +1057,10 @@ pub trait ParserCommons<'a>: Parser<'a> {
Op::SHL Op::SHL
} else if self.try_consume_exactly(">>") { } else if self.try_consume_exactly(">>") {
Op::SHR Op::SHR
} else if self.try_consume_exactly("<=") {
Op::LE
} else if self.try_consume_exactly(">=") {
Op::GE
} else if self.try_consume_exactly("<") { } else if self.try_consume_exactly("<") {
Op::LT Op::LT
} else if self.try_consume_exactly(">") { } else if self.try_consume_exactly(">") {
@ -1094,6 +1098,10 @@ pub trait ParserCommons<'a>: Parser<'a> {
Op::SHL Op::SHL
} else if self.starts_with(">>") { } else if self.starts_with(">>") {
Op::SHR Op::SHR
} else if self.starts_with("<=") {
Op::LE
} else if self.starts_with(">=") {
Op::GE
} else if self.starts_with("<") { } else if self.starts_with("<") {
Op::LT Op::LT
} else if self.starts_with(">") { } else if self.starts_with(">") {

View File

@ -180,7 +180,7 @@ impl<'t, 'l> EncodeTermState<'t, 'l> {
let node = self.new_opr(); let node = self.new_opr();
self.link(fst, node.0); self.link(fst, node.0);
self.encode_term(snd, node.1); 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 // Partially apply with snd, flip
(fst, Term::Num { val }) => { (fst, Term::Num { val }) => {
@ -194,7 +194,7 @@ impl<'t, 'l> EncodeTermState<'t, 'l> {
let node2 = self.new_opr(); let node2 = self.new_opr();
self.link(node1.2, node2.0); self.link(node1.2, node2.0);
self.encode_term(snd, node2.1); self.encode_term(snd, node2.1);
self.link(up, node2.2); self.encode_le_ge_oprs(opr, up, node2.2);
} else { } else {
// flip // flip
let val = val.to_bits(); let val = val.to_bits();
@ -203,7 +203,7 @@ impl<'t, 'l> EncodeTermState<'t, 'l> {
let node = self.new_opr(); let node = self.new_opr();
self.encode_term(fst, node.0); self.encode_term(fst, node.0);
self.link(snd, node.1); self.link(snd, node.1);
self.link(up, node.2); self.encode_le_ge_oprs(opr, up, node.2);
} }
} }
// Don't partially apply // Don't partially apply
@ -216,7 +216,7 @@ impl<'t, 'l> EncodeTermState<'t, 'l> {
let node2 = self.new_opr(); let node2 = self.new_opr();
self.link(node1.2, node2.0); self.link(node1.2, node2.0);
self.encode_term(snd, node2.1); 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>) { fn encode_pat(&mut self, pat: &Pattern, up: Place<'t>) {
maybe_grow(|| match pat { maybe_grow(|| match pat {
Pattern::Var(None) => self.link(up, Place::Tree(LoanedMut::new(Tree::Era))), 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::ATN => hvm::hvm::OP_AND,
Op::LOG => hvm::hvm::OP_OR, Op::LOG => hvm::hvm::OP_OR,
Op::POW => hvm::hvm::OP_XOR, Op::POW => hvm::hvm::OP_XOR,
Op::LE => hvm::hvm::OP_GT,
Op::GE => hvm::hvm::OP_LT,
} }
} }
} }

View File

@ -1176,6 +1176,8 @@ impl Op {
Op::NEQ => 3, Op::NEQ => 3,
Op::LT => 4, Op::LT => 4,
Op::GT => 4, Op::GT => 4,
Op::LE => 4,
Op::GE => 4,
Op::SHL => 5, Op::SHL => 5,
Op::SHR => 5, Op::SHR => 5,
Op::ADD => 6, Op::ADD => 6,

View File

@ -5,4 +5,7 @@ def main:
-5 + -1 == -6, -5 + -1 == -6,
-3 * -9 == +27, -3 * -9 == +27,
0.250 + 0.125 == 0.375, 0.250 + 0.125 == 0.375,
1 >= 1 == 1,
1 <= 0 == 0,
42 >= 43 == 0,
] ]

View File

@ -3,7 +3,7 @@ source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/ops.bend input_file: tests/golden_tests/run_file/ops.bend
--- ---
NumScott: NumScott:
[1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1]
Scott: Scott:
[1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1]