Add .idea to gitignore and prep return value types Add middleend module
for AST return value tracking The changes introduce support for tracking return values and labels across AST nodes. The key changes include: - Adding ReturnValue type to BaseValue - Adding return_type field to LoxFunction - Adding label field to Block statements - Creating middleend crawler module for AST traversal Add return value types to AST This accurately captures both changes: adding `.idea/` to gitignore and adding return value types to the AST structure. The changes include adding return value fields to statement nodes and adjusting the interpreter logic accordingly.
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
use crate::common::{
|
||||
ast::{AstNode, Expr, Stmt},
|
||||
lox_result::LoxResult,
|
||||
};
|
||||
|
||||
// Enum per tutti i possibili target di operazioni
|
||||
enum OperationTarget<'a> {
|
||||
Stmt(&'a mut Stmt),
|
||||
Expr(&'a mut Expr),
|
||||
AstStmt(&'a mut AstNode<Stmt>),
|
||||
AstExpr(&'a mut AstNode<Expr>),
|
||||
}
|
||||
|
||||
// Trait per le operazioni
|
||||
trait Operation: Send + Sync {
|
||||
fn execute(&self, target: OperationTarget) -> LoxResult<()>;
|
||||
}
|
||||
|
||||
// Operazione concreta per set_label
|
||||
struct SetLabelOperation {
|
||||
new_label: String,
|
||||
}
|
||||
|
||||
impl SetLabelOperation {
|
||||
fn new(label: String) -> Self {
|
||||
Self { new_label: label }
|
||||
}
|
||||
}
|
||||
|
||||
impl Operation for SetLabelOperation {
|
||||
fn execute(&self, target: OperationTarget) -> LoxResult<()> {
|
||||
match target {
|
||||
OperationTarget::Stmt(stmt) => {
|
||||
if let Stmt::Block { label, .. } = stmt {
|
||||
*label = self.new_label.clone();
|
||||
}
|
||||
}
|
||||
OperationTarget::AstStmt(node) => {
|
||||
if let Stmt::Block { label, .. } = &mut node.node {
|
||||
*label = self.new_label.clone();
|
||||
}
|
||||
}
|
||||
_ => {} // Expr non hanno label
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Trait per convertire tipi in OperationTarget
|
||||
trait AsOperationTarget {
|
||||
fn as_target(&mut self) -> OperationTarget;
|
||||
}
|
||||
|
||||
impl AsOperationTarget for Stmt {
|
||||
fn as_target(&mut self) -> OperationTarget {
|
||||
OperationTarget::Stmt(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsOperationTarget for Expr {
|
||||
fn as_target(&mut self) -> OperationTarget {
|
||||
OperationTarget::Expr(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsOperationTarget for AstNode<Stmt> {
|
||||
fn as_target(&mut self) -> OperationTarget {
|
||||
OperationTarget::AstStmt(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsOperationTarget for AstNode<Expr> {
|
||||
fn as_target(&mut self) -> OperationTarget {
|
||||
OperationTarget::AstExpr(self)
|
||||
}
|
||||
}
|
||||
|
||||
// Crawler generico con Command Pattern
|
||||
struct Crawler<T: AsOperationTarget> {
|
||||
node: T,
|
||||
operations: Vec<Box<dyn Operation>>,
|
||||
}
|
||||
|
||||
impl<T: AsOperationTarget> Crawler<T> {
|
||||
fn new(node: T) -> Self {
|
||||
Self {
|
||||
node,
|
||||
operations: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn add_operation(&mut self, op: Box<dyn Operation>) {
|
||||
self.operations.push(op);
|
||||
}
|
||||
|
||||
fn execute_operations(&mut self) -> LoxResult<()> {
|
||||
for op in &self.operations {
|
||||
op.execute(self.node.as_target())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn with_operation(mut self, op: Box<dyn Operation>) -> Self {
|
||||
self.operations.push(op);
|
||||
self
|
||||
}
|
||||
|
||||
fn get_node(&self) -> &T {
|
||||
&self.node
|
||||
}
|
||||
|
||||
fn get_node_mut(&mut self) -> &mut T {
|
||||
&mut self.node
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod crawler;
|
||||
Reference in New Issue
Block a user