refactor(vm): update up codegen, remove duplicated enums

This commit is contained in:
Rachel Powers
2024-05-14 16:26:23 -07:00
parent 708d68e38a
commit 25b7046ed2
21 changed files with 695 additions and 3070 deletions

File diff suppressed because one or more lines are too long

View File

@@ -522,7 +522,11 @@ impl Device {
pub fn get_fields(&self, vm: &VM) -> BTreeMap<LogicType, LogicField> {
let mut copy = self.fields.clone();
if let Some(ic_id) = &self.ic {
let ic = vm.circuit_holders.get(ic_id).expect("our own ic to exist").borrow();
let ic = vm
.circuit_holders
.get(ic_id)
.expect("our own ic to exist")
.borrow();
copy.insert(
LogicType::LineNumber,
LogicField {

View File

@@ -1,4 +1,10 @@
use crate::vm::{instructions::enums::InstructionOp, object::{errors::{LogicError, MemoryError}, ObjectID}};
use crate::vm::{
instructions::enums::InstructionOp,
object::{
errors::{LogicError, MemoryError},
ObjectID,
},
};
use serde_derive::{Deserialize, Serialize};
use std::error::Error as StdError;
use std::fmt::Display;
@@ -38,7 +44,6 @@ pub enum VMError {
NotProgrammable(ObjectID),
}
#[derive(Error, Debug, Serialize, Deserialize)]
pub enum TemplateError {
#[error("object id {0} has a non conforming set of interfaces")]

View File

@@ -246,9 +246,9 @@ impl Program {
}
}
pub fn get_line(&self, line: u32) -> Result<&Instruction, ICError> {
pub fn get_line(&self, line: usize) -> Result<&Instruction, ICError> {
self.instructions
.get(line as usize)
.get(line)
.ok_or(ICError::InstructionPointerOutOfRange(line))
}
}
@@ -2030,7 +2030,8 @@ impl IC {
if ic_id == &this.id {
this.peek_addr(addr)
} else {
let ic = vm.circuit_holders.get(ic_id).unwrap().borrow();
let ic =
vm.circuit_holders.get(ic_id).unwrap().borrow();
ic.peek_addr(addr)
}
}?;
@@ -2063,7 +2064,8 @@ impl IC {
if ic_id == &this.id {
this.peek_addr(addr)
} else {
let ic = vm.circuit_holders.get(ic_id).unwrap().borrow();
let ic =
vm.circuit_holders.get(ic_id).unwrap().borrow();
ic.peek_addr(addr)
}
}?;

View File

@@ -204,7 +204,7 @@ impl Connection {
}
}
#[derive(ObjectInterface!, Debug, Serialize, Deserialize)]
#[derive(ObjectInterface!, Debug)]
#[custom(implements(Object { Storage, Logicable, Network}))]
pub struct CableNetwork {
#[custom(object_id)]
@@ -216,8 +216,7 @@ pub struct CableNetwork {
/// required by object interface but atm unused by network
pub name: Name,
#[custom(object_vm_ref)]
#[serde(skip)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
/// data enabled objects (must be devices)
pub devices: HashSet<ObjectID>,
/// power only connections
@@ -387,14 +386,14 @@ impl Network for CableNetwork {
}
fn get_devices(&self) -> Vec<ObjectID> {
self.devices.iter().copied().collect_vec()
self.devices.iter().copied().collect_vec()
}
fn get_power_only(&self) -> Vec<ObjectID> {
self.power_only.iter().copied().collect_vec()
}
fn get_channel_data(&self) -> &[f64; 8] {
fn get_channel_data(&self) -> &[f64; 8] {
&self.channels
}
}
@@ -429,21 +428,6 @@ impl From<NetworkRef<'_>> for FrozenCableNetwork {
power_only: value.get_power_only(),
channels: *value.get_channel_data(),
}
}
}
impl From<FrozenCableNetwork> for CableNetwork {
fn from(value: FrozenCableNetwork) -> Self {
CableNetwork {
id: value.id,
prefab: Name::new(""),
name: Name::new(""),
vm: None,
devices: value.devices.into_iter().collect(),
power_only: value.power_only.into_iter().collect(),
channels: value.channels,
}
}
}
@@ -454,17 +438,28 @@ pub enum NetworkError {
}
impl CableNetwork {
pub fn new(id: u32) -> Self {
pub fn new(id: u32, vm: Rc<VM>) -> Self {
CableNetwork {
id,
prefab: Name::new(""),
name: Name::new(""),
vm: None,
vm,
devices: HashSet::new(),
power_only: HashSet::new(),
channels: [f64::NAN; 8],
}
}
pub fn from_frozen(value: FrozenCableNetwork, vm: Rc<VM>) -> Self {
CableNetwork {
id: value.id,
prefab: Name::new(""),
name: Name::new(""),
vm,
devices: value.devices.into_iter().collect(),
power_only: value.power_only.into_iter().collect(),
channels: value.channels,
}
}
pub fn set_channel(&mut self, chan: usize, val: f64) -> Result<f64, NetworkError> {
if chan > 7 {

View File

@@ -3,7 +3,6 @@ pub mod instructions;
pub mod object;
use crate::{
device::Device,
errors::{ICError, TemplateError, VMError},
interpreter::ICState,
network::{CableConnectionType, CableNetwork, Connection, ConnectionRole, FrozenCableNetwork},
@@ -80,7 +79,7 @@ impl VM {
operation_modified: RefCell::new(Vec::new()),
});
let default_network = VMObject::new(CableNetwork::new(default_network_key), vm.clone());
let default_network = VMObject::new(CableNetwork::new(default_network_key, vm.clone()));
vm.networks
.borrow_mut()
.insert(default_network_key, default_network);
@@ -138,7 +137,7 @@ impl VM {
let next_id = self.network_id_space.borrow_mut().next();
self.networks.borrow_mut().insert(
next_id,
VMObject::new(CableNetwork::new(next_id), self.clone()),
VMObject::new(CableNetwork::new(next_id, self.clone())),
);
next_id
}
@@ -884,10 +883,7 @@ impl VM {
.map(|network| {
(
network.id,
VMObject::new(
std::convert::Into::<CableNetwork>::into(network),
self.clone(),
),
VMObject::new(CableNetwork::from_frozen(network, self.clone())),
)
})
.collect();
@@ -1009,7 +1005,7 @@ impl VMTransaction {
self.id_space.next()
};
let obj = template.build(obj_id, self.vm.clone());
let obj = template.build(obj_id, &self.vm);
if let Some(storage) = obj.borrow_mut().as_mut_storage() {
for (slot_index, occupant_template) in

File diff suppressed because it is too large Load Diff

View File

@@ -8679,11 +8679,6 @@ The ProKompile can stack a wide variety of things such as <link=IngotPage><color
props(name = r#"Kit (Power Umbilical)"#, desc = r#""#, value = "1603046970")
)]
ItemKitElectricUmbilical = 1603046970i32,
#[strum(
serialize = "ItemJetpackTurbine",
props(name = r#"Turbine Jetpack"#, desc = r#""#, value = "1604261183")
)]
ItemJetpackTurbine = 1604261183i32,
#[strum(
serialize = "Lander",
props(name = r#"Lander"#, desc = r#""#, value = "1605130615")

View File

@@ -24,4 +24,3 @@ pub static CONSTANTS_LOOKUP: phf::Map<&'static str, f64> = phf_map! {
"pinf" => f64::INFINITY,
"pi" => 3.141592653589793f64,
};

View File

@@ -887,7 +887,6 @@ impl InstructionOp {
pub fn execute<T>(
&self,
ic: &mut T,
vm: &crate::vm::VM,
operands: &[crate::vm::instructions::operands::Operand],
) -> Result<(), crate::errors::ICError>
where
@@ -902,97 +901,88 @@ impl InstructionOp {
}
match self {
Self::Nop => Ok(()),
Self::Abs => ic.execute_abs(vm, &operands[0], &operands[1]),
Self::Acos => ic.execute_acos(vm, &operands[0], &operands[1]),
Self::Add => ic.execute_add(vm, &operands[0], &operands[1], &operands[2]),
Self::Alias => ic.execute_alias(vm, &operands[0], &operands[1]),
Self::And => ic.execute_and(vm, &operands[0], &operands[1], &operands[2]),
Self::Asin => ic.execute_asin(vm, &operands[0], &operands[1]),
Self::Atan => ic.execute_atan(vm, &operands[0], &operands[1]),
Self::Atan2 => ic.execute_atan2(vm, &operands[0], &operands[1], &operands[2]),
Self::Bap => ic.execute_bap(vm, &operands[0], &operands[1], &operands[2], &operands[3]),
Self::Bapal => {
ic.execute_bapal(vm, &operands[0], &operands[1], &operands[2], &operands[3])
}
Self::Bapz => ic.execute_bapz(vm, &operands[0], &operands[1], &operands[2]),
Self::Bapzal => ic.execute_bapzal(vm, &operands[0], &operands[1], &operands[2]),
Self::Bdns => ic.execute_bdns(vm, &operands[0], &operands[1]),
Self::Bdnsal => ic.execute_bdnsal(vm, &operands[0], &operands[1]),
Self::Bdse => ic.execute_bdse(vm, &operands[0], &operands[1]),
Self::Bdseal => ic.execute_bdseal(vm, &operands[0], &operands[1]),
Self::Beq => ic.execute_beq(vm, &operands[0], &operands[1], &operands[2]),
Self::Beqal => ic.execute_beqal(vm, &operands[0], &operands[1], &operands[2]),
Self::Beqz => ic.execute_beqz(vm, &operands[0], &operands[1]),
Self::Beqzal => ic.execute_beqzal(vm, &operands[0], &operands[1]),
Self::Bge => ic.execute_bge(vm, &operands[0], &operands[1], &operands[2]),
Self::Bgeal => ic.execute_bgeal(vm, &operands[0], &operands[1], &operands[2]),
Self::Bgez => ic.execute_bgez(vm, &operands[0], &operands[1]),
Self::Bgezal => ic.execute_bgezal(vm, &operands[0], &operands[1]),
Self::Bgt => ic.execute_bgt(vm, &operands[0], &operands[1], &operands[2]),
Self::Bgtal => ic.execute_bgtal(vm, &operands[0], &operands[1], &operands[2]),
Self::Bgtz => ic.execute_bgtz(vm, &operands[0], &operands[1]),
Self::Bgtzal => ic.execute_bgtzal(vm, &operands[0], &operands[1]),
Self::Ble => ic.execute_ble(vm, &operands[0], &operands[1], &operands[2]),
Self::Bleal => ic.execute_bleal(vm, &operands[0], &operands[1], &operands[2]),
Self::Blez => ic.execute_blez(vm, &operands[0], &operands[1]),
Self::Blezal => ic.execute_blezal(vm, &operands[0], &operands[1]),
Self::Blt => ic.execute_blt(vm, &operands[0], &operands[1], &operands[2]),
Self::Bltal => ic.execute_bltal(vm, &operands[0], &operands[1], &operands[2]),
Self::Bltz => ic.execute_bltz(vm, &operands[0], &operands[1]),
Self::Bltzal => ic.execute_bltzal(vm, &operands[0], &operands[1]),
Self::Bna => ic.execute_bna(vm, &operands[0], &operands[1], &operands[2], &operands[3]),
Self::Bnaal => {
ic.execute_bnaal(vm, &operands[0], &operands[1], &operands[2], &operands[3])
}
Self::Bnan => ic.execute_bnan(vm, &operands[0], &operands[1]),
Self::Bnaz => ic.execute_bnaz(vm, &operands[0], &operands[1], &operands[2]),
Self::Bnazal => ic.execute_bnazal(vm, &operands[0], &operands[1], &operands[2]),
Self::Bne => ic.execute_bne(vm, &operands[0], &operands[1], &operands[2]),
Self::Bneal => ic.execute_bneal(vm, &operands[0], &operands[1], &operands[2]),
Self::Bnez => ic.execute_bnez(vm, &operands[0], &operands[1]),
Self::Bnezal => ic.execute_bnezal(vm, &operands[0], &operands[1]),
Self::Brap => {
ic.execute_brap(vm, &operands[0], &operands[1], &operands[2], &operands[3])
}
Self::Brapz => ic.execute_brapz(vm, &operands[0], &operands[1], &operands[2]),
Self::Brdns => ic.execute_brdns(vm, &operands[0], &operands[1]),
Self::Brdse => ic.execute_brdse(vm, &operands[0], &operands[1]),
Self::Breq => ic.execute_breq(vm, &operands[0], &operands[1], &operands[2]),
Self::Breqz => ic.execute_breqz(vm, &operands[0], &operands[1]),
Self::Brge => ic.execute_brge(vm, &operands[0], &operands[1], &operands[2]),
Self::Brgez => ic.execute_brgez(vm, &operands[0], &operands[1]),
Self::Brgt => ic.execute_brgt(vm, &operands[0], &operands[1], &operands[2]),
Self::Brgtz => ic.execute_brgtz(vm, &operands[0], &operands[1]),
Self::Brle => ic.execute_brle(vm, &operands[0], &operands[1], &operands[2]),
Self::Brlez => ic.execute_brlez(vm, &operands[0], &operands[1]),
Self::Brlt => ic.execute_brlt(vm, &operands[0], &operands[1], &operands[2]),
Self::Brltz => ic.execute_brltz(vm, &operands[0], &operands[1]),
Self::Brna => {
ic.execute_brna(vm, &operands[0], &operands[1], &operands[2], &operands[3])
}
Self::Brnan => ic.execute_brnan(vm, &operands[0], &operands[1]),
Self::Brnaz => ic.execute_brnaz(vm, &operands[0], &operands[1], &operands[2]),
Self::Brne => ic.execute_brne(vm, &operands[0], &operands[1], &operands[2]),
Self::Brnez => ic.execute_brnez(vm, &operands[0], &operands[1]),
Self::Ceil => ic.execute_ceil(vm, &operands[0], &operands[1]),
Self::Clr => ic.execute_clr(vm, &operands[0]),
Self::Clrd => ic.execute_clrd(vm, &operands[0]),
Self::Cos => ic.execute_cos(vm, &operands[0], &operands[1]),
Self::Define => ic.execute_define(vm, &operands[0], &operands[1]),
Self::Div => ic.execute_div(vm, &operands[0], &operands[1], &operands[2]),
Self::Exp => ic.execute_exp(vm, &operands[0], &operands[1]),
Self::Floor => ic.execute_floor(vm, &operands[0], &operands[1]),
Self::Get => ic.execute_get(vm, &operands[0], &operands[1], &operands[2]),
Self::Getd => ic.execute_getd(vm, &operands[0], &operands[1], &operands[2]),
Self::Hcf => ic.execute_hcf(vm),
Self::J => ic.execute_j(vm, &operands[0]),
Self::Jal => ic.execute_jal(vm, &operands[0]),
Self::Jr => ic.execute_jr(vm, &operands[0]),
Self::L => ic.execute_l(vm, &operands[0], &operands[1], &operands[2]),
Self::Label => ic.execute_label(vm, &operands[0], &operands[1]),
Self::Lb => ic.execute_lb(vm, &operands[0], &operands[1], &operands[2], &operands[3]),
Self::Abs => ic.execute_abs(&operands[0], &operands[1]),
Self::Acos => ic.execute_acos(&operands[0], &operands[1]),
Self::Add => ic.execute_add(&operands[0], &operands[1], &operands[2]),
Self::Alias => ic.execute_alias(&operands[0], &operands[1]),
Self::And => ic.execute_and(&operands[0], &operands[1], &operands[2]),
Self::Asin => ic.execute_asin(&operands[0], &operands[1]),
Self::Atan => ic.execute_atan(&operands[0], &operands[1]),
Self::Atan2 => ic.execute_atan2(&operands[0], &operands[1], &operands[2]),
Self::Bap => ic.execute_bap(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Bapal => ic.execute_bapal(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Bapz => ic.execute_bapz(&operands[0], &operands[1], &operands[2]),
Self::Bapzal => ic.execute_bapzal(&operands[0], &operands[1], &operands[2]),
Self::Bdns => ic.execute_bdns(&operands[0], &operands[1]),
Self::Bdnsal => ic.execute_bdnsal(&operands[0], &operands[1]),
Self::Bdse => ic.execute_bdse(&operands[0], &operands[1]),
Self::Bdseal => ic.execute_bdseal(&operands[0], &operands[1]),
Self::Beq => ic.execute_beq(&operands[0], &operands[1], &operands[2]),
Self::Beqal => ic.execute_beqal(&operands[0], &operands[1], &operands[2]),
Self::Beqz => ic.execute_beqz(&operands[0], &operands[1]),
Self::Beqzal => ic.execute_beqzal(&operands[0], &operands[1]),
Self::Bge => ic.execute_bge(&operands[0], &operands[1], &operands[2]),
Self::Bgeal => ic.execute_bgeal(&operands[0], &operands[1], &operands[2]),
Self::Bgez => ic.execute_bgez(&operands[0], &operands[1]),
Self::Bgezal => ic.execute_bgezal(&operands[0], &operands[1]),
Self::Bgt => ic.execute_bgt(&operands[0], &operands[1], &operands[2]),
Self::Bgtal => ic.execute_bgtal(&operands[0], &operands[1], &operands[2]),
Self::Bgtz => ic.execute_bgtz(&operands[0], &operands[1]),
Self::Bgtzal => ic.execute_bgtzal(&operands[0], &operands[1]),
Self::Ble => ic.execute_ble(&operands[0], &operands[1], &operands[2]),
Self::Bleal => ic.execute_bleal(&operands[0], &operands[1], &operands[2]),
Self::Blez => ic.execute_blez(&operands[0], &operands[1]),
Self::Blezal => ic.execute_blezal(&operands[0], &operands[1]),
Self::Blt => ic.execute_blt(&operands[0], &operands[1], &operands[2]),
Self::Bltal => ic.execute_bltal(&operands[0], &operands[1], &operands[2]),
Self::Bltz => ic.execute_bltz(&operands[0], &operands[1]),
Self::Bltzal => ic.execute_bltzal(&operands[0], &operands[1]),
Self::Bna => ic.execute_bna(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Bnaal => ic.execute_bnaal(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Bnan => ic.execute_bnan(&operands[0], &operands[1]),
Self::Bnaz => ic.execute_bnaz(&operands[0], &operands[1], &operands[2]),
Self::Bnazal => ic.execute_bnazal(&operands[0], &operands[1], &operands[2]),
Self::Bne => ic.execute_bne(&operands[0], &operands[1], &operands[2]),
Self::Bneal => ic.execute_bneal(&operands[0], &operands[1], &operands[2]),
Self::Bnez => ic.execute_bnez(&operands[0], &operands[1]),
Self::Bnezal => ic.execute_bnezal(&operands[0], &operands[1]),
Self::Brap => ic.execute_brap(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Brapz => ic.execute_brapz(&operands[0], &operands[1], &operands[2]),
Self::Brdns => ic.execute_brdns(&operands[0], &operands[1]),
Self::Brdse => ic.execute_brdse(&operands[0], &operands[1]),
Self::Breq => ic.execute_breq(&operands[0], &operands[1], &operands[2]),
Self::Breqz => ic.execute_breqz(&operands[0], &operands[1]),
Self::Brge => ic.execute_brge(&operands[0], &operands[1], &operands[2]),
Self::Brgez => ic.execute_brgez(&operands[0], &operands[1]),
Self::Brgt => ic.execute_brgt(&operands[0], &operands[1], &operands[2]),
Self::Brgtz => ic.execute_brgtz(&operands[0], &operands[1]),
Self::Brle => ic.execute_brle(&operands[0], &operands[1], &operands[2]),
Self::Brlez => ic.execute_brlez(&operands[0], &operands[1]),
Self::Brlt => ic.execute_brlt(&operands[0], &operands[1], &operands[2]),
Self::Brltz => ic.execute_brltz(&operands[0], &operands[1]),
Self::Brna => ic.execute_brna(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Brnan => ic.execute_brnan(&operands[0], &operands[1]),
Self::Brnaz => ic.execute_brnaz(&operands[0], &operands[1], &operands[2]),
Self::Brne => ic.execute_brne(&operands[0], &operands[1], &operands[2]),
Self::Brnez => ic.execute_brnez(&operands[0], &operands[1]),
Self::Ceil => ic.execute_ceil(&operands[0], &operands[1]),
Self::Clr => ic.execute_clr(&operands[0]),
Self::Clrd => ic.execute_clrd(&operands[0]),
Self::Cos => ic.execute_cos(&operands[0], &operands[1]),
Self::Define => ic.execute_define(&operands[0], &operands[1]),
Self::Div => ic.execute_div(&operands[0], &operands[1], &operands[2]),
Self::Exp => ic.execute_exp(&operands[0], &operands[1]),
Self::Floor => ic.execute_floor(&operands[0], &operands[1]),
Self::Get => ic.execute_get(&operands[0], &operands[1], &operands[2]),
Self::Getd => ic.execute_getd(&operands[0], &operands[1], &operands[2]),
Self::Hcf => ic.execute_hcf(),
Self::J => ic.execute_j(&operands[0]),
Self::Jal => ic.execute_jal(&operands[0]),
Self::Jr => ic.execute_jr(&operands[0]),
Self::L => ic.execute_l(&operands[0], &operands[1], &operands[2]),
Self::Label => ic.execute_label(&operands[0], &operands[1]),
Self::Lb => ic.execute_lb(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Lbn => ic.execute_lbn(
vm,
&operands[0],
&operands[1],
&operands[2],
@@ -1000,7 +990,6 @@ impl InstructionOp {
&operands[4],
),
Self::Lbns => ic.execute_lbns(
vm,
&operands[0],
&operands[1],
&operands[2],
@@ -1009,74 +998,73 @@ impl InstructionOp {
&operands[5],
),
Self::Lbs => ic.execute_lbs(
vm,
&operands[0],
&operands[1],
&operands[2],
&operands[3],
&operands[4],
),
Self::Ld => ic.execute_ld(vm, &operands[0], &operands[1], &operands[2]),
Self::Log => ic.execute_log(vm, &operands[0], &operands[1]),
Self::Lr => ic.execute_lr(vm, &operands[0], &operands[1], &operands[2], &operands[3]),
Self::Ls => ic.execute_ls(vm, &operands[0], &operands[1], &operands[2], &operands[3]),
Self::Max => ic.execute_max(vm, &operands[0], &operands[1], &operands[2]),
Self::Min => ic.execute_min(vm, &operands[0], &operands[1], &operands[2]),
Self::Mod => ic.execute_mod(vm, &operands[0], &operands[1], &operands[2]),
Self::Move => ic.execute_move(vm, &operands[0], &operands[1]),
Self::Mul => ic.execute_mul(vm, &operands[0], &operands[1], &operands[2]),
Self::Nor => ic.execute_nor(vm, &operands[0], &operands[1], &operands[2]),
Self::Not => ic.execute_not(vm, &operands[0], &operands[1]),
Self::Or => ic.execute_or(vm, &operands[0], &operands[1], &operands[2]),
Self::Peek => ic.execute_peek(vm, &operands[0]),
Self::Poke => ic.execute_poke(vm, &operands[0], &operands[1]),
Self::Pop => ic.execute_pop(vm, &operands[0]),
Self::Push => ic.execute_push(vm, &operands[0]),
Self::Put => ic.execute_put(vm, &operands[0], &operands[1], &operands[2]),
Self::Putd => ic.execute_putd(vm, &operands[0], &operands[1], &operands[2]),
Self::Rand => ic.execute_rand(vm, &operands[0]),
Self::Round => ic.execute_round(vm, &operands[0], &operands[1]),
Self::S => ic.execute_s(vm, &operands[0], &operands[1], &operands[2]),
Self::Sap => ic.execute_sap(vm, &operands[0], &operands[1], &operands[2], &operands[3]),
Self::Sapz => ic.execute_sapz(vm, &operands[0], &operands[1], &operands[2]),
Self::Sb => ic.execute_sb(vm, &operands[0], &operands[1], &operands[2]),
Self::Sbn => ic.execute_sbn(vm, &operands[0], &operands[1], &operands[2], &operands[3]),
Self::Sbs => ic.execute_sbs(vm, &operands[0], &operands[1], &operands[2], &operands[3]),
Self::Sd => ic.execute_sd(vm, &operands[0], &operands[1], &operands[2]),
Self::Sdns => ic.execute_sdns(vm, &operands[0], &operands[1]),
Self::Sdse => ic.execute_sdse(vm, &operands[0], &operands[1]),
Self::Ld => ic.execute_ld(&operands[0], &operands[1], &operands[2]),
Self::Log => ic.execute_log(&operands[0], &operands[1]),
Self::Lr => ic.execute_lr(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Ls => ic.execute_ls(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Max => ic.execute_max(&operands[0], &operands[1], &operands[2]),
Self::Min => ic.execute_min(&operands[0], &operands[1], &operands[2]),
Self::Mod => ic.execute_mod(&operands[0], &operands[1], &operands[2]),
Self::Move => ic.execute_move(&operands[0], &operands[1]),
Self::Mul => ic.execute_mul(&operands[0], &operands[1], &operands[2]),
Self::Nor => ic.execute_nor(&operands[0], &operands[1], &operands[2]),
Self::Not => ic.execute_not(&operands[0], &operands[1]),
Self::Or => ic.execute_or(&operands[0], &operands[1], &operands[2]),
Self::Peek => ic.execute_peek(&operands[0]),
Self::Poke => ic.execute_poke(&operands[0], &operands[1]),
Self::Pop => ic.execute_pop(&operands[0]),
Self::Push => ic.execute_push(&operands[0]),
Self::Put => ic.execute_put(&operands[0], &operands[1], &operands[2]),
Self::Putd => ic.execute_putd(&operands[0], &operands[1], &operands[2]),
Self::Rand => ic.execute_rand(&operands[0]),
Self::Round => ic.execute_round(&operands[0], &operands[1]),
Self::S => ic.execute_s(&operands[0], &operands[1], &operands[2]),
Self::Sap => ic.execute_sap(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Sapz => ic.execute_sapz(&operands[0], &operands[1], &operands[2]),
Self::Sb => ic.execute_sb(&operands[0], &operands[1], &operands[2]),
Self::Sbn => ic.execute_sbn(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Sbs => ic.execute_sbs(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Sd => ic.execute_sd(&operands[0], &operands[1], &operands[2]),
Self::Sdns => ic.execute_sdns(&operands[0], &operands[1]),
Self::Sdse => ic.execute_sdse(&operands[0], &operands[1]),
Self::Select => {
ic.execute_select(vm, &operands[0], &operands[1], &operands[2], &operands[3])
ic.execute_select(&operands[0], &operands[1], &operands[2], &operands[3])
}
Self::Seq => ic.execute_seq(vm, &operands[0], &operands[1], &operands[2]),
Self::Seqz => ic.execute_seqz(vm, &operands[0], &operands[1]),
Self::Sge => ic.execute_sge(vm, &operands[0], &operands[1], &operands[2]),
Self::Sgez => ic.execute_sgez(vm, &operands[0], &operands[1]),
Self::Sgt => ic.execute_sgt(vm, &operands[0], &operands[1], &operands[2]),
Self::Sgtz => ic.execute_sgtz(vm, &operands[0], &operands[1]),
Self::Sin => ic.execute_sin(vm, &operands[0], &operands[1]),
Self::Sla => ic.execute_sla(vm, &operands[0], &operands[1], &operands[2]),
Self::Sle => ic.execute_sle(vm, &operands[0], &operands[1], &operands[2]),
Self::Sleep => ic.execute_sleep(vm, &operands[0]),
Self::Slez => ic.execute_slez(vm, &operands[0], &operands[1]),
Self::Sll => ic.execute_sll(vm, &operands[0], &operands[1], &operands[2]),
Self::Slt => ic.execute_slt(vm, &operands[0], &operands[1], &operands[2]),
Self::Sltz => ic.execute_sltz(vm, &operands[0], &operands[1]),
Self::Sna => ic.execute_sna(vm, &operands[0], &operands[1], &operands[2], &operands[3]),
Self::Snan => ic.execute_snan(vm, &operands[0], &operands[1]),
Self::Snanz => ic.execute_snanz(vm, &operands[0], &operands[1]),
Self::Snaz => ic.execute_snaz(vm, &operands[0], &operands[1], &operands[2]),
Self::Sne => ic.execute_sne(vm, &operands[0], &operands[1], &operands[2]),
Self::Snez => ic.execute_snez(vm, &operands[0], &operands[1]),
Self::Sqrt => ic.execute_sqrt(vm, &operands[0], &operands[1]),
Self::Sra => ic.execute_sra(vm, &operands[0], &operands[1], &operands[2]),
Self::Srl => ic.execute_srl(vm, &operands[0], &operands[1], &operands[2]),
Self::Ss => ic.execute_ss(vm, &operands[0], &operands[1], &operands[2], &operands[3]),
Self::Sub => ic.execute_sub(vm, &operands[0], &operands[1], &operands[2]),
Self::Tan => ic.execute_tan(vm, &operands[0], &operands[1]),
Self::Trunc => ic.execute_trunc(vm, &operands[0], &operands[1]),
Self::Xor => ic.execute_xor(vm, &operands[0], &operands[1], &operands[2]),
Self::Yield => ic.execute_yield(vm),
Self::Seq => ic.execute_seq(&operands[0], &operands[1], &operands[2]),
Self::Seqz => ic.execute_seqz(&operands[0], &operands[1]),
Self::Sge => ic.execute_sge(&operands[0], &operands[1], &operands[2]),
Self::Sgez => ic.execute_sgez(&operands[0], &operands[1]),
Self::Sgt => ic.execute_sgt(&operands[0], &operands[1], &operands[2]),
Self::Sgtz => ic.execute_sgtz(&operands[0], &operands[1]),
Self::Sin => ic.execute_sin(&operands[0], &operands[1]),
Self::Sla => ic.execute_sla(&operands[0], &operands[1], &operands[2]),
Self::Sle => ic.execute_sle(&operands[0], &operands[1], &operands[2]),
Self::Sleep => ic.execute_sleep(&operands[0]),
Self::Slez => ic.execute_slez(&operands[0], &operands[1]),
Self::Sll => ic.execute_sll(&operands[0], &operands[1], &operands[2]),
Self::Slt => ic.execute_slt(&operands[0], &operands[1], &operands[2]),
Self::Sltz => ic.execute_sltz(&operands[0], &operands[1]),
Self::Sna => ic.execute_sna(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Snan => ic.execute_snan(&operands[0], &operands[1]),
Self::Snanz => ic.execute_snanz(&operands[0], &operands[1]),
Self::Snaz => ic.execute_snaz(&operands[0], &operands[1], &operands[2]),
Self::Sne => ic.execute_sne(&operands[0], &operands[1], &operands[2]),
Self::Snez => ic.execute_snez(&operands[0], &operands[1]),
Self::Sqrt => ic.execute_sqrt(&operands[0], &operands[1]),
Self::Sra => ic.execute_sra(&operands[0], &operands[1], &operands[2]),
Self::Srl => ic.execute_srl(&operands[0], &operands[1], &operands[2]),
Self::Ss => ic.execute_ss(&operands[0], &operands[1], &operands[2], &operands[3]),
Self::Sub => ic.execute_sub(&operands[0], &operands[1], &operands[2]),
Self::Tan => ic.execute_tan(&operands[0], &operands[1]),
Self::Trunc => ic.execute_trunc(&operands[0], &operands[1]),
Self::Xor => ic.execute_xor(&operands[0], &operands[1], &operands[2]),
Self::Yield => ic.execute_yield(),
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -47,13 +47,11 @@ impl DerefMut for VMObject {
}
impl VMObject {
pub fn new<T>(val: T, vm: Rc<VM>) -> Self
pub fn new<T>(val: T) -> Self
where
T: Object + 'static,
{
let mut obj = VMObject(Rc::new(RefCell::new(val)));
obj.set_vm(vm);
obj
VMObject(Rc::new(RefCell::new(val)))
}
pub fn set_vm(&mut self, vm: Rc<VM>) {
@@ -61,11 +59,7 @@ impl VMObject {
}
pub fn get_vm(&self) -> Rc<VM> {
self
.borrow()
.get_vm()
.expect("VMObject with no VM?")
.clone()
self.borrow().get_vm().clone()
}
}

View File

@@ -1,11 +1,18 @@
use super::{macros::*, traits::*};
use crate::{network::Connection, vm::{
enums::script_enums::LogicType,
object::{
macros::ObjectInterface, templates::{DeviceInfo, ItemInfo}, traits::*, LogicField, Name, ObjectID, Slot,
}, VM,
}};
use crate::{
network::Connection,
vm::{
enums::script_enums::LogicType,
object::{
macros::ObjectInterface,
templates::{DeviceInfo, ItemInfo},
traits::*,
LogicField, Name, ObjectID, Slot,
},
VM,
},
};
use macro_rules_attribute::derive;
use std::{collections::BTreeMap, rc::Rc};
@@ -19,7 +26,7 @@ pub struct Generic {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub small_grid: bool,
}
@@ -33,7 +40,7 @@ pub struct GenericStorage {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub small_grid: bool,
pub slots: Vec<Slot>,
}
@@ -48,7 +55,7 @@ pub struct GenericLogicable {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub small_grid: bool,
pub slots: Vec<Slot>,
pub fields: BTreeMap<LogicType, LogicField>,
@@ -65,7 +72,7 @@ pub struct GenericLogicableDevice {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub small_grid: bool,
pub slots: Vec<Slot>,
pub fields: BTreeMap<LogicType, LogicField>,
@@ -85,7 +92,7 @@ pub struct GenericLogicableDeviceMemoryReadable {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub small_grid: bool,
pub slots: Vec<Slot>,
pub fields: BTreeMap<LogicType, LogicField>,
@@ -106,7 +113,7 @@ pub struct GenericLogicableDeviceMemoryReadWriteable {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub small_grid: bool,
pub slots: Vec<Slot>,
pub fields: BTreeMap<LogicType, LogicField>,
@@ -127,7 +134,7 @@ pub struct GenericItem {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub item_info: ItemInfo,
pub parent_slot: Option<ParentSlotInfo>,
}
@@ -142,7 +149,7 @@ pub struct GenericItemStorage {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub item_info: ItemInfo,
pub parent_slot: Option<ParentSlotInfo>,
pub slots: Vec<Slot>,
@@ -158,7 +165,7 @@ pub struct GenericItemLogicable {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub item_info: ItemInfo,
pub parent_slot: Option<ParentSlotInfo>,
pub slots: Vec<Slot>,
@@ -176,7 +183,7 @@ pub struct GenericItemLogicableMemoryReadable {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub item_info: ItemInfo,
pub parent_slot: Option<ParentSlotInfo>,
pub slots: Vec<Slot>,
@@ -195,7 +202,7 @@ pub struct GenericItemLogicableMemoryReadWriteable {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub item_info: ItemInfo,
pub parent_slot: Option<ParentSlotInfo>,
pub slots: Vec<Slot>,

View File

@@ -14,7 +14,7 @@ macro_rules! object_trait {
fn get_mut_prefab(&mut self) -> &mut crate::vm::object::Name;
fn get_name(&self) -> &crate::vm::object::Name;
fn get_mut_name(&mut self) -> &mut crate::vm::object::Name;
fn get_vm(&self) -> Option<&std::rc::Rc<crate::vm::VM>>;
fn get_vm(&self) -> &std::rc::Rc<crate::vm::VM>;
fn set_vm(&mut self, vm: std::rc::Rc<crate::vm::VM>);
fn type_name(&self) -> &str;
fn as_object(&self) -> &dyn $trait_name;
@@ -82,7 +82,7 @@ pub(crate) use object_trait;
///
/// - `id` must be `crate::vm::object::ObjectID`
/// - `prefab` and `name` must be `crate::vm::object::Name`
/// - `vm_ref` must be `Option<std::rc::Rc<crate::vm::VM>>`
/// - `vm_ref` must be `std::rc::Rc<crate::vm::VM>`
macro_rules! ObjectInterface {
{
@body_final
@@ -119,12 +119,12 @@ macro_rules! ObjectInterface {
&mut self.$name_field
}
fn get_vm(&self) -> Option<&std::rc::Rc<crate::vm::VM>> {
self.$vm_ref_field.as_ref()
fn get_vm(&self) -> &std::rc::Rc<crate::vm::VM> {
&self.$vm_ref_field
}
fn set_vm(&mut self, vm: std::rc::Rc<crate::vm::VM>) {
self.$vm_ref_field = Some(vm);
self.$vm_ref_field = vm;
}
fn type_name(&self) -> &str {

View File

@@ -1,7 +1,7 @@
use std::rc::Rc;
use crate::vm::{enums::prefabs::StationpediaPrefab, VM};
use crate::vm::object::VMObject;
use crate::vm::{enums::prefabs::StationpediaPrefab, VM};
use super::templates::ObjectTemplate;
use super::ObjectID;
@@ -9,7 +9,11 @@ use super::ObjectID;
pub mod structs;
#[allow(unused)]
pub fn object_from_prefab_template(template: &ObjectTemplate, id: ObjectID, vm: Rc<VM>) -> Option<VMObject> {
pub fn object_from_prefab_template(
template: &ObjectTemplate,
id: ObjectID,
vm: &Rc<VM>,
) -> Option<VMObject> {
let prefab = StationpediaPrefab::from_repr(template.prefab_info().prefab_hash);
match prefab {
// Some(StationpediaPrefab::ItemIntegratedCircuit10) => {

View File

@@ -155,7 +155,7 @@ pub struct ItemIntegratedCircuit10 {
#[custom(object_name)]
pub name: Name,
#[custom(object_vm_ref)]
pub vm: Option<Rc<VM>>,
pub vm: Rc<VM>,
pub fields: BTreeMap<LogicType, LogicField>,
pub memory: [f64; 512],
pub parent_slot: Option<ParentSlotInfo>,
@@ -1388,9 +1388,9 @@ impl BrnazInstruction for ItemIntegratedCircuit10 {
impl BdseInstruction for ItemIntegratedCircuit10 {
/// bdse d? a(r?|num)
fn execute_bdse(&mut self, vm: &VM, d: &Operand, a: &Operand) -> Result<(), ICError> {
let (device, _connection) = d.as_device(self, InstructionOp::Bdse, 1)?;
let (device_id, _connection) = d.as_device(self, InstructionOp::Bdse, 1)?;
let a = a.as_value(self, InstructionOp::Bdse, 2)?;
if device.is_some() {
if let Some(device_id) = device_id {
// FIXME: collect device and get logicable
self.set_next_instruction(a);
}

View File

@@ -73,11 +73,11 @@ impl ObjectTemplate {
}
}
pub fn build(&self, id: ObjectID, vm: Rc<VM>) -> VMObject {
pub fn build(&self, id: ObjectID, vm: &Rc<VM>) -> VMObject {
if let Some(obj) = stationpedia::object_from_prefab_template(&self, id, vm) {
obj
} else {
self.build_generic(id, vm)
self.build_generic(id, vm.clone())
}
}
@@ -198,335 +198,206 @@ impl ObjectTemplate {
fn build_generic(&self, id: ObjectID, vm: Rc<VM>) -> VMObject {
use ObjectTemplate::*;
match self {
Structure(s) => VMObject::new(
Generic {
id,
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm: None,
small_grid: s.structure.small_grid,
},
vm.clone(),
),
StructureSlots(s) => VMObject::new(
GenericStorage {
id,
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm: None,
small_grid: s.structure.small_grid,
slots: s
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: Vec::new(),
writeable_logic: Vec::new(),
occupant: None,
})
.collect(),
},
vm.clone(),
),
StructureLogic(s) => VMObject::new(
GenericLogicable {
id,
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm: None,
small_grid: s.structure.small_grid,
slots: s
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Read | MemoryAccess::ReadWrite => {
Some(key)
}
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
writeable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Write | MemoryAccess::ReadWrite => {
Some(key)
}
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
occupant: None,
})
.collect(),
fields: s
.logic
.logic_types
.types
.iter()
.map(|(key, access)| {
(
*key,
LogicField {
field_type: *access,
value: s
.logic
.logic_values
.map(|values| values.get(key))
.flatten()
.copied()
.unwrap_or(0.0),
},
)
})
.collect(),
modes: s.logic.modes.clone(),
},
vm.clone(),
),
StructureLogicDevice(s) => VMObject::new(
GenericLogicableDevice {
id,
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm: None,
small_grid: s.structure.small_grid,
slots: s
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Read | MemoryAccess::ReadWrite => {
Some(key)
}
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
writeable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Write | MemoryAccess::ReadWrite => {
Some(key)
}
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
occupant: None,
})
.collect(),
fields: s
.logic
.logic_types
.types
.iter()
.map(|(key, access)| {
(
*key,
LogicField {
field_type: *access,
value: s
.logic
.logic_values
.map(|values| values.get(key))
.flatten()
.copied()
.unwrap_or(0.0),
},
)
})
.collect(),
modes: s.logic.modes.clone(),
connections: s
.device
.connection_list
.iter()
.map(|conn_info| {
Connection::from_info(conn_info.typ, conn_info.role, conn_info.network)
})
.collect(),
pins: s
.device
.device_pins
.map(|pins| Some(pins.clone()))
.unwrap_or_else(|| {
s.device
.device_pins_length
.map(|pins_len| vec![None; pins_len])
}),
device_info: s.device.clone(),
},
vm.clone(),
),
Structure(s) => VMObject::new(Generic {
id,
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm,
small_grid: s.structure.small_grid,
}),
StructureSlots(s) => VMObject::new(GenericStorage {
id,
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm,
small_grid: s.structure.small_grid,
slots: s
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: Vec::new(),
writeable_logic: Vec::new(),
occupant: None,
})
.collect(),
}),
StructureLogic(s) => VMObject::new(GenericLogicable {
id,
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm,
small_grid: s.structure.small_grid,
slots: s
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Read | MemoryAccess::ReadWrite => Some(key),
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
writeable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Write | MemoryAccess::ReadWrite => Some(key),
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
occupant: None,
})
.collect(),
fields: s
.logic
.logic_types
.types
.iter()
.map(|(key, access)| {
(
*key,
LogicField {
field_type: *access,
value: s
.logic
.logic_values
.map(|values| values.get(key))
.flatten()
.copied()
.unwrap_or(0.0),
},
)
})
.collect(),
modes: s.logic.modes.clone(),
}),
StructureLogicDevice(s) => VMObject::new(GenericLogicableDevice {
id,
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm,
small_grid: s.structure.small_grid,
slots: s
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Read | MemoryAccess::ReadWrite => Some(key),
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
writeable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Write | MemoryAccess::ReadWrite => Some(key),
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
occupant: None,
})
.collect(),
fields: s
.logic
.logic_types
.types
.iter()
.map(|(key, access)| {
(
*key,
LogicField {
field_type: *access,
value: s
.logic
.logic_values
.map(|values| values.get(key))
.flatten()
.copied()
.unwrap_or(0.0),
},
)
})
.collect(),
modes: s.logic.modes.clone(),
connections: s
.device
.connection_list
.iter()
.map(|conn_info| {
Connection::from_info(conn_info.typ, conn_info.role, conn_info.network)
})
.collect(),
pins: s
.device
.device_pins
.map(|pins| Some(pins.clone()))
.unwrap_or_else(|| {
s.device
.device_pins_length
.map(|pins_len| vec![None; pins_len])
}),
device_info: s.device.clone(),
}),
StructureLogicDeviceMemory(s)
if matches!(s.memory.memory_access, MemoryAccess::Read) =>
{
VMObject::new(
GenericLogicableDeviceMemoryReadable {
id,
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm: None,
small_grid: s.structure.small_grid,
slots: s
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Read | MemoryAccess::ReadWrite => {
Some(key)
}
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
writeable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Write | MemoryAccess::ReadWrite => {
Some(key)
}
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
occupant: None,
})
.collect(),
fields: s
.logic
.logic_types
.types
.iter()
.map(|(key, access)| {
(
*key,
LogicField {
field_type: *access,
value: s
.logic
.logic_values
.map(|values| values.get(key))
.flatten()
.copied()
.unwrap_or(0.0),
},
)
})
.collect(),
modes: s.logic.modes.clone(),
connections: s
.device
.connection_list
.iter()
.map(|conn_info| {
Connection::from_info(
conn_info.typ,
conn_info.role,
conn_info.network,
)
})
.collect(),
pins: s
.device
.device_pins
.map(|pins| Some(pins.clone()))
.unwrap_or_else(|| {
s.device
.device_pins_length
.map(|pins_len| vec![None; pins_len])
}),
device_info: s.device.clone(),
memory: s
.memory
.values
.clone()
.unwrap_or_else(|| vec![0.0; s.memory.memory_size as usize]),
},
vm.clone(),
)
}
StructureLogicDeviceMemory(s) => VMObject::new(
GenericLogicableDeviceMemoryReadWriteable {
VMObject::new(GenericLogicableDeviceMemoryReadable {
id,
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm: None,
vm,
small_grid: s.structure.small_grid,
slots: s
.slots
@@ -621,29 +492,16 @@ impl ObjectTemplate {
.values
.clone()
.unwrap_or_else(|| vec![0.0; s.memory.memory_size as usize]),
},
vm.clone(),
),
Item(i) => VMObject::new(
GenericItem {
})
}
StructureLogicDeviceMemory(s) => {
VMObject::new(GenericLogicableDeviceMemoryReadWriteable {
id,
prefab: Name::from_prefab_name(&i.prefab.prefab_name),
name: Name::new(&i.prefab.name),
vm: None,
item_info: i.item.clone(),
parent_slot: None,
},
vm.clone(),
),
ItemSlots(i) => VMObject::new(
GenericItemStorage {
id,
prefab: Name::from_prefab_name(&i.prefab.prefab_name),
name: Name::new(&i.prefab.name),
vm: None,
item_info: i.item.clone(),
parent_slot: None,
slots: i
prefab: Name::from_prefab_name(&s.prefab.prefab_name),
name: Name::new(&s.prefab.name),
vm,
small_grid: s.structure.small_grid,
slots: s
.slots
.iter()
.enumerate()
@@ -652,32 +510,7 @@ impl ObjectTemplate {
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: Vec::new(),
writeable_logic: Vec::new(),
occupant: None,
})
.collect(),
},
vm.clone(),
),
ItemLogic(i) => VMObject::new(
GenericItemLogicable {
id,
prefab: Name::from_prefab_name(&i.prefab.prefab_name),
name: Name::new(&i.prefab.name),
vm: None,
item_info: i.item.clone(),
parent_slot: None,
slots: i
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: i
readable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
@@ -695,7 +528,7 @@ impl ObjectTemplate {
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
writeable_logic: i
writeable_logic: s
.logic
.logic_slot_types
.get(&(index as u32))
@@ -716,7 +549,7 @@ impl ObjectTemplate {
occupant: None,
})
.collect(),
fields: i
fields: s
.logic
.logic_types
.types
@@ -726,7 +559,7 @@ impl ObjectTemplate {
*key,
LogicField {
field_type: *access,
value: i
value: s
.logic
.logic_values
.map(|values| values.get(key))
@@ -737,104 +570,142 @@ impl ObjectTemplate {
)
})
.collect(),
modes: i.logic.modes.clone(),
},
vm.clone(),
),
ItemLogicMemory(i) if matches!(i.memory.memory_access, MemoryAccess::Read) => {
VMObject::new(
GenericItemLogicableMemoryReadable {
id,
prefab: Name::from_prefab_name(&i.prefab.prefab_name),
name: Name::new(&i.prefab.name),
vm: None,
item_info: i.item.clone(),
parent_slot: None,
slots: i
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: i
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Read | MemoryAccess::ReadWrite => {
Some(key)
}
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
writeable_logic: i
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Write | MemoryAccess::ReadWrite => {
Some(key)
}
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
occupant: None,
})
.collect(),
fields: i
.logic
.logic_types
.types
.iter()
.map(|(key, access)| {
(
*key,
LogicField {
field_type: *access,
value: i
.logic
.logic_values
.map(|values| values.get(key))
.flatten()
.copied()
.unwrap_or(0.0),
},
)
})
.collect(),
modes: i.logic.modes.clone(),
memory: i
.memory
.values
.clone()
.unwrap_or_else(|| vec![0.0; i.memory.memory_size as usize]),
},
vm.clone(),
)
modes: s.logic.modes.clone(),
connections: s
.device
.connection_list
.iter()
.map(|conn_info| {
Connection::from_info(conn_info.typ, conn_info.role, conn_info.network)
})
.collect(),
pins: s
.device
.device_pins
.map(|pins| Some(pins.clone()))
.unwrap_or_else(|| {
s.device
.device_pins_length
.map(|pins_len| vec![None; pins_len])
}),
device_info: s.device.clone(),
memory: s
.memory
.values
.clone()
.unwrap_or_else(|| vec![0.0; s.memory.memory_size as usize]),
})
}
ItemLogicMemory(i) => VMObject::new(
GenericItemLogicableMemoryReadWriteable {
Item(i) => VMObject::new(GenericItem {
id,
prefab: Name::from_prefab_name(&i.prefab.prefab_name),
name: Name::new(&i.prefab.name),
vm,
item_info: i.item.clone(),
parent_slot: None,
}),
ItemSlots(i) => VMObject::new(GenericItemStorage {
id,
prefab: Name::from_prefab_name(&i.prefab.prefab_name),
name: Name::new(&i.prefab.name),
vm,
item_info: i.item.clone(),
parent_slot: None,
slots: i
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: Vec::new(),
writeable_logic: Vec::new(),
occupant: None,
})
.collect(),
}),
ItemLogic(i) => VMObject::new(GenericItemLogicable {
id,
prefab: Name::from_prefab_name(&i.prefab.prefab_name),
name: Name::new(&i.prefab.name),
vm,
item_info: i.item.clone(),
parent_slot: None,
slots: i
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: i
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Read | MemoryAccess::ReadWrite => Some(key),
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
writeable_logic: i
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Write | MemoryAccess::ReadWrite => Some(key),
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
occupant: None,
})
.collect(),
fields: i
.logic
.logic_types
.types
.iter()
.map(|(key, access)| {
(
*key,
LogicField {
field_type: *access,
value: i
.logic
.logic_values
.map(|values| values.get(key))
.flatten()
.copied()
.unwrap_or(0.0),
},
)
})
.collect(),
modes: i.logic.modes.clone(),
}),
ItemLogicMemory(i) if matches!(i.memory.memory_access, MemoryAccess::Read) => {
VMObject::new(GenericItemLogicableMemoryReadable {
id,
prefab: Name::from_prefab_name(&i.prefab.prefab_name),
name: Name::new(&i.prefab.name),
vm: None,
vm,
item_info: i.item.clone(),
parent_slot: None,
slots: i
@@ -912,9 +783,87 @@ impl ObjectTemplate {
.values
.clone()
.unwrap_or_else(|| vec![0.0; i.memory.memory_size as usize]),
},
vm.clone(),
),
})
}
ItemLogicMemory(i) => VMObject::new(GenericItemLogicableMemoryReadWriteable {
id,
prefab: Name::from_prefab_name(&i.prefab.prefab_name),
name: Name::new(&i.prefab.name),
vm,
item_info: i.item.clone(),
parent_slot: None,
slots: i
.slots
.iter()
.enumerate()
.map(|(index, info)| Slot {
parent: id,
index,
name: info.name.clone(),
typ: info.typ,
readable_logic: i
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Read | MemoryAccess::ReadWrite => Some(key),
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
writeable_logic: i
.logic
.logic_slot_types
.get(&(index as u32))
.map(|s_info| {
s_info
.slot_types
.iter()
.filter_map(|(key, access)| match access {
MemoryAccess::Write | MemoryAccess::ReadWrite => Some(key),
_ => None,
})
.copied()
.collect::<Vec<_>>()
})
.unwrap_or_else(|| Vec::new()),
occupant: None,
})
.collect(),
fields: i
.logic
.logic_types
.types
.iter()
.map(|(key, access)| {
(
*key,
LogicField {
field_type: *access,
value: i
.logic
.logic_values
.map(|values| values.get(key))
.flatten()
.copied()
.unwrap_or(0.0),
},
)
})
.collect(),
modes: i.logic.modes.clone(),
memory: i
.memory
.values
.clone()
.unwrap_or_else(|| vec![0.0; i.memory.memory_size as usize]),
}),
}
}

View File

@@ -1,7 +1,10 @@
use serde_derive::{Deserialize, Serialize};
use crate::{
errors::ICError, interpreter::ICState, network::Connection, vm::{
errors::ICError,
interpreter::ICState,
network::Connection,
vm::{
enums::{
basic_enums::{Class as SlotClass, GasType, SortingClass},
script_enums::{LogicSlotType, LogicType},
@@ -13,7 +16,7 @@ use crate::{
ObjectID, Slot,
},
VM,
}
},
};
use std::{collections::BTreeMap, fmt::Debug, rc::Rc};

View File

@@ -433,7 +433,12 @@ impl VMRef {
#[wasm_bindgen(getter)]
pub fn ics(&self) -> Vec<u32> {
self.vm.borrow().circuit_holders.keys().copied().collect_vec()
self.vm
.borrow()
.circuit_holders
.keys()
.copied()
.collect_vec()
}
#[wasm_bindgen(getter, js_name = "lastOperationModified")]

View File

@@ -21,6 +21,12 @@ pub fn generate_enums(
std::fs::create_dir(&enums_path)?;
}
let basic_enum_names = enums
.basic_enums
.values()
.map(|enm| enm.enum_name.clone())
.collect::<Vec<_>>();
let mut writer =
std::io::BufWriter::new(std::fs::File::create(enums_path.join("script_enums.rs"))?);
write_repr_enum_use_header(&mut writer)?;
@@ -31,7 +37,20 @@ pub fn generate_enums(
let mut writer =
std::io::BufWriter::new(std::fs::File::create(enums_path.join("basic_enums.rs"))?);
write_repr_enum_use_header(&mut writer)?;
let script_enums_in_basic = enums
.script_enums
.values()
.filter(|enm| basic_enum_names.contains(&enm.enum_name))
.collect::<Vec<_>>();
let script_enums_in_basic_names = script_enums_in_basic
.iter()
.map(|enm| enm.enum_name.as_str())
.collect::<Vec<_>>();
write_repr_basic_use_header(&mut writer, script_enums_in_basic.as_slice())?;
for enm in enums.basic_enums.values() {
if script_enums_in_basic_names.contains(&enm.enum_name.as_str()) {
continue;
}
write_enum_listing(&mut writer, enm)?;
}
write_enum_aggragate_mod(&mut writer, &enums.basic_enums)?;
@@ -346,6 +365,22 @@ fn write_repr_enum_use_header<T: std::io::Write>(
Ok(())
}
fn write_repr_basic_use_header<T: std::io::Write>(
writer: &mut BufWriter<T>,
script_enums: &[&crate::enums::EnumListing],
) -> color_eyre::Result<()> {
write!(
writer,
"use crate::vm::enums::script_enums::{{ {} }};",
script_enums
.iter()
.map(|enm| enm.enum_name.to_case(Case::Pascal))
.collect::<Vec<_>>()
.join(", ")
)?;
Ok(())
}
fn write_repr_enum<'a, T: std::io::Write, I, P>(
writer: &mut BufWriter<T>,
name: &str,
@@ -362,8 +397,7 @@ where
sorted.sort_by_key(|(_, variant)| &variant.value);
let default_derive = if sorted
.iter()
.find(|(name, _)| name == "None" || name == "Default")
.is_some()
.any(|(name, _)| name == "None" || name == "Default")
{
"Default, "
} else {

View File

@@ -90,7 +90,6 @@ fn write_instructions_enum<T: std::io::Write>(
pub fn execute<T>(\n \
&self,\n \
ic: &mut T,\n \
vm: &crate::vm::VM,\n \
operands: &[crate::vm::instructions::operands::Operand],\n \
) -> Result<(), crate::errors::ICError>\n \
where\n \
@@ -114,7 +113,7 @@ fn write_instructions_enum<T: std::io::Write>(
let trait_name = name.to_case(Case::Pascal);
writeln!(
writer,
" Self::{trait_name} => ic.execute_{name}(vm, {operands}),",
" Self::{trait_name} => ic.execute_{name}({operands}),",
)?;
}
@@ -154,7 +153,7 @@ fn write_instruction_trait<T: std::io::Write>(
writer,
"pub trait {trait_name}: IntegratedCircuit {{\n \
/// {example} \n \
fn execute_{name}(&mut self, vm: &crate::vm::VM, {operands}) -> Result<(), crate::errors::ICError>;\n\
fn execute_{name}(&mut self, {operands}) -> Result<(), crate::errors::ICError>;\n\
}}"
)?;
Ok(())