rant/stdlib/
verify.rs

1use super::*;
2use crate::util;
3
4
5/// `[$is-odd: val (int)]`
6///
7/// Returns true if `val` is odd.
8pub fn is_odd(vm: &mut VM, val: i64) -> RantStdResult {
9  vm.cur_frame_mut().write(val % 2 != 0);
10  Ok(())
11}
12
13/// `[$is-even: val (int)]`
14///
15/// Returns true if `val` is even.
16pub fn is_even(vm: &mut VM, val: i64) -> RantStdResult {
17  vm.cur_frame_mut().write(val % 2 == 0);
18  Ok(())
19}
20
21/// `[$is-factor: value (int); factor (int)]`
22///
23/// Returns true if `value` is divisible by `factor`.
24pub fn is_factor(vm: &mut VM, (value, factor): (i64, i64)) -> RantStdResult {
25  vm.cur_frame_mut().write(factor != 0 && value % factor == 0);
26  Ok(())
27}
28
29pub fn is_string(vm: &mut VM, value: RantValue) -> RantStdResult {
30  vm.cur_frame_mut().write(value.get_type() == RantValueType::String);
31  Ok(())
32}
33
34pub fn is_int(vm: &mut VM, value: RantValue) -> RantStdResult {
35  vm.cur_frame_mut().write(value.get_type() == RantValueType::Int);
36  Ok(())
37}
38
39pub fn is_float(vm: &mut VM, value: RantValue) -> RantStdResult {
40  vm.cur_frame_mut().write(value.get_type() == RantValueType::Float);
41  Ok(())
42}
43
44pub fn is_number(vm: &mut VM, value: RantValue) -> RantStdResult {
45  vm.cur_frame_mut().write(matches!(value.get_type(), RantValueType::Int | RantValueType::Float));
46  Ok(())
47}
48
49pub fn is_between(vm: &mut VM, (value, a, b): (RantValue, RantValue, RantValue)) -> RantStdResult {
50  let (a, b) = util::minmax(a, b);
51  let result = value >= a && value <= b;
52  vm.cur_frame_mut().write(result);
53  Ok(())
54}
55
56pub fn is(vm: &mut VM, (value, type_name): (RantValue, String)) -> RantStdResult {
57  vm.cur_frame_mut().write(value.type_name() == type_name);
58  Ok(())
59}
60
61pub fn is_nothing(vm: &mut VM, value: RantValue) -> RantStdResult {
62  vm.cur_frame_mut().write(value.is_nothing());
63  Ok(())
64}
65
66pub fn is_some(vm: &mut VM, value: RantValue) -> RantStdResult {
67  vm.cur_frame_mut().write(!value.is_nothing());
68  Ok(())
69}
70
71pub fn is_bool(vm: &mut VM, value: RantValue) -> RantStdResult {
72  vm.cur_frame_mut().write(value.get_type() == RantValueType::Boolean);
73  Ok(())
74}
75
76pub fn is_nan(vm: &mut VM, value: RantValue) -> RantStdResult {
77  vm.cur_frame_mut().write(value.is_nan());
78  Ok(())
79}