From 547def990ee09d5c5dec822d1b87062747c2cb42 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 26 Feb 2018 02:11:56 -0800 Subject: [PATCH] Operator changes --- src/schala_lang/builtin.rs | 3 ++- src/schala_lang/eval.rs | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/schala_lang/builtin.rs b/src/schala_lang/builtin.rs index 634e48f..e462d75 100644 --- a/src/schala_lang/builtin.rs +++ b/src/schala_lang/builtin.rs @@ -63,7 +63,8 @@ lazy_static! { "+" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 10), "-" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 10), "*" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20), - "/" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20), + "/" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Float))))), (), 20), + "//" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20), "%" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20), "++" => (Func(bx!(Const(StringT)), bx!(Func(bx!(Const(StringT)), bx!(Const(StringT))))), (), 30), }; diff --git a/src/schala_lang/eval.rs b/src/schala_lang/eval.rs index 19e0f42..4efe4ea 100644 --- a/src/schala_lang/eval.rs +++ b/src/schala_lang/eval.rs @@ -183,7 +183,12 @@ impl<'a> State<'a> { ("++", Str(s1), Str(s2)) => Str(format!("{}{}", s1, s2)), ("-", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l - r), ("*", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l * r), - ("/", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l / r), + ("/", UnsignedInt(l), UnsignedInt(r)) => Float((l as f64)/ (r as f64)), + ("//", UnsignedInt(l), UnsignedInt(r)) => if r == 0 { + return Err(format!("Runtime error: divide by zero")); + } else { + UnsignedInt(l / r) + }, ("%", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l % r), _ => return Err(format!("Runtime error: not yet implemented")), })