Update various types to use their own definitions

This commit refactors several modules to use the defined types from
`ast::types` and `ast::nodes` directly, rather than using fully
qualified paths. This improves code readability and reduces redundancy.

Specifically, the following changes were made:

- In `analyzer.rs`, `crate::ast::types::Identity` and
  `crate::ast::types::Purity` are now used directly.
- In `binder.rs`, `crate::ast::types::NodeIdentity`,
  `crate::ast::types::SourceLocation`,
  `crate::ast::types::RecordLayout`, and `crate::ast::types::Value` are
  now used directly.
- In `macros.rs`, types like `Address`, `VirtualId`, `NodeIdentity`,
  `SourceLocation`, `StaticType`, and `Purity` are now used directly.
- In `optimizer/engine.rs`, `Address<VirtualId>` is now used directly.
- In `type_checker.rs`, `BoundPhase`, `Signature`, `Keyword`, and
  `RecordLayout` are now used directly.
- In `environment.rs`, `CompilerScope`, `LocalInfo`, `CapturePass`,
  `AnalyzedPhase`, `NativeFunction`, and `Closure` are now used
  directly.
- In `nodes.rs`, `RecordLayout`, `Keyword`, `Purity`, and `StaticType`
  are now used directly.
- In `parser.rs`, `SourceLocation` and `NodeIdentity` are now used
  directly.
- In `rtl/math.rs`, `NativeFunction`, `Purity`, `Signature`,
  `StaticType`, `Value`, `RefCell`, and `Rc` are now used directly.
- In `rtl/streams.rs`, `ScalarValue`, `SeriesMember`, `Object`,
  `PipeFn`, `Value`, `VM`, `RingBuffer`, `RecordSeries`, `SeriesView`,
  `build_map_stream`, and `build_pipeline_node` are now used directly.
- In `rtl/type_registry.rs`, `RecordLayout`, `Keyword`, `Purity`,
  `Signature`, `StaticType`, and `Value` are now used directly.
- In `vm.rs`, `RecordSeries`, `SeriesView`, `build_map_stream`,
  `build_pipeline_node`, `StreamNode`, `Object`, and `PipeFn` are now
  used directly.
