Add source location tracking to AST nodes

The commit expands the source location tracking through the AST by:

1. Switching from raw Expr/Stmt nodes to AstNode wrappers containing
source slices 2. Updating interpreter and parser to preserve location
info through node traversal 3. Enhancing error reporting with richer
source context information
This commit is contained in:
Giulio Agostini
2025-10-04 19:02:33 +02:00
parent 113a683beb
commit 41253e932a
11 changed files with 886 additions and 352 deletions
-40
View File
@@ -552,43 +552,3 @@ pub fn pretty_stmt_compact(stmt: &Stmt) -> String {
pub fn pretty_tree<T: PrettyPrint>(item: &T) -> String {
pretty_print_with_config(item, &PrettyConfig::tree())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::frontend::{
source_registry::{SourceId, SourcePosition, SourceSlice},
tokens::{LiteralValue, TokenType},
};
#[test]
fn test_pretty_expr_literal() {
let expr = Expr::Literal {
value: LiteralValue::Number(42.0),
};
let compact = expr.pretty_compact();
assert!(compact.contains("42"));
let full = expr.pretty();
assert!(full.contains("Literal"));
assert!(full.contains("42"));
}
#[test]
fn test_pretty_expr_binary() {
let expr = Expr::Binary {
left: Box::new(Expr::Literal {
value: LiteralValue::Number(1.0),
}),
operator: TokenType::Plus,
right: Box::new(Expr::Literal {
value: LiteralValue::Number(2.0),
}),
};
let compact = expr.pretty_compact();
assert!(compact.contains("1"));
assert!(compact.contains("2"));
}
}