2024-11-28 01:10:49 -05:00
|
|
|
use crate::{Destination, Instruction, Operation};
|
2024-11-26 07:14:30 -05:00
|
|
|
|
|
|
|
pub struct LoadConstant {
|
2024-11-28 01:10:49 -05:00
|
|
|
pub destination: Destination,
|
2024-11-26 07:14:30 -05:00
|
|
|
pub constant_index: u16,
|
|
|
|
pub jump_next: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&Instruction> for LoadConstant {
|
|
|
|
fn from(instruction: &Instruction) -> Self {
|
2024-11-28 01:10:49 -05:00
|
|
|
let destination = if instruction.a_is_local() {
|
|
|
|
Destination::Local(instruction.a())
|
|
|
|
} else {
|
|
|
|
Destination::Register(instruction.a())
|
|
|
|
};
|
|
|
|
|
2024-11-26 07:14:30 -05:00
|
|
|
LoadConstant {
|
2024-11-28 01:10:49 -05:00
|
|
|
destination,
|
2024-11-26 07:14:30 -05:00
|
|
|
constant_index: instruction.b(),
|
|
|
|
jump_next: instruction.c_as_boolean(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<LoadConstant> for Instruction {
|
|
|
|
fn from(load_constant: LoadConstant) -> Self {
|
2024-11-28 01:10:49 -05:00
|
|
|
let (a, a_is_local) = match load_constant.destination {
|
|
|
|
Destination::Local(local) => (local, true),
|
|
|
|
Destination::Register(register) => (register, false),
|
|
|
|
};
|
|
|
|
|
2024-11-26 07:14:30 -05:00
|
|
|
*Instruction::new(Operation::LoadConstant)
|
2024-11-28 01:10:49 -05:00
|
|
|
.set_a(a)
|
|
|
|
.set_a_is_local(a_is_local)
|
2024-11-26 07:14:30 -05:00
|
|
|
.set_b(load_constant.constant_index)
|
|
|
|
.set_c_to_boolean(load_constant.jump_next)
|
|
|
|
}
|
|
|
|
}
|