From 98f597f00aec1c67db1f4865eab6e5cfa388fe46 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 19 Aug 2018 21:11:43 -0700 Subject: [PATCH] Implement comparison operators correctly --- TODO.md | 7 +++++++ schala-lang/src/builtin.rs | 12 ++++++++++-- schala-lang/src/eval.rs | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/TODO.md b/TODO.md index 82a1103..15452c1 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,13 @@ # TODO Items +*A neat idea for pattern matching optimization would be if you could match on one of several things in a list +ex: +if x { + is (comp, LHSPat, RHSPat) if comp in ["==, "<"] -> ... +} + + - https://nshipster.com/never/ -https://cranelift.readthedocs.io/en/latest/?badge=latest diff --git a/schala-lang/src/builtin.rs b/schala-lang/src/builtin.rs index 7e5cf08..32b3d73 100644 --- a/schala-lang/src/builtin.rs +++ b/schala-lang/src/builtin.rs @@ -47,6 +47,8 @@ impl BinOp { Period => ".", Pipe => "|", Slash => "/", + LAngleBracket => "<", + RAngleBracket => ">", _ => return None }; Some(BinOp::from_sigil(s)) @@ -67,10 +69,11 @@ impl BinOp { Period => ".", Pipe => "|", Slash => "/", + LAngleBracket => "<", + RAngleBracket => ">", _ => return None }; - let default = 10_000_000; - Some(BINOPS.get(s).map(|x| x.2.clone()).unwrap_or(default)) + Some(BINOPS.get(s).map(|x| x.2.clone()).expect("Custom operators not handled yet")) } } @@ -120,5 +123,10 @@ lazy_static! { "^" => (Func(bx!(Const(Nat)), bx!(Func(bx!(Const(Nat)), bx!(Const(Nat))))), (), 20), "&" => (Func(bx!(Const(Nat)), bx!(Func(bx!(Const(Nat)), bx!(Const(Nat))))), (), 20), "|" => (Func(bx!(Const(Nat)), bx!(Func(bx!(Const(Nat)), bx!(Const(Nat))))), (), 20), + ">" => (Func(bx!(Const(Nat)), bx!(Func(bx!(Const(Nat)), bx!(Const(Nat))))), (), 20), + ">=" => (Func(bx!(Const(Nat)), bx!(Func(bx!(Const(Nat)), bx!(Const(Nat))))), (), 20), + "<" => (Func(bx!(Const(Nat)), bx!(Func(bx!(Const(Nat)), bx!(Const(Nat))))), (), 20), + "<=" => (Func(bx!(Const(Nat)), bx!(Func(bx!(Const(Nat)), bx!(Const(Nat))))), (), 20), + "==" => (Func(bx!(Const(Nat)), bx!(Func(bx!(Const(Nat)), bx!(Const(Nat))))), (), 20), }; } diff --git a/schala-lang/src/eval.rs b/schala-lang/src/eval.rs index ba6dc23..3ae69a5 100644 --- a/schala-lang/src/eval.rs +++ b/schala-lang/src/eval.rs @@ -305,12 +305,29 @@ impl<'a> State<'a> { ("&", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l & r)), ("|", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l | r)), + /* comparisons */ ("==", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l == r)), ("==", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l == r)), ("==", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l == r)), ("==", &[Lit(Bool(l)), Lit(Bool(r))]) => Lit(Bool(l == r)), ("==", &[Lit(StringLit(ref l)), Lit(StringLit(ref r))]) => Lit(Bool(l == r)), + ("<", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l < r)), + ("<", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l < r)), + ("<", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l < r)), + + ("<=", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l <= r)), + ("<=", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l <= r)), + ("<=", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l <= r)), + + (">", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l > r)), + (">", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l > r)), + (">", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l > r)), + + (">=", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l >= r)), + (">=", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l >= r)), + (">=", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l >= r)), + /* prefix ops */ ("!", &[Lit(Bool(true))]) => Lit(Bool(false)), ("!", &[Lit(Bool(false))]) => Lit(Bool(true)),