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
+42 -42
View File
@@ -2,7 +2,7 @@ use crate::ast::compiler::bound_nodes::{
Address, BoundKind, DeclarationKind, GlobalIdx, LocalSlot, Node, UpvalueIdx, Address, BoundKind, DeclarationKind, GlobalIdx, LocalSlot, Node, UpvalueIdx,
}; };
use crate::ast::diagnostics::Diagnostics; 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 crate::ast::types::{Identity, StaticType, Purity};
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
@@ -152,7 +152,7 @@ impl Binder {
initial_scopes: Vec<CompilerScope>, initial_scopes: Vec<CompilerScope>,
initial_slot_count: u32, initial_slot_count: u32,
fixed_scope_idx: i32, fixed_scope_idx: i32,
node: &UntypedNode, node: &SyntaxNode,
diagnostics: &mut Diagnostics, diagnostics: &mut Diagnostics,
) -> Result<BindingResult, String> { ) -> Result<BindingResult, String> {
let mut binder = Self::new(initial_scopes, initial_slot_count, fixed_scope_idx); let mut binder = Self::new(initial_scopes, initial_slot_count, fixed_scope_idx);
@@ -208,17 +208,17 @@ impl Binder {
pub fn bind( pub fn bind(
&mut self, &mut self,
node: &UntypedNode, node: &SyntaxNode,
ctx: ExprContext, ctx: ExprContext,
diag: &mut Diagnostics, diag: &mut Diagnostics,
) -> Node { ) -> Node {
match &node.kind { match &node.kind {
UntypedKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop), SyntaxKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop),
UntypedKind::Constant(v) => { SyntaxKind::Constant(v) => {
self.make_node(node.identity.clone(), BoundKind::Constant(v.clone())) 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) { if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
self.make_node( self.make_node(
node.identity.clone(), node.identity.clone(),
@@ -232,11 +232,11 @@ impl Binder {
} }
} }
UntypedKind::FieldAccessor(k) => { SyntaxKind::FieldAccessor(k) => {
self.make_node(node.identity.clone(), BoundKind::FieldAccessor(*k)) self.make_node(node.identity.clone(), BoundKind::FieldAccessor(*k))
} }
UntypedKind::If { SyntaxKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
@@ -264,14 +264,14 @@ impl Binder {
) )
} }
UntypedKind::Def { target, value } => { SyntaxKind::Def { target, value } => {
if ctx == ExprContext::Expression { if ctx == ExprContext::Expression {
diag.push_error( diag.push_error(
"Statement 'def' cannot be used as an expression.", "Statement 'def' cannot be used as an expression.",
Some(node.identity.clone()), 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( let addr_opt = self.declare_variable(
name, name,
node.identity.clone(), 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); 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) { if let Some(addr) = self.resolve_variable(sym, diag, &target.identity) {
self.make_node( self.make_node(
node.identity.clone(), 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()); let mut bound_inputs = Vec::with_capacity(inputs.len());
for input in inputs { for input in inputs {
bound_inputs.push(Rc::new(self.bind(input, ExprContext::Expression, diag))); 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(); let identity = node.identity.clone();
self.functions self.functions
.push(FunctionCompiler::new(identity.clone(), vec![], 0)); .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 callee = self.bind(callee, ExprContext::Expression, diag);
let args = self.bind(args.as_ref(), 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 { if self.functions.len() <= 1 {
diag.push_error( diag.push_error(
"'again' is only allowed inside a function or lambda.", "'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(); self.functions.last_mut().unwrap().push_scope();
let mut bound_exprs = Vec::new(); let mut bound_exprs = Vec::new();
for (i, expr) in exprs.iter().enumerate() { 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(); let mut bound_elems = Vec::new();
for e in elements { for e in elements {
bound_elems.push(Rc::new(self.bind(e, ExprContext::Expression, diag))); 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 bound_values = Vec::new();
let mut layout_fields = 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); let bound_expanded = self.bind(expanded.as_ref(), ctx, diag);
self.make_node( self.make_node(
node.identity.clone(), node.identity.clone(),
@@ -499,10 +499,10 @@ impl Binder {
) )
} }
UntypedKind::Template(_) SyntaxKind::Template(_)
| UntypedKind::Placeholder(_) | SyntaxKind::Placeholder(_)
| UntypedKind::Splice(_) | SyntaxKind::Splice(_)
| UntypedKind::MacroDecl { .. } => { | SyntaxKind::MacroDecl { .. } => {
diag.push_error( diag.push_error(
format!( format!(
"Macro construct {:?} found in Binder. Macros must be expanded before binding.", "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) self.make_node(node.identity.clone(), BoundKind::Error)
} }
UntypedKind::Extension(_) => { SyntaxKind::Extension(_) => {
diag.push_error( diag.push_error(
"Custom extensions not supported in Binder yet", "Custom extensions not supported in Binder yet",
Some(node.identity.clone()), Some(node.identity.clone()),
); );
self.make_node(node.identity.clone(), BoundKind::Error) 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(), identity: node.identity.clone(),
kind: crate::ast::compiler::bound_nodes::BoundKind::Error, kind: crate::ast::compiler::bound_nodes::BoundKind::Error,
ty: (), ty: (),
@@ -600,12 +600,12 @@ impl Binder {
fn bind_pattern( fn bind_pattern(
&mut self, &mut self,
node: &UntypedNode, node: &SyntaxNode,
kind: DeclarationKind, kind: DeclarationKind,
diag: &mut Diagnostics, diag: &mut Diagnostics,
) -> Node { ) -> Node {
match &node.kind { match &node.kind {
UntypedKind::Identifier(sym) => { SyntaxKind::Identifier(sym) => {
if let Some(addr) = self.declare_variable(sym, node.identity.clone(), kind, diag) { if let Some(addr) = self.declare_variable(sym, node.identity.clone(), kind, diag) {
self.make_node( self.make_node(
node.identity.clone(), node.identity.clone(),
@@ -621,7 +621,7 @@ impl Binder {
self.make_node(node.identity.clone(), BoundKind::Error) self.make_node(node.identity.clone(), BoundKind::Error)
} }
} }
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
let mut bound_elems = Vec::new(); let mut bound_elems = Vec::new();
for e in elements { for e in elements {
bound_elems.push(Rc::new(self.bind_pattern(e, kind, diag))); bound_elems.push(Rc::new(self.bind_pattern(e, kind, diag)));
@@ -645,11 +645,11 @@ impl Binder {
fn bind_assign_pattern( fn bind_assign_pattern(
&mut self, &mut self,
node: &UntypedNode, node: &SyntaxNode,
diag: &mut Diagnostics, diag: &mut Diagnostics,
) -> Node { ) -> Node {
match &node.kind { match &node.kind {
UntypedKind::Identifier(sym) => { SyntaxKind::Identifier(sym) => {
if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) { if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
self.make_node( self.make_node(
node.identity.clone(), node.identity.clone(),
@@ -662,7 +662,7 @@ impl Binder {
self.make_node(node.identity.clone(), BoundKind::Error) self.make_node(node.identity.clone(), BoundKind::Error)
} }
} }
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
let mut bound_elems = Vec::new(); let mut bound_elems = Vec::new();
for e in elements { for e in elements {
bound_elems.push(Rc::new(self.bind_assign_pattern(e, diag))); bound_elems.push(Rc::new(self.bind_assign_pattern(e, diag)));
@@ -703,10 +703,10 @@ mod tests {
fn test_upvalue_capture_sets_is_boxed() { fn test_upvalue_capture_sets_is_boxed() {
let source = "(fn [] (do (def x 10) (def f (fn [] x)) x))"; let source = "(fn [] (do (def x 10) (def f (fn [] x)) x))";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new(); 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::Lambda { body, .. } = &bound.kind {
if let BoundKind::Block { exprs } = &body.kind { if let BoundKind::Block { exprs } = &body.kind {
@@ -732,10 +732,10 @@ mod tests {
fn test_no_capture_not_boxed() { fn test_no_capture_not_boxed() {
let source = "(fn [] (do (def x 10) x))"; let source = "(fn [] (do (def x 10) x))";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new(); 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::Lambda { body, .. } = &bound.kind {
if let BoundKind::Block { exprs } = &body.kind { if let BoundKind::Block { exprs } = &body.kind {
@@ -761,10 +761,10 @@ mod tests {
fn test_redefinition_error() { fn test_redefinition_error() {
let source = "(do (def x 1) (def x 2) x)"; let source = "(do (def x 1) (def x 2) x)";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new(); 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.has_errors());
assert!(diagnostics.items.iter().any(|i| i.message.contains("already defined"))); assert!(diagnostics.items.iter().any(|i| i.message.contains("already defined")));
@@ -773,15 +773,15 @@ mod tests {
#[test] #[test]
fn test_repro_global_redefinition() { fn test_repro_global_redefinition() {
let source1 = "(def x 1) 1"; 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 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 source2 = "(def x 2) 2";
let untyped2 = Parser::new(source2).parse_expression(); let syntax2 = Parser::new(source2).parse_expression();
let mut diagnostics2 = Diagnostics::new(); let mut diagnostics2 = Diagnostics::new();
// Here we simulate frozen scope by passing fixed_scope_idx = 0 // 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.has_errors());
assert!(diagnostics2.items.iter().any(|i| i.message.contains("frozen/immutable"))); assert!(diagnostics2.items.iter().any(|i| i.message.contains("frozen/immutable")));
+2 -2
View File
@@ -167,8 +167,8 @@ pub enum BoundKind<T = ()> {
/// An expanded macro call, preserving the original call for debugging and UI. /// An expanded macro call, preserving the original call for debugging and UI.
Expansion { Expansion {
/// The original call from the untyped AST. /// The original call from the syntax AST.
original_call: Rc<crate::ast::nodes::UntypedNode>, original_call: Rc<crate::ast::nodes::SyntaxNode>,
/// The result of binding the expanded AST. /// The result of binding the expanded AST.
bound_expanded: Rc<Node<T>>, bound_expanded: Rc<Node<T>>,
}, },
+144 -144
View File
@@ -1,4 +1,4 @@
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode}; use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::types::{Identity, Value}; use crate::ast::types::{Identity, Value};
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
@@ -7,19 +7,19 @@ use std::rc::Rc;
pub trait MacroEvaluator { pub trait MacroEvaluator {
fn evaluate( fn evaluate(
&self, &self,
node: &UntypedNode, node: &SyntaxNode,
bindings: &HashMap<Rc<str>, UntypedNode>, bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String>; ) -> Result<Value, String>;
} }
/// Internal state for template expansion (Hygiene context + Parameter binding) /// Internal state for template expansion (Hygiene context + Parameter binding)
struct ExpansionState<'a> { struct ExpansionState<'a> {
bindings: &'a HashMap<Rc<str>, UntypedNode>, bindings: &'a HashMap<Rc<str>, SyntaxNode>,
/// The identity of the current expansion instance, used to "color" internal symbols. /// The identity of the current expansion instance, used to "color" internal symbols.
expansion_id: Identity, expansion_id: Identity,
} }
type MacroMap = HashMap<Rc<str>, (Vec<Rc<str>>, UntypedNode)>; type MacroMap = HashMap<Rc<str>, (Vec<Rc<str>>, SyntaxNode)>;
/// A registry for macro declarations. /// A registry for macro declarations.
#[derive(Clone)] #[derive(Clone)]
@@ -50,7 +50,7 @@ impl MacroRegistry {
} }
} }
pub fn define(&mut self, name: Rc<str>, params: Vec<Rc<str>>, body: UntypedNode) { pub fn define(&mut self, name: Rc<str>, params: Vec<Rc<str>>, body: SyntaxNode) {
if let Some(current_rc) = self.scopes.last_mut() { if let Some(current_rc) = self.scopes.last_mut() {
// Copy-on-Write: If the Rc is shared, clone the map before modifying. // Copy-on-Write: If the Rc is shared, clone the map before modifying.
let current = Rc::make_mut(current_rc); let current = Rc::make_mut(current_rc);
@@ -58,7 +58,7 @@ impl MacroRegistry {
} }
} }
pub fn lookup(&self, name: &str) -> Option<(Vec<Rc<str>>, UntypedNode)> { pub fn lookup(&self, name: &str) -> Option<(Vec<Rc<str>>, SyntaxNode)> {
for scope in self.scopes.iter().rev() { for scope in self.scopes.iter().rev() {
if let Some(m) = scope.get(name) { if let Some(m) = scope.get(name) {
return Some(m.clone()); return Some(m.clone());
@@ -85,30 +85,30 @@ impl<E: MacroEvaluator> MacroExpander<E> {
self.registry self.registry
} }
pub fn expand(&mut self, node: UntypedNode) -> Result<UntypedNode, String> { pub fn expand(&mut self, node: SyntaxNode) -> Result<SyntaxNode, String> {
self.expand_recursive(node) self.expand_recursive(node)
} }
fn expand_recursive(&mut self, node: UntypedNode) -> Result<UntypedNode, String> { fn expand_recursive(&mut self, node: SyntaxNode) -> Result<SyntaxNode, String> {
match node.kind { match node.kind {
UntypedKind::MacroDecl { name, params, body } => { SyntaxKind::MacroDecl { name, params, body } => {
let p_names = self.extract_param_names(&params)?; let p_names = self.extract_param_names(&params)?;
self.registry.define(name.name, p_names, *body); self.registry.define(name.name, p_names, *body);
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Nop, kind: SyntaxKind::Nop,
}) })
} }
UntypedKind::Call { callee, args } => { SyntaxKind::Call { callee, args } => {
if let UntypedKind::Identifier(ref sym) = callee.kind if let SyntaxKind::Identifier(ref sym) = callee.kind
&& let Some((params, body)) = self.registry.lookup(&sym.name) && let Some((params, body)) = self.registry.lookup(&sym.name)
{ {
let expanded_args = self.expand_recursive(*args)?; let expanded_args = self.expand_recursive(*args)?;
// Extract argument nodes from the expanded tuple // Extract argument nodes from the expanded tuple
let arg_elements = if let UntypedKind::Tuple { elements } = expanded_args.kind { let arg_elements = if let SyntaxKind::Tuple { elements } = expanded_args.kind {
elements elements
} else { } else {
vec![expanded_args] vec![expanded_args]
@@ -127,9 +127,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let expanded_callee = self.expand_recursive(*callee)?; let expanded_callee = self.expand_recursive(*callee)?;
let expanded_args = self.expand_recursive(*args)?; let expanded_args = self.expand_recursive(*args)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Call { kind: SyntaxKind::Call {
callee: Box::new(expanded_callee), callee: Box::new(expanded_callee),
args: Box::new(expanded_args), args: Box::new(expanded_args),
}, },
@@ -137,7 +137,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Block { exprs } => { SyntaxKind::Block { exprs } => {
self.registry.push(); self.registry.push();
let mut expanded_exprs = Vec::new(); let mut expanded_exprs = Vec::new();
for expr in exprs { for expr in exprs {
@@ -145,24 +145,24 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} }
self.registry.pop(); self.registry.pop();
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Block { kind: SyntaxKind::Block {
exprs: expanded_exprs, exprs: expanded_exprs,
}, },
}) })
} }
UntypedKind::Pipe { inputs, lambda } => { SyntaxKind::Pipe { inputs, lambda } => {
let mut expanded_inputs = Vec::with_capacity(inputs.len()); let mut expanded_inputs = Vec::with_capacity(inputs.len());
for input in inputs { for input in inputs {
expanded_inputs.push(self.expand_recursive(input)?); expanded_inputs.push(self.expand_recursive(input)?);
} }
let expanded_lambda = Box::new(self.expand_recursive(*lambda)?); let expanded_lambda = Box::new(self.expand_recursive(*lambda)?);
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Pipe { kind: SyntaxKind::Pipe {
inputs: expanded_inputs, inputs: expanded_inputs,
lambda: expanded_lambda, lambda: expanded_lambda,
}, },
@@ -170,15 +170,15 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Lambda { params, body } => { SyntaxKind::Lambda { params, body } => {
self.registry.push(); self.registry.push();
let expanded_params = self.expand_recursive(*params)?; let expanded_params = self.expand_recursive(*params)?;
let expanded_body = self.expand_recursive((*body).clone())?; let expanded_body = self.expand_recursive((*body).clone())?;
self.registry.pop(); self.registry.pop();
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Lambda { kind: SyntaxKind::Lambda {
params: Box::new(expanded_params), params: Box::new(expanded_params),
body: Rc::new(expanded_body), body: Rc::new(expanded_body),
}, },
@@ -186,7 +186,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::If { SyntaxKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
@@ -198,9 +198,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} else { } else {
None None
}; };
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::If { kind: SyntaxKind::If {
cond: Box::new(cond), cond: Box::new(cond),
then_br: Box::new(then_br), then_br: Box::new(then_br),
else_br, else_br,
@@ -209,12 +209,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Def { target, value } => { SyntaxKind::Def { target, value } => {
let target = self.expand_recursive(*target)?; let target = self.expand_recursive(*target)?;
let value = self.expand_recursive(*value)?; let value = self.expand_recursive(*value)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Def { kind: SyntaxKind::Def {
target: Box::new(target), target: Box::new(target),
value: Box::new(value), value: Box::new(value),
}, },
@@ -222,12 +222,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Assign { target, value } => { SyntaxKind::Assign { target, value } => {
let target = self.expand_recursive(*target)?; let target = self.expand_recursive(*target)?;
let value = self.expand_recursive(*value)?; let value = self.expand_recursive(*value)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Assign { kind: SyntaxKind::Assign {
target: Box::new(target), target: Box::new(target),
value: Box::new(value), value: Box::new(value),
}, },
@@ -235,39 +235,39 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
let mut expanded_elements = Vec::new(); let mut expanded_elements = Vec::new();
for e in elements { for e in elements {
expanded_elements.push(self.expand_recursive(e)?); expanded_elements.push(self.expand_recursive(e)?);
} }
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Tuple { kind: SyntaxKind::Tuple {
elements: expanded_elements, elements: expanded_elements,
}, },
}) })
} }
UntypedKind::Record { fields } => { SyntaxKind::Record { fields } => {
let mut expanded_fields = Vec::new(); let mut expanded_fields = Vec::new();
for (k, v) in fields { for (k, v) in fields {
expanded_fields.push((self.expand_recursive(k)?, self.expand_recursive(v)?)); expanded_fields.push((self.expand_recursive(k)?, self.expand_recursive(v)?));
} }
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Record { kind: SyntaxKind::Record {
fields: expanded_fields, fields: expanded_fields,
}, },
}) })
} }
UntypedKind::Expansion { call, expanded } => { SyntaxKind::Expansion { call, expanded } => {
let expanded = self.expand_recursive(*expanded)?; let expanded = self.expand_recursive(*expanded)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Expansion { kind: SyntaxKind::Expansion {
call, call,
expanded: Box::new(expanded), expanded: Box::new(expanded),
}, },
@@ -275,11 +275,11 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Again { args } => { SyntaxKind::Again { args } => {
let expanded_args = self.expand_recursive(*args)?; let expanded_args = self.expand_recursive(*args)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Again { kind: SyntaxKind::Again {
args: Box::new(expanded_args), args: Box::new(expanded_args),
}, },
@@ -295,9 +295,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
identity: Identity, identity: Identity,
name: &str, name: &str,
params: Vec<Rc<str>>, params: Vec<Rc<str>>,
args: Vec<UntypedNode>, args: Vec<SyntaxNode>,
body: UntypedNode, body: SyntaxNode,
) -> Result<UntypedNode, String> { ) -> Result<SyntaxNode, String> {
if params.len() != args.len() { if params.len() != args.len() {
return Err(format!( return Err(format!(
"Macro {} expects {} arguments, but got {}", "Macro {} expects {} arguments, but got {}",
@@ -313,7 +313,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} }
// AST-Authority: Substitution and Hygiene ONLY happen if there is a Template node. // AST-Authority: Substitution and Hygiene ONLY happen if there is a Template node.
let expanded_body = if let UntypedKind::Template(inner) = body.kind { let expanded_body = if let SyntaxKind::Template(inner) = body.kind {
let mut state = ExpansionState { let mut state = ExpansionState {
bindings: &bindings, bindings: &bindings,
expansion_id: identity.clone(), expansion_id: identity.clone(),
@@ -324,17 +324,17 @@ impl<E: MacroEvaluator> MacroExpander<E> {
body body
}; };
let original_call = UntypedNode { let original_call = SyntaxNode {
identity: identity.clone(), identity: identity.clone(),
kind: UntypedKind::Call { kind: SyntaxKind::Call {
callee: Box::new(UntypedNode { callee: Box::new(SyntaxNode {
identity: identity.clone(), identity: identity.clone(),
kind: UntypedKind::Identifier(Symbol::from(name)), kind: SyntaxKind::Identifier(Symbol::from(name)),
}), }),
args: Box::new(UntypedNode { args: Box::new(SyntaxNode {
identity: identity.clone(), identity: identity.clone(),
kind: UntypedKind::Tuple { kind: SyntaxKind::Tuple {
elements: bindings.into_values().collect(), elements: bindings.into_values().collect(),
}, },
@@ -343,9 +343,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}; };
Ok(UntypedNode { Ok(SyntaxNode {
identity, identity,
kind: UntypedKind::Expansion { kind: SyntaxKind::Expansion {
call: Box::new(original_call), call: Box::new(original_call),
expanded: Box::new(expanded_body), expanded: Box::new(expanded_body),
}, },
@@ -353,10 +353,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
fn extract_param_names(&self, node: &UntypedNode) -> Result<Vec<Rc<str>>, String> { fn extract_param_names(&self, node: &SyntaxNode) -> Result<Vec<Rc<str>>, String> {
match &node.kind { match &node.kind {
UntypedKind::Identifier(sym) => Ok(vec![sym.name.clone()]), SyntaxKind::Identifier(sym) => Ok(vec![sym.name.clone()]),
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
let mut names = Vec::new(); let mut names = Vec::new();
for el in elements { for el in elements {
names.extend(self.extract_param_names(el)?); names.extend(self.extract_param_names(el)?);
@@ -369,23 +369,23 @@ impl<E: MacroEvaluator> MacroExpander<E> {
fn expand_template( fn expand_template(
&self, &self,
node: UntypedNode, node: SyntaxNode,
state: &mut ExpansionState, state: &mut ExpansionState,
) -> Result<UntypedNode, String> { ) -> Result<SyntaxNode, String> {
match node.kind { match node.kind {
UntypedKind::Identifier(mut sym) => { SyntaxKind::Identifier(mut sym) => {
// Inside a template, all internal identifiers are colored. // Inside a template, all internal identifiers are colored.
sym.context = Some(state.expansion_id.clone()); sym.context = Some(state.expansion_id.clone());
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Identifier(sym), kind: SyntaxKind::Identifier(sym),
}) })
} }
UntypedKind::Placeholder(inner) => { SyntaxKind::Placeholder(inner) => {
// Break out of template for substitution/evaluation // Break out of template for substitution/evaluation
if let UntypedKind::Identifier(ref sym) = inner.kind if let SyntaxKind::Identifier(ref sym) = inner.kind
&& let Some(arg) = state.bindings.get(&sym.name) && let Some(arg) = state.bindings.get(&sym.name)
{ {
return Ok(arg.clone()); return Ok(arg.clone());
@@ -394,9 +394,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
Ok(self.value_to_node(val, node.identity)) Ok(self.value_to_node(val, node.identity))
} }
UntypedKind::Splice(inner) => { SyntaxKind::Splice(inner) => {
// Splicing is also a form of substitution // Splicing is also a form of substitution
if let UntypedKind::Identifier(ref sym) = inner.kind if let SyntaxKind::Identifier(ref sym) = inner.kind
&& let Some(arg) = state.bindings.get(&sym.name) && let Some(arg) = state.bindings.get(&sym.name)
{ {
return Ok(arg.clone()); return Ok(arg.clone());
@@ -405,12 +405,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
Ok(self.value_to_node(val, node.identity)) Ok(self.value_to_node(val, node.identity))
} }
UntypedKind::Def { target, value } => { SyntaxKind::Def { target, value } => {
let target = self.expand_template(*target, state)?; let target = self.expand_template(*target, state)?;
let val = self.expand_template(*value, state)?; let val = self.expand_template(*value, state)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Def { kind: SyntaxKind::Def {
target: Box::new(target), target: Box::new(target),
value: Box::new(val), value: Box::new(val),
}, },
@@ -418,12 +418,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Assign { target, value } => { SyntaxKind::Assign { target, value } => {
let target = self.expand_template(*target, state)?; let target = self.expand_template(*target, state)?;
let value = self.expand_template(*value, state)?; let value = self.expand_template(*value, state)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Assign { kind: SyntaxKind::Assign {
target: Box::new(target), target: Box::new(target),
value: Box::new(value), value: Box::new(value),
}, },
@@ -431,43 +431,43 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
let mut new_elements = Vec::new(); let mut new_elements = Vec::new();
for e in elements { for e in elements {
if let UntypedKind::Splice(ref inner) = e.kind { if let SyntaxKind::Splice(ref inner) = e.kind {
let val = self.evaluator.evaluate(inner, state.bindings)?; let val = self.evaluator.evaluate(inner, state.bindings)?;
self.handle_splice_value(val, node.identity.clone(), &mut new_elements)?; self.handle_splice_value(val, node.identity.clone(), &mut new_elements)?;
} else { } else {
new_elements.push(self.expand_template(e, state)?); new_elements.push(self.expand_template(e, state)?);
} }
} }
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Tuple { kind: SyntaxKind::Tuple {
elements: new_elements, elements: new_elements,
}, },
}) })
} }
UntypedKind::Block { exprs } => { SyntaxKind::Block { exprs } => {
let mut new_exprs = Vec::new(); let mut new_exprs = Vec::new();
for e in exprs { for e in exprs {
if let UntypedKind::Splice(ref inner) = e.kind { if let SyntaxKind::Splice(ref inner) = e.kind {
let val = self.evaluator.evaluate(inner, state.bindings)?; let val = self.evaluator.evaluate(inner, state.bindings)?;
self.handle_splice_value(val, node.identity.clone(), &mut new_exprs)?; self.handle_splice_value(val, node.identity.clone(), &mut new_exprs)?;
} else { } else {
new_exprs.push(self.expand_template(e, state)?); new_exprs.push(self.expand_template(e, state)?);
} }
} }
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Block { exprs: new_exprs }, kind: SyntaxKind::Block { exprs: new_exprs },
}) })
} }
UntypedKind::Record { fields } => { SyntaxKind::Record { fields } => {
let mut new_fields = Vec::new(); let mut new_fields = Vec::new();
for (k, v) in fields { for (k, v) in fields {
new_fields.push(( new_fields.push((
@@ -475,19 +475,19 @@ impl<E: MacroEvaluator> MacroExpander<E> {
self.expand_template(v, state)?, self.expand_template(v, state)?,
)); ));
} }
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Record { fields: new_fields }, kind: SyntaxKind::Record { fields: new_fields },
}) })
} }
UntypedKind::Call { callee, args } => { SyntaxKind::Call { callee, args } => {
let expanded_callee = self.expand_template(*callee, state)?; let expanded_callee = self.expand_template(*callee, state)?;
let expanded_args = self.expand_template(*args, state)?; let expanded_args = self.expand_template(*args, state)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Call { kind: SyntaxKind::Call {
callee: Box::new(expanded_callee), callee: Box::new(expanded_callee),
args: Box::new(expanded_args), args: Box::new(expanded_args),
}, },
@@ -495,7 +495,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::If { SyntaxKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
@@ -507,9 +507,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} else { } else {
None None
}; };
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::If { kind: SyntaxKind::If {
cond: Box::new(cond), cond: Box::new(cond),
then_br: Box::new(then_br), then_br: Box::new(then_br),
else_br, else_br,
@@ -518,15 +518,15 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Pipe { inputs, lambda } => { SyntaxKind::Pipe { inputs, lambda } => {
let mut expanded_inputs = Vec::with_capacity(inputs.len()); let mut expanded_inputs = Vec::with_capacity(inputs.len());
for input in inputs { for input in inputs {
expanded_inputs.push(self.expand_template(input, state)?); expanded_inputs.push(self.expand_template(input, state)?);
} }
let expanded_lambda = Box::new(self.expand_template(*lambda, state)?); let expanded_lambda = Box::new(self.expand_template(*lambda, state)?);
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Pipe { kind: SyntaxKind::Pipe {
inputs: expanded_inputs, inputs: expanded_inputs,
lambda: expanded_lambda, lambda: expanded_lambda,
}, },
@@ -534,12 +534,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Lambda { params, body } => { SyntaxKind::Lambda { params, body } => {
let expanded_params = self.expand_template(*params, state)?; let expanded_params = self.expand_template(*params, state)?;
let body = self.expand_template((*body).clone(), state)?; let body = self.expand_template((*body).clone(), state)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Lambda { kind: SyntaxKind::Lambda {
params: Box::new(expanded_params), params: Box::new(expanded_params),
body: Rc::new(body), body: Rc::new(body),
}, },
@@ -547,11 +547,11 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Again { args } => { SyntaxKind::Again { args } => {
let expanded_args = self.expand_template(*args, state)?; let expanded_args = self.expand_template(*args, state)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Again { kind: SyntaxKind::Again {
args: Box::new(expanded_args), args: Box::new(expanded_args),
}, },
@@ -566,19 +566,19 @@ impl<E: MacroEvaluator> MacroExpander<E> {
&self, &self,
val: Value, val: Value,
_identity: Identity, _identity: Identity,
target: &mut Vec<UntypedNode>, target: &mut Vec<SyntaxNode>,
) -> Result<(), String> { ) -> Result<(), String> {
match val { match val {
Value::Object(obj) => { Value::Object(obj) => {
if let Some(node) = obj.as_any().downcast_ref::<UntypedNode>() { if let Some(node) = obj.as_any().downcast_ref::<SyntaxNode>() {
match &node.kind { match &node.kind {
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
for e in elements { for e in elements {
target.push(e.clone()); target.push(e.clone());
} }
return Ok(()); return Ok(());
} }
UntypedKind::Block { exprs } => { SyntaxKind::Block { exprs } => {
for e in exprs { for e in exprs {
target.push(e.clone()); target.push(e.clone());
} }
@@ -599,21 +599,21 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} }
} }
fn value_to_node(&self, val: Value, identity: Identity) -> UntypedNode { fn value_to_node(&self, val: Value, identity: Identity) -> SyntaxNode {
match val { match val {
Value::Object(obj) => { Value::Object(obj) => {
if let Some(node) = obj.as_any().downcast_ref::<UntypedNode>() { if let Some(node) = obj.as_any().downcast_ref::<SyntaxNode>() {
return node.clone(); return node.clone();
} }
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Constant(Value::Object(obj)), kind: SyntaxKind::Constant(Value::Object(obj)),
} }
} }
_ => UntypedNode { _ => SyntaxNode {
identity, identity,
kind: UntypedKind::Constant(val), kind: SyntaxKind::Constant(val),
}, },
} }
@@ -632,10 +632,10 @@ mod tests {
impl MacroEvaluator for SimpleEvaluator { impl MacroEvaluator for SimpleEvaluator {
fn evaluate( fn evaluate(
&self, &self,
node: &UntypedNode, node: &SyntaxNode,
bindings: &HashMap<Rc<str>, UntypedNode>, bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String> { ) -> Result<Value, String> {
if let UntypedKind::Identifier(ref sym) = node.kind if let SyntaxKind::Identifier(ref sym) = node.kind
&& let Some(arg) = bindings.get(&sym.name) && let Some(arg) = bindings.get(&sym.name)
{ {
return Ok(Value::Object(Rc::new(arg.clone()) as Rc<dyn Object>)); return Ok(Value::Object(Rc::new(arg.clone()) as Rc<dyn Object>));
@@ -655,19 +655,19 @@ mod tests {
(m)) (m))
"; ";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator); let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap(); let expanded = expander.expand(syntax).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind if let SyntaxKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion { && let SyntaxKind::Expansion {
expanded: result, expanded: result,
call, call,
} = &exprs[1].kind } = &exprs[1].kind
{ {
if let UntypedKind::Def { target, .. } = &result.kind { if let SyntaxKind::Def { target, .. } = &result.kind {
if let UntypedKind::Identifier(sym) = &target.kind { if let SyntaxKind::Identifier(sym) = &target.kind {
assert_eq!(sym.context, Some(call.identity.clone())); assert_eq!(sym.context, Some(call.identity.clone()));
assert_eq!(sym.name.as_ref(), "y"); assert_eq!(sym.name.as_ref(), "y");
} else { } else {
@@ -687,30 +687,30 @@ mod tests {
(unless false 42)) (unless false 42))
"; ";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator); let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap(); let expanded = expander.expand(syntax).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind if let SyntaxKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion { && let SyntaxKind::Expansion {
call: _, call: _,
expanded: result, expanded: result,
} = &exprs[1].kind } = &exprs[1].kind
&& let UntypedKind::If { && let SyntaxKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
} = &result.kind } = &result.kind
{ {
if let UntypedKind::Identifier(sym) = &cond.kind { if let SyntaxKind::Identifier(sym) = &cond.kind {
assert_eq!(sym.name.as_ref(), "false"); assert_eq!(sym.name.as_ref(), "false");
} else { } else {
panic!("Expected identifier 'false', got {:?}", cond.kind); panic!("Expected identifier 'false', got {:?}", cond.kind);
} }
assert!(matches!(then_br.kind, UntypedKind::Nop)); assert!(matches!(then_br.kind, SyntaxKind::Nop));
if let Some(eb) = else_br { if let Some(eb) = else_br {
if let UntypedKind::Constant(Value::Int(n)) = &eb.kind { if let SyntaxKind::Constant(Value::Int(n)) = &eb.kind {
assert_eq!(*n, 42); assert_eq!(*n, 42);
} else { } else {
panic!("Expected 42, got {:?}", eb.kind); panic!("Expected 42, got {:?}", eb.kind);
@@ -729,23 +729,23 @@ mod tests {
(def t (wrap [1 2 3]))) (def t (wrap [1 2 3])))
"; ";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator); let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap(); let expanded = expander.expand(syntax).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind if let SyntaxKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Def { value, .. } = &exprs[1].kind && let SyntaxKind::Def { value, .. } = &exprs[1].kind
&& let UntypedKind::Expansion { && let SyntaxKind::Expansion {
expanded: result, .. expanded: result, ..
} = &value.kind } = &value.kind
&& let UntypedKind::Tuple { elements } = &result.kind && let SyntaxKind::Tuple { elements } = &result.kind
{ {
assert_eq!(elements.len(), 5); assert_eq!(elements.len(), 5);
if let UntypedKind::Constant(Value::Int(n)) = &elements[0].kind { if let SyntaxKind::Constant(Value::Int(n)) = &elements[0].kind {
assert_eq!(*n, 0); assert_eq!(*n, 0);
} }
if let UntypedKind::Constant(Value::Int(n)) = &elements[4].kind { if let SyntaxKind::Constant(Value::Int(n)) = &elements[4].kind {
assert_eq!(*n, 4); assert_eq!(*n, 4);
} }
} }
@@ -759,10 +759,10 @@ mod tests {
(square 3)) (square 3))
"; ";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator); let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap(); let expanded = expander.expand(syntax).unwrap();
// Convert test globals into a CompilerScope // Convert test globals into a CompilerScope
let mut locals = HashMap::new(); let mut locals = HashMap::new();
@@ -800,10 +800,10 @@ mod tests {
y) y)
"; ";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator); let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap(); let expanded = expander.expand(syntax).unwrap();
let mut diag = crate::ast::diagnostics::Diagnostics::new(); let mut diag = crate::ast::diagnostics::Diagnostics::new();
let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag); let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag);
@@ -820,10 +820,10 @@ mod tests {
y) y)
"; ";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator); let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap(); let expanded = expander.expand(syntax).unwrap();
let mut diag = crate::ast::diagnostics::Diagnostics::new(); let mut diag = crate::ast::diagnostics::Diagnostics::new();
let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag); let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag);
+30 -30
View File
@@ -1,7 +1,7 @@
use crate::ast::compiler::analyzer::Analyzer; use crate::ast::compiler::analyzer::Analyzer;
use crate::ast::compiler::binder::Binder; use crate::ast::compiler::binder::Binder;
use crate::ast::compiler::{TypeChecker, TypedNode}; 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::parser::Parser;
use crate::ast::vm::{TracingObserver, VM}; use crate::ast::vm::{TracingObserver, VM};
use std::cell::RefCell; use std::cell::RefCell;
@@ -122,10 +122,10 @@ struct RuntimeMacroEvaluator {
impl MacroEvaluator for RuntimeMacroEvaluator { impl MacroEvaluator for RuntimeMacroEvaluator {
fn evaluate( fn evaluate(
&self, &self,
node: &UntypedNode, node: &SyntaxNode,
bindings: &HashMap<Rc<str>, UntypedNode>, bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String> { ) -> 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) && let Some(arg_node) = bindings.get(&sym.name)
{ {
return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>)); return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>));
@@ -290,7 +290,7 @@ impl Environment {
&self, &self,
source: &str, source: &str,
base_path: &Path, base_path: &Path,
all_files: &mut Vec<(PathBuf, UntypedNode)>, all_files: &mut Vec<(PathBuf, SyntaxNode)>,
) -> Result<(), String> { ) -> Result<(), String> {
let directives = self.extract_use_directives(source); let directives = self.extract_use_directives(source);
@@ -312,7 +312,7 @@ impl Environment {
self.collect_dependencies(&lib_source, lib_base, all_files)?; self.collect_dependencies(&lib_source, lib_base, all_files)?;
let mut parser = Parser::new(&lib_source); let mut parser = Parser::new(&lib_source);
let untyped_ast = parser.parse_expression(); let syntax_ast = parser.parse_expression();
if parser.diagnostics.has_errors() { if parser.diagnostics.has_errors() {
return Err(format!( return Err(format!(
@@ -322,7 +322,7 @@ impl Environment {
)); ));
} }
all_files.push((abs_path, untyped_ast)); all_files.push((abs_path, syntax_ast));
} }
} }
Ok(()) Ok(())
@@ -358,34 +358,34 @@ impl Environment {
/// It returns the base path which can be used to resolve further things if necessary. /// 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> { 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 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 // 1. Always load the embedded system library first as a virtual module
let system_path = PathBuf::from(SYSTEM_LIB_PATH); let system_path = PathBuf::from(SYSTEM_LIB_PATH);
if !self.loaded_modules.borrow().contains(&system_path) { if !self.loaded_modules.borrow().contains(&system_path) {
self.loaded_modules.borrow_mut().insert(system_path.clone()); self.loaded_modules.borrow_mut().insert(system_path.clone());
let mut parser = Parser::new(SYSTEM_LIB_SOURCE); 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() { if parser.diagnostics.has_errors() {
return Err(format!( return Err(format!(
"Failed to parse embedded system library:\n{}", "Failed to parse embedded system library:\n{}",
parser.diagnostics.items[0].message parser.diagnostics.items[0].message
)); ));
} }
files.push((system_path, untyped_ast)); files.push((system_path, syntax_ast));
} }
// 2. Collect dependencies from the main source // 2. Collect dependencies from the main source
self.collect_dependencies(source, base_path, &mut files)?; self.collect_dependencies(source, base_path, &mut files)?;
// Pass 1: Discovery (Globals and Macros) // Pass 1: Discovery (Globals and Macros)
for (_, untyped_ast) in &files { for (_, syntax_ast) in &files {
self.discover_globals(untyped_ast); self.discover_globals(syntax_ast);
} }
// Pass 2: Compilation and Initialization // Pass 2: Compilation and Initialization
for (path, untyped_ast) in files { for (path, syntax_ast) in files {
let typed_ast = self.compile_untyped(untyped_ast).map_err(|e: String| { let typed_ast = self.compile_syntax(syntax_ast).map_err(|e: String| {
format!("Compilation error in {}:\n{}", path.display(), e) format!("Compilation error in {}:\n{}", path.display(), e)
})?; })?;
self.run_script_compiled(typed_ast).map_err(|e: String| { self.run_script_compiled(typed_ast).map_err(|e: String| {
@@ -396,10 +396,10 @@ impl Environment {
Ok(()) Ok(())
} }
fn discover_globals(&self, node: &UntypedNode) { fn discover_globals(&self, node: &SyntaxNode) {
match &node.kind { match &node.kind {
UntypedKind::Def { target, .. } => { SyntaxKind::Def { target, .. } => {
if let UntypedKind::Identifier(sym) = &target.kind { if let SyntaxKind::Identifier(sym) = &target.kind {
let mut root_scopes = self.root_scopes.borrow_mut(); let mut root_scopes = self.root_scopes.borrow_mut();
let last_idx = root_scopes.len() - 1; let last_idx = root_scopes.len() - 1;
let current_scope = &mut root_scopes[last_idx]; 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(); 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 { match &node.kind {
UntypedKind::Identifier(sym) => vec![sym.name.clone()], SyntaxKind::Identifier(sym) => vec![sym.name.clone()],
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
elements.iter().flat_map(extract_names).collect() elements.iter().flat_map(extract_names).collect()
} }
_ => vec![], _ => vec![],
@@ -436,7 +436,7 @@ impl Environment {
let p_names = extract_names(params); let p_names = extract_names(params);
registry.define(name.name.clone(), p_names, body.as_ref().clone()); registry.define(name.name.clone(), p_names, body.as_ref().clone());
} }
UntypedKind::Block { exprs } => { SyntaxKind::Block { exprs } => {
for expr in exprs { for expr in exprs {
self.discover_globals(expr); self.discover_globals(expr);
} }
@@ -445,8 +445,8 @@ impl Environment {
} }
} }
fn compile_pipeline(&self, untyped_ast: UntypedNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> { fn compile_pipeline(&self, syntax_ast: SyntaxNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> {
let expanded_ast = match self.get_expander().expand(untyped_ast) { let expanded_ast = match self.get_expander().expand(syntax_ast) {
Ok(ast) => ast, Ok(ast) => ast,
Err(e) => { Err(e) => {
diagnostics.push_error(e, None); diagnostics.push_error(e, None);
@@ -505,10 +505,10 @@ impl Environment {
Some(checker.check(&wrapped_ast, &[], diagnostics)) 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 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() { if diagnostics.has_errors() || typed_ast_opt.is_none() {
return Err(diagnostics return Err(diagnostics
@@ -627,7 +627,7 @@ impl Environment {
} }
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped_ast = parser.parse_expression(); let syntax_ast = parser.parse_expression();
if !parser.at_eof() { if !parser.at_eof() {
parser parser
@@ -640,7 +640,7 @@ impl Environment {
} }
let mut diagnostics = parser.diagnostics; 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 { CompilationResult {
ast: typed_ast, ast: typed_ast,
@@ -737,7 +737,7 @@ impl Environment {
let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args)); let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
let typed_reg = self.typed_function_registry.clone(); 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 mono_cache = self.monomorph_cache.clone();
let root_values = self.root_values.clone(); let root_values = self.root_values.clone();
let root_types = self.root_types.clone(); let root_types = self.root_types.clone();
@@ -764,7 +764,7 @@ impl Environment {
let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow()); let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow());
let sub_registry = Rc::new(EnvFunctionRegistry { let sub_registry = Rc::new(EnvFunctionRegistry {
registry: untyped_reg.clone(), registry: syntax_reg.clone(),
analyzed_registry: typed_reg.clone(), analyzed_registry: typed_reg.clone(),
}); });
let sub_rtl_lookup = let sub_rtl_lookup =
+29 -29
View File
@@ -32,12 +32,12 @@ impl From<&str> for Symbol {
/// A parser AST Node wrapper to preserve identity and metadata /// A parser AST Node wrapper to preserve identity and metadata
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct UntypedNode { pub struct SyntaxNode {
pub identity: Identity, pub identity: Identity,
pub kind: UntypedKind, pub kind: SyntaxKind,
} }
impl Object for UntypedNode { impl Object for SyntaxNode {
fn type_name(&self) -> &'static str { fn type_name(&self) -> &'static str {
"ast-node" "ast-node"
} }
@@ -59,68 +59,68 @@ impl Clone for Box<dyn CustomNode> {
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum UntypedKind { pub enum SyntaxKind {
Nop, Nop,
Constant(Value), Constant(Value),
/// A general identifier (used for both references and declarations in the untyped AST). /// A general identifier (used for both references and declarations in the syntax AST).
Identifier(Symbol), Identifier(Symbol),
/// A first-class field accessor (e.g. .name) /// A first-class field accessor (e.g. .name)
FieldAccessor(crate::ast::types::Keyword), FieldAccessor(crate::ast::types::Keyword),
If { If {
cond: Box<UntypedNode>, cond: Box<SyntaxNode>,
then_br: Box<UntypedNode>, then_br: Box<SyntaxNode>,
else_br: Option<Box<UntypedNode>>, else_br: Option<Box<SyntaxNode>>,
}, },
Def { Def {
target: Box<UntypedNode>, target: Box<SyntaxNode>,
value: Box<UntypedNode>, value: Box<SyntaxNode>,
}, },
Assign { Assign {
target: Box<UntypedNode>, target: Box<SyntaxNode>,
value: Box<UntypedNode>, value: Box<SyntaxNode>,
}, },
Lambda { Lambda {
params: Box<UntypedNode>, params: Box<SyntaxNode>,
body: Rc<UntypedNode>, body: Rc<SyntaxNode>,
}, },
Call { Call {
callee: Box<UntypedNode>, callee: Box<SyntaxNode>,
args: Box<UntypedNode>, args: Box<SyntaxNode>,
}, },
Again { Again {
args: Box<UntypedNode>, args: Box<SyntaxNode>,
}, },
Pipe { Pipe {
inputs: Vec<UntypedNode>, inputs: Vec<SyntaxNode>,
lambda: Box<UntypedNode>, lambda: Box<SyntaxNode>,
}, },
Block { Block {
exprs: Vec<UntypedNode>, exprs: Vec<SyntaxNode>,
}, },
Tuple { Tuple {
elements: Vec<UntypedNode>, elements: Vec<SyntaxNode>,
}, },
Record { Record {
fields: Vec<(UntypedNode, UntypedNode)>, fields: Vec<(SyntaxNode, SyntaxNode)>,
}, },
/// A macro declaration that can be expanded at compile time. /// A macro declaration that can be expanded at compile time.
MacroDecl { MacroDecl {
name: Symbol, name: Symbol,
params: Box<UntypedNode>, params: Box<SyntaxNode>,
body: Box<UntypedNode>, body: Box<SyntaxNode>,
}, },
/// A template for AST nodes, allowing for substitutions. (Quasiquote) /// A template for AST nodes, allowing for substitutions. (Quasiquote)
Template(Box<UntypedNode>), Template(Box<SyntaxNode>),
/// A placeholder inside a template to be replaced by a node or value. (Unquote) /// A placeholder inside a template to be replaced by a node or value. (Unquote)
Placeholder(Box<UntypedNode>), Placeholder(Box<SyntaxNode>),
/// A placeholder that flattens a sequence or merges a record into its surroundings. (Splice) /// A placeholder that flattens a sequence or merges a record into its surroundings. (Splice)
Splice(Box<UntypedNode>), Splice(Box<SyntaxNode>),
/// Represents an expanded macro call, preserving the original call for debugging. /// Represents an expanded macro call, preserving the original call for debugging.
Expansion { Expansion {
/// The original call from the source AST. /// The original call from the source AST.
call: Box<UntypedNode>, call: Box<SyntaxNode>,
/// The resulting AST after macro expansion. /// The resulting AST after macro expansion.
expanded: Box<UntypedNode>, expanded: Box<SyntaxNode>,
}, },
/// A diagnostic poison node, allowing compilation to continue after an error. /// A diagnostic poison node, allowing compilation to continue after an error.
Error, Error,
+84 -84
View File
@@ -1,6 +1,6 @@
use crate::ast::diagnostics::Diagnostics; use crate::ast::diagnostics::Diagnostics;
use crate::ast::lexer::{Lexer, Token, TokenKind}; use crate::ast::lexer::{Lexer, Token, TokenKind};
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode}; use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::types::{Identity, Keyword, NodeIdentity, Value}; use crate::ast::types::{Identity, Keyword, NodeIdentity, Value};
use std::rc::Rc; use std::rc::Rc;
@@ -50,7 +50,7 @@ impl<'a> Parser<'a> {
&self.current_token.kind &self.current_token.kind
} }
pub fn parse_expression(&mut self) -> UntypedNode { pub fn parse_expression(&mut self) -> SyntaxNode {
let token_loc = self.current_token.location; let token_loc = self.current_token.location;
let identity = NodeIdentity::new(token_loc); let identity = NodeIdentity::new(token_loc);
@@ -61,13 +61,13 @@ impl<'a> Parser<'a> {
TokenKind::Quote => { TokenKind::Quote => {
self.advance(); // consume ' self.advance(); // consume '
let expr = self.parse_expression(); let expr = self.parse_expression();
UntypedNode { SyntaxNode {
identity: identity.clone(), identity: identity.clone(),
kind: UntypedKind::Call { kind: SyntaxKind::Call {
callee: Box::new(self.make_id_node("quote", identity.clone())), callee: Box::new(self.make_id_node("quote", identity.clone())),
args: Box::new(UntypedNode { args: Box::new(SyntaxNode {
identity, identity,
kind: UntypedKind::Tuple { kind: SyntaxKind::Tuple {
elements: vec![expr], elements: vec![expr],
}, },
@@ -79,9 +79,9 @@ impl<'a> Parser<'a> {
TokenKind::Backtick => { TokenKind::Backtick => {
self.advance(); // consume ` self.advance(); // consume `
let expr = self.parse_expression(); let expr = self.parse_expression();
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Template(Box::new(expr)), kind: SyntaxKind::Template(Box::new(expr)),
} }
} }
@@ -90,16 +90,16 @@ impl<'a> Parser<'a> {
if *self.peek() == TokenKind::At { if *self.peek() == TokenKind::At {
self.advance(); // consume @ self.advance(); // consume @
let expr = self.parse_expression(); let expr = self.parse_expression();
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Splice(Box::new(expr)), kind: SyntaxKind::Splice(Box::new(expr)),
} }
} else { } else {
let expr = self.parse_expression(); let expr = self.parse_expression();
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Placeholder(Box::new(expr)), kind: SyntaxKind::Placeholder(Box::new(expr)),
} }
} }
@@ -126,39 +126,39 @@ impl<'a> Parser<'a> {
} }
} }
fn parse_atom(&mut self) -> UntypedNode { fn parse_atom(&mut self) -> SyntaxNode {
let token = self.advance(); let token = self.advance();
let identity = NodeIdentity::new(token.location); let identity = NodeIdentity::new(token.location);
let kind = match token.kind { let kind = match token.kind {
TokenKind::Integer(n) => UntypedKind::Constant(Value::Int(n)), TokenKind::Integer(n) => SyntaxKind::Constant(Value::Int(n)),
TokenKind::Float(n) => UntypedKind::Constant(Value::Float(n)), TokenKind::Float(n) => SyntaxKind::Constant(Value::Float(n)),
TokenKind::String(s) => UntypedKind::Constant(Value::Text(s)), TokenKind::String(s) => SyntaxKind::Constant(Value::Text(s)),
TokenKind::Keyword(k) => UntypedKind::Constant(Value::Keyword(Keyword::intern(&k))), TokenKind::Keyword(k) => SyntaxKind::Constant(Value::Keyword(Keyword::intern(&k))),
TokenKind::Identifier(id) => match id.as_ref() { TokenKind::Identifier(id) => match id.as_ref() {
"..." => UntypedKind::Nop, "..." => SyntaxKind::Nop,
s if s.starts_with('.') && s.len() > 1 => { s if s.starts_with('.') && s.len() > 1 => {
UntypedKind::FieldAccessor(Keyword::intern(&s[1..])) SyntaxKind::FieldAccessor(Keyword::intern(&s[1..]))
} }
_ => UntypedKind::Identifier(id.into()), _ => SyntaxKind::Identifier(id.into()),
}, },
TokenKind::EOF => UntypedKind::Error, // Error already logged by advance TokenKind::EOF => SyntaxKind::Error, // Error already logged by advance
_ => { _ => {
self.diagnostics.push_error( self.diagnostics.push_error(
format!("Unexpected token in atom: {:?}", token.kind), format!("Unexpected token in atom: {:?}", token.kind),
Some(identity.clone()), Some(identity.clone()),
); );
UntypedKind::Error SyntaxKind::Error
} }
}; };
UntypedNode { SyntaxNode {
identity, identity,
kind, kind,
} }
} }
fn parse_list(&mut self) -> UntypedNode { fn parse_list(&mut self) -> SyntaxNode {
let start_loc = self.advance().location; // consume '(' let start_loc = self.advance().location; // consume '('
let identity = NodeIdentity::new(start_loc); let identity = NodeIdentity::new(start_loc);
@@ -168,16 +168,16 @@ impl<'a> Parser<'a> {
Some(identity.clone()), Some(identity.clone()),
); );
self.advance(); // consume ) self.advance(); // consume )
return UntypedNode { return SyntaxNode {
identity, identity,
kind: UntypedKind::Error, kind: SyntaxKind::Error,
}; };
} }
let head = self.parse_expression(); let head = self.parse_expression();
let node = if let UntypedKind::Identifier(ref sym) = head.kind { let node = if let SyntaxKind::Identifier(ref sym) = head.kind {
match sym.name.as_ref() { match sym.name.as_ref() {
"if" => self.parse_if(identity), "if" => self.parse_if(identity),
"fn" => self.parse_fn(identity), "fn" => self.parse_fn(identity),
@@ -197,29 +197,29 @@ impl<'a> Parser<'a> {
node node
} }
fn parse_again(&mut self, identity: Identity) -> UntypedNode { fn parse_again(&mut self, identity: Identity) -> SyntaxNode {
let mut elements = Vec::new(); let mut elements = Vec::new();
while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF { while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF {
elements.push(self.parse_expression()); elements.push(self.parse_expression());
} }
let args_node = UntypedNode { let args_node = SyntaxNode {
identity: identity.clone(), identity: identity.clone(),
kind: UntypedKind::Tuple { elements }, kind: SyntaxKind::Tuple { elements },
}; };
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Again { kind: SyntaxKind::Again {
args: Box::new(args_node), args: Box::new(args_node),
}, },
} }
} }
fn parse_if(&mut self, identity: Identity) -> UntypedNode { fn parse_if(&mut self, identity: Identity) -> SyntaxNode {
let cond = Box::new(self.parse_expression()); let cond = Box::new(self.parse_expression());
let then_br = Box::new(self.parse_expression()); let then_br = Box::new(self.parse_expression());
let mut else_br = None; let mut else_br = None;
@@ -228,9 +228,9 @@ impl<'a> Parser<'a> {
else_br = Some(Box::new(self.parse_expression())); else_br = Some(Box::new(self.parse_expression()));
} }
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::If { kind: SyntaxKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
@@ -239,63 +239,63 @@ impl<'a> Parser<'a> {
} }
} }
fn parse_def(&mut self, identity: Identity) -> UntypedNode { fn parse_def(&mut self, identity: Identity) -> SyntaxNode {
let target = Box::new(self.parse_pattern()); let target = Box::new(self.parse_pattern());
let value = Box::new(self.parse_expression()); let value = Box::new(self.parse_expression());
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Def { target, value }, kind: SyntaxKind::Def { target, value },
} }
} }
fn parse_assign(&mut self, identity: Identity) -> UntypedNode { fn parse_assign(&mut self, identity: Identity) -> SyntaxNode {
// (assign target value) // (assign target value)
let target = Box::new(self.parse_expression()); let target = Box::new(self.parse_expression());
let value = Box::new(self.parse_expression()); let value = Box::new(self.parse_expression());
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Assign { target, value }, kind: SyntaxKind::Assign { target, value },
} }
} }
fn parse_do(&mut self, identity: Identity) -> UntypedNode { fn parse_do(&mut self, identity: Identity) -> SyntaxNode {
let mut exprs = Vec::new(); let mut exprs = Vec::new();
while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF { while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF {
exprs.push(self.parse_expression()); exprs.push(self.parse_expression());
} }
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Block { exprs }, kind: SyntaxKind::Block { exprs },
} }
} }
fn parse_pipe(&mut self, identity: Identity) -> UntypedNode { fn parse_pipe(&mut self, identity: Identity) -> SyntaxNode {
let inputs_node = self.parse_expression(); let inputs_node = self.parse_expression();
let inputs = match inputs_node.kind { let inputs = match inputs_node.kind {
UntypedKind::Tuple { elements } => elements, SyntaxKind::Tuple { elements } => elements,
_ => vec![inputs_node], _ => vec![inputs_node],
}; };
let lambda = Box::new(self.parse_expression()); let lambda = Box::new(self.parse_expression());
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Pipe { inputs, lambda }, kind: SyntaxKind::Pipe { inputs, lambda },
} }
} }
fn parse_fn(&mut self, identity: Identity) -> UntypedNode { fn parse_fn(&mut self, identity: Identity) -> SyntaxNode {
let params = Box::new(self.parse_param_vector()); let params = Box::new(self.parse_param_vector());
let body = self.parse_expression(); let body = self.parse_expression();
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Lambda { kind: SyntaxKind::Lambda {
params, params,
body: Rc::new(body), body: Rc::new(body),
}, },
@@ -303,10 +303,10 @@ impl<'a> Parser<'a> {
} }
} }
fn parse_macro_decl(&mut self, identity: Identity) -> UntypedNode { fn parse_macro_decl(&mut self, identity: Identity) -> SyntaxNode {
let name_node = self.parse_expression(); let name_node = self.parse_expression();
let name = match name_node.kind { let name = match name_node.kind {
UntypedKind::Identifier(sym) => sym, SyntaxKind::Identifier(sym) => sym,
_ => { _ => {
self.diagnostics.push_error( self.diagnostics.push_error(
"Expected identifier for macro name", "Expected identifier for macro name",
@@ -317,9 +317,9 @@ impl<'a> Parser<'a> {
}; };
let params = Box::new(self.parse_param_vector()); let params = Box::new(self.parse_param_vector());
let body = self.parse_expression(); let body = self.parse_expression();
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::MacroDecl { kind: SyntaxKind::MacroDecl {
name, name,
params, params,
body: Box::new(body), body: Box::new(body),
@@ -328,7 +328,7 @@ impl<'a> Parser<'a> {
} }
} }
fn parse_param_vector(&mut self) -> UntypedNode { fn parse_param_vector(&mut self) -> SyntaxNode {
if *self.peek() != TokenKind::LeftBracket { if *self.peek() != TokenKind::LeftBracket {
self.diagnostics.push_error( self.diagnostics.push_error(
format!( format!(
@@ -337,16 +337,16 @@ impl<'a> Parser<'a> {
), ),
Some(NodeIdentity::new(self.current_token.location)), Some(NodeIdentity::new(self.current_token.location)),
); );
return UntypedNode { return SyntaxNode {
identity: NodeIdentity::new(self.current_token.location), identity: NodeIdentity::new(self.current_token.location),
kind: UntypedKind::Error, kind: SyntaxKind::Error,
}; };
} }
self.parse_pattern() self.parse_pattern()
} }
fn parse_pattern(&mut self) -> UntypedNode { fn parse_pattern(&mut self) -> SyntaxNode {
let next = self.peek(); let next = self.peek();
match next { match next {
TokenKind::Identifier(_) => { TokenKind::Identifier(_) => {
@@ -355,9 +355,9 @@ impl<'a> Parser<'a> {
TokenKind::Identifier(s) => s.into(), TokenKind::Identifier(s) => s.into(),
_ => unreachable!(), _ => unreachable!(),
}; };
UntypedNode { SyntaxNode {
identity: NodeIdentity::new(token.location), identity: NodeIdentity::new(token.location),
kind: UntypedKind::Identifier(sym), kind: SyntaxKind::Identifier(sym),
} }
} }
@@ -371,9 +371,9 @@ impl<'a> Parser<'a> {
} }
self.expect(TokenKind::RightBracket); self.expect(TokenKind::RightBracket);
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Tuple { elements }, kind: SyntaxKind::Tuple { elements },
} }
} }
@@ -382,16 +382,16 @@ impl<'a> Parser<'a> {
if *self.peek() == TokenKind::At { if *self.peek() == TokenKind::At {
self.advance(); // consume @ self.advance(); // consume @
let expr = self.parse_expression(); let expr = self.parse_expression();
UntypedNode { SyntaxNode {
identity: NodeIdentity::new(token.location), identity: NodeIdentity::new(token.location),
kind: UntypedKind::Splice(Box::new(expr)), kind: SyntaxKind::Splice(Box::new(expr)),
} }
} else { } else {
let expr = self.parse_expression(); let expr = self.parse_expression();
UntypedNode { SyntaxNode {
identity: NodeIdentity::new(token.location), identity: NodeIdentity::new(token.location),
kind: UntypedKind::Placeholder(Box::new(expr)), kind: SyntaxKind::Placeholder(Box::new(expr)),
} }
} }
@@ -405,16 +405,16 @@ impl<'a> Parser<'a> {
Some(NodeIdentity::new(self.current_token.location)), Some(NodeIdentity::new(self.current_token.location)),
); );
self.advance(); self.advance();
UntypedNode { SyntaxNode {
identity: NodeIdentity::new(self.current_token.location), identity: NodeIdentity::new(self.current_token.location),
kind: UntypedKind::Error, kind: SyntaxKind::Error,
} }
} }
} }
} }
fn parse_call(&mut self, callee: UntypedNode, identity: Identity) -> UntypedNode { fn parse_call(&mut self, callee: SyntaxNode, identity: Identity) -> SyntaxNode {
let mut elements = Vec::new(); let mut elements = Vec::new();
while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF { while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF {
@@ -422,15 +422,15 @@ impl<'a> Parser<'a> {
} }
// The arguments are wrapped in a Tuple node, reusing the call's identity/location. // The arguments are wrapped in a Tuple node, reusing the call's identity/location.
let args_node = UntypedNode { let args_node = SyntaxNode {
identity: identity.clone(), identity: identity.clone(),
kind: UntypedKind::Tuple { elements }, kind: SyntaxKind::Tuple { elements },
}; };
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Call { kind: SyntaxKind::Call {
callee: Box::new(callee), callee: Box::new(callee),
args: Box::new(args_node), args: Box::new(args_node),
}, },
@@ -438,7 +438,7 @@ impl<'a> Parser<'a> {
} }
} }
fn parse_vector_literal(&mut self) -> UntypedNode { fn parse_vector_literal(&mut self) -> SyntaxNode {
let token = self.advance(); let token = self.advance();
let mut elements = Vec::new(); let mut elements = Vec::new();
@@ -449,14 +449,14 @@ impl<'a> Parser<'a> {
self.expect(TokenKind::RightBracket); self.expect(TokenKind::RightBracket);
UntypedNode { SyntaxNode {
identity: NodeIdentity::new(token.location), identity: NodeIdentity::new(token.location),
kind: UntypedKind::Tuple { elements }, kind: SyntaxKind::Tuple { elements },
} }
} }
fn parse_record_literal(&mut self) -> UntypedNode { fn parse_record_literal(&mut self) -> SyntaxNode {
let token = self.advance(); let token = self.advance();
let mut fields = Vec::new(); let mut fields = Vec::new();
@@ -466,7 +466,7 @@ impl<'a> Parser<'a> {
// strictly we could allow any expression and check at runtime. // strictly we could allow any expression and check at runtime.
// Delphi enforces keywords. We can do minimal check here. // Delphi enforces keywords. We can do minimal check here.
match &key_node.kind { match &key_node.kind {
UntypedKind::Constant(Value::Keyword(_)) => {} SyntaxKind::Constant(Value::Keyword(_)) => {}
_ => { _ => {
self.diagnostics.push_error( self.diagnostics.push_error(
"Record keys must be keywords (syntactically)", "Record keys must be keywords (syntactically)",
@@ -488,9 +488,9 @@ impl<'a> Parser<'a> {
} }
self.expect(TokenKind::RightBrace); self.expect(TokenKind::RightBrace);
UntypedNode { SyntaxNode {
identity: NodeIdentity::new(token.location), identity: NodeIdentity::new(token.location),
kind: UntypedKind::Record { fields }, kind: SyntaxKind::Record { fields },
} }
} }
@@ -512,10 +512,10 @@ impl<'a> Parser<'a> {
} }
} }
fn make_id_node(&self, name: &str, identity: Identity) -> UntypedNode { fn make_id_node(&self, name: &str, identity: Identity) -> SyntaxNode {
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Identifier(Symbol::from(name)), kind: SyntaxKind::Identifier(Symbol::from(name)),
} }
} }
+5 -5
View File
@@ -1,7 +1,7 @@
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::ast::environment::Environment; use crate::ast::environment::Environment;
use crate::ast::nodes::UntypedKind; use crate::ast::nodes::SyntaxKind;
use crate::ast::parser::Parser; use crate::ast::parser::Parser;
use crate::ast::types::Value; use crate::ast::types::Value;
@@ -11,7 +11,7 @@ mod tests {
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let ast = parser.parse_expression(); let ast = parser.parse_expression();
if let UntypedKind::Constant(Value::Int(val)) = ast.kind { if let SyntaxKind::Constant(Value::Int(val)) = ast.kind {
assert_eq!(val, 123); assert_eq!(val, 123);
} else { } else {
panic!("Expected Integer constant, got {:?}", ast.kind); panic!("Expected Integer constant, got {:?}", ast.kind);
@@ -24,7 +24,7 @@ mod tests {
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let ast = parser.parse_expression(); let ast = parser.parse_expression();
if let UntypedKind::Constant(Value::Int(val)) = ast.kind { if let SyntaxKind::Constant(Value::Int(val)) = ast.kind {
assert_eq!(val, -42); assert_eq!(val, -42);
} else { } else {
panic!("Expected Integer constant, got {:?}", ast.kind); panic!("Expected Integer constant, got {:?}", ast.kind);
@@ -37,7 +37,7 @@ mod tests {
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let ast = parser.parse_expression(); let ast = parser.parse_expression();
if let UntypedKind::Constant(Value::Float(val)) = ast.kind { if let SyntaxKind::Constant(Value::Float(val)) = ast.kind {
assert_eq!(val, 123.45); assert_eq!(val, 123.45);
} else { } else {
panic!("Expected Float constant, got {:?}", ast.kind); panic!("Expected Float constant, got {:?}", ast.kind);
@@ -50,7 +50,7 @@ mod tests {
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let ast = parser.parse_expression(); let ast = parser.parse_expression();
if let UntypedKind::Constant(Value::Float(val)) = ast.kind { if let SyntaxKind::Constant(Value::Float(val)) = ast.kind {
assert_eq!(val, -10.5); assert_eq!(val, -10.5);
} else { } else {
panic!("Expected Float constant, got {:?}", ast.kind); panic!("Expected Float constant, got {:?}", ast.kind);