use alloc::format;
use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;
use crate::runtime::{Runtime, RuntimeError, RuntimeFailure, TypeName, Value};
#[derive(Clone, Copy)]
pub(crate) enum Kind {
Signed(u8),
Unsigned(u8),
Float32,
Float64,
Struct,
}
pub(crate) fn the_class_of(name: &str) -> Option<(&'static str, i64, Kind)> {
let lowered = name.to_lowercase();
Some(match lowered.as_str() {
"int8" => ("Int8", 1, Kind::Signed(8)),
"uint8" => ("UInt8", 1, Kind::Unsigned(8)),
"int16" => ("Int16", 2, Kind::Signed(16)),
"uint16" => ("UInt16", 2, Kind::Unsigned(16)),
"int32" => ("Int32", 4, Kind::Signed(32)),
"uint32" => ("UInt32", 4, Kind::Unsigned(32)),
"int64" => ("Int64", 8, Kind::Signed(64)),
"intptr" => ("IntPtr", 8, Kind::Signed(64)),
"float32" => ("Float32", 4, Kind::Float32),
"float64" => ("Float64", 8, Kind::Float64),
"struct" => ("Struct", 0, Kind::Struct),
_ => return None,
})
}
pub(crate) fn is_a_class(name: &str) -> bool {
the_class_of(name).is_some()
}
pub(crate) fn names() -> Vec<&'static str> {
vec!["__value", "size"]
}
pub(crate) fn construct(
class: &str,
arguments: &[Value],
_rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
let (spelled, size, kind) = the_class_of(class)?;
let converted = if matches!(kind, Kind::Struct) {
Value::Unset
} else {
let value = arguments.first().cloned().unwrap_or(Value::Int(0));
match the_value_of(kind, &value, spelled, line) {
Ok(converted) => converted,
Err(failure) => return Some(Err(failure)),
}
};
let Value::Object(object) = crate::classes::object(spelled) else {
return None;
};
object.borrow_mut().put(THE_VALUE, converted);
object.borrow_mut().put(THE_SIZE, Value::Int(size));
Some(Ok(Value::Object(object)))
}
pub(crate) fn property(
class: &str,
name: &str,
this: &Value,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
let (_, size, kind) = the_class_of(class)?;
let lowered = name.to_lowercase();
match lowered.as_str() {
"__value" => {
if matches!(kind, Kind::Struct) {
return Some(Err(RuntimeFailure::Error(RuntimeError {
message: "This struct does not define __Value.".to_string(),
line,
})));
}
let Value::Object(object) = this else {
return None;
};
let value = object.borrow().value_of(THE_VALUE).unwrap_or(Value::Unset);
Some(Ok(value))
}
"size" => Some(Ok(Value::Int(size))),
"ptr" => {
let Value::Object(object) = this else {
return None;
};
match object.borrow().value_of(THE_DATA_POINTER) {
Some(address) => Some(Ok(address)),
None => Some(Err(RuntimeFailure::Error(RuntimeError {
message: format!(
"the {class} of this tree holds its value in itself and has no address of its own"
),
line,
}))),
}
}
_ => None,
}
}
fn the_value_of(kind: Kind, value: &Value, name: &str, line: u32) -> Result<Value, RuntimeFailure> {
let number = value.to_number_strict().map_err(|_| {
RuntimeFailure::Error(RuntimeError {
message: crate::runtime::requires(1, name, TypeName::Number, value),
line,
})
})?;
let whole = number as i64;
Ok(match kind {
Kind::Float32 => Value::Float(number as f32 as f64),
Kind::Float64 => Value::Float(number),
Kind::Signed(bits) => {
let cut = match bits {
8 => whole as i8 as i64,
16 => whole as i16 as i64,
32 => whole as i32 as i64,
_ => whole,
};
Value::Int(cut)
}
Kind::Struct => Value::Unset,
Kind::Unsigned(bits) => {
let cut = match bits {
8 => whole as u8 as i64,
16 => whole as u16 as i64,
32 => whole as u32 as i64,
_ => whole as u64 as i64,
};
Value::Int(cut)
}
})
}
const THE_VALUE: &str = "\u{1}typed value";
pub(crate) const THE_DATA_POINTER: &str = "\u{1}data pointer";
const THE_SIZE: &str = "\u{1}typed size";
pub(crate) fn the_view_at(
class: &str,
address: i64,
what: &str,
parameter: usize,
line: u32,
) -> Option<Result<Value, RuntimeError>> {
let (spelled, size, kind) = the_class_of(class)?;
if (address as u64) < 65536 && address != 0 && size != 0 {
return Some(Err(RuntimeError {
message: format!("Parameter #{parameter} of {what} is invalid."),
line,
}));
}
if !matches!(kind, Kind::Struct) {
return Some(Err(RuntimeError {
message: format!(
"the {spelled} of this tree holds its value in itself and not in memory: a view of it at an address has no field to read"
),
line,
}));
}
let Value::Object(object) = crate::classes::object(spelled) else {
return None;
};
object.borrow_mut().put(THE_VALUE, Value::Unset);
object.borrow_mut().put(THE_SIZE, Value::Int(size));
object
.borrow_mut()
.put(THE_DATA_POINTER, Value::Int(address));
Some(Ok(Value::Object(object)))
}
pub(crate) fn the_static_call(
class: &str,
method: &str,
arguments: &[Value],
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
let (spelled, _, _) = the_class_of(class)?;
if !method.eq_ignore_ascii_case("at") {
return None;
}
let what = format!("{spelled}.At");
let address = match arguments.first().and_then(|value| value.to_number()) {
Some(number) => number as i64,
None => {
let value = arguments.first().unwrap_or(&Value::Unset);
return Some(Err(RuntimeFailure::Error(RuntimeError {
message: crate::runtime::requires(
1,
&what,
crate::runtime::TypeName::Number,
value,
),
line,
})));
}
};
match the_view_at(class, address, &what, 1, line)? {
Ok(value) => Some(Ok(value)),
Err(error) => Some(Err(RuntimeFailure::Error(error))),
}
}