2024-09-10 02:57:14 +00:00
|
|
|
use std::fmt::{self, Debug, Display, Formatter};
|
2024-09-07 10:38:12 +00:00
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-09-11 07:10:12 +00:00
|
|
|
use crate::{AnnotatedError, Identifier, Instruction, 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 03:07:20 +00:00
|
|
|
code: Vec<(Instruction, Span)>,
|
2024-09-11 08:22:54 +00:00
|
|
|
constants: Vec<Option<Value>>,
|
2024-09-11 07:10:12 +00:00
|
|
|
identifiers: Vec<Local>,
|
|
|
|
scope_depth: usize,
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Chunk {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
code: Vec::new(),
|
|
|
|
constants: Vec::new(),
|
2024-09-11 07:10:12 +00:00
|
|
|
identifiers: Vec::new(),
|
|
|
|
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 03:07:20 +00:00
|
|
|
code: Vec<(Instruction, Span)>,
|
2024-09-07 16:15:47 +00:00
|
|
|
constants: Vec<Value>,
|
2024-09-09 23:23:49 +00:00
|
|
|
identifiers: Vec<Local>,
|
2024-09-07 16:15:47 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
code,
|
2024-09-11 08:22:54 +00:00
|
|
|
constants: constants.into_iter().map(Some).collect(),
|
2024-09-11 07:10:12 +00:00
|
|
|
identifiers,
|
|
|
|
scope_depth: 0,
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
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-12 03:07:20 +00:00
|
|
|
pub fn get_code(
|
|
|
|
&self,
|
|
|
|
offset: usize,
|
|
|
|
position: Span,
|
|
|
|
) -> Result<&(Instruction, Span), ChunkError> {
|
2024-09-07 16:15:47 +00:00
|
|
|
self.code
|
|
|
|
.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 03:07:20 +00:00
|
|
|
pub fn push_code(&mut self, instruction: Instruction, position: Span) {
|
|
|
|
self.code.push((instruction, position));
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
pub fn get_constant(&self, index: u16, position: Span) -> Result<&Value, ChunkError> {
|
2024-09-07 10:38:12 +00:00
|
|
|
self.constants
|
2024-09-09 23:23:49 +00:00
|
|
|
.get(index as usize)
|
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-12 03:07:20 +00:00
|
|
|
pub fn use_constant(&mut self, index: u16, position: Span) -> Result<Value, ChunkError> {
|
2024-09-11 08:22:54 +00:00
|
|
|
self.constants
|
2024-09-12 03:07:20 +00:00
|
|
|
.get_mut(index as usize)
|
|
|
|
.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-12 03:07:20 +00:00
|
|
|
pub fn push_constant(&mut self, value: Value, position: Span) -> Result<u16, 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-12 03:07:20 +00:00
|
|
|
Ok(starting_length as u16)
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 23:23:49 +00:00
|
|
|
pub fn contains_identifier(&self, identifier: &Identifier) -> bool {
|
2024-09-11 07:10:12 +00:00
|
|
|
self.identifiers
|
|
|
|
.iter()
|
|
|
|
.any(|local| &local.identifier == identifier)
|
2024-09-09 23:23:49 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
pub fn get_local(&self, index: u16, position: Span) -> Result<&Local, ChunkError> {
|
2024-09-10 13:26:05 +00:00
|
|
|
self.identifiers
|
|
|
|
.get(index as usize)
|
2024-09-11 07:10:12 +00:00
|
|
|
.ok_or(ChunkError::IdentifierIndexOutOfBounds { index, position })
|
2024-09-10 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 08:22:54 +00:00
|
|
|
pub fn get_identifier(&self, index: u8) -> Option<&Identifier> {
|
|
|
|
if let Some(local) = self.identifiers.get(index as usize) {
|
|
|
|
Some(&local.identifier)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 22:19:59 +00:00
|
|
|
pub fn get_identifier_index(
|
|
|
|
&self,
|
|
|
|
identifier: &Identifier,
|
|
|
|
position: Span,
|
2024-09-12 03:07:20 +00:00
|
|
|
) -> Result<u16, ChunkError> {
|
2024-09-09 23:23:49 +00:00
|
|
|
self.identifiers
|
2024-09-11 07:10:12 +00:00
|
|
|
.iter()
|
|
|
|
.rev()
|
2024-09-11 08:22:54 +00:00
|
|
|
.enumerate()
|
2024-09-11 07:10:12 +00:00
|
|
|
.find_map(|(index, local)| {
|
|
|
|
if &local.identifier == identifier {
|
2024-09-12 03:07:20 +00:00
|
|
|
Some(index as u16)
|
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-11 07:10:12 +00:00
|
|
|
pub fn declare_variable(
|
|
|
|
&mut self,
|
|
|
|
identifier: Identifier,
|
|
|
|
position: Span,
|
2024-09-12 03:07:20 +00:00
|
|
|
) -> Result<u16, ChunkError> {
|
2024-09-11 07:10:12 +00:00
|
|
|
let starting_length = self.identifiers.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-11 07:10:12 +00:00
|
|
|
self.identifiers.push(Local {
|
|
|
|
identifier,
|
|
|
|
depth: self.scope_depth,
|
|
|
|
});
|
2024-09-07 16:15:47 +00:00
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
Ok(starting_length as u16)
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07 10:38:12 +00:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.code.clear();
|
|
|
|
self.constants.clear();
|
2024-09-09 23:23:49 +00:00
|
|
|
self.identifiers.clear();
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 07:10:12 +00:00
|
|
|
pub fn identifiers(&self) -> &[Local] {
|
|
|
|
&self.identifiers
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pop_identifier(&mut self) -> Option<Local> {
|
|
|
|
self.identifiers.pop()
|
|
|
|
}
|
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
pub fn disassemble(&self, name: &str) -> String {
|
|
|
|
let mut output = String::new();
|
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
let name_length = name.len();
|
2024-09-12 03:07:20 +00:00
|
|
|
let buffer_length = 34_usize.saturating_sub(name_length + 2);
|
2024-09-10 14:44:15 +00:00
|
|
|
let name_buffer = " ".repeat(buffer_length / 2);
|
2024-09-12 04:39:31 +00:00
|
|
|
let name_line = format!("{name_buffer}{name}{name_buffer}\n");
|
2024-09-12 03:07:20 +00:00
|
|
|
let name_underline = format!("{name_buffer}{}{name_buffer}\n", "-".repeat(name_length));
|
2024-09-10 14:44:15 +00:00
|
|
|
|
|
|
|
output.push_str(&name_line);
|
2024-09-12 03:07:20 +00:00
|
|
|
output.push_str(&name_underline);
|
2024-09-10 14:44:15 +00:00
|
|
|
output.push_str("\n Code \n");
|
2024-09-12 04:39:31 +00:00
|
|
|
output.push_str("------ -------- ------------\n");
|
|
|
|
output.push_str("OFFSET POSITION INSTRUCTION\n");
|
|
|
|
output.push_str("------ -------- ------------\n");
|
2024-09-07 10:38:12 +00:00
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
for (offset, (instruction, position)) in self.code.iter().enumerate() {
|
2024-09-10 13:26:05 +00:00
|
|
|
let display = format!(
|
2024-09-12 04:39:31 +00:00
|
|
|
"{offset:04} {position} {}\n",
|
|
|
|
instruction.disassemble(self)
|
2024-09-10 13:26:05 +00:00
|
|
|
);
|
2024-09-10 00:55:00 +00:00
|
|
|
|
2024-09-10 13:26:05 +00:00
|
|
|
output.push_str(&display);
|
2024-09-10 00:55:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 08:22:54 +00:00
|
|
|
output.push_str("\n Constants\n");
|
2024-09-10 13:26:05 +00:00
|
|
|
output.push_str("----- ---- -----\n");
|
2024-09-10 00:55:00 +00:00
|
|
|
output.push_str("INDEX KIND VALUE\n");
|
2024-09-10 13:26:05 +00:00
|
|
|
output.push_str("----- ---- -----\n");
|
2024-09-10 00:55:00 +00:00
|
|
|
|
2024-09-11 08:22:54 +00:00
|
|
|
for (index, value_option) in self.constants.iter().enumerate() {
|
|
|
|
let value_kind_display = match value_option {
|
|
|
|
Some(Value::Raw(_)) => "RAW ",
|
|
|
|
Some(Value::Reference(_)) => "REF ",
|
|
|
|
Some(Value::Mutable(_)) => "MUT ",
|
|
|
|
None => "EMPTY",
|
2024-09-10 00:55:00 +00:00
|
|
|
};
|
2024-09-11 08:22:54 +00:00
|
|
|
let value_display = value_option
|
|
|
|
.as_ref()
|
|
|
|
.map(|value| value.to_string())
|
|
|
|
.unwrap_or_else(|| "EMPTY".to_string());
|
|
|
|
let display = format!("{index:3} {value_kind_display} {value_display}\n",);
|
2024-09-10 00:55:00 +00:00
|
|
|
|
|
|
|
output.push_str(&display);
|
|
|
|
}
|
|
|
|
|
2024-09-11 08:22:54 +00:00
|
|
|
output.push_str("\n Identifiers\n");
|
2024-09-11 07:10:12 +00:00
|
|
|
output.push_str("----- ---------- -----\n");
|
2024-09-11 08:22:54 +00:00
|
|
|
output.push_str("INDEX NAME DEPTH\n");
|
2024-09-11 07:10:12 +00:00
|
|
|
output.push_str("----- ---------- -----\n");
|
2024-09-10 13:26:05 +00:00
|
|
|
|
2024-09-11 07:10:12 +00:00
|
|
|
for (index, Local { identifier, depth }) in self.identifiers.iter().enumerate() {
|
|
|
|
let display = format!("{index:3} {:10} {depth}\n", identifier.as_str());
|
2024-09-07 21:16:14 +00:00
|
|
|
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 {
|
2024-09-10 05:04:30 +00:00
|
|
|
write!(f, "{}", self.disassemble("Chunk Disassembly"))
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 22:48:01 +00:00
|
|
|
impl Debug for Chunk {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-09-10 05:04:30 +00:00
|
|
|
write!(f, "{}", self.disassemble("Chunk Disassembly"))
|
2024-09-07 22:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-11 08:22:54 +00:00
|
|
|
impl Eq for Chunk {}
|
|
|
|
|
|
|
|
impl PartialEq for Chunk {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.code == other.code
|
|
|
|
&& self.constants == other.constants
|
|
|
|
&& self.identifiers == other.identifiers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-11 07:10:12 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Local {
|
|
|
|
pub identifier: Identifier,
|
|
|
|
pub depth: usize,
|
|
|
|
}
|
|
|
|
|
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 03:07:20 +00:00
|
|
|
index: u16,
|
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 03:07:20 +00:00
|
|
|
index: u16,
|
2024-09-10 22:19:59 +00:00
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-11 07:10:12 +00:00
|
|
|
IdentifierIndexOutOfBounds {
|
2024-09-12 03:07:20 +00:00
|
|
|
index: u16,
|
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-11 07:10:12 +00:00
|
|
|
ChunkError::IdentifierIndexOutOfBounds { .. } => "Identifier index out of bounds",
|
|
|
|
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-11 07:10:12 +00:00
|
|
|
ChunkError::IdentifierIndexOutOfBounds { index, .. } => {
|
2024-09-10 22:19:59 +00:00
|
|
|
Some(format!("Identifier index: {}", index))
|
|
|
|
}
|
|
|
|
ChunkError::IdentifierNotFound { identifier, .. } => {
|
|
|
|
Some(format!("Identifier: {}", identifier))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
_ => todo!(),
|
|
|
|
}
|
|
|
|
}
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|