1
0
This commit is contained in:
Jeff 2024-09-14 15:06:13 -04:00
parent 9418cd5b70
commit aa8b1215a8
2 changed files with 17 additions and 37 deletions

View File

@ -26,24 +26,16 @@ impl Chunk {
pub fn with_data( pub fn with_data(
instructions: Vec<(Instruction, Span)>, instructions: Vec<(Instruction, Span)>,
constants: Vec<Value>, constants: Vec<Value>,
identifiers: Vec<Local>, locals: Vec<Local>,
) -> Self { ) -> Self {
Self { Self {
instructions, instructions,
constants: constants.into_iter().map(Some).collect(), constants: constants.into_iter().map(Some).collect(),
locals: identifiers, locals,
scope_depth: 0, scope_depth: 0,
} }
} }
pub fn len(&self) -> usize {
self.instructions.len()
}
pub fn is_empty(&self) -> bool {
self.instructions.is_empty()
}
pub fn scope_depth(&self) -> usize { pub fn scope_depth(&self) -> usize {
self.scope_depth self.scope_depth
} }
@ -79,7 +71,7 @@ impl Chunk {
}) })
} }
pub fn use_constant(&mut self, index: usize, position: Span) -> Result<Value, ChunkError> { pub fn take_constant(&mut self, index: usize, position: Span) -> Result<Value, ChunkError> {
self.constants self.constants
.get_mut(index) .get_mut(index)
.ok_or_else(|| ChunkError::ConstantIndexOutOfBounds { index, position })? .ok_or_else(|| ChunkError::ConstantIndexOutOfBounds { index, position })?
@ -99,12 +91,6 @@ impl Chunk {
} }
} }
pub fn contains_identifier(&self, identifier: &Identifier) -> bool {
self.locals
.iter()
.any(|local| &local.identifier == identifier)
}
pub fn get_local(&self, index: usize, position: Span) -> Result<&Local, ChunkError> { pub fn get_local(&self, index: usize, position: Span) -> Result<&Local, ChunkError> {
self.locals self.locals
.get(index) .get(index)
@ -181,20 +167,6 @@ impl Chunk {
self.scope_depth -= 1; self.scope_depth -= 1;
} }
pub fn clear(&mut self) {
self.instructions.clear();
self.constants.clear();
self.locals.clear();
}
pub fn identifiers(&self) -> &[Local] {
&self.locals
}
pub fn pop_identifier(&mut self) -> Option<Local> {
self.locals.pop()
}
pub fn disassembler<'a>(&'a self, name: &'a str) -> ChunkDisassembler<'a> { pub fn disassembler<'a>(&'a self, name: &'a str) -> ChunkDisassembler<'a> {
ChunkDisassembler::new(name, self) ChunkDisassembler::new(name, self)
} }

View File

@ -50,7 +50,7 @@ impl Vm {
Operation::LoadConstant => { Operation::LoadConstant => {
let to_register = instruction.destination as usize; let to_register = instruction.destination as usize;
let from_constant = u16::from_le_bytes(instruction.arguments) as usize; let from_constant = u16::from_le_bytes(instruction.arguments) as usize;
let value = self.chunk.use_constant(from_constant, position)?; let value = self.chunk.take_constant(from_constant, position)?;
self.insert(value, to_register, position)?; self.insert(value, to_register, position)?;
} }
@ -156,11 +156,19 @@ impl Vm {
fn clone(&mut self, index: usize, position: Span) -> Result<Value, VmError> { fn clone(&mut self, index: usize, position: Span) -> Result<Value, VmError> {
if let Some(register) = self.register_stack.get_mut(index) { if let Some(register) = self.register_stack.get_mut(index) {
if let Some(value) = register.take() { let cloneable = if let Some(value) = register.take() {
Ok(register.insert(value.into_reference()).clone()) if value.is_raw() {
value.into_reference()
} else {
value
}
} else { } else {
Err(VmError::EmptyRegister { index, position }) return Err(VmError::EmptyRegister { index, position });
} };
*register = Some(cloneable.clone());
Ok(cloneable)
} else { } else {
Err(VmError::RegisterIndexOutOfBounds { position }) Err(VmError::RegisterIndexOutOfBounds { position })
} }
@ -182,7 +190,7 @@ impl Vm {
if let Ok(value) = self.take(index, position) { if let Ok(value) = self.take(index, position) {
Ok(value) Ok(value)
} else { } else {
let value = self.chunk.use_constant(index, position)?; let value = self.chunk.take_constant(index, position)?;
Ok(value) Ok(value)
} }