- In `utils/tester.rs`, `TypedNode` is now used directly.
This commit is contained in:
2026-03-22 18:30:56 +01:00
parent 1cbc656554
commit 007446a167
13 changed files with 112 additions and 104 deletions
+27 -27
View File
@@ -1,9 +1,9 @@
use crate::ast::compiler::analyzer::Analyzer;
use crate::ast::compiler::binder::Binder;
use crate::ast::compiler::{TypeChecker, TypedNode};
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::compiler::binder::{Binder, CompilerScope, LocalInfo};
use crate::ast::compiler::{CapturePass, TypeChecker, TypedNode};
use crate::ast::nodes::{AnalyzedPhase, Symbol, SyntaxKind, SyntaxNode};
use crate::ast::parser::Parser;
use crate::ast::vm::{TracingObserver, VM};
use crate::ast::vm::{Closure, TracingObserver, VM};
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
@@ -19,9 +19,9 @@ use crate::ast::compiler::lowering::Lowering;
use crate::ast::compiler::macros::{MacroEvaluator, MacroExpander, MacroRegistry};
use crate::ast::compiler::optimizer::Optimizer;
use crate::ast::compiler::specializer::{FunctionRegistry, MonoCache, Specializer};
use crate::ast::rtl::intrinsics;
use crate::ast::rtl::{self, intrinsics};
use crate::ast::types::{
NodeIdentity, Object, Purity, SourceLocation, StaticType, Value,
NativeFunction, NodeIdentity, Object, Purity, SourceLocation, StaticType, Value,
};
use crate::ast::diagnostics::{DiagnosticLevel, Diagnostics};
@@ -75,7 +75,7 @@ pub struct Environment {
pub root_purity: Rc<RefCell<Vec<Purity>>>,
pub root_values: Rc<RefCell<Vec<Value>>>,
pub fixed_scope_idx: i32,
pub root_scopes: Rc<RefCell<Vec<crate::ast::compiler::binder::CompilerScope>>>,
pub root_scopes: Rc<RefCell<Vec<CompilerScope>>>,
pub root_slot_count: Rc<RefCell<u32>>,
pub function_registry: Rc<RefCell<GlobalFunctionRegistry>>,
pub typed_function_registry: Rc<RefCell<GlobalAnalyzedRegistry>>,
@@ -93,7 +93,7 @@ struct EnvFunctionRegistry {
}
impl FunctionRegistry for EnvFunctionRegistry {
fn resolve(&self, addr: Address<VirtualId>) -> Option<Rc<Node<crate::ast::nodes::AnalyzedPhase>>> {
fn resolve(&self, addr: Address<VirtualId>) -> Option<Rc<Node<AnalyzedPhase>>> {
if let Address::Global(idx) = addr {
self.analyzed_registry.borrow().get(&idx).cloned()
} else {
@@ -103,7 +103,7 @@ impl FunctionRegistry for EnvFunctionRegistry {
}
struct RuntimeMacroEvaluator {
root_scopes: Rc<RefCell<Vec<crate::ast::compiler::binder::CompilerScope>>>,
root_scopes: Rc<RefCell<Vec<CompilerScope>>>,
root_slot_count: Rc<RefCell<u32>>,
root_types: Rc<RefCell<Vec<StaticType>>>,
root_values: Rc<RefCell<Vec<Value>>>,
@@ -128,7 +128,7 @@ impl MacroEvaluator for RuntimeMacroEvaluator {
let initial_slot_count = *self.root_slot_count.borrow();
let (bound_ast, captures, _, _) = Binder::bind_root(initial_scopes, initial_slot_count, self.fixed_scope_idx, node, &mut diag)?;
let bound_ast = crate::ast::compiler::captures::CapturePass::apply(bound_ast, &captures);
let bound_ast = CapturePass::apply(bound_ast, &captures);
let checker = TypeChecker::new(self.root_types.clone());
let typed_ast = checker.check(&bound_ast, &[], &mut diag);
@@ -161,7 +161,7 @@ impl Environment {
root_purity: Rc::new(RefCell::new(Vec::new())),
root_values: Rc::new(RefCell::new(Vec::new())),
fixed_scope_idx: -1,
root_scopes: Rc::new(RefCell::new(vec![crate::ast::compiler::binder::CompilerScope::new()])),
root_scopes: Rc::new(RefCell::new(vec![CompilerScope::new()])),
root_slot_count: Rc::new(RefCell::new(0)),
function_registry: Rc::new(RefCell::new(HashMap::new())),
typed_function_registry: Rc::new(RefCell::new(HashMap::new())),
@@ -173,12 +173,12 @@ impl Environment {
search_paths: Rc::new(RefCell::new(Vec::new())),
loaded_modules: Rc::new(RefCell::new(HashSet::new())),
};
crate::ast::rtl::register(&env);
rtl::register(&env);
let mut env = env;
env.fixed_scope_idx = 0;
// Push the first mutable user scope (Level 1)
env.root_scopes.borrow_mut().push(crate::ast::compiler::binder::CompilerScope::new());
env.root_scopes.borrow_mut().push(CompilerScope::new());
// Automatically add standard search paths (CWD and CWD/rtl)
if let Ok(cwd) = std::env::current_dir() {
@@ -401,7 +401,7 @@ impl Environment {
let slot = VirtualId(*slot_count);
current_scope.locals.insert(
sym.clone(),
crate::ast::compiler::binder::LocalInfo {
LocalInfo {
addr: Address::Local(slot),
identity: node.identity.clone(),
_ty: StaticType::Any,
@@ -462,7 +462,7 @@ impl Environment {
*self.root_scopes.borrow_mut() = final_scopes;
*self.root_slot_count.borrow_mut() = final_slot_count;
let bound_ast = crate::ast::compiler::captures::CapturePass::apply(bound_ast, &captures);
let bound_ast = CapturePass::apply(bound_ast, &captures);
// Pre-allocate global slots to prevent out-of-bounds during specialization/optimization
{
@@ -520,7 +520,7 @@ impl Environment {
&self,
name: &str,
ty: StaticType,
func: Rc<crate::ast::types::NativeFunction>,
func: Rc<NativeFunction>,
) {
let mut types = self.root_types.borrow_mut();
let mut values = self.root_values.borrow_mut();
@@ -535,7 +535,7 @@ impl Environment {
let global_idx = GlobalIdx(*slot_count);
root_scopes[0].locals.insert(
Symbol::from(name),
crate::ast::compiler::binder::LocalInfo {
LocalInfo {
addr: Address::Global(global_idx),
identity,
_ty: ty.clone(),
@@ -559,7 +559,7 @@ impl Environment {
self.register_native(
name,
ty,
Rc::new(crate::ast::types::NativeFunction {
Rc::new(NativeFunction {
func: Rc::new(func),
purity: purity_level,
}),
@@ -580,7 +580,7 @@ impl Environment {
let global_idx = GlobalIdx(*slot_count);
root_scopes[0].locals.insert(
Symbol::from(name),
crate::ast::compiler::binder::LocalInfo {
LocalInfo {
addr: Address::Global(global_idx),
identity,
_ty: ty.clone(),
@@ -650,7 +650,7 @@ impl Environment {
Lowering::lower(optimized)
}
pub fn instantiate(&self, node: ExecNode) -> Rc<crate::ast::types::NativeFunction> {
pub fn instantiate(&self, node: ExecNode) -> Rc<NativeFunction> {
let root_values = self.root_values.clone();
if let NodeKind::Lambda {
params,
@@ -659,7 +659,7 @@ impl Environment {
} = &node.kind
&& info.upvalues.is_empty()
{
let closure = Rc::new(crate::ast::vm::Closure::new(
let closure = Rc::new(Closure::new(
params.clone(),
body.ty.original.clone(),
body.clone(),
@@ -667,9 +667,9 @@ impl Environment {
info.positional_count,
node.ty.stack_size,
));
let closure_obj: Rc<dyn crate::ast::types::Object> = closure;
let closure_obj: Rc<dyn Object> = closure;
return Rc::new(crate::ast::types::NativeFunction {
return Rc::new(NativeFunction {
purity: Purity::Impure,
func: Rc::new(move |args| {
let mut vm = VM::new(root_values.clone());
@@ -682,7 +682,7 @@ impl Environment {
}
let exec_node = Rc::new(node);
Rc::new(crate::ast::types::NativeFunction {
Rc::new(NativeFunction {
purity: Purity::Impure,
func: Rc::new(move |args| {
let mut vm = VM::new(root_values.clone());
@@ -694,7 +694,7 @@ impl Environment {
if let Value::Object(obj) = &res
&& obj
.as_any()
.downcast_ref::<crate::ast::vm::Closure>()
.downcast_ref::<Closure>()
.is_some()
{
match vm.run_with_args(obj.clone(), args) {
@@ -722,7 +722,7 @@ impl Environment {
let optimization = self.optimization;
let compiler = Rc::new(
move |func_template: Rc<Node<crate::ast::nodes::AnalyzedPhase>>,
move |func_template: Rc<Node<AnalyzedPhase>>,
arg_types: &[StaticType]|
-> Result<(Value, StaticType), String> {
let mut diag = Diagnostics::new();
@@ -837,7 +837,7 @@ impl Environment {
// All Myc scripts are wrapped in a parameterless lambda for consistency.
let mut final_result = result;
if let Ok(Value::Object(obj)) = &final_result
&& let Some(_closure) = obj.as_any().downcast_ref::<crate::ast::vm::Closure>()
&& let Some(_closure) = obj.as_any().downcast_ref::<Closure>()
{
final_result = vm.run_with_args_observed(&mut observer, obj.clone(), &[]);
}