2024-09-07 10:38:12 +00:00
|
|
|
use std::fmt::{self, Debug, Display, Formatter};
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
use crate::{Identifier, Instruction, Span, Value};
|
2024-09-07 10:38:12 +00:00
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
2024-09-07 10:38:12 +00:00
|
|
|
pub struct Chunk {
|
|
|
|
code: Vec<(u8, Span)>,
|
|
|
|
constants: Vec<Value>,
|
2024-09-07 16:15:47 +00:00
|
|
|
identifiers: Vec<Identifier>,
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Chunk {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
code: Vec::new(),
|
|
|
|
constants: Vec::new(),
|
2024-09-07 16:15:47 +00:00
|
|
|
identifiers: Vec::new(),
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
pub fn with_data(
|
|
|
|
code: Vec<(u8, Span)>,
|
|
|
|
constants: Vec<Value>,
|
|
|
|
identifiers: Vec<Identifier>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
code,
|
|
|
|
constants,
|
|
|
|
identifiers,
|
|
|
|
}
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.code.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.code.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn capacity(&self) -> usize {
|
|
|
|
self.code.capacity()
|
|
|
|
}
|
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
pub fn read(&self, offset: usize) -> Result<&(u8, Span), ChunkError> {
|
|
|
|
self.code
|
|
|
|
.get(offset)
|
|
|
|
.ok_or(ChunkError::CodeIndextOfBounds(offset))
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(&mut self, instruction: u8, position: Span) {
|
|
|
|
self.code.push((instruction, position));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_constant(&self, index: usize) -> Result<&Value, ChunkError> {
|
|
|
|
self.constants
|
|
|
|
.get(index)
|
2024-09-07 16:15:47 +00:00
|
|
|
.ok_or(ChunkError::ConstantIndexOutOfBounds(index))
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_constant(&mut self, value: Value) -> Result<u8, ChunkError> {
|
|
|
|
let starting_length = self.constants.len();
|
|
|
|
|
|
|
|
if starting_length + 1 > (u8::MAX as usize) {
|
2024-09-07 16:15:47 +00:00
|
|
|
Err(ChunkError::ConstantOverflow)
|
2024-09-07 10:38:12 +00:00
|
|
|
} else {
|
|
|
|
self.constants.push(value);
|
|
|
|
|
|
|
|
Ok(starting_length as u8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
pub fn get_identifier(&self, index: usize) -> Result<&Identifier, ChunkError> {
|
|
|
|
self.identifiers
|
|
|
|
.get(index)
|
|
|
|
.ok_or(ChunkError::IdentifierIndexOutOfBounds(index))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_identifier(&mut self, identifier: Identifier) -> Result<u8, ChunkError> {
|
|
|
|
let starting_length = self.constants.len();
|
|
|
|
|
|
|
|
if starting_length + 1 > (u8::MAX as usize) {
|
|
|
|
Err(ChunkError::IdentifierOverflow)
|
|
|
|
} else {
|
|
|
|
self.identifiers.push(identifier);
|
|
|
|
|
|
|
|
Ok(starting_length as u8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.code.clear();
|
|
|
|
self.constants.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disassemble(&self, name: &str) -> String {
|
|
|
|
let mut output = String::new();
|
|
|
|
|
|
|
|
output.push_str("== ");
|
|
|
|
output.push_str(name);
|
|
|
|
output.push_str(" ==\n");
|
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
let mut previous = None;
|
2024-09-07 10:38:12 +00:00
|
|
|
|
|
|
|
for (offset, (byte, position)) in self.code.iter().enumerate() {
|
2024-09-07 21:16:14 +00:00
|
|
|
if let Some(Instruction::Constant) = previous {
|
|
|
|
let display = format!("{position} {offset:04} CONSTANT_INDEX {byte}\n");
|
|
|
|
previous = None;
|
2024-09-07 10:38:12 +00:00
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
output.push_str(&display);
|
2024-09-07 10:38:12 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
if let Some(
|
|
|
|
Instruction::DefineGlobal | Instruction::GetGlobal | Instruction::SetGlobal,
|
|
|
|
) = previous
|
|
|
|
{
|
|
|
|
let display = format!("{position} {offset:04} IDENTIFIER_INDEX {byte}\n");
|
|
|
|
previous = None;
|
2024-09-07 10:38:12 +00:00
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
output.push_str(&display);
|
2024-09-07 10:38:12 +00:00
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
continue;
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
2024-09-07 21:16:14 +00:00
|
|
|
|
|
|
|
let instruction = Instruction::from_byte(*byte).unwrap();
|
|
|
|
let display = format!("{} {}\n", position, instruction.disassemble(self, offset));
|
|
|
|
previous = Some(instruction);
|
|
|
|
|
|
|
|
output.push_str(&display);
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Chunk {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Chunk {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.disassemble("Chunk"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum ChunkError {
|
2024-09-07 16:15:47 +00:00
|
|
|
CodeIndextOfBounds(usize),
|
|
|
|
ConstantOverflow,
|
2024-09-07 10:38:12 +00:00
|
|
|
ConstantIndexOutOfBounds(usize),
|
2024-09-07 16:15:47 +00:00
|
|
|
IdentifierIndexOutOfBounds(usize),
|
|
|
|
IdentifierOverflow,
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|