Clean up
This commit is contained in:
parent
9418cd5b70
commit
aa8b1215a8
@ -26,24 +26,16 @@ impl Chunk {
|
||||
pub fn with_data(
|
||||
instructions: Vec<(Instruction, Span)>,
|
||||
constants: Vec<Value>,
|
||||
identifiers: Vec<Local>,
|
||||
locals: Vec<Local>,
|
||||
) -> Self {
|
||||
Self {
|
||||
instructions,
|
||||
constants: constants.into_iter().map(Some).collect(),
|
||||
locals: identifiers,
|
||||
locals,
|
||||
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 {
|
||||
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
|
||||
.get_mut(index)
|
||||
.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> {
|
||||
self.locals
|
||||
.get(index)
|
||||
@ -181,20 +167,6 @@ impl Chunk {
|
||||
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> {
|
||||
ChunkDisassembler::new(name, self)
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ impl Vm {
|
||||
Operation::LoadConstant => {
|
||||
let to_register = instruction.destination 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)?;
|
||||
}
|
||||
@ -156,11 +156,19 @@ impl Vm {
|
||||
|
||||
fn clone(&mut self, index: usize, position: Span) -> Result<Value, VmError> {
|
||||
if let Some(register) = self.register_stack.get_mut(index) {
|
||||
if let Some(value) = register.take() {
|
||||
Ok(register.insert(value.into_reference()).clone())
|
||||
let cloneable = if let Some(value) = register.take() {
|
||||
if value.is_raw() {
|
||||
value.into_reference()
|
||||
} else {
|
||||
value
|
||||
}
|
||||
} else {
|
||||
Err(VmError::EmptyRegister { index, position })
|
||||
}
|
||||
return Err(VmError::EmptyRegister { index, position });
|
||||
};
|
||||
|
||||
*register = Some(cloneable.clone());
|
||||
|
||||
Ok(cloneable)
|
||||
} else {
|
||||
Err(VmError::RegisterIndexOutOfBounds { position })
|
||||
}
|
||||
@ -182,7 +190,7 @@ impl Vm {
|
||||
if let Ok(value) = self.take(index, position) {
|
||||
Ok(value)
|
||||
} else {
|
||||
let value = self.chunk.use_constant(index, position)?;
|
||||
let value = self.chunk.take_constant(index, position)?;
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user