From a3f253999336f5ae1252e59b258366fccbe9fed4 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 1 Nov 2021 12:40:41 -0700 Subject: [PATCH] Support NotEqual builtin --- schala-lang/language/src/builtin.rs | 3 +++ schala-lang/language/src/tree_walk_eval/evaluator.rs | 6 ++++++ schala-lang/language/src/tree_walk_eval/test.rs | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/schala-lang/language/src/builtin.rs b/schala-lang/language/src/builtin.rs index 7de1f16..7b0ad01 100644 --- a/schala-lang/language/src/builtin.rs +++ b/schala-lang/language/src/builtin.rs @@ -33,6 +33,7 @@ pub enum Builtin { IOGetLine, Assignment, Concatenate, + NotEqual, } impl Builtin { @@ -65,6 +66,7 @@ impl Builtin { Concatenate => ty!(StringT -> StringT -> StringT), Increment => ty!(Nat -> Int), Negate => ty!(Nat -> Int), + NotEqual => ty!(Nat -> Nat -> Bool), } } } @@ -116,6 +118,7 @@ impl FromStr for Builtin { "<" => LessThan, "<=" => LessThanOrEqual, "==" => Equality, + "!=" => NotEqual, "=" => Assignment, "<=>" => Comparison, "print" => IOPrint, diff --git a/schala-lang/language/src/tree_walk_eval/evaluator.rs b/schala-lang/language/src/tree_walk_eval/evaluator.rs index 04c18f0..d86bb97 100644 --- a/schala-lang/language/src/tree_walk_eval/evaluator.rs +++ b/schala-lang/language/src/tree_walk_eval/evaluator.rs @@ -393,6 +393,12 @@ impl<'a, 'b> Evaluator<'a, 'b> { (Equality, Lit(Bool(l)), Lit(Bool(r))) => Bool(l == r).into(), (Equality, Lit(StringLit(ref l)), Lit(StringLit(ref r))) => Bool(l == r).into(), + (NotEqual, Lit(Nat(l)), Lit(Nat(r))) => Bool(l != r).into(), + (NotEqual, Lit(Int(l)), Lit(Int(r))) => Bool(l != r).into(), + (NotEqual, Lit(Float(l)), Lit(Float(r))) => Bool(l != r).into(), + (NotEqual, Lit(Bool(l)), Lit(Bool(r))) => Bool(l != r).into(), + (NotEqual, Lit(StringLit(ref l)), Lit(StringLit(ref r))) => Bool(l != r).into(), + (LessThan, Lit(Nat(l)), Lit(Nat(r))) => Bool(l < r).into(), (LessThan, Lit(Int(l)), Lit(Int(r))) => Bool(l < r).into(), (LessThan, Lit(Float(l)), Lit(Float(r))) => Bool(l < r).into(), diff --git a/schala-lang/language/src/tree_walk_eval/test.rs b/schala-lang/language/src/tree_walk_eval/test.rs index 1c6ec77..4fdddcb 100644 --- a/schala-lang/language/src/tree_walk_eval/test.rs +++ b/schala-lang/language/src/tree_walk_eval/test.rs @@ -419,7 +419,7 @@ fn loops() { let source = r#" let mut a = 0 let mut count = 0 -while !(a == 5) { +while a != 5 { a = a + 1 count = count + 100 }