Refactor: Replace UntypedNode with SyntaxNode

This commit replaces the `UntypedNode` enum with the more accurately
named `SyntaxNode`. This change is primarily for clarity and better
reflects the role of these nodes as representing the structure of the
source code prior to semantic analysis.

The corresponding enum `UntypedKind` has also been renamed to
`SyntaxKind` to maintain consistency.

No functional changes are introduced by this refactoring; it is purely a
renaming and organizational update.
This commit is contained in:
Michael Schimmel
2026-03-13 14:21:28 +01:00
parent 7d72a99fa1
commit 84226f6a16
31 changed files with 8611 additions and 8611 deletions
+30 -30
View File
@@ -1,7 +1,7 @@
use crate::ast::compiler::analyzer::Analyzer;
use crate::ast::compiler::binder::Binder;
use crate::ast::compiler::{TypeChecker, TypedNode};
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode};
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::parser::Parser;
use crate::ast::vm::{TracingObserver, VM};
use std::cell::RefCell;
@@ -122,10 +122,10 @@ struct RuntimeMacroEvaluator {
impl MacroEvaluator for RuntimeMacroEvaluator {
fn evaluate(
&self,
node: &UntypedNode,
bindings: &HashMap<Rc<str>, UntypedNode>,
node: &SyntaxNode,
bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String> {
if let UntypedKind::Identifier(sym) = &node.kind
if let SyntaxKind::Identifier(sym) = &node.kind
&& let Some(arg_node) = bindings.get(&sym.name)
{
return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>));
@@ -290,7 +290,7 @@ impl Environment {
&self,
source: &str,
base_path: &Path,
all_files: &mut Vec<(PathBuf, UntypedNode)>,
all_files: &mut Vec<(PathBuf, SyntaxNode)>,
) -> Result<(), String> {
let directives = self.extract_use_directives(source);
@@ -312,7 +312,7 @@ impl Environment {
self.collect_dependencies(&lib_source, lib_base, all_files)?;
let mut parser = Parser::new(&lib_source);
let untyped_ast = parser.parse_expression();
let syntax_ast = parser.parse_expression();
if parser.diagnostics.has_errors() {
return Err(format!(
@@ -322,7 +322,7 @@ impl Environment {
));
}
all_files.push((abs_path, untyped_ast));
all_files.push((abs_path, syntax_ast));
}
}
Ok(())
@@ -358,34 +358,34 @@ impl Environment {
/// It returns the base path which can be used to resolve further things if necessary.
pub fn preload_dependencies(&self, source: &str, file_path: Option<&Path>) -> Result<(), String> {
let base_path = file_path.and_then(|p| p.parent()).unwrap_or_else(|| Path::new("."));
let mut files: Vec<(PathBuf, UntypedNode)> = Vec::new();
let mut files: Vec<(PathBuf, SyntaxNode)> = Vec::new();
// 1. Always load the embedded system library first as a virtual module
let system_path = PathBuf::from(SYSTEM_LIB_PATH);
if !self.loaded_modules.borrow().contains(&system_path) {
self.loaded_modules.borrow_mut().insert(system_path.clone());
let mut parser = Parser::new(SYSTEM_LIB_SOURCE);
let untyped_ast = parser.parse_expression();
let syntax_ast = parser.parse_expression();
if parser.diagnostics.has_errors() {
return Err(format!(
"Failed to parse embedded system library:\n{}",
parser.diagnostics.items[0].message
));
}
files.push((system_path, untyped_ast));
files.push((system_path, syntax_ast));
}
// 2. Collect dependencies from the main source
self.collect_dependencies(source, base_path, &mut files)?;
// Pass 1: Discovery (Globals and Macros)
for (_, untyped_ast) in &files {
self.discover_globals(untyped_ast);
for (_, syntax_ast) in &files {
self.discover_globals(syntax_ast);
}
// Pass 2: Compilation and Initialization
for (path, untyped_ast) in files {
let typed_ast = self.compile_untyped(untyped_ast).map_err(|e: String| {
for (path, syntax_ast) in files {
let typed_ast = self.compile_syntax(syntax_ast).map_err(|e: String| {
format!("Compilation error in {}:\n{}", path.display(), e)
})?;
self.run_script_compiled(typed_ast).map_err(|e: String| {
@@ -396,10 +396,10 @@ impl Environment {
Ok(())
}
fn discover_globals(&self, node: &UntypedNode) {
fn discover_globals(&self, node: &SyntaxNode) {
match &node.kind {
UntypedKind::Def { target, .. } => {
if let UntypedKind::Identifier(sym) = &target.kind {
SyntaxKind::Def { target, .. } => {
if let SyntaxKind::Identifier(sym) = &target.kind {
let mut root_scopes = self.root_scopes.borrow_mut();
let last_idx = root_scopes.len() - 1;
let current_scope = &mut root_scopes[last_idx];
@@ -420,13 +420,13 @@ impl Environment {
}
}
}
UntypedKind::MacroDecl { name, params, body } => {
SyntaxKind::MacroDecl { name, params, body } => {
let mut registry = self.macro_registry.borrow_mut();
fn extract_names(node: &UntypedNode) -> Vec<Rc<str>> {
fn extract_names(node: &SyntaxNode) -> Vec<Rc<str>> {
match &node.kind {
UntypedKind::Identifier(sym) => vec![sym.name.clone()],
UntypedKind::Tuple { elements } => {
SyntaxKind::Identifier(sym) => vec![sym.name.clone()],
SyntaxKind::Tuple { elements } => {
elements.iter().flat_map(extract_names).collect()
}
_ => vec![],
@@ -436,7 +436,7 @@ impl Environment {
let p_names = extract_names(params);
registry.define(name.name.clone(), p_names, body.as_ref().clone());
}
UntypedKind::Block { exprs } => {
SyntaxKind::Block { exprs } => {
for expr in exprs {
self.discover_globals(expr);
}
@@ -445,8 +445,8 @@ impl Environment {
}
}
fn compile_pipeline(&self, untyped_ast: UntypedNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> {
let expanded_ast = match self.get_expander().expand(untyped_ast) {
fn compile_pipeline(&self, syntax_ast: SyntaxNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> {
let expanded_ast = match self.get_expander().expand(syntax_ast) {
Ok(ast) => ast,
Err(e) => {
diagnostics.push_error(e, None);
@@ -505,10 +505,10 @@ impl Environment {
Some(checker.check(&wrapped_ast, &[], diagnostics))
}
fn compile_untyped(&self, untyped_ast: UntypedNode) -> Result<TypedNode, String> {
fn compile_syntax(&self, syntax_ast: SyntaxNode) -> Result<TypedNode, String> {
let mut diagnostics = Diagnostics::new();
let typed_ast_opt = self.compile_pipeline(untyped_ast, &mut diagnostics);
let typed_ast_opt = self.compile_pipeline(syntax_ast, &mut diagnostics);
if diagnostics.has_errors() || typed_ast_opt.is_none() {
return Err(diagnostics
@@ -627,7 +627,7 @@ impl Environment {
}
let mut parser = Parser::new(source);
let untyped_ast = parser.parse_expression();
let syntax_ast = parser.parse_expression();
if !parser.at_eof() {
parser
@@ -640,7 +640,7 @@ impl Environment {
}
let mut diagnostics = parser.diagnostics;
let typed_ast = self.compile_pipeline(untyped_ast, &mut diagnostics);
let typed_ast = self.compile_pipeline(syntax_ast, &mut diagnostics);
CompilationResult {
ast: typed_ast,
@@ -737,7 +737,7 @@ impl Environment {
let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
let typed_reg = self.typed_function_registry.clone();
let untyped_reg = self.function_registry.clone();
let syntax_reg = self.function_registry.clone();
let mono_cache = self.monomorph_cache.clone();
let root_values = self.root_values.clone();
let root_types = self.root_types.clone();
@@ -764,7 +764,7 @@ impl Environment {
let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow());
let sub_registry = Rc::new(EnvFunctionRegistry {
registry: untyped_reg.clone(),
registry: syntax_reg.clone(),
analyzed_registry: typed_reg.clone(),
});
let sub_rtl_lookup =