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:
+42
-42
@@ -2,7 +2,7 @@ use crate::ast::compiler::bound_nodes::{
|
||||
Address, BoundKind, DeclarationKind, GlobalIdx, LocalSlot, Node, UpvalueIdx,
|
||||
};
|
||||
use crate::ast::diagnostics::Diagnostics;
|
||||
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode};
|
||||
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
|
||||
use crate::ast::types::{Identity, StaticType, Purity};
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
@@ -152,7 +152,7 @@ impl Binder {
|
||||
initial_scopes: Vec<CompilerScope>,
|
||||
initial_slot_count: u32,
|
||||
fixed_scope_idx: i32,
|
||||
node: &UntypedNode,
|
||||
node: &SyntaxNode,
|
||||
diagnostics: &mut Diagnostics,
|
||||
) -> Result<BindingResult, String> {
|
||||
let mut binder = Self::new(initial_scopes, initial_slot_count, fixed_scope_idx);
|
||||
@@ -208,17 +208,17 @@ impl Binder {
|
||||
|
||||
pub fn bind(
|
||||
&mut self,
|
||||
node: &UntypedNode,
|
||||
node: &SyntaxNode,
|
||||
ctx: ExprContext,
|
||||
diag: &mut Diagnostics,
|
||||
) -> Node {
|
||||
match &node.kind {
|
||||
UntypedKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop),
|
||||
UntypedKind::Constant(v) => {
|
||||
SyntaxKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop),
|
||||
SyntaxKind::Constant(v) => {
|
||||
self.make_node(node.identity.clone(), BoundKind::Constant(v.clone()))
|
||||
}
|
||||
|
||||
UntypedKind::Identifier(sym) => {
|
||||
SyntaxKind::Identifier(sym) => {
|
||||
if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
|
||||
self.make_node(
|
||||
node.identity.clone(),
|
||||
@@ -232,11 +232,11 @@ impl Binder {
|
||||
}
|
||||
}
|
||||
|
||||
UntypedKind::FieldAccessor(k) => {
|
||||
SyntaxKind::FieldAccessor(k) => {
|
||||
self.make_node(node.identity.clone(), BoundKind::FieldAccessor(*k))
|
||||
}
|
||||
|
||||
UntypedKind::If {
|
||||
SyntaxKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
@@ -264,14 +264,14 @@ impl Binder {
|
||||
)
|
||||
}
|
||||
|
||||
UntypedKind::Def { target, value } => {
|
||||
SyntaxKind::Def { target, value } => {
|
||||
if ctx == ExprContext::Expression {
|
||||
diag.push_error(
|
||||
"Statement 'def' cannot be used as an expression.",
|
||||
Some(node.identity.clone()),
|
||||
);
|
||||
}
|
||||
if let UntypedKind::Identifier(ref name) = target.kind {
|
||||
if let SyntaxKind::Identifier(ref name) = target.kind {
|
||||
let addr_opt = self.declare_variable(
|
||||
name,
|
||||
node.identity.clone(),
|
||||
@@ -308,10 +308,10 @@ impl Binder {
|
||||
}
|
||||
}
|
||||
|
||||
UntypedKind::Assign { target, value } => {
|
||||
SyntaxKind::Assign { target, value } => {
|
||||
let val_node = self.bind(value, ExprContext::Expression, diag);
|
||||
|
||||
if let UntypedKind::Identifier(sym) = &target.kind {
|
||||
if let SyntaxKind::Identifier(sym) = &target.kind {
|
||||
if let Some(addr) = self.resolve_variable(sym, diag, &target.identity) {
|
||||
self.make_node(
|
||||
node.identity.clone(),
|
||||
@@ -335,7 +335,7 @@ impl Binder {
|
||||
}
|
||||
}
|
||||
|
||||
UntypedKind::Pipe { inputs, lambda } => {
|
||||
SyntaxKind::Pipe { inputs, lambda } => {
|
||||
let mut bound_inputs = Vec::with_capacity(inputs.len());
|
||||
for input in inputs {
|
||||
bound_inputs.push(Rc::new(self.bind(input, ExprContext::Expression, diag)));
|
||||
@@ -352,7 +352,7 @@ impl Binder {
|
||||
)
|
||||
}
|
||||
|
||||
UntypedKind::Lambda { params, body } => {
|
||||
SyntaxKind::Lambda { params, body } => {
|
||||
let identity = node.identity.clone();
|
||||
self.functions
|
||||
.push(FunctionCompiler::new(identity.clone(), vec![], 0));
|
||||
@@ -392,7 +392,7 @@ impl Binder {
|
||||
)
|
||||
}
|
||||
|
||||
UntypedKind::Call { callee, args } => {
|
||||
SyntaxKind::Call { callee, args } => {
|
||||
let callee = self.bind(callee, ExprContext::Expression, diag);
|
||||
let args = self.bind(args.as_ref(), ExprContext::Expression, diag);
|
||||
|
||||
@@ -405,7 +405,7 @@ impl Binder {
|
||||
)
|
||||
}
|
||||
|
||||
UntypedKind::Again { args } => {
|
||||
SyntaxKind::Again { args } => {
|
||||
if self.functions.len() <= 1 {
|
||||
diag.push_error(
|
||||
"'again' is only allowed inside a function or lambda.",
|
||||
@@ -422,7 +422,7 @@ impl Binder {
|
||||
)
|
||||
}
|
||||
|
||||
UntypedKind::Block { exprs } => {
|
||||
SyntaxKind::Block { exprs } => {
|
||||
self.functions.last_mut().unwrap().push_scope();
|
||||
let mut bound_exprs = Vec::new();
|
||||
for (i, expr) in exprs.iter().enumerate() {
|
||||
@@ -440,7 +440,7 @@ impl Binder {
|
||||
)
|
||||
}
|
||||
|
||||
UntypedKind::Tuple { elements } => {
|
||||
SyntaxKind::Tuple { elements } => {
|
||||
let mut bound_elems = Vec::new();
|
||||
for e in elements {
|
||||
bound_elems.push(Rc::new(self.bind(e, ExprContext::Expression, diag)));
|
||||
@@ -453,7 +453,7 @@ impl Binder {
|
||||
)
|
||||
}
|
||||
|
||||
UntypedKind::Record { fields } => {
|
||||
SyntaxKind::Record { fields } => {
|
||||
let mut bound_values = Vec::new();
|
||||
let mut layout_fields = Vec::new();
|
||||
|
||||
@@ -488,7 +488,7 @@ impl Binder {
|
||||
)
|
||||
}
|
||||
|
||||
UntypedKind::Expansion { call, expanded } => {
|
||||
SyntaxKind::Expansion { call, expanded } => {
|
||||
let bound_expanded = self.bind(expanded.as_ref(), ctx, diag);
|
||||
self.make_node(
|
||||
node.identity.clone(),
|
||||
@@ -499,10 +499,10 @@ impl Binder {
|
||||
)
|
||||
}
|
||||
|
||||
UntypedKind::Template(_)
|
||||
| UntypedKind::Placeholder(_)
|
||||
| UntypedKind::Splice(_)
|
||||
| UntypedKind::MacroDecl { .. } => {
|
||||
SyntaxKind::Template(_)
|
||||
| SyntaxKind::Placeholder(_)
|
||||
| SyntaxKind::Splice(_)
|
||||
| SyntaxKind::MacroDecl { .. } => {
|
||||
diag.push_error(
|
||||
format!(
|
||||
"Macro construct {:?} found in Binder. Macros must be expanded before binding.",
|
||||
@@ -513,14 +513,14 @@ impl Binder {
|
||||
self.make_node(node.identity.clone(), BoundKind::Error)
|
||||
}
|
||||
|
||||
UntypedKind::Extension(_) => {
|
||||
SyntaxKind::Extension(_) => {
|
||||
diag.push_error(
|
||||
"Custom extensions not supported in Binder yet",
|
||||
Some(node.identity.clone()),
|
||||
);
|
||||
self.make_node(node.identity.clone(), BoundKind::Error)
|
||||
}
|
||||
UntypedKind::Error => crate::ast::compiler::bound_nodes::Node {
|
||||
SyntaxKind::Error => crate::ast::compiler::bound_nodes::Node {
|
||||
identity: node.identity.clone(),
|
||||
kind: crate::ast::compiler::bound_nodes::BoundKind::Error,
|
||||
ty: (),
|
||||
@@ -600,12 +600,12 @@ impl Binder {
|
||||
|
||||
fn bind_pattern(
|
||||
&mut self,
|
||||
node: &UntypedNode,
|
||||
node: &SyntaxNode,
|
||||
kind: DeclarationKind,
|
||||
diag: &mut Diagnostics,
|
||||
) -> Node {
|
||||
match &node.kind {
|
||||
UntypedKind::Identifier(sym) => {
|
||||
SyntaxKind::Identifier(sym) => {
|
||||
if let Some(addr) = self.declare_variable(sym, node.identity.clone(), kind, diag) {
|
||||
self.make_node(
|
||||
node.identity.clone(),
|
||||
@@ -621,7 +621,7 @@ impl Binder {
|
||||
self.make_node(node.identity.clone(), BoundKind::Error)
|
||||
}
|
||||
}
|
||||
UntypedKind::Tuple { elements } => {
|
||||
SyntaxKind::Tuple { elements } => {
|
||||
let mut bound_elems = Vec::new();
|
||||
for e in elements {
|
||||
bound_elems.push(Rc::new(self.bind_pattern(e, kind, diag)));
|
||||
@@ -645,11 +645,11 @@ impl Binder {
|
||||
|
||||
fn bind_assign_pattern(
|
||||
&mut self,
|
||||
node: &UntypedNode,
|
||||
node: &SyntaxNode,
|
||||
diag: &mut Diagnostics,
|
||||
) -> Node {
|
||||
match &node.kind {
|
||||
UntypedKind::Identifier(sym) => {
|
||||
SyntaxKind::Identifier(sym) => {
|
||||
if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
|
||||
self.make_node(
|
||||
node.identity.clone(),
|
||||
@@ -662,7 +662,7 @@ impl Binder {
|
||||
self.make_node(node.identity.clone(), BoundKind::Error)
|
||||
}
|
||||
}
|
||||
UntypedKind::Tuple { elements } => {
|
||||
SyntaxKind::Tuple { elements } => {
|
||||
let mut bound_elems = Vec::new();
|
||||
for e in elements {
|
||||
bound_elems.push(Rc::new(self.bind_assign_pattern(e, diag)));
|
||||
@@ -703,10 +703,10 @@ mod tests {
|
||||
fn test_upvalue_capture_sets_is_boxed() {
|
||||
let source = "(fn [] (do (def x 10) (def f (fn [] x)) x))";
|
||||
let mut parser = Parser::new(source);
|
||||
let untyped = parser.parse_expression();
|
||||
let syntax = parser.parse_expression();
|
||||
|
||||
let mut diagnostics = Diagnostics::new();
|
||||
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics).unwrap();
|
||||
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &syntax, &mut diagnostics).unwrap();
|
||||
|
||||
if let BoundKind::Lambda { body, .. } = &bound.kind {
|
||||
if let BoundKind::Block { exprs } = &body.kind {
|
||||
@@ -732,10 +732,10 @@ mod tests {
|
||||
fn test_no_capture_not_boxed() {
|
||||
let source = "(fn [] (do (def x 10) x))";
|
||||
let mut parser = Parser::new(source);
|
||||
let untyped = parser.parse_expression();
|
||||
let syntax = parser.parse_expression();
|
||||
|
||||
let mut diagnostics = Diagnostics::new();
|
||||
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics).unwrap();
|
||||
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &syntax, &mut diagnostics).unwrap();
|
||||
|
||||
if let BoundKind::Lambda { body, .. } = &bound.kind {
|
||||
if let BoundKind::Block { exprs } = &body.kind {
|
||||
@@ -761,10 +761,10 @@ mod tests {
|
||||
fn test_redefinition_error() {
|
||||
let source = "(do (def x 1) (def x 2) x)";
|
||||
let mut parser = Parser::new(source);
|
||||
let untyped = parser.parse_expression();
|
||||
let syntax = parser.parse_expression();
|
||||
|
||||
let mut diagnostics = Diagnostics::new();
|
||||
let _ = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics);
|
||||
let _ = Binder::bind_root(vec![], 0, 0, &syntax, &mut diagnostics);
|
||||
|
||||
assert!(diagnostics.has_errors());
|
||||
assert!(diagnostics.items.iter().any(|i| i.message.contains("already defined")));
|
||||
@@ -773,15 +773,15 @@ mod tests {
|
||||
#[test]
|
||||
fn test_repro_global_redefinition() {
|
||||
let source1 = "(def x 1) 1";
|
||||
let untyped1 = Parser::new(source1).parse_expression();
|
||||
let syntax1 = Parser::new(source1).parse_expression();
|
||||
let mut diagnostics = Diagnostics::new();
|
||||
let (_, _, scopes1, slots1) = Binder::bind_root(vec![], 0, -1, &untyped1, &mut diagnostics).unwrap();
|
||||
let (_, _, scopes1, slots1) = Binder::bind_root(vec![], 0, -1, &syntax1, &mut diagnostics).unwrap();
|
||||
|
||||
let source2 = "(def x 2) 2";
|
||||
let untyped2 = Parser::new(source2).parse_expression();
|
||||
let syntax2 = Parser::new(source2).parse_expression();
|
||||
let mut diagnostics2 = Diagnostics::new();
|
||||
// Here we simulate frozen scope by passing fixed_scope_idx = 0
|
||||
let _ = Binder::bind_root(scopes1, slots1, 0, &untyped2, &mut diagnostics2);
|
||||
let _ = Binder::bind_root(scopes1, slots1, 0, &syntax2, &mut diagnostics2);
|
||||
|
||||
assert!(diagnostics2.has_errors());
|
||||
assert!(diagnostics2.items.iter().any(|i| i.message.contains("frozen/immutable")));
|
||||
|
||||
Reference in New Issue
Block a user