2024-09-13 01:14:15 +00:00
|
|
|
use std::fmt::{self, Debug, Display, Formatter};
|
2024-09-07 10:38:12 +00:00
|
|
|
|
2024-09-12 23:25:20 +00:00
|
|
|
use colored::Colorize;
|
2024-09-07 10:38:12 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-09-19 15:41:18 +00:00
|
|
|
use crate::{AnnotatedError, Identifier, Instruction, Operation, Span, Value};
|
2024-09-07 10:38:12 +00:00
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
#[derive(Clone)]
|
2024-09-07 10:38:12 +00:00
|
|
|
pub struct Chunk {
|
2024-09-12 13:11:49 +00:00
|
|
|
instructions: Vec<(Instruction, Span)>,
|
2024-09-11 08:22:54 +00:00
|
|
|
constants: Vec<Option<Value>>,
|
2024-09-12 13:11:49 +00:00
|
|
|
locals: Vec<Local>,
|
2024-09-11 07:10:12 +00:00
|
|
|
scope_depth: usize,
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Chunk {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2024-09-12 13:11:49 +00:00
|
|
|
instructions: Vec::new(),
|
2024-09-07 10:38:12 +00:00
|
|
|
constants: Vec::new(),
|
2024-09-12 13:11:49 +00:00
|
|
|
locals: Vec::new(),
|
2024-09-11 07:10:12 +00:00
|
|
|
scope_depth: 0,
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
pub fn with_data(
|
2024-09-12 13:11:49 +00:00
|
|
|
instructions: Vec<(Instruction, Span)>,
|
2024-09-07 16:15:47 +00:00
|
|
|
constants: Vec<Value>,
|
2024-09-14 19:06:13 +00:00
|
|
|
locals: Vec<Local>,
|
2024-09-07 16:15:47 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2024-09-12 13:11:49 +00:00
|
|
|
instructions,
|
2024-09-11 08:22:54 +00:00
|
|
|
constants: constants.into_iter().map(Some).collect(),
|
2024-09-14 19:06:13 +00:00
|
|
|
locals,
|
2024-09-11 07:10:12 +00:00
|
|
|
scope_depth: 0,
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 22:19:59 +00:00
|
|
|
pub fn scope_depth(&self) -> usize {
|
2024-09-11 07:10:12 +00:00
|
|
|
self.scope_depth
|
2024-09-10 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 15:41:18 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.instructions.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.instructions.is_empty()
|
|
|
|
}
|
|
|
|
|
2024-09-12 13:11:49 +00:00
|
|
|
pub fn get_instruction(
|
2024-09-12 03:07:20 +00:00
|
|
|
&self,
|
|
|
|
offset: usize,
|
|
|
|
position: Span,
|
|
|
|
) -> Result<&(Instruction, Span), ChunkError> {
|
2024-09-12 13:11:49 +00:00
|
|
|
self.instructions
|
2024-09-07 16:15:47 +00:00
|
|
|
.get(offset)
|
2024-09-10 22:19:59 +00:00
|
|
|
.ok_or(ChunkError::CodeIndexOfBounds { offset, position })
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 13:11:49 +00:00
|
|
|
pub fn push_instruction(&mut self, instruction: Instruction, position: Span) {
|
|
|
|
self.instructions.push((instruction, position));
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 15:41:18 +00:00
|
|
|
pub fn insert_instruction(&mut self, index: usize, instruction: Instruction, position: Span) {
|
|
|
|
self.instructions.insert(index, (instruction, position));
|
|
|
|
}
|
|
|
|
|
2024-09-13 05:10:07 +00:00
|
|
|
pub fn pop_instruction(&mut self, position: Span) -> Result<(Instruction, Span), ChunkError> {
|
|
|
|
self.instructions
|
|
|
|
.pop()
|
|
|
|
.ok_or(ChunkError::InstructionUnderflow { position })
|
2024-09-12 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 15:41:18 +00:00
|
|
|
pub fn get_previous(&self, position: Span) -> Result<&(Instruction, Span), ChunkError> {
|
2024-09-17 21:23:37 +00:00
|
|
|
self.instructions
|
|
|
|
.last()
|
|
|
|
.ok_or(ChunkError::InstructionUnderflow { position })
|
|
|
|
}
|
|
|
|
|
2024-09-19 15:41:18 +00:00
|
|
|
pub fn get_last_operation(&self, position: Span) -> Result<Operation, ChunkError> {
|
|
|
|
self.get_previous(position)
|
|
|
|
.map(|(instruction, _)| instruction.operation())
|
|
|
|
}
|
|
|
|
|
2024-09-15 01:05:03 +00:00
|
|
|
pub fn get_constant(&self, index: u8, position: Span) -> Result<&Value, ChunkError> {
|
|
|
|
let index = index as usize;
|
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
self.constants
|
2024-09-12 13:11:49 +00:00
|
|
|
.get(index)
|
2024-09-10 22:19:59 +00:00
|
|
|
.ok_or(ChunkError::ConstantIndexOutOfBounds { index, position })
|
2024-09-11 08:22:54 +00:00
|
|
|
.and_then(|value| {
|
|
|
|
value
|
|
|
|
.as_ref()
|
|
|
|
.ok_or(ChunkError::ConstantAlreadyUsed { index, position })
|
|
|
|
})
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-15 01:05:03 +00:00
|
|
|
pub fn take_constant(&mut self, index: u8, position: Span) -> Result<Value, ChunkError> {
|
|
|
|
let index = index as usize;
|
|
|
|
|
2024-09-11 08:22:54 +00:00
|
|
|
self.constants
|
2024-09-12 13:11:49 +00:00
|
|
|
.get_mut(index)
|
2024-09-12 03:07:20 +00:00
|
|
|
.ok_or_else(|| ChunkError::ConstantIndexOutOfBounds { index, position })?
|
2024-09-11 08:22:54 +00:00
|
|
|
.take()
|
2024-09-12 03:07:20 +00:00
|
|
|
.ok_or(ChunkError::ConstantAlreadyUsed { index, position })
|
2024-09-10 07:42:25 +00:00
|
|
|
}
|
|
|
|
|
2024-09-15 01:05:03 +00:00
|
|
|
pub fn push_constant(&mut self, value: Value, position: Span) -> Result<u8, ChunkError> {
|
2024-09-07 10:38:12 +00:00
|
|
|
let starting_length = self.constants.len();
|
|
|
|
|
|
|
|
if starting_length + 1 > (u8::MAX as usize) {
|
2024-09-11 07:10:12 +00:00
|
|
|
Err(ChunkError::ConstantOverflow { position })
|
2024-09-07 10:38:12 +00:00
|
|
|
} else {
|
2024-09-11 08:22:54 +00:00
|
|
|
self.constants.push(Some(value));
|
2024-09-07 10:38:12 +00:00
|
|
|
|
2024-09-15 01:05:03 +00:00
|
|
|
Ok(starting_length as u8)
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-18 11:58:31 +00:00
|
|
|
pub fn locals(&self) -> &[Local] {
|
|
|
|
&self.locals
|
|
|
|
}
|
|
|
|
|
2024-09-15 01:05:03 +00:00
|
|
|
pub fn get_local(&self, index: u8, position: Span) -> Result<&Local, ChunkError> {
|
|
|
|
let index = index as usize;
|
|
|
|
|
2024-09-12 13:11:49 +00:00
|
|
|
self.locals
|
2024-09-12 17:03:24 +00:00
|
|
|
.get(index)
|
2024-09-12 13:11:49 +00:00
|
|
|
.ok_or(ChunkError::LocalIndexOutOfBounds { index, position })
|
2024-09-10 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
2024-09-15 01:05:03 +00:00
|
|
|
pub fn get_identifier(&self, index: u8) -> Option<&Identifier> {
|
2024-09-17 21:23:37 +00:00
|
|
|
self.locals
|
|
|
|
.get(index as usize)
|
|
|
|
.map(|local| &local.identifier)
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 13:11:49 +00:00
|
|
|
pub fn get_local_index(
|
2024-09-10 22:19:59 +00:00
|
|
|
&self,
|
|
|
|
identifier: &Identifier,
|
|
|
|
position: Span,
|
2024-09-15 01:05:03 +00:00
|
|
|
) -> Result<u8, ChunkError> {
|
2024-09-12 13:11:49 +00:00
|
|
|
self.locals
|
2024-09-11 07:10:12 +00:00
|
|
|
.iter()
|
2024-09-11 08:22:54 +00:00
|
|
|
.enumerate()
|
2024-09-12 18:16:26 +00:00
|
|
|
.rev()
|
2024-09-11 07:10:12 +00:00
|
|
|
.find_map(|(index, local)| {
|
|
|
|
if &local.identifier == identifier {
|
2024-09-15 01:05:03 +00:00
|
|
|
Some(index as u8)
|
2024-09-11 07:10:12 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2024-09-10 22:19:59 +00:00
|
|
|
.ok_or(ChunkError::IdentifierNotFound {
|
|
|
|
identifier: identifier.clone(),
|
|
|
|
position,
|
|
|
|
})
|
2024-09-09 23:23:49 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 13:11:49 +00:00
|
|
|
pub fn declare_local(
|
2024-09-11 07:10:12 +00:00
|
|
|
&mut self,
|
|
|
|
identifier: Identifier,
|
2024-09-18 15:27:41 +00:00
|
|
|
mutable: bool,
|
2024-09-15 01:05:03 +00:00
|
|
|
register_index: u8,
|
2024-09-11 07:10:12 +00:00
|
|
|
position: Span,
|
2024-09-15 01:05:03 +00:00
|
|
|
) -> Result<u8, ChunkError> {
|
2024-09-12 13:11:49 +00:00
|
|
|
let starting_length = self.locals.len();
|
2024-09-07 16:15:47 +00:00
|
|
|
|
|
|
|
if starting_length + 1 > (u8::MAX as usize) {
|
2024-09-11 07:10:12 +00:00
|
|
|
Err(ChunkError::IdentifierOverflow { position })
|
2024-09-07 16:15:47 +00:00
|
|
|
} else {
|
2024-09-15 01:05:03 +00:00
|
|
|
self.locals.push(Local::new(
|
|
|
|
identifier,
|
2024-09-18 15:27:41 +00:00
|
|
|
mutable,
|
2024-09-15 01:05:03 +00:00
|
|
|
self.scope_depth,
|
|
|
|
Some(register_index),
|
|
|
|
));
|
2024-09-07 16:15:47 +00:00
|
|
|
|
2024-09-15 01:05:03 +00:00
|
|
|
Ok(starting_length as u8)
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 13:11:49 +00:00
|
|
|
pub fn define_local(
|
|
|
|
&mut self,
|
2024-09-15 05:24:04 +00:00
|
|
|
local_index: u8,
|
2024-09-13 06:28:18 +00:00
|
|
|
register_index: u8,
|
2024-09-12 13:11:49 +00:00
|
|
|
position: Span,
|
|
|
|
) -> Result<(), ChunkError> {
|
2024-09-15 05:24:04 +00:00
|
|
|
let local = self.locals.get_mut(local_index as usize).ok_or_else(|| {
|
|
|
|
ChunkError::LocalIndexOutOfBounds {
|
|
|
|
index: local_index as usize,
|
|
|
|
position,
|
|
|
|
}
|
|
|
|
})?;
|
2024-09-12 13:11:49 +00:00
|
|
|
|
2024-09-13 06:28:18 +00:00
|
|
|
local.register_index = Some(register_index);
|
2024-09-12 13:11:49 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
pub fn begin_scope(&mut self) {
|
2024-09-11 07:10:12 +00:00
|
|
|
self.scope_depth += 1;
|
2024-09-10 14:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn end_scope(&mut self) {
|
2024-09-11 07:10:12 +00:00
|
|
|
self.scope_depth -= 1;
|
2024-09-10 14:44:15 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 23:25:20 +00:00
|
|
|
pub fn disassembler<'a>(&'a self, name: &'a str) -> ChunkDisassembler<'a> {
|
|
|
|
ChunkDisassembler::new(name, self)
|
2024-09-12 17:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Chunk {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Chunk {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-09-12 23:25:20 +00:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
2024-09-14 18:31:40 +00:00
|
|
|
self.disassembler("Chunk Display")
|
|
|
|
.styled(true)
|
|
|
|
.disassemble()
|
2024-09-12 23:25:20 +00:00
|
|
|
)
|
2024-09-12 17:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for Chunk {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-09-12 23:25:20 +00:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
2024-09-14 18:31:40 +00:00
|
|
|
self.disassembler("Chunk Debug Display")
|
|
|
|
.styled(false)
|
|
|
|
.disassemble()
|
2024-09-12 23:25:20 +00:00
|
|
|
)
|
2024-09-12 17:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Chunk {}
|
|
|
|
|
|
|
|
impl PartialEq for Chunk {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.instructions == other.instructions
|
|
|
|
&& self.constants == other.constants
|
|
|
|
&& self.locals == other.locals
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Local {
|
|
|
|
pub identifier: Identifier,
|
2024-09-18 15:27:41 +00:00
|
|
|
pub mutable: bool,
|
2024-09-12 17:03:24 +00:00
|
|
|
pub depth: usize,
|
2024-09-13 06:28:18 +00:00
|
|
|
pub register_index: Option<u8>,
|
2024-09-12 17:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Local {
|
2024-09-18 15:27:41 +00:00
|
|
|
pub fn new(
|
|
|
|
identifier: Identifier,
|
|
|
|
mutable: bool,
|
|
|
|
depth: usize,
|
|
|
|
register_index: Option<u8>,
|
|
|
|
) -> Self {
|
2024-09-12 17:03:24 +00:00
|
|
|
Self {
|
|
|
|
identifier,
|
2024-09-18 15:27:41 +00:00
|
|
|
mutable,
|
2024-09-12 17:03:24 +00:00
|
|
|
depth,
|
2024-09-13 06:28:18 +00:00
|
|
|
register_index,
|
2024-09-12 17:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 23:25:20 +00:00
|
|
|
pub struct ChunkDisassembler<'a> {
|
2024-09-12 17:03:24 +00:00
|
|
|
name: &'a str,
|
2024-09-12 23:25:20 +00:00
|
|
|
chunk: &'a Chunk,
|
2024-09-18 17:42:32 +00:00
|
|
|
width: Option<usize>,
|
2024-09-12 23:25:20 +00:00
|
|
|
styled: bool,
|
2024-09-12 17:03:24 +00:00
|
|
|
}
|
2024-09-07 10:38:12 +00:00
|
|
|
|
2024-09-12 23:25:20 +00:00
|
|
|
impl<'a> ChunkDisassembler<'a> {
|
2024-09-13 05:10:07 +00:00
|
|
|
const INSTRUCTION_HEADER: [&'static str; 5] = [
|
|
|
|
"",
|
|
|
|
"Instructions",
|
|
|
|
"------------",
|
2024-09-19 00:02:12 +00:00
|
|
|
"INDEX OPERATION INFO POSITION",
|
|
|
|
"----- --------------- ------------------------------ --------",
|
2024-09-13 05:10:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const CONSTANT_HEADER: [&'static str; 5] = [
|
|
|
|
"",
|
|
|
|
"Constants",
|
|
|
|
"---------",
|
2024-09-18 17:42:32 +00:00
|
|
|
"INDEX VALUE ",
|
|
|
|
"----- ---------",
|
2024-09-13 05:10:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const LOCAL_HEADER: [&'static str; 5] = [
|
|
|
|
"",
|
|
|
|
"Locals",
|
|
|
|
"------",
|
2024-09-18 15:27:41 +00:00
|
|
|
"INDEX IDENTIFIER MUTABLE DEPTH REGISTER",
|
|
|
|
"----- ---------- ------- ----- --------",
|
2024-09-13 05:10:07 +00:00
|
|
|
];
|
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
/// The default width of the disassembly output. To correctly align the output, this should
|
|
|
|
/// return the width of the longest line that the disassembler is guaranteed to produce.
|
2024-09-19 00:02:12 +00:00
|
|
|
pub fn default_width() -> usize {
|
2024-09-18 17:42:32 +00:00
|
|
|
let longest_line = Self::INSTRUCTION_HEADER[4];
|
|
|
|
|
2024-09-19 00:02:12 +00:00
|
|
|
longest_line.chars().count()
|
2024-09-18 17:42:32 +00:00
|
|
|
}
|
2024-09-13 05:10:07 +00:00
|
|
|
|
2024-09-12 23:25:20 +00:00
|
|
|
pub fn new(name: &'a str, chunk: &'a Chunk) -> Self {
|
|
|
|
Self {
|
|
|
|
name,
|
|
|
|
chunk,
|
2024-09-18 17:42:32 +00:00
|
|
|
width: None,
|
2024-09-12 23:25:20 +00:00
|
|
|
styled: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-15 01:05:03 +00:00
|
|
|
pub fn width(&mut self, width: usize) -> &mut Self {
|
2024-09-18 17:42:32 +00:00
|
|
|
self.width = Some(width);
|
2024-09-15 01:05:03 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn styled(&mut self, styled: bool) -> &mut Self {
|
|
|
|
self.styled = styled;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-09-12 23:25:20 +00:00
|
|
|
pub fn disassemble(&self) -> String {
|
2024-09-19 00:02:12 +00:00
|
|
|
let width = self.width.unwrap_or_else(Self::default_width);
|
2024-09-18 17:42:32 +00:00
|
|
|
let center = |line: &str| format!("{line:^width$}\n");
|
2024-09-13 05:10:07 +00:00
|
|
|
let style = |line: String| {
|
|
|
|
if self.styled {
|
|
|
|
line.bold().to_string()
|
2024-09-12 23:25:20 +00:00
|
|
|
} else {
|
2024-09-13 05:10:07 +00:00
|
|
|
line
|
2024-09-12 17:03:24 +00:00
|
|
|
}
|
2024-09-12 23:25:20 +00:00
|
|
|
};
|
2024-09-10 00:55:00 +00:00
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
let mut disassembly = String::with_capacity(self.predict_length());
|
2024-09-14 17:59:11 +00:00
|
|
|
let name_line = style(center(self.name));
|
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
disassembly.push_str(&name_line);
|
2024-09-13 05:10:07 +00:00
|
|
|
|
2024-09-15 08:25:24 +00:00
|
|
|
let info_line = center(&format!(
|
|
|
|
"{} instructions, {} constants, {} locals",
|
|
|
|
self.chunk.instructions.len(),
|
|
|
|
self.chunk.constants.len(),
|
|
|
|
self.chunk.locals.len()
|
|
|
|
));
|
|
|
|
let styled_info_line = {
|
|
|
|
if self.styled {
|
2024-09-18 17:42:32 +00:00
|
|
|
info_line.dimmed().to_string()
|
2024-09-15 08:25:24 +00:00
|
|
|
} else {
|
|
|
|
info_line
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
disassembly.push_str(&styled_info_line);
|
2024-09-15 08:25:24 +00:00
|
|
|
|
2024-09-13 05:10:07 +00:00
|
|
|
for line in Self::INSTRUCTION_HEADER {
|
2024-09-18 17:42:32 +00:00
|
|
|
disassembly.push_str(&style(center(line)));
|
2024-09-13 05:10:07 +00:00
|
|
|
}
|
2024-09-12 17:03:24 +00:00
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
for (index, (instruction, position)) in self.chunk.instructions.iter().enumerate() {
|
2024-09-12 23:25:20 +00:00
|
|
|
let position = position.to_string();
|
2024-09-15 05:24:04 +00:00
|
|
|
let operation = instruction.operation().to_string();
|
2024-09-12 23:25:20 +00:00
|
|
|
let info_option = instruction.disassembly_info(Some(self.chunk));
|
2024-09-18 17:42:32 +00:00
|
|
|
|
2024-09-12 23:25:20 +00:00
|
|
|
let instruction_display = if let Some(info) = info_option {
|
2024-09-19 00:02:12 +00:00
|
|
|
format!("{index:<5} {operation:15} {info:30} {position:8}")
|
2024-09-12 23:25:20 +00:00
|
|
|
} else {
|
2024-09-19 00:02:12 +00:00
|
|
|
format!("{index:<5} {operation:15} {:30} {position:8}", " ")
|
2024-09-12 23:25:20 +00:00
|
|
|
};
|
2024-09-12 17:03:24 +00:00
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
disassembly.push_str(¢er(&instruction_display));
|
2024-09-12 23:25:20 +00:00
|
|
|
}
|
2024-09-10 00:55:00 +00:00
|
|
|
|
2024-09-13 05:10:07 +00:00
|
|
|
for line in Self::CONSTANT_HEADER {
|
2024-09-18 17:42:32 +00:00
|
|
|
disassembly.push_str(&style(center(line)));
|
2024-09-13 05:10:07 +00:00
|
|
|
}
|
2024-09-12 17:03:24 +00:00
|
|
|
|
2024-09-12 23:25:20 +00:00
|
|
|
for (index, value_option) in self.chunk.constants.iter().enumerate() {
|
2024-09-11 08:22:54 +00:00
|
|
|
let value_display = value_option
|
|
|
|
.as_ref()
|
|
|
|
.map(|value| value.to_string())
|
2024-09-18 17:42:32 +00:00
|
|
|
.unwrap_or("empty".to_string());
|
2024-09-19 02:00:24 +00:00
|
|
|
let trucated_length = 8;
|
|
|
|
let with_elipsis = trucated_length - 3;
|
|
|
|
let constant_display = if value_display.len() > with_elipsis {
|
|
|
|
format!("{index:<5} {value_display:.<trucated_length$.with_elipsis$}")
|
2024-09-18 17:42:32 +00:00
|
|
|
} else {
|
2024-09-19 02:00:24 +00:00
|
|
|
format!("{index:<5} {value_display:<trucated_length$}")
|
2024-09-18 17:42:32 +00:00
|
|
|
};
|
2024-09-10 00:55:00 +00:00
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
disassembly.push_str(¢er(&constant_display));
|
2024-09-10 00:55:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-13 05:10:07 +00:00
|
|
|
for line in Self::LOCAL_HEADER {
|
2024-09-18 17:42:32 +00:00
|
|
|
disassembly.push_str(&style(center(line)));
|
2024-09-13 05:10:07 +00:00
|
|
|
}
|
2024-09-10 13:26:05 +00:00
|
|
|
|
2024-09-12 13:11:49 +00:00
|
|
|
for (
|
|
|
|
index,
|
|
|
|
Local {
|
|
|
|
identifier,
|
|
|
|
depth,
|
2024-09-13 06:28:18 +00:00
|
|
|
register_index,
|
2024-09-18 15:27:41 +00:00
|
|
|
mutable,
|
2024-09-12 13:11:49 +00:00
|
|
|
},
|
2024-09-12 23:25:20 +00:00
|
|
|
) in self.chunk.locals.iter().enumerate()
|
2024-09-12 13:11:49 +00:00
|
|
|
{
|
2024-09-13 06:28:18 +00:00
|
|
|
let register_display = register_index
|
2024-09-12 13:11:49 +00:00
|
|
|
.as_ref()
|
|
|
|
.map(|value| value.to_string())
|
2024-09-14 17:59:11 +00:00
|
|
|
.unwrap_or_else(|| "empty".to_string());
|
2024-09-12 17:03:24 +00:00
|
|
|
let identifier_display = identifier.as_str();
|
2024-09-18 15:27:41 +00:00
|
|
|
let local_display = format!(
|
2024-09-18 17:42:32 +00:00
|
|
|
"{index:<5} {identifier_display:10} {mutable:7} {depth:<5} {register_display:8}"
|
2024-09-18 15:27:41 +00:00
|
|
|
);
|
2024-09-12 13:11:49 +00:00
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
disassembly.push_str(¢er(&local_display));
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-14 18:31:40 +00:00
|
|
|
let expected_length = self.predict_length();
|
2024-09-18 17:42:32 +00:00
|
|
|
let actual_length = disassembly.len();
|
2024-09-14 18:31:40 +00:00
|
|
|
|
|
|
|
if !self.styled && expected_length != actual_length {
|
|
|
|
log::debug!(
|
|
|
|
"Chunk disassembly was not optimized correctly, expected string length {expected_length}, got {actual_length}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.styled && expected_length > actual_length {
|
|
|
|
log::debug!(
|
|
|
|
"Chunk disassembly was not optimized correctly, expected string length to be at least{expected_length}, got {actual_length}",
|
|
|
|
);
|
|
|
|
}
|
2024-09-13 05:10:07 +00:00
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
disassembly
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-13 05:10:07 +00:00
|
|
|
/// Predicts the capacity of the disassembled output. This is used to pre-allocate the string
|
|
|
|
/// buffer to avoid reallocations.
|
|
|
|
///
|
|
|
|
/// The capacity is calculated as follows:
|
|
|
|
/// - Get the number of static lines, i.e. lines that are always present in the disassembly
|
|
|
|
/// - Get the number of dynamic lines, i.e. lines that are generated from the chunk
|
2024-09-15 08:25:24 +00:00
|
|
|
/// - Add an one to the width to account for the newline character
|
2024-09-13 05:10:07 +00:00
|
|
|
/// - Multiply the total number of lines by the width of the disassembly output
|
2024-09-14 17:59:11 +00:00
|
|
|
///
|
|
|
|
/// The result is accurate only if the output is not styled. Otherwise the extra bytes added by
|
|
|
|
/// the ANSI escape codes will make the result too low. It still works as a lower bound in that
|
|
|
|
/// case.
|
|
|
|
fn predict_length(&self) -> usize {
|
2024-09-18 17:42:32 +00:00
|
|
|
const EXTRA_LINES: usize = 2; // There is one info line and one empty line after the name
|
2024-09-14 17:59:11 +00:00
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
let static_line_count = Self::INSTRUCTION_HEADER.len()
|
|
|
|
+ Self::CONSTANT_HEADER.len()
|
|
|
|
+ Self::LOCAL_HEADER.len()
|
|
|
|
+ EXTRA_LINES;
|
2024-09-13 05:10:07 +00:00
|
|
|
let dynamic_line_count =
|
|
|
|
self.chunk.instructions.len() + self.chunk.constants.len() + self.chunk.locals.len();
|
2024-09-18 17:42:32 +00:00
|
|
|
let total_line_count = static_line_count + dynamic_line_count;
|
2024-09-19 00:02:12 +00:00
|
|
|
let width = self.width.unwrap_or_else(Self::default_width) + 1;
|
2024-09-13 05:10:07 +00:00
|
|
|
|
2024-09-18 17:42:32 +00:00
|
|
|
total_line_count * width
|
2024-09-12 09:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 23:23:49 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2024-09-07 10:38:12 +00:00
|
|
|
pub enum ChunkError {
|
2024-09-10 22:19:59 +00:00
|
|
|
CodeIndexOfBounds {
|
|
|
|
offset: usize,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-11 08:22:54 +00:00
|
|
|
ConstantAlreadyUsed {
|
2024-09-12 13:11:49 +00:00
|
|
|
index: usize,
|
2024-09-11 08:22:54 +00:00
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-11 07:10:12 +00:00
|
|
|
ConstantOverflow {
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-10 22:19:59 +00:00
|
|
|
ConstantIndexOutOfBounds {
|
2024-09-12 13:11:49 +00:00
|
|
|
index: usize,
|
2024-09-10 22:19:59 +00:00
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-13 05:10:07 +00:00
|
|
|
InstructionUnderflow {
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-12 13:11:49 +00:00
|
|
|
LocalIndexOutOfBounds {
|
|
|
|
index: usize,
|
2024-09-11 07:10:12 +00:00
|
|
|
position: Span,
|
|
|
|
},
|
|
|
|
IdentifierOverflow {
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-10 22:19:59 +00:00
|
|
|
IdentifierNotFound {
|
|
|
|
identifier: Identifier,
|
|
|
|
position: Span,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AnnotatedError for ChunkError {
|
|
|
|
fn title() -> &'static str {
|
|
|
|
"Chunk Error"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
ChunkError::CodeIndexOfBounds { .. } => "Code index out of bounds",
|
2024-09-11 08:22:54 +00:00
|
|
|
ChunkError::ConstantAlreadyUsed { .. } => "Constant already used",
|
2024-09-11 07:10:12 +00:00
|
|
|
ChunkError::ConstantOverflow { .. } => "Constant overflow",
|
2024-09-10 22:19:59 +00:00
|
|
|
ChunkError::ConstantIndexOutOfBounds { .. } => "Constant index out of bounds",
|
2024-09-13 05:10:07 +00:00
|
|
|
ChunkError::InstructionUnderflow { .. } => "Instruction underflow",
|
2024-09-12 13:11:49 +00:00
|
|
|
ChunkError::LocalIndexOutOfBounds { .. } => "Identifier index out of bounds",
|
2024-09-11 07:10:12 +00:00
|
|
|
ChunkError::IdentifierOverflow { .. } => "Identifier overflow",
|
2024-09-10 22:19:59 +00:00
|
|
|
ChunkError::IdentifierNotFound { .. } => "Identifier not found",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn details(&self) -> Option<String> {
|
|
|
|
match self {
|
|
|
|
ChunkError::CodeIndexOfBounds { offset, .. } => Some(format!("Code index: {}", offset)),
|
2024-09-11 08:22:54 +00:00
|
|
|
ChunkError::ConstantAlreadyUsed { index, .. } => {
|
|
|
|
Some(format!("Constant index: {}", index))
|
|
|
|
}
|
2024-09-10 22:19:59 +00:00
|
|
|
ChunkError::ConstantIndexOutOfBounds { index, .. } => {
|
|
|
|
Some(format!("Constant index: {}", index))
|
|
|
|
}
|
2024-09-13 05:10:07 +00:00
|
|
|
ChunkError::InstructionUnderflow { .. } => None,
|
2024-09-12 13:11:49 +00:00
|
|
|
ChunkError::LocalIndexOutOfBounds { index, .. } => {
|
2024-09-10 22:19:59 +00:00
|
|
|
Some(format!("Identifier index: {}", index))
|
|
|
|
}
|
|
|
|
ChunkError::IdentifierNotFound { identifier, .. } => {
|
|
|
|
Some(format!("Identifier: {}", identifier))
|
|
|
|
}
|
2024-09-17 16:08:40 +00:00
|
|
|
ChunkError::IdentifierOverflow { .. } => Some("Identifier overflow".to_string()),
|
|
|
|
ChunkError::ConstantOverflow { .. } => Some("Constant overflow".to_string()),
|
2024-09-10 22:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn position(&self) -> Span {
|
|
|
|
match self {
|
|
|
|
ChunkError::CodeIndexOfBounds { position, .. } => *position,
|
2024-09-11 08:22:54 +00:00
|
|
|
ChunkError::ConstantAlreadyUsed { position, .. } => *position,
|
2024-09-10 22:19:59 +00:00
|
|
|
ChunkError::ConstantIndexOutOfBounds { position, .. } => *position,
|
|
|
|
ChunkError::IdentifierNotFound { position, .. } => *position,
|
2024-09-17 16:08:40 +00:00
|
|
|
ChunkError::InstructionUnderflow { position, .. } => *position,
|
|
|
|
ChunkError::LocalIndexOutOfBounds { position, .. } => *position,
|
|
|
|
ChunkError::IdentifierOverflow { position, .. } => *position,
|
|
|
|
ChunkError::ConstantOverflow { position, .. } => *position,
|
2024-09-10 22:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|