use alloc::{format, string::String};
use ellie_core::defs::PlatformArchitecture;
use crate::{
heap_memory::HeapMemory,
instruction_utils::A2O,
raw_type::StaticRawType,
stack::Stack,
stack_memory::StackMemory,
utils::{AddressingValues, ThreadPanicReason},
};
use super::{ExecuterPanic, ExecuterResult, StaticProgram};
impl super::InstructionExecuter for A2O {
fn execute(
&self,
heap_memory: &mut HeapMemory,
_program: StaticProgram,
current_stack: &mut Stack,
_stack_memory: &mut StackMemory,
addressing_value: &AddressingValues,
_arch: PlatformArchitecture,
) -> Result<super::ExecuterResult, super::ExecuterPanic> {
match addressing_value {
AddressingValues::Implicit => {
match current_stack.registers.A.type_id.id {
7 => (),
13 => {
let pointer = current_stack.registers.B.to_int() as usize;
let mref = match heap_memory.get(&pointer) {
Some(e) => e.clone(),
None => {
return Err(ExecuterPanic {
reason: ThreadPanicReason::NullReference(pointer),
code_location: format!("{}:{}", file!(), line!()),
})
}
};
match mref.type_id.id {
6 => {
let a_value = String::from_utf8(mref.data).unwrap();
current_stack.registers.A =
StaticRawType::from_bool(!a_value.is_empty());
}
9 | 11 | 12 => {
return Err(ExecuterPanic {
reason: ThreadPanicReason::CannotConvertToType(
mref.type_id.id,
7,
),
code_location: format!("{}:{}", file!(), line!()),
});
}
e => {
return Err(ExecuterPanic {
reason: ThreadPanicReason::CannotConvertToType(e, 7),
code_location: format!("{}:{}", file!(), line!()),
});
}
}
}
e => {
return Err(ExecuterPanic {
reason: ThreadPanicReason::CannotConvertToType(e, 7),
code_location: format!("{}:{}", file!(), line!()),
});
}
};
Ok(ExecuterResult::Continue)
}
_ => Err(ExecuterPanic {
reason: ThreadPanicReason::IllegalAddressingValue,
code_location: format!("{}:{}", file!(), line!()),
}),
}
}
}