1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use super::*;
use crate::util;


/// `[$is-odd: val (int)]`
///
/// Returns true if `val` is odd.
pub(crate) fn is_odd(vm: &mut VM, val: i64) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(val % 2 != 0));
  Ok(())
}

/// `[$is-even: val (int)]`
///
/// Returns true if `val` is even.
pub(crate) fn is_even(vm: &mut VM, val: i64) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(val % 2 == 0));
  Ok(())
}

/// `[$is-factor: value (int); factor (int)]`
///
/// Returns true if `value` is divisible by `factor`.
pub(crate) fn is_factor(vm: &mut VM, (value, factor): (i64, i64)) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(factor != 0 && value % factor == 0));
  Ok(())
}

pub(crate) fn is_string(vm: &mut VM, value: RantValue) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(value.get_type() == RantValueType::String));
  Ok(())
}

pub(crate) fn is_int(vm: &mut VM, value: RantValue) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(value.get_type() == RantValueType::Int));
  Ok(())
}

pub(crate) fn is_float(vm: &mut VM, value: RantValue) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(value.get_type() == RantValueType::Float));
  Ok(())
}

pub(crate) fn is_number(vm: &mut VM, value: RantValue) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(matches!(value.get_type(), RantValueType::Int | RantValueType::Float)));
  Ok(())
}

pub(crate) fn is_between(vm: &mut VM, (value, a, b): (RantValue, RantValue, RantValue)) -> RantStdResult {
  let (a, b) = util::minmax(a, b);
  let result = value >= a && value <= b;
  vm.cur_frame_mut().write_value(RantValue::Boolean(result));
  Ok(())
}

pub(crate) fn is(vm: &mut VM, (value, type_name): (RantValue, String)) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(value.type_name() == type_name));
  Ok(())
}

pub(crate) fn is_empty(vm: &mut VM, value: RantValue) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(value.is_empty()));
  Ok(())
}

pub(crate) fn is_some(vm: &mut VM, value: RantValue) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(!value.is_empty()));
  Ok(())
}

pub(crate) fn is_bool(vm: &mut VM, value: RantValue) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(value.get_type() == RantValueType::Boolean));
  Ok(())
}

pub(crate) fn is_nan(vm: &mut VM, value: RantValue) -> RantStdResult {
  vm.cur_frame_mut().write_value(RantValue::Boolean(value.is_nan()));
  Ok(())
}