From 4b13104f2e20992e6e542fdf0408a868ac120229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vid=20Drobni=C4=8D?= Date: Mon, 17 Jun 2024 18:44:08 +0200 Subject: [PATCH] feat: add is_null builtin --- runtime/src/builtin.rs | 9 +++++++++ runtime/src/vm/test.rs | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/runtime/src/builtin.rs b/runtime/src/builtin.rs index 0f25448..bb3d8e4 100644 --- a/runtime/src/builtin.rs +++ b/runtime/src/builtin.rs @@ -15,6 +15,7 @@ pub enum Builtin { Char, Float, Bool, + IsNull, Floor, Ceil, @@ -42,6 +43,7 @@ impl Display for Builtin { Builtin::Char => write!(f, "char"), Builtin::Float => write!(f, "float"), Builtin::Bool => write!(f, "bool"), + Builtin::IsNull => write!(f, "is_null"), Builtin::Floor => write!(f, "floor"), Builtin::Ceil => write!(f, "ceil"), Builtin::Round => write!(f, "round"), @@ -67,6 +69,7 @@ impl Builtin { "char" => Self::Char, "float" => Self::Float, "bool" => Self::Bool, + "is_null" => Self::IsNull, "floor" => Self::Floor, "ceil" => Self::Ceil, "round" => Self::Round, @@ -95,6 +98,7 @@ impl Builtin { Builtin::Char => call_char(args), Builtin::Float => call_float(args), Builtin::Bool => call_bool(args), + Builtin::IsNull => is_null(args), Builtin::Floor => call_round(args, |f| f.floor(), Builtin::Floor), Builtin::Ceil => call_round(args, |f| f.ceil(), Builtin::Ceil), @@ -237,6 +241,11 @@ fn call_bool(args: &[Object]) -> Result { Ok(Object::Boolean(args[0].is_truthy())) } +fn is_null(args: &[Object]) -> Result { + validate_args_len(args, 1)?; + Ok(Object::Boolean(args[0] == Object::Null)) +} + fn call_round(args: &[Object], round: F, builtin: Builtin) -> Result where F: Fn(f64) -> f64, diff --git a/runtime/src/vm/test.rs b/runtime/src/vm/test.rs index 508cc50..8e8be2c 100644 --- a/runtime/src/vm/test.rs +++ b/runtime/src/vm/test.rs @@ -634,6 +634,10 @@ fn builtin() { ("bool(0)", Ok(Object::Boolean(true))), ("bool(\"false\")", Ok(Object::Boolean(true))), ("bool(int(\"\"))", Ok(Object::Boolean(false))), + // IsNull + ("is_null(0)", Ok(Object::Boolean(false))), + ("is_null(false)", Ok(Object::Boolean(false))), + ("is_null(int(\"a\"))", Ok(Object::Boolean(true))), ]; for (input, expected) in tests {