Skip to content

Commit b58c828

Browse files
committed
Refactor AST Types and Expressions
- Introduced `TypeInfo` struct to hold type information including name and parameters. - Updated `Expression` enum to include optional type information for various expression types. - Modified `Identifier` struct to remove `ty` field and adjusted related implementations. - Refactored `WatEmitter` to accommodate changes in expression handling and type emission. - Removed unused display implementations for various types to clean up code. - Updated tests to reflect changes in expression handling and type assertions.
1 parent b068c4a commit b58c828

File tree

13 files changed

+1863
-581
lines changed

13 files changed

+1863
-581
lines changed

.vscode/settings.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ inf-wasmparser = "0.0.8"
3333
wat-fmt = "0.0.8"
3434
wasm-fmt = { path = "./wasm-fmt", version = "0.0.1" }
3535
tree-sitter = "0.25.3"
36-
tree-sitter-inference = "0.0.29"
36+
tree-sitter-inference = "0.0.30"
3737
anyhow = "1.0.98"

ast/src/arena.rs

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, rc::Rc};
22

3-
use crate::types::AstNode;
3+
use crate::types::{AstNode, Definition, TypeDefinition};
44

55
#[derive(Default, Clone)]
6-
pub(crate) struct Arena {
6+
pub struct Arena {
77
pub(crate) nodes: HashMap<u32, AstNode>,
88
pub(crate) node_routes: Vec<NodeRoute>,
99
}
@@ -36,26 +36,75 @@ impl Arena {
3636
self.node_routes.push(node);
3737
}
3838

39-
#[must_use]
40-
pub(crate) fn find_parent_node(&self, id: u32) -> Option<u32> {
39+
pub fn find_node(&self, id: u32) -> Option<AstNode> {
40+
self.nodes.get(&id).cloned()
41+
}
42+
43+
pub fn find_parent_node(&self, id: u32) -> Option<u32> {
4144
self.node_routes
4245
.iter()
4346
.find(|n| n.id == id)
4447
.cloned()
4548
.and_then(|node| node.parent)
4649
}
4750

48-
// pub fn check_expressions_typed(&self) {
49-
// for node in self.nodes.values() {
50-
// match &node {
51-
// AstNode::UzumakiExpression(expr) => {
52-
// assert!(
53-
// expr.ty
54-
// )
55-
// }
56-
// }
57-
// }
58-
// }
51+
pub fn get_children_cmp<F>(&self, id: u32, comparator: F) -> Vec<AstNode>
52+
where
53+
F: Fn(&AstNode) -> bool,
54+
{
55+
let mut result = Vec::new();
56+
let mut stack: Vec<AstNode> = Vec::new();
57+
58+
if let Some(root_node) = self.find_node(id) {
59+
stack.push(root_node.clone());
60+
}
61+
62+
while let Some(current_node) = stack.pop() {
63+
if comparator(&current_node) {
64+
result.push(current_node.clone());
65+
}
66+
stack.extend(
67+
self.list_nodes_children(current_node.id())
68+
.into_iter()
69+
.filter(|child| comparator(child)),
70+
);
71+
}
72+
73+
result
74+
}
75+
76+
pub fn list_type_definitions(&self) -> Vec<Rc<TypeDefinition>> {
77+
self.list_nodes_cmp(|node| {
78+
if let AstNode::Definition(Definition::Type(type_def)) = node {
79+
Some(type_def.clone())
80+
} else {
81+
None
82+
}
83+
})
84+
.collect()
85+
}
86+
87+
fn list_nodes_children(&self, id: u32) -> Vec<AstNode> {
88+
self.node_routes
89+
.iter()
90+
.find(|n| n.id == id)
91+
.map(|node| {
92+
node.children
93+
.iter()
94+
.filter_map(|child_id| self.nodes.get(child_id).cloned())
95+
.collect()
96+
})
97+
.unwrap_or_default()
98+
}
99+
100+
fn list_nodes_cmp<'a, T, F>(&'a self, cmp: F) -> impl Iterator<Item = T> + 'a
101+
where
102+
F: Fn(&AstNode) -> Option<T> + Clone + 'a,
103+
T: Clone + 'static,
104+
{
105+
let cmp = cmp.clone();
106+
self.nodes.iter().filter_map(move |(_, node)| cmp(node))
107+
}
59108
}
60109

61110
#[derive(Clone, Default)]

0 commit comments

Comments
 (0)