Skip to content

Commit

Permalink
feat: change behavior of bool builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
viddrobnic committed Jun 17, 2024
1 parent 3db1711 commit 98759b8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
3 changes: 3 additions & 0 deletions examples/overview.aoc
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,11 @@ float(1) // 1.0
float("1.1") // 1.1
float("asdf") // null

// bool builtin does "is truthy" behavior, similar to if/else and loops
bool(false) // false
bool("true") // true
bool("false") // true
bool(0) //true

floor(1.9) // 1.0
ceil(1.1) // 2.0
Expand Down
17 changes: 1 addition & 16 deletions runtime/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,22 +234,7 @@ fn call_float(args: &[Object]) -> Result<Object, ErrorKind> {
fn call_bool(args: &[Object]) -> Result<Object, ErrorKind> {
validate_args_len(args, 1)?;

let res = match &args[0] {
Object::Boolean(bool) => *bool,
Object::String(str) => match str.parse() {
Ok(res) => res,
Err(_) => return Ok(Object::Null),
},

obj => {
return Err(ErrorKind::InvalidBuiltinArg {
builtin: Builtin::Bool,
data_type: obj.into(),
})
}
};

Ok(Object::Boolean(res))
Ok(Object::Boolean(args[0].is_truthy()))
}

fn call_round<F>(args: &[Object], round: F, builtin: Builtin) -> Result<Object, ErrorKind>
Expand Down
16 changes: 4 additions & 12 deletions runtime/src/vm/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,10 @@ fn builtin() {
("char('a')", Ok(Object::Char(b'a'))),
("char(97)", Ok(Object::Char(b'a'))),
("char(10000)", Ok(Object::Char(16))),
// Bool (is truthy)
("bool(0)", Ok(Object::Boolean(true))),
("bool(\"false\")", Ok(Object::Boolean(true))),
("bool(int(\"\"))", Ok(Object::Boolean(false))),
];

for (input, expected) in tests {
Expand All @@ -654,18 +658,6 @@ fn builtin_float() {
}
}

#[test]
fn builtin_bool() {
let tests = [
("bool(\"true\")", Object::Boolean(true)),
("bool(\"false\")", Object::Boolean(false)),
];

for (input, expected) in tests {
run_test(input, Ok(expected));
}
}

#[test]
fn builtin_string() {
let tests = [
Expand Down

0 comments on commit 98759b8

Please sign in to comment.