use alloc::format;
use alloc::string::{String, ToString};
use crate::ast::BinaryOp;
use crate::runtime::{Runtime, RuntimeError, Value};
pub(crate) fn the_illegal_letters_of_a_name() -> &'static [char] {
&[
' ', '\t', '!', '"', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<',
'=', '>', '?', '[', '\\', ']', '^', '`', '{', '|', '}', '~',
]
}
pub(crate) fn the_place_of_the_argument_that_wants_the_name(name: &str) -> Option<usize> {
match name.to_lowercase().as_str() {
"isset" | "isbyref" => Some(0),
_ => None,
}
}
pub(crate) fn every_argument_that_is_a_variable_keeps_it(name: &str) -> bool {
name.eq_ignore_ascii_case("DllCall")
}
pub(crate) fn the_place_of_the_argument_that_wants_the_variable(name: &str) -> Option<usize> {
match name.to_lowercase().as_str() {
"varsetcapacity" => Some(0),
"strreplace" => Some(3),
"numput" => Some(1),
"numget" => Some(0),
"loadpicture" => Some(2),
"regexmatch" => Some(2),
"regexreplace" => Some(3),
"lv_gettext" | "tv_gettext" => Some(0),
_ => None,
}
}
pub(crate) fn set_format(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let text = |index: usize| {
values
.get(index)
.map(|value| value.to_text())
.unwrap_or_default()
.trim()
.to_string()
};
let number_type = text(0);
let format = text(1);
match super::parameters::the_kind_of_a_number(&number_type) {
Some(super::parameters::TheKindOfANumber::Integer) => {
let format = match super::parameters::the_format_of_the_whole_numbers(&format) {
Some(format) => format,
None => {
return Err(RuntimeError {
message: format!("Parameter #2 invalid.\n Specifically: {format}"),
line,
});
}
};
rt.set_integer_format(format);
}
Some(super::parameters::TheKindOfANumber::Float) => {
let format = match super::parameters::the_format_of_the_floats(&format) {
Some(format) => format,
None => {
return Err(RuntimeError {
message: format!("Parameter #2 invalid.\n Specifically: {format}"),
line,
});
}
};
rt.set_float_format(format);
}
None => {
return Err(RuntimeError {
message: format!("Parameter #1 invalid.\n Specifically: {number_type}"),
line,
});
}
}
Ok(Value::Text(String::new()))
}
pub(crate) fn auto_trim(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let text = the_one_parameter_of_a_command(values);
let trimmed = text.trim();
match super::parameters::the_switch_of_the_v1(trimmed) {
Some(trim) => rt.set_auto_trim(trim),
None => {
return Err(RuntimeError {
message: format!("Parameter #1 invalid.\n Specifically: {trimmed}"),
line,
});
}
}
Ok(Value::Text(String::new()))
}
pub(crate) fn set_batch_lines(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let text = the_one_parameter_of_a_command(values);
let trimmed = text.trim();
let number = match trimmed.parse::<i64>() {
Ok(number) => number,
Err(_) if super::numbers::the_text_is_a_hex(trimmed) => {
super::numbers::the_whole_number_of_a_hex(trimmed)
}
Err(_) => {
return Err(RuntimeError {
message: format!("Parameter #1 invalid.\n Specifically: {trimmed}"),
line,
});
}
};
let number = if number == 0 { 10 } else { number };
rt.set_batch_lines(&number.to_string());
Ok(Value::Text(String::new()))
}
pub(crate) fn the_one_parameter_of_a_command(values: &[Value]) -> String {
let mut text = String::new();
for (index, value) in values.iter().enumerate() {
if index > 0 {
text.push_str(", ");
}
text.push_str(&value.to_text());
}
text
}
pub(crate) fn string_case_sense(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let text = the_one_parameter_of_a_command(values);
let trimmed = text.trim();
let sense = match super::parameters::the_case_sense_of_the_v1(trimmed) {
Some(sense) => sense,
None => {
return Err(RuntimeError {
message: format!("Parameter #1 invalid.\n Specifically: {trimmed}"),
line,
});
}
};
rt.set_case_sense(sense);
Ok(Value::Text(String::new()))
}
pub(crate) fn set_detect_hidden_text_of_the_v1(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let on = super::parameters::the_switch_of_the_v1(
&values
.first()
.map(|value| value.to_text())
.unwrap_or_default(),
)
.unwrap_or(false);
crate::settings::set_detect_hidden_text(rt, &[Value::Int(on as i64)], line, 0)
}
pub(crate) fn set_detect_hidden_windows_of_the_v1(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let on = super::parameters::the_switch_of_the_v1(
&values
.first()
.map(|value| value.to_text())
.unwrap_or_default(),
)
.unwrap_or(false);
crate::settings::set_detect_hidden_windows(rt, &[Value::Int(on as i64)], line, 0)
}
pub(crate) fn set_store_caps_lock_mode_of_the_v1(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let on = super::parameters::the_switch_of_the_v1(
&values
.first()
.map(|value| value.to_text())
.unwrap_or_default(),
)
.unwrap_or(false);
crate::settings::set_store_caps_lock_mode(rt, &[Value::Int(on as i64)], line, 0)
}
pub(crate) fn coord_mode_of_the_v1(
_rt: &mut Runtime<'_>,
values: &[Value],
_line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let target = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let relative = values
.get(1)
.map(|value| value.to_text())
.unwrap_or_default();
crate::settings::set_the_coord_mode_of_the_v1(&target, &relative);
Ok(Value::Text(String::new()))
}
pub(crate) fn set_control_delay_of_the_v1(
_rt: &mut Runtime<'_>,
values: &[Value],
_line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let text = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let delay = crate::v1::the_number_of_a_text(&text)
.and_then(|value| value.to_number())
.unwrap_or(0.0) as i32;
let previous = crate::settings::set_the_control_delay_of_the_v1(delay);
Ok(Value::Int(previous as i64))
}
pub(crate) fn set_win_delay_of_the_v1(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let text = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let delay = crate::v1::the_number_of_a_text(&text)
.and_then(|value| value.to_number())
.unwrap_or(0.0);
crate::settings::set_win_delay(rt, &[Value::Float(delay)], line, 0)
}
pub(crate) fn set_mouse_delay_of_the_v1(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let delay = values
.first()
.and_then(|value| value.to_number())
.unwrap_or(0.0);
let mode = values
.get(1)
.map(|value| value.to_text())
.unwrap_or_default();
let mode = if mode.trim().eq_ignore_ascii_case("play") {
"Play"
} else {
""
};
crate::settings::set_mouse_delay(
rt,
&[Value::Float(delay), Value::Text(mode.to_string())],
line,
0,
)
}
pub(crate) fn set_key_delay_of_the_v1(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let delay = values
.first()
.and_then(|value| value.to_number())
.unwrap_or(0.0);
let mode = values
.get(1)
.map(|value| value.to_text())
.unwrap_or_default();
let mode = if mode.trim().eq_ignore_ascii_case("play") {
"Play"
} else {
""
};
crate::settings::set_key_delay(
rt,
&[Value::Float(delay), Value::Text(mode.to_string())],
line,
0,
)
}
pub(crate) fn file_encoding_of_the_v1(
_rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let name = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
if name.trim().is_empty() {
return Ok(Value::Text(crate::settings::file_encoding().to_string()));
}
crate::settings::set_file_encoding(&name)
.map(|encoding| Value::Text(encoding.to_string()))
.map_err(|message| RuntimeError { message, line })
}
fn the_text_without_the_quotes(text: &str) -> &str {
let text = text.trim();
if text.len() >= 2 && text.starts_with('"') && text.ends_with('"') {
return &text[1..text.len() - 1];
}
text
}
pub(crate) fn the_command_has_a_body(name: &str) -> bool {
matches!(
name.to_lowercase().as_str(),
"ifinstring"
| "ifnotinstring"
| "ifmsgbox"
| "ifexist"
| "ifnotexist"
| "ifequal"
| "ifnotequal"
| "ifless"
| "iflessorequal"
| "ifgreater"
| "ifgreaterorequal"
| "ifwinactive"
| "ifwinnotactive"
| "ifwinexist"
| "ifwinnotexist"
)
}
pub(crate) fn the_command_is_the_else(name: &str) -> bool {
name.to_lowercase() == "else"
}
pub(crate) fn if_in_string(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_search(rt, values, line, "IfInString")
}
pub(crate) fn if_not_in_string(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_search(rt, values, line, "IfNotInString")
}
fn the_condition_of_the_search(
rt: &mut Runtime<'_>,
values: &[Value],
_line: u32,
name: &str,
) -> Result<Value, RuntimeError> {
let haystack = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let needle = values
.get(1)
.map(|value| value.to_text())
.unwrap_or_default();
let holds = crate::runtime::the_text_contains(&haystack, &needle, rt.case_sense().the_method());
Ok(Value::Int(if name == "IfNotInString" {
!holds
} else {
holds
} as i64))
}
pub(crate) fn if_msg_box(
_rt: &mut Runtime<'_>,
_values: &[Value],
_line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
Ok(Value::Int(0))
}
pub(crate) fn if_exist(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_existence(rt, values, line, false)
}
pub(crate) fn if_not_exist(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_existence(rt, values, line, true)
}
fn the_condition_of_existence(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
negated: bool,
) -> Result<Value, RuntimeError> {
let pattern = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let attributes = rt
.desktop()
.attributes_of_the_pattern(&pattern, "")
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
let holds = !attributes.concat().is_empty();
Ok(Value::Int((holds ^ negated) as i64))
}
pub(crate) fn if_equal(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_comparison(rt, values, line, BinaryOp::Equal)
}
pub(crate) fn if_not_equal(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_comparison(rt, values, line, BinaryOp::NotEqual)
}
pub(crate) fn if_less(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_comparison(rt, values, line, BinaryOp::Less)
}
pub(crate) fn if_less_or_equal(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_comparison(rt, values, line, BinaryOp::LessEqual)
}
pub(crate) fn if_greater(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_comparison(rt, values, line, BinaryOp::Greater)
}
pub(crate) fn if_greater_or_equal(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_comparison(rt, values, line, BinaryOp::GreaterEqual)
}
fn the_condition_of_the_comparison(
rt: &mut Runtime<'_>,
values: &[Value],
_line: u32,
op: BinaryOp,
) -> Result<Value, RuntimeError> {
let left = values
.first()
.cloned()
.unwrap_or(Value::Text(String::new()));
let right = values
.get(1)
.map(|value| value.to_text())
.unwrap_or_default();
let left_number = match &left {
Value::Int(number) => Some(Value::Int(*number)),
Value::Float(number) => Some(Value::Float(*number)),
Value::Text(text) => crate::v1::the_number_of_a_text(text),
_ => None,
};
let right_number = crate::v1::the_number_of_a_text(&right);
let answer = match op {
BinaryOp::Equal | BinaryOp::NotEqual => {
let method = rt.case_sense().the_method();
let same = match (left_number, right_number) {
(Some(one), Some(other)) => crate::runtime::the_same_number(&one, &other),
_ => crate::runtime::the_same_text(&left.to_text(), &right, method),
};
if op == BinaryOp::Equal {
same
} else {
!same
}
}
_ => {
let numbers = match (left_number, right_number) {
(Some(one), Some(other)) => Some((
one.to_number().unwrap_or(0.0),
other.to_number().unwrap_or(0.0),
)),
_ => None,
};
rt.the_order_of_the_v1(numbers, &left.to_text(), &right, &op)
}
};
Ok(Value::Int(answer as i64))
}
pub(crate) fn if_win_exist(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_window(rt, values, line, false, false)
}
pub(crate) fn if_win_not_exist(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_window(rt, values, line, false, true)
}
pub(crate) fn if_win_active(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_window(rt, values, line, true, false)
}
pub(crate) fn if_win_not_active(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
the_condition_of_the_window(rt, values, line, true, true)
}
fn the_condition_of_the_window(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
active: bool,
negated: bool,
) -> Result<Value, RuntimeError> {
let title = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let mode = crate::settings::title_match_mode();
let holds = if active {
match rt
.desktop()
.window_active(&title, mode)
.map_err(|reason| RuntimeError {
message: reason,
line,
})? {
crate::desktop::WindowAnswer::Active => true,
crate::desktop::WindowAnswer::NotActive | crate::desktop::WindowAnswer::Absent => false,
crate::desktop::WindowAnswer::Unknown(reason) => {
return Err(RuntimeError {
message: reason,
line,
});
}
}
} else {
let windows = rt
.desktop()
.matching_windows(&title, mode)
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
if let Some(window) = windows.first() {
rt.remember_the_last_window_of_the_v1(&window.id);
true
} else {
false
}
};
Ok(Value::Int((holds ^ negated) as i64))
}
pub(crate) fn the_type_of_the_is_is_known(word: &str) -> bool {
matches!(
word.trim().to_ascii_lowercase().as_str(),
"integer"
| "float"
| "number"
| "digit"
| "xdigit"
| "alpha"
| "upper"
| "lower"
| "alnum"
| "space"
| "time"
| "date"
)
}
pub(crate) fn the_value_is_of_the_type(text: &str, word: &str) -> bool {
match word.trim().to_ascii_lowercase().as_str() {
"integer" => the_text_is_an_integer_of_the_is(text),
"float" => the_text_is_a_float_of_the_is(text),
"number" => {
the_text_is_an_integer_of_the_is(text) || the_text_is_a_float_of_the_is(text)
}
"digit" => text.chars().all(|character| character.is_ascii_digit()),
"xdigit" => {
let rest = text
.strip_prefix("0x")
.or_else(|| text.strip_prefix("0X"))
.unwrap_or(text);
rest.chars().all(|character| character.is_ascii_hexdigit())
}
"alpha" => text.chars().all(|character| character.is_alphabetic()),
"upper" => text.chars().all(|character| character.is_uppercase()),
"lower" => text.chars().all(|character| character.is_lowercase()),
"alnum" => text.chars().all(|character| character.is_alphanumeric()),
"space" => text.chars().all(|character| character.is_whitespace()),
"time" | "date" => the_text_is_a_timestamp_of_the_is(text),
_ => false,
}
}
fn the_text_is_an_integer_of_the_is(text: &str) -> bool {
let trimmed = text.trim();
let unsigned = trimmed
.strip_prefix('+')
.or_else(|| trimmed.strip_prefix('-'))
.unwrap_or(trimmed);
if let Some(hex) = unsigned
.strip_prefix("0x")
.or_else(|| unsigned.strip_prefix("0X"))
{
return !hex.is_empty() && hex.chars().all(|character| character.is_ascii_hexdigit());
}
!unsigned.is_empty() && unsigned.chars().all(|character| character.is_ascii_digit())
}
fn the_text_is_a_float_of_the_is(text: &str) -> bool {
let trimmed = text.trim();
let unsigned = trimmed
.strip_prefix('+')
.or_else(|| trimmed.strip_prefix('-'))
.unwrap_or(trimmed);
if !unsigned.contains('.') {
return false;
}
let mut points = 0;
let mut digits = 0;
for character in unsigned.chars() {
if character == '.' {
points += 1;
if points > 1 {
return false;
}
} else if character.is_ascii_digit() {
digits += 1;
} else {
return false;
}
}
digits > 0
}
fn the_text_is_a_timestamp_of_the_is(text: &str) -> bool {
let trimmed = text.trim();
if trimmed.is_empty() || !trimmed.chars().all(|character| character.is_ascii_digit()) {
return false;
}
let length = trimmed.len().min(14);
if length < 4 || length % 2 != 0 {
return false;
}
let part = |from: usize, to: usize| -> u32 {
trimmed[from..to].parse().unwrap_or(0)
};
let year = part(0, 4);
if year < 1601 {
return false;
}
if length >= 6 {
let month = part(4, 6);
if !(1..=12).contains(&month) {
return false;
}
if length >= 8 {
let day = part(6, 8);
if day < 1 || day > the_days_of_the_month(year, month) {
return false;
}
if length >= 10 && part(8, 10) > 23 {
return false;
}
if length >= 12 && part(10, 12) > 59 {
return false;
}
if length >= 14 && part(12, 14) > 59 {
return false;
}
}
}
true
}
fn the_days_of_the_month(year: u32, month: u32) -> u32 {
match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 => {
if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 {
29
} else {
28
}
}
_ => 0,
}
}
pub(crate) fn else_of_the_v1(
_rt: &mut Runtime<'_>,
_values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
Err(RuntimeError {
message: "ELSE with no matching IF".into(),
line,
})
}
pub(crate) fn transform(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let sub = values
.get(1)
.map(|value| value.to_text())
.unwrap_or_default();
let first = values
.get(2)
.map(|value| value.to_text())
.unwrap_or_default();
let second = values
.get(3)
.map(|value| value.to_text())
.unwrap_or_default();
let answer = the_answer_of_the_transform(rt, &sub, &first, &second, line)?;
crate::v1::write_the_answer(rt, "Transform", values, 0, answer.clone(), line)?;
Ok(answer)
}
fn the_answer_of_the_transform(
rt: &mut Runtime<'_>,
sub: &str,
first: &str,
second: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let number = |text: &str| {
crate::v1::the_number_of_a_text(text)
.and_then(|value| value.to_number())
.unwrap_or(0.0)
};
let value = |text: &str| crate::v1::the_number_of_a_text(text);
fn plain(text: &str) -> &str {
the_text_without_the_quotes(text)
}
let invalid = |message: String| Err(RuntimeError { message, line });
match sub.trim().to_lowercase().as_str() {
"asc" => Ok(match first.chars().next() {
Some(character) => Value::Int(character as i64),
None => Value::Text(String::new()),
}),
"chr" => {
let code = number(plain(first));
if !(0.0..=255.0).contains(&code) {
return invalid(format!(
"Parameter #3 invalid.\n Specifically: {}",
plain(first)
));
}
let code = code as u32;
Ok(match char::from_u32(code) {
Some(character) if code != 0 => Value::Text(character.to_string()),
_ => Value::Text(String::new()),
})
}
"mod" => {
let divisor = number(plain(second));
if divisor == 0.0 {
return invalid(format!(
"Divide by zero\n Specifically: {}",
plain(second)
));
}
let left = number(plain(first));
Ok(Value::Int((left % divisor) as i64))
}
"exp" => Ok(Value::Float(crate::runtime::exp(number(plain(first))))),
"sqrt" => {
let root = number(plain(first));
if root < 0.0 {
return invalid(format!(
"Parameter #3 must be a positive integer in this case.\n Specifically: {}",
plain(first)
));
}
Ok(Value::Float(crate::runtime::sqrt(root)))
}
"log" | "ln" => {
let logarithm = number(plain(first));
if logarithm < 0.0 {
return invalid(format!(
"Parameter #3 must be a positive integer in this case.\n Specifically: {}",
plain(first)
));
}
let answer = if sub.trim().eq_ignore_ascii_case("log") {
crate::runtime::log10(logarithm)
} else {
crate::runtime::ln(logarithm)
};
Ok(Value::Float(answer))
}
"round" => {
let digits = number(plain(second));
let rounded = number(plain(first));
let factor = crate::runtime::powf(10.0, digits);
let answer = crate::runtime::round(rounded * factor) / factor;
if digits <= 0.0 {
return Ok(Value::Int(answer as i64));
}
Ok(Value::Float(answer))
}
"ceil" => Ok(Value::Int(crate::runtime::ceil(number(plain(first))) as i64)),
"floor" => Ok(Value::Int(
crate::runtime::floor(number(plain(first))) as i64
)),
"abs" => {
let text = plain(first);
if text.is_empty() {
return invalid("Parameter #3 must be a number in this case.".to_string());
}
let text = text.trim_start();
match text.strip_prefix('-') {
Some(rest) => {
if value(rest).is_none() {
return Ok(Value::Text(String::new()));
}
Ok(Value::Text(rest.to_string()))
}
None => Ok(Value::Text(text.to_string())),
}
}
"sin" => Ok(Value::Float(crate::runtime::sin(number(plain(first))))),
"cos" => Ok(Value::Float(crate::runtime::cos(number(plain(first))))),
"tan" => Ok(Value::Float(crate::runtime::tan(number(plain(first))))),
"asin" | "acos" | "atan" => {
let arc = number(plain(first));
let answer = match sub.trim().to_lowercase().as_str() {
"asin" => crate::runtime::asin(arc),
"acos" => crate::runtime::acos(arc),
_ => crate::runtime::atan(arc),
};
if answer.is_nan() {
return Ok(Value::Text(String::new()));
}
Ok(Value::Float(answer))
}
"pow" => {
let base = number(plain(first));
if base < 0.0 {
return invalid(format!(
"Parameter #3 must be a positive integer in this case.\n Specifically: {}",
plain(first)
));
}
if let (Some(Value::Int(left)), Some(Value::Int(right))) =
(value(plain(first)), value(plain(second)))
{
if right >= 0 {
if let Some(answer) = left.checked_pow(right as u32) {
return Ok(Value::Int(answer));
}
}
}
Ok(Value::Float(crate::runtime::powf(
base,
number(plain(second)),
)))
}
"bitnot" => {
let bits = number(plain(first)) as u32;
Ok(Value::Int((!bits) as i64))
}
"bitand" => Ok(Value::Int(
number(plain(first)) as i64 & number(plain(second)) as i64,
)),
"bitor" => Ok(Value::Int(
number(plain(first)) as i64 | number(plain(second)) as i64,
)),
"bitxor" => Ok(Value::Int(
number(plain(first)) as i64 ^ number(plain(second)) as i64,
)),
"bitshiftleft" => Ok(Value::Int(
(number(plain(first)) as i64) << (number(plain(second)) as i64 & 63),
)),
"bitshiftright" => Ok(Value::Int(
(number(plain(first)) as i64) >> (number(plain(second)) as i64 & 63),
)),
"deref" => {
let text = rt
.the_substitution_of_the_v1(first, line)
.map_err(|failure| RuntimeError {
message: crate::runtime::the_message_of(&failure),
line,
})?;
Ok(Value::Text(text))
}
"html" => invalid(
"this runtime does not carry the sub-command of the v1.\n Specifically: HTML"
.to_string(),
),
_ => invalid(format!("Parameter #2 invalid.\n Specifically: {sub}")),
}
}
pub(crate) fn is_set(
rt: &mut Runtime<'_>,
values: &[Value],
_line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let name = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let answer = if rt.has_a_value(&name) { 1 } else { 0 };
Ok(Value::Int(answer))
}
pub(crate) fn is_func(
rt: &mut Runtime<'_>,
values: &[Value],
_line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let value = values.first();
if let Some(Value::Object(object)) = value {
let object = object.borrow();
if object.class == "func" {
let builtin = matches!(object.value_of("isbuiltin"), Some(Value::Int(1)));
return Ok(Value::Int(if builtin { 2 } else { 1 }));
}
}
let name = value.map(|value| value.to_text()).unwrap_or_default();
let answer = if rt.has_a_function(&name) {
1
} else if rt.has_a_builtin_function(&name) {
2
} else {
0
};
Ok(Value::Int(answer))
}
pub(crate) fn is_by_ref(
rt: &mut Runtime<'_>,
values: &[Value],
_line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let name = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let answer = if rt.is_a_reference(&name) { 1 } else { 0 };
Ok(Value::Int(answer))
}
pub(crate) fn object(
_rt: &mut Runtime<'_>,
_values: &[Value],
_line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
Ok(crate::classes::object(""))
}
pub(crate) fn func(
rt: &mut Runtime<'_>,
values: &[Value],
_line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let name = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
if let Some(value) = rt.the_function_of_the_script(&name) {
return Ok(value);
}
if let Some(value) = rt.the_builtin_function(&name) {
return Ok(value);
}
Ok(Value::Int(0))
}
pub(crate) fn on_error(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let value = values.first();
if let Some(Value::Object(object)) = value {
if object.borrow().class == "func" {
let callback = rt.the_callback(value, "OnError", line)?;
let order = the_order_of_the_handler(values.get(1), line)?;
match order {
1 => rt.set_error_callback(&callback.name, line),
-1 => {
rt.set_error_callback(&callback.name, line);
rt.order_error_callback(&callback.name, true);
}
_ => rt.remove_error_callback(&callback.name),
}
return Ok(Value::Int(1));
}
}
let name = value.map(|value| value.to_text()).unwrap_or_default();
if name.is_empty() {
return Err(RuntimeError {
message: crate::runtime::invalid_parameter(1, "OnError"),
line,
});
}
let order = the_order_of_the_handler(values.get(1), line)?;
match order {
1 => rt.set_error_callback(&name, line),
-1 => {
rt.set_error_callback(&name, line);
rt.order_error_callback(&name, true);
}
_ => rt.remove_error_callback(&name),
}
Ok(Value::Int(1))
}
fn the_order_of_the_handler(value: Option<&Value>, line: u32) -> Result<i32, RuntimeError> {
let Some(value) = value else { return Ok(1) };
if value.to_text().is_empty() {
return Ok(1);
}
let number = value.to_number_strict().map_err(|_| RuntimeError {
message: crate::runtime::wants_a_reference(2, "OnError", "a Number", value),
line,
})?;
Ok(number as i32)
}
pub(crate) fn file_read_line(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let path = values
.get(1)
.map(|value| value.to_text())
.unwrap_or_default();
let number = values
.get(2)
.map(|value| value.to_text())
.unwrap_or_default();
let number: usize = number.trim().parse().unwrap_or(0);
let lines = match rt.desktop().read_file(&path) {
Ok(lines) => lines,
Err(reason) => {
rt.set_the_error_level(Value::Int(1));
rt.note(format!("the file {path} was not read: {reason}"));
let answer = Value::Text(String::new());
crate::v1::write_the_answer(rt, "FileReadLine", values, 0, answer.clone(), line)?;
return Ok(answer);
}
};
rt.set_the_error_level(Value::Int(0));
let answer = match number.checked_sub(1).and_then(|at| lines.get(at)) {
Some(text) => Value::Text(text.clone()),
None => Value::Text(String::new()),
};
crate::v1::write_the_answer(rt, "FileReadLine", values, 0, answer.clone(), line)?;
Ok(answer)
}
pub(crate) fn input(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let options = values
.get(1)
.map(|value| value.to_text())
.unwrap_or_default();
let title = values
.get(2)
.map(|value| value.to_text())
.unwrap_or_default();
let default = values
.get(3)
.map(|value| value.to_text())
.unwrap_or_default();
let options = crate::dialogs::the_options_of_the_input_box(&options, line)?;
let answer = rt
.desktop()
.input_box(&title, &title, &options, &default)
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
let answer = Value::Text(answer.value);
crate::v1::write_the_answer(rt, "Input", values, 0, answer.clone(), line)?;
Ok(answer)
}