Refactor: Rename TCO module to lowering
The TCO (Tail Call Optimization) module has been renamed to `lowering`. This change better reflects the module's broader responsibility, which includes not only TCO but also general AST transformations and preparation for VM execution. The `optimize` function has been renamed to `lower` to align with the module's new name.
This commit is contained in:
@@ -116,11 +116,11 @@ fn calc_stack_size<T>(root_node: &Node<BoundKind<T>, T>) -> u32 {
|
|||||||
(max_slot + 1) as u32
|
(max_slot + 1) as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TCO;
|
pub struct Lowering;
|
||||||
|
|
||||||
impl TCO {
|
impl Lowering {
|
||||||
/// Lowers an AnalyzedNode to an ExecNode and marks tail positions.
|
/// Lowers an AnalyzedNode to an ExecNode, marking tail positions and calculating stack sizes.
|
||||||
pub fn optimize(node: AnalyzedNode) -> ExecNode {
|
pub fn lower(node: AnalyzedNode) -> ExecNode {
|
||||||
let root_stack_size = calc_stack_size(&node);
|
let root_stack_size = calc_stack_size(&node);
|
||||||
let mut exec_node = Self::transform(Rc::new(node), true);
|
let mut exec_node = Self::transform(Rc::new(node), true);
|
||||||
exec_node.ty.stack_size = root_stack_size;
|
exec_node.ty.stack_size = root_stack_size;
|
||||||
@@ -7,7 +7,7 @@ pub mod lambda_collector;
|
|||||||
pub mod macros;
|
pub mod macros;
|
||||||
pub mod optimizer;
|
pub mod optimizer;
|
||||||
pub mod specializer;
|
pub mod specializer;
|
||||||
pub mod tco;
|
pub mod lowering;
|
||||||
pub mod type_checker;
|
pub mod type_checker;
|
||||||
|
|
||||||
pub use binder::*;
|
pub use binder::*;
|
||||||
@@ -17,5 +17,5 @@ pub use dumper::*;
|
|||||||
pub use macros::*;
|
pub use macros::*;
|
||||||
pub use optimizer::*;
|
pub use optimizer::*;
|
||||||
pub use specializer::*;
|
pub use specializer::*;
|
||||||
pub use tco::*;
|
pub use lowering::*;
|
||||||
pub use type_checker::*;
|
pub use type_checker::*;
|
||||||
|
|||||||
@@ -15,10 +15,10 @@ use crate::ast::compiler::bound_nodes::{
|
|||||||
};
|
};
|
||||||
use crate::ast::compiler::dumper::Dumper;
|
use crate::ast::compiler::dumper::Dumper;
|
||||||
use crate::ast::compiler::lambda_collector::LambdaCollector;
|
use crate::ast::compiler::lambda_collector::LambdaCollector;
|
||||||
|
use crate::ast::compiler::lowering::{ExecNode, Lowering};
|
||||||
use crate::ast::compiler::macros::{MacroEvaluator, MacroExpander, MacroRegistry};
|
use crate::ast::compiler::macros::{MacroEvaluator, MacroExpander, MacroRegistry};
|
||||||
use crate::ast::compiler::optimizer::Optimizer;
|
use crate::ast::compiler::optimizer::Optimizer;
|
||||||
use crate::ast::compiler::specializer::{FunctionRegistry, MonoCache, Specializer};
|
use crate::ast::compiler::specializer::{FunctionRegistry, MonoCache, Specializer};
|
||||||
use crate::ast::compiler::tco::{ExecNode, TCO};
|
|
||||||
use crate::ast::rtl::intrinsics;
|
use crate::ast::rtl::intrinsics;
|
||||||
use crate::ast::types::{
|
use crate::ast::types::{
|
||||||
Identity, NodeIdentity, Object, Purity, SourceLocation, StaticType, Value,
|
Identity, NodeIdentity, Object, Purity, SourceLocation, StaticType, Value,
|
||||||
@@ -141,7 +141,7 @@ impl MacroEvaluator for RuntimeMacroEvaluator {
|
|||||||
.join("\n"));
|
.join("\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let exec_ast = TCO::optimize(Analyzer::analyze(&typed_ast, &HashMap::new())); // Minimal analysis for macro eval
|
let exec_ast = Lowering::lower(Analyzer::analyze(&typed_ast, &HashMap::new())); // Minimal analysis for macro eval
|
||||||
|
|
||||||
let mut vm = VM::new(self.global_values.clone());
|
let mut vm = VM::new(self.global_values.clone());
|
||||||
vm.run(&exec_ast)
|
vm.run(&exec_ast)
|
||||||
@@ -586,8 +586,8 @@ impl Environment {
|
|||||||
.with_registry(self.typed_function_registry.clone());
|
.with_registry(self.typed_function_registry.clone());
|
||||||
let optimized = optimizer.optimize(specialized);
|
let optimized = optimizer.optimize(specialized);
|
||||||
|
|
||||||
// 5. TCO
|
// 5. Lowering
|
||||||
TCO::optimize(optimized)
|
Lowering::lower(optimized)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn instantiate(&self, node: ExecNode) -> Rc<crate::ast::types::NativeFunction> {
|
pub fn instantiate(&self, node: ExecNode) -> Rc<crate::ast::types::NativeFunction> {
|
||||||
@@ -705,14 +705,14 @@ impl Environment {
|
|||||||
.with_purity(global_purity.clone());
|
.with_purity(global_purity.clone());
|
||||||
let optimized_ast = optimizer.optimize(specialized_ast);
|
let optimized_ast = optimizer.optimize(specialized_ast);
|
||||||
|
|
||||||
let tco_ast = TCO::optimize(optimized_ast);
|
let exec_ast = Lowering::lower(optimized_ast);
|
||||||
let mut vm = VM::new(global_values.clone());
|
let mut vm = VM::new(global_values.clone());
|
||||||
let compiled_val = match vm.run(&tco_ast) {
|
let compiled_val = match vm.run(&exec_ast) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => return Err(format!("VM Error during specialization: {}", e)),
|
Err(e) => return Err(format!("VM Error during specialization: {}", e)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let ret_type = tco_ast.ty.ty.clone();
|
let ret_type = exec_ast.ty.ty.clone();
|
||||||
Ok((compiled_val, ret_type))
|
Ok((compiled_val, ret_type))
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -774,4 +774,3 @@ impl Environment {
|
|||||||
Ok((final_result, observer.logs))
|
Ok((final_result, observer.logs))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind};
|
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind};
|
||||||
use crate::ast::compiler::tco::ExecNode;
|
use crate::ast::compiler::lowering::ExecNode;
|
||||||
use crate::ast::nodes::Node;
|
use crate::ast::nodes::Node;
|
||||||
use crate::ast::types::{Object, Value};
|
use crate::ast::types::{Object, Value};
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
|||||||
Reference in New Issue
Block a user