Skip to content

Commit

Permalink
feat: add is_null builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
viddrobnic committed Jun 17, 2024
1 parent 98759b8 commit 4b13104
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions runtime/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum Builtin {
Char,
Float,
Bool,
IsNull,

Floor,
Ceil,
Expand Down Expand Up @@ -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"),
Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -237,6 +241,11 @@ fn call_bool(args: &[Object]) -> Result<Object, ErrorKind> {
Ok(Object::Boolean(args[0].is_truthy()))
}

fn is_null(args: &[Object]) -> Result<Object, ErrorKind> {
validate_args_len(args, 1)?;
Ok(Object::Boolean(args[0] == Object::Null))
}

fn call_round<F>(args: &[Object], round: F, builtin: Builtin) -> Result<Object, ErrorKind>
where
F: Fn(f64) -> f64,
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/vm/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4b13104

Please sign in to comment.