Refactor UntypedKind::Parameter to Identifier
The `Parameter` kind in `UntypedKind` was only used for identifiers that were being declared or referenced. This commit renames it to `Identifier` and updates all the necessary code to reflect this change. This simplifies the AST and makes it more consistent. Additionally, a new macro `repeat` has been added to `src/ast/system.myc`.
This commit is contained in:
@@ -197,11 +197,6 @@ impl Binder {
|
||||
}
|
||||
}
|
||||
|
||||
UntypedKind::Parameter(_) => {
|
||||
diag.push_error("Unexpected 'Parameter' node in general binder context. This should be handled via 'bind_pattern'.", Some(node.identity.clone()));
|
||||
self.make_node(node.identity.clone(), BoundKind::Error)
|
||||
}
|
||||
|
||||
UntypedKind::FieldAccessor(k) => {
|
||||
self.make_node(node.identity.clone(), BoundKind::FieldAccessor(*k))
|
||||
}
|
||||
@@ -230,7 +225,7 @@ impl Binder {
|
||||
|
||||
UntypedKind::Def { target, value } => {
|
||||
// Special case: Single identifier (to support recursion)
|
||||
if let UntypedKind::Parameter(ref name) = target.kind {
|
||||
if let UntypedKind::Identifier(ref name) = target.kind {
|
||||
let addr_opt = self.declare_variable(
|
||||
name,
|
||||
node.identity.clone(), // Identity of the Def node
|
||||
@@ -543,7 +538,7 @@ impl Binder {
|
||||
diag: &mut Diagnostics,
|
||||
) -> BoundNode {
|
||||
match &node.kind {
|
||||
UntypedKind::Parameter(sym) => {
|
||||
UntypedKind::Identifier(sym) => {
|
||||
if let Some(addr) = self.declare_variable(sym, node.identity.clone(), kind, diag) {
|
||||
self.make_node(
|
||||
node.identity.clone(),
|
||||
|
||||
@@ -344,7 +344,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
|
||||
fn extract_param_names(&self, node: &Node<UntypedKind>) -> Result<Vec<Rc<str>>, String> {
|
||||
match &node.kind {
|
||||
UntypedKind::Parameter(sym) => Ok(vec![sym.name.clone()]),
|
||||
UntypedKind::Identifier(sym) => Ok(vec![sym.name.clone()]),
|
||||
UntypedKind::Tuple { elements } => {
|
||||
let mut names = Vec::new();
|
||||
for el in elements {
|
||||
@@ -372,15 +372,6 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
})
|
||||
}
|
||||
|
||||
UntypedKind::Parameter(mut sym) => {
|
||||
sym.context = Some(state.expansion_id.clone());
|
||||
Ok(Node {
|
||||
identity: node.identity,
|
||||
kind: UntypedKind::Parameter(sym),
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
UntypedKind::Placeholder(inner) => {
|
||||
// Break out of template for substitution/evaluation
|
||||
if let UntypedKind::Identifier(ref sym) = inner.kind
|
||||
@@ -654,11 +645,11 @@ mod tests {
|
||||
} = &exprs[1].kind
|
||||
{
|
||||
if let UntypedKind::Def { target, .. } = &result.kind {
|
||||
if let UntypedKind::Parameter(sym) = &target.kind {
|
||||
if let UntypedKind::Identifier(sym) = &target.kind {
|
||||
assert_eq!(sym.context, Some(call.identity.clone()));
|
||||
assert_eq!(sym.name.as_ref(), "y");
|
||||
} else {
|
||||
panic!("Expected Parameter target, got {:?}", target.kind);
|
||||
panic!("Expected Identifier target, got {:?}", target.kind);
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Def result, got {:?}", result.kind);
|
||||
|
||||
@@ -381,7 +381,7 @@ impl Environment {
|
||||
fn discover_globals(&self, node: &Node<UntypedKind>) {
|
||||
match &node.kind {
|
||||
UntypedKind::Def { target, .. } => {
|
||||
if let UntypedKind::Parameter(sym) = &target.kind {
|
||||
if let UntypedKind::Identifier(sym) = &target.kind {
|
||||
let mut names = self.global_names.borrow_mut();
|
||||
if !names.contains_key(sym) {
|
||||
let idx = GlobalIdx(names.len() as u32);
|
||||
@@ -391,12 +391,11 @@ impl Environment {
|
||||
}
|
||||
UntypedKind::MacroDecl { name, params, body } => {
|
||||
let mut registry = self.macro_registry.borrow_mut();
|
||||
|
||||
|
||||
fn extract_names(node: &Node<UntypedKind>) -> Vec<Rc<str>> {
|
||||
match &node.kind {
|
||||
UntypedKind::Parameter(sym) => vec![sym.name.clone()],
|
||||
UntypedKind::Tuple { elements } => {
|
||||
elements.iter().flat_map(extract_names).collect()
|
||||
UntypedKind::Identifier(sym) => vec![sym.name.clone()],
|
||||
UntypedKind::Tuple { elements } => { elements.iter().flat_map(extract_names).collect()
|
||||
}
|
||||
_ => vec![],
|
||||
}
|
||||
|
||||
+1
-1
@@ -63,8 +63,8 @@ impl Clone for Box<dyn CustomNode> {
|
||||
pub enum UntypedKind {
|
||||
Nop,
|
||||
Constant(Value),
|
||||
/// A general identifier (used for both references and declarations in the untyped AST).
|
||||
Identifier(Symbol),
|
||||
Parameter(Symbol),
|
||||
/// A first-class field accessor (e.g. .name)
|
||||
FieldAccessor(crate::ast::types::Keyword),
|
||||
If {
|
||||
|
||||
+1
-1
@@ -358,7 +358,7 @@ impl<'a> Parser<'a> {
|
||||
};
|
||||
Node {
|
||||
identity: NodeIdentity::new(token.location),
|
||||
kind: UntypedKind::Parameter(sym),
|
||||
kind: UntypedKind::Identifier(sym),
|
||||
ty: (),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,17 @@
|
||||
)))
|
||||
)
|
||||
|
||||
(macro repeat [var limit body]
|
||||
`((fn [~var __limit]
|
||||
(if (< ~var __limit)
|
||||
(do
|
||||
~body
|
||||
(again (+ ~var 1) __limit)
|
||||
)
|
||||
))
|
||||
0 ~limit)
|
||||
)
|
||||
|
||||
;; Creates a stateful cache (Series) from a stateless stream.
|
||||
(macro cache [lookback type src]
|
||||
`(do
|
||||
|
||||
Reference in New Issue
Block a user