stack traces in the brush editor

after 35 thousand years it's finally here
good erro message
This commit is contained in:
りき萌 2025-06-25 20:51:34 +02:00
parent c1612b2a94
commit e49885c83a
11 changed files with 710 additions and 150 deletions

View file

@ -1,10 +1,9 @@
use core::{
fmt::{self, Display},
mem::transmute,
};
use core::fmt::{self, Display};
use alloc::{borrow::ToOwned, string::String, vec::Vec};
use crate::source::Span;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Opcode {
@ -56,6 +55,14 @@ pub const CAPTURE_CAPTURE: u8 = 1;
#[derive(Debug, Clone)]
pub struct Chunk {
pub bytecode: Vec<u8>,
pub span_info: Vec<SpanRun>,
pub current_span: Span,
}
#[derive(Debug, Clone)]
pub struct SpanRun {
pub span: Span,
pub len: u16,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -66,6 +73,8 @@ impl Chunk {
if capacity <= (1 << 16) {
Ok(Chunk {
bytecode: Vec::with_capacity(capacity),
span_info: Vec::new(),
current_span: Span::new(0, 0),
})
} else {
Err(ChunkSizeError)
@ -76,6 +85,16 @@ impl Chunk {
Offset(self.bytecode.len() as u16)
}
fn push_span_info(&mut self, span: Span, len: u16) {
if let Some(info) = self.span_info.last_mut() {
if info.span == span {
info.len += len;
return;
}
}
self.span_info.push(SpanRun { span, len });
}
pub fn emit_bytes(&mut self, bytes: &[u8]) -> Result<Offset, EmitError> {
if self.bytecode.len() + bytes.len() > self.bytecode.capacity() {
return Err(EmitError);
@ -83,6 +102,7 @@ impl Chunk {
let offset = Offset(self.bytecode.len() as u16);
self.bytecode.extend_from_slice(bytes);
self.push_span_info(self.current_span, bytes.len() as u16);
Ok(offset)
}
@ -122,40 +142,15 @@ impl Chunk {
self.patch_u16(offset, x.0);
}
// NOTE: I'm aware these aren't the fastest implementations since they validate quite a lot
// during runtime, but this is just an MVP. It doesn't have to be blazingly fast.
pub fn read_u8(&self, pc: &mut usize) -> Result<u8, ReadError> {
let x = self.bytecode.get(*pc).copied();
*pc += 1;
x.ok_or(ReadError)
}
pub fn read_u16(&self, pc: &mut usize) -> Result<u16, ReadError> {
let xs = &self.bytecode[*pc..*pc + 2];
*pc += 2;
Ok(u16::from_le_bytes(xs.try_into().map_err(|_| ReadError)?))
}
pub fn read_u32(&self, pc: &mut usize) -> Result<u32, ReadError> {
let xs = &self.bytecode[*pc..*pc + 4];
*pc += 4;
Ok(u32::from_le_bytes(xs.try_into().map_err(|_| ReadError)?))
}
pub fn read_f32(&self, pc: &mut usize) -> Result<f32, ReadError> {
let xs = &self.bytecode[*pc..*pc + 4];
*pc += 4;
Ok(f32::from_le_bytes(xs.try_into().map_err(|_| ReadError)?))
}
pub fn read_opcode(&self, pc: &mut usize) -> Result<Opcode, ReadError> {
let x = self.read_u8(pc)?;
if x <= Opcode::Return as u8 {
Ok(unsafe { transmute::<u8, Opcode>(x) })
} else {
Err(ReadError)
pub fn find_span(&self, pc: u16) -> Option<&Span> {
let mut cur = 0;
for info in &self.span_info {
if pc >= cur && pc < cur + info.len {
return Some(&info.span);
}
cur += info.len;
}
None
}
}
@ -183,15 +178,6 @@ impl Display for EmitError {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReadError;
impl Display for ReadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid bytecode: out of bounds read or invalid opcode")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct DefId(u16);

View file

@ -1,9 +1,14 @@
use core::{
error::Error,
fmt::{self, Display},
mem::take,
};
use alloc::vec::Vec;
use alloc::{
format,
string::{String, ToString},
vec::Vec,
};
use crate::{
ast::{Ast, NodeId, NodeKind},
@ -42,6 +47,11 @@ pub struct Compiler<'a> {
pub chunk: &'a mut Chunk,
pub diagnostics: Vec<Diagnostic>,
scopes: Vec<Scope<'a>>,
// NOTE: This mechanism is kind of non-obvious at first, but keep in mind this is the name of
// any functions emitted within the current scope---not of the current function.
// The convention is to set this to the current def's name, and any functions inside the current
// function get λ as a suffix. The more nesting, the more lambdas---e.g. myFunctionλλ.
function_name: String,
}
#[derive(Debug, Clone, Copy)]
@ -60,6 +70,8 @@ impl<'a> Compiler<'a> {
captures: Vec::new(),
let_count: 0,
}]),
// For unnamed functions within the brush's toplevel.
function_name: "(brush)λ".into(),
}
}
@ -86,7 +98,10 @@ impl<'a> Compiler<'a> {
type CompileResult<T = ()> = Result<T, CompileError>;
pub fn compile_expr<'a>(c: &mut Compiler<'a>, src: &Source<'a>, node_id: NodeId) -> CompileResult {
match src.ast.kind(node_id) {
let previous_span = c.chunk.current_span;
c.chunk.current_span = src.ast.span(node_id);
let result = match src.ast.kind(node_id) {
// The nil node is special, as it inhabits node ID 0.
NodeKind::Nil => {
unreachable!("Nil node should never be emitted (ParenEmpty is used for nil literals)")
@ -121,7 +136,11 @@ pub fn compile_expr<'a>(c: &mut Compiler<'a>, src: &Source<'a>, node_id: NodeId)
// Error nodes are ignored, because for each error node an appropriate parser
// diagnostic is emitted anyways.
NodeKind::Error => Ok(()),
}
};
c.chunk.current_span = previous_span;
result
}
fn compile_nil(c: &mut Compiler) -> CompileResult {
@ -518,7 +537,17 @@ fn compile_lambda<'a>(c: &mut Compiler<'a>, src: &Source<'a>, node_id: NodeId) -
c.chunk.emit_opcode(Opcode::Function)?;
c.chunk.emit_u8(param_count)?;
let after_offset = c.chunk.emit_u16(0)?;
let then_offset = c.chunk.emit_u16(0)?;
let function_name = &c.function_name[0..c.function_name.len().min(u8::MAX as usize)];
c.chunk.emit_u8(function_name.len() as u8)?;
c.chunk.emit_bytes(function_name.as_bytes())?;
// Swap the current name for a name with a λ at the end.
// This is to make lambdas within the current lambda shown as a different functions in
// stack traces.
let previous_function_name = take(&mut c.function_name);
c.function_name = format!("{previous_function_name}λ");
c.scopes.push(Scope {
locals,
@ -528,8 +557,10 @@ fn compile_lambda<'a>(c: &mut Compiler<'a>, src: &Source<'a>, node_id: NodeId) -
compile_expr(c, src, body)?;
c.chunk.emit_opcode(Opcode::Return)?;
let after = u16::try_from(c.chunk.bytecode.len()).expect("chunk is too large");
c.chunk.patch_u16(after_offset, after);
c.function_name = previous_function_name;
let then = u16::try_from(c.chunk.bytecode.len()).expect("chunk is too large");
c.chunk.patch_u16(then_offset, then);
let scope = c.scopes.pop().unwrap();
let let_count = u8::try_from(scope.let_count).unwrap_or_else(|_| {
@ -580,6 +611,7 @@ fn compile_toplevel<'a>(c: &mut Compiler<'a>, src: &Source<'a>, node_id: NodeId)
));
}
c.chunk.current_span = src.ast.span(toplevel_expr);
match compile_toplevel_expr(c, src, toplevel_expr)? {
ToplevelExpr::Def => (),
ToplevelExpr::Result if result_expr.is_none() => result_expr = Some(toplevel_expr),
@ -676,10 +708,20 @@ fn compile_def<'a>(c: &mut Compiler<'a>, src: &Source<'a>, node_id: NodeId) -> C
// zero def instead.
let def_id = c.defs.get_def(name).unwrap_or_default();
let previous_function_name = take(&mut c.function_name);
if src.ast.kind(right) == NodeKind::Lambda {
c.function_name = name.to_string();
} else {
// Name for lambdas within the current def, which is not a lambda itself.
c.function_name = format!("{name}λ");
}
compile_expr(c, src, right)?;
c.chunk.emit_opcode(Opcode::SetDef)?;
c.chunk.emit_u16(def_id.to_u16())?;
c.function_name = previous_function_name;
Ok(())
}