Add add_prototype-function

This commit is contained in:
Sofia 2026-03-16 18:10:54 +02:00
parent df8c196bb9
commit 1091f341b9
2 changed files with 15 additions and 4 deletions

View File

@ -69,10 +69,11 @@ fn main() {
environment: Default::default(), environment: Default::default(),
constants, constants,
prototypes: Default::default(), prototypes: Default::default(),
proto_counter: 0,
}; };
vm.prototypes.insert(0, instructions); let chunk_id = vm.new_prototype(instructions);
for (i, prototype) in state.prototypes.into_iter().enumerate() { for prototype in state.prototypes {
vm.prototypes.insert((i + 1) as u32, prototype); vm.new_prototype(prototype);
} }
dbg!(&vm.prototypes); dbg!(&vm.prototypes);
@ -85,7 +86,7 @@ fn main() {
vm::Value::RustFunction(Rc::new(RefCell::new(Print))), vm::Value::RustFunction(Rc::new(RefCell::new(Print))),
); );
let closure = vm.create_closure(0); let closure = vm.create_closure(chunk_id);
let mut run = closure.run(Vec::new()); let mut run = closure.run(Vec::new());

View File

@ -253,6 +253,16 @@ pub struct VirtualMachine {
pub environment: Rc<RefCell<Environment>>, pub environment: Rc<RefCell<Environment>>,
pub constants: Vec<Constant>, pub constants: Vec<Constant>,
pub prototypes: HashMap<u32, Vec<Instruction>>, pub prototypes: HashMap<u32, Vec<Instruction>>,
pub proto_counter: u32,
}
impl VirtualMachine {
pub fn new_prototype(&mut self, instructions: Vec<Instruction>) -> u32 {
let proto_id = self.proto_counter;
self.proto_counter += 1;
self.prototypes.insert(proto_id, instructions);
proto_id
}
} }
impl VirtualMachine { impl VirtualMachine {