use alloc::format;
use alloc::string::{String, ToString};
use crate::ast::AssignOp;
use crate::runtime::{Runtime, RuntimeError, Value};
#[derive(Clone, Copy)]
enum TheValue {
Text(&'static str),
Number(i64),
TheTrim,
TheFormats,
TheCaseSense,
TheBatchLines,
TheFlag(&'static str),
TheEncoding,
Refused(&'static str),
}
const NOT_A_WINDOWS: &str = "this name is about Windows, and this machine is not.";
const NO_ADDRESS: &str = "this desktop does not know the address of the network of the machine.";
const THE_VARIABLES: &[(&str, TheValue)] = &[
("A_Args", TheValue::Text("")),
("A_AutoTrim", TheValue::TheTrim),
("A_BatchLines", TheValue::Text("10ms")),
("A_CaretX", TheValue::Text("")),
("A_CaretY", TheValue::Text("")),
("A_DefaultGui", TheValue::Number(1)),
("A_DefaultListView", TheValue::Text("")),
("A_DefaultTreeView", TheValue::Text("")),
("A_DetectHiddenText", TheValue::TheFlag("detecthiddentext")),
(
"A_DetectHiddenWindows",
TheValue::TheFlag("detecthiddenwindows"),
),
("A_ExitReason", TheValue::Text("")),
("A_FileEncoding", TheValue::TheEncoding),
("A_FormatFloat", TheValue::TheFormats),
("A_FormatInteger", TheValue::TheFormats),
("A_Gui", TheValue::Text("")),
("A_GuiControl", TheValue::Text("")),
("A_GuiControlEvent", TheValue::Text("")),
("A_GuiEvent", TheValue::Text("")),
("A_GuiHeight", TheValue::Text("")),
("A_GuiWidth", TheValue::Text("")),
("A_GuiX", TheValue::Text("")),
("A_GuiY", TheValue::Text("")),
("A_IPAddress1", TheValue::Refused(NO_ADDRESS)),
("A_IsUnicode", TheValue::Number(1)),
("A_MenuMaskKey", TheValue::Text("")),
("A_NumBatchLines", TheValue::TheBatchLines),
("A_OSType", TheValue::Refused(NOT_A_WINDOWS)),
("A_StringCaseSense", TheValue::TheCaseSense),
(
"A_StoreCapsLockMode",
TheValue::TheFlag("storecapslockmode"),
),
("ErrorLevel", TheValue::Number(0)),
("A_ThisLabel", TheValue::Text("")),
("A_ThisMenu", TheValue::Text("")),
("A_ThisMenuItem", TheValue::Text("")),
("A_ThisMenuItemPos", TheValue::Text("")),
];
const NOT_AN_OUTPUT: &str = "Not allowed as an output variable.";
const THE_WRITABLE: &[&str] = &["a_args", "a_clipboard", "clipboard", "errorlevel"];
pub fn is_read_only(name: &str) -> bool {
let wanted = name.to_lowercase();
if THE_WRITABLE.contains(&wanted.as_str()) {
return false;
}
if wanted == "comspec" || wanted == "programfiles" {
return true;
}
let Some(stem) = wanted.strip_prefix("a_") else {
return false;
};
if NOT_A_VARIABLE_OF_THE_V1.contains(&stem) {
return false;
}
crate::variables::carries(stem) || carries(&wanted)
}
const THE_WITHOUT_THE_PREFIX: &[&str] = &["clipboard", "comspec", "errorlevel", "programfiles"];
const NOT_A_VARIABLE_OF_THE_V1: &[&str] = &[
"allowmainwindow",
"hotkeyinterval",
"hotkeymodifiertimeout",
"maxhotkeysperinterval",
"menumaskkey",
"traymenu",
];
pub fn may_not_be_declared(name: &str) -> bool {
let wanted = name.to_lowercase();
if THE_WITHOUT_THE_PREFIX.contains(&wanted.as_str()) {
return true;
}
let Some(stem) = wanted.strip_prefix("a_") else {
return false;
};
if stem == "args" {
return false;
}
if NOT_A_VARIABLE_OF_THE_V1.contains(&stem) {
return false;
}
crate::variables::carries(stem) || carries(&wanted)
}
pub fn carries(name: &str) -> bool {
let wanted = name.to_lowercase();
THE_VARIABLES
.iter()
.any(|(carried, _)| carried.to_lowercase() == wanted)
}
pub fn refusal_of(name: &str) -> Option<&'static str> {
let wanted = name.to_lowercase();
for (carried, value) in THE_VARIABLES {
if carried.to_lowercase() != wanted {
continue;
}
return match value {
TheValue::Refused(reason) => Some(*reason),
_ => None,
};
}
None
}
pub fn lookup(name: &str, rt: &mut Runtime) -> Option<Result<Value, RuntimeError>> {
let wanted = name.to_lowercase();
for (carried, value) in THE_VARIABLES {
if carried.to_lowercase() != wanted {
continue;
}
return Some(match value {
TheValue::Text(text) => Ok(Value::Text((*text).to_string())),
TheValue::Number(number) => Ok(Value::Int(*number)),
TheValue::TheTrim => Ok(Value::Text(
if rt.auto_trim() { "On" } else { "Off" }.to_string(),
)),
TheValue::TheFormats => Ok(Value::Text(if wanted.starts_with("a_formatfloat") {
rt.float_format().name()
} else {
rt.integer_format().name().to_string()
})),
TheValue::TheCaseSense => Ok(Value::Text(rt.case_sense().name().to_string())),
TheValue::TheBatchLines => Ok(Value::Text(rt.batch_lines().to_string())),
TheValue::TheFlag(name) => {
let on = match crate::settings::value_of(name) {
Some(Ok(Value::Int(number))) => number != 0,
_ => false,
};
Ok(Value::Text(if on { "On" } else { "Off" }.to_string()))
}
TheValue::TheEncoding => {
let encoding = crate::settings::file_encoding();
if encoding == "CP0" {
Ok(Value::Text(String::new()))
} else {
Ok(Value::Text(encoding.to_string()))
}
}
TheValue::Refused(reason) => Err(RuntimeError {
message: (*reason).to_string(),
line: 0,
}),
});
}
None
}
pub fn write(
name: &str,
_op: AssignOp,
_value: &Value,
line: u32,
_rt: &mut Runtime,
) -> Option<Result<Value, RuntimeError>> {
let wanted = name.to_lowercase();
if wanted == "a_args" {
return None;
}
if wanted == "errorlevel" {
return None;
}
if !is_read_only(&wanted) {
return None;
}
let message = format!("{NOT_AN_OUTPUT}\n Specifically: {name}");
Some(Err(RuntimeError { message, line }))
}