Skip to content

Commit e08af5f

Browse files
author
DigitalCodeCrafter
committed
added symbol resolution
1 parent b16d581 commit e08af5f

6 files changed

Lines changed: 1011 additions & 126 deletions

File tree

src/compiler.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

2-
// [X] lexer
2+
// [x] lexer
33
// [/] parser
44
// [/] expander
5-
// [ ] resolver
5+
// [/] resolver
66
// [ ] validator
77
// [ ] type checker
88
// [ ] lowerer
@@ -17,6 +17,7 @@ mod lexer;
1717
mod parser;
1818
mod ast;
1919
mod expander;
20+
mod resolver;
2021

2122
type CResult<T> = Result<T, CompilerError>;
2223
#[derive(Debug)]
@@ -49,20 +50,23 @@ trait ToCompileResult<T> {
4950
fn into_cresult(self) -> CResult<T>;
5051
}
5152

52-
5353
pub fn compile(file_path: impl AsRef<Path>) -> Result<(), CompilerError> {
54-
let tokens = lexer::lex_file(&file_path).map_err(|err| CompilerError::IoError(err))?.into_cresult()?;
54+
let src = std::fs::read_to_string(&file_path)
55+
.map_err(|e| CompilerError::IoError(e))?;
56+
57+
let tokens = lexer::lex_all(&src).into_cresult()?;
5558

5659
let mut ast = parser::parse_tokens(tokens).into_cresult()?;
5760

5861
let mut expander = expander::Expander::new(&mut ast, file_path.as_ref().parent().unwrap(), |src| {
59-
let mut lexer = lexer::Lexer::new(src);
60-
lexer.lex_all().into_cresult()
62+
lexer::lex_all(src).into_cresult()
6163
}, |tokens| {
6264
parser::parse_tokens(tokens).into_cresult()
6365
});
64-
6566
expander.expand_modules()?;
6667

68+
let mut resolver = resolver::Resolver::new(&ast);
69+
let result = resolver.resolve();
70+
6771
todo!()
6872
}

src/compiler/ast.rs

Lines changed: 223 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,249 @@ pub enum NodeKind {
3636
TypeArray { ty: NodeId, len: NodeId },
3737
TypeSlice { ty: NodeId },
3838
ErrorType,
39-
39+
4040
PathSegment { ident: String, args: Vec<NodeId> },
41-
41+
4242
// Statements
4343
LetStmt { name: String, mutable: bool, ty: Option<NodeId>, value: Option<NodeId> },
4444
ExprStmt { expr: NodeId },
4545
EmptyStmt,
46-
46+
4747
// Items
4848
Function { public: bool, name: String, params: Vec<(String, NodeId)>, return_type: Option<NodeId>, body: NodeId },
4949
Module { public: bool, name: String, items: Option<Vec<NodeId>> },
5050
UseDecl { public: bool, use_tree: NodeId },
51-
51+
5252
UsePath { ident: String, tree: NodeId },
5353
UseGroup { trees: Vec<NodeId> },
5454
UseName { ident: String },
55+
UseRename { ident: String, name: String },
5556
UseGlob,
5657
}
58+
impl NodeKind {
59+
/// returns an iterator over the children of a node
60+
pub fn children(&self) ->impl Iterator<Item = NodeId> {
61+
use NodeKind::*;
62+
63+
enum Children<'a> {
64+
None(std::iter::Empty<NodeId>),
65+
One(std::iter::Once<NodeId>),
66+
Two(std::array::IntoIter<NodeId, 2>),
67+
Three(std::array::IntoIter<NodeId, 3>),
68+
Slice(std::iter::Copied<std::slice::Iter<'a, NodeId>>),
69+
Chain(std::iter::Chain<std::iter::Once<NodeId>, std::iter::Copied<std::slice::Iter<'a, NodeId>>>),
70+
ChainOpt(std::iter::Chain<std::iter::Copied<std::option::Iter<'a, NodeId>>, std::iter::Copied<std::option::Iter<'a, NodeId>>>),
71+
FuncChain(
72+
std::iter::Chain<
73+
std::iter::Chain<
74+
std::iter::Once<NodeId>,
75+
std::iter::Copied<std::option::Iter<'a, NodeId>>
76+
>,
77+
std::iter::Map<
78+
std::slice::Iter<'a, (String, NodeId)>,
79+
fn(&(String, NodeId)) -> NodeId
80+
>
81+
>
82+
)
83+
84+
}
85+
86+
impl<'a> Iterator for Children<'a> {
87+
type Item = NodeId;
88+
fn next(&mut self) -> Option<Self::Item> {
89+
match self {
90+
Children::None(i) => i.next(),
91+
Children::One(i) => i.next(),
92+
Children::Two(i) => i.next(),
93+
Children::Three(i) => i.next(),
94+
Children::Slice(i) => i.next(),
95+
Children::Chain(i) => i.next(),
96+
Children::ChainOpt(i) => i.next(),
97+
Children::FuncChain(i) => i.next(),
98+
}
99+
}
100+
}
101+
102+
match self {
103+
Unary { expr: id, .. }
104+
| Loop { block: id }
105+
| ExprStmt { expr: id }
106+
| UseDecl { use_tree: id , ..}
107+
| TupleIndexExpression { tuple: id , .. }
108+
| TypeSlice { ty: id }
109+
| Return { expr: Some(id) }
110+
| Break { expr: Some(id) }
111+
| UsePath {tree: id , ..} => Children::One(std::iter::once(*id)),
112+
113+
Block { nodes: ids }
114+
| Tuple { elements: ids }
115+
| Array { elements: ids }
116+
| PathExpression { segments: ids }
117+
| TypePath { segments: ids }
118+
| TypeTuple { elements: ids }
119+
| UseGroup { trees: ids }
120+
| Module { items: Some(ids) , .. }
121+
| PathSegment {args: ids , ..} => Children::Slice(ids.iter().copied()),
122+
123+
Binary { lhs: id0, rhs: id1, .. }
124+
| ArrayRepeat { value: id0, count: id1 }
125+
| IndexExpression { array: id0 , index: id1 }
126+
| If { cond: id0, then_block: id1, else_block: None }
127+
| TypeArray { ty: id0, len: id1 } => Children::Two([*id0, *id1].into_iter()),
128+
129+
Call { callee: id, args: ids } => Children::Chain(std::iter::once(*id).chain(ids.iter().copied())),
130+
131+
If { cond, then_block, else_block: Some(else_block) } => Children::Three(
132+
[*cond, *then_block, *else_block].into_iter()
133+
),
134+
135+
LetStmt {ty: optid0, value: optid1 , .. } => Children::ChainOpt(
136+
optid0.iter().copied().chain(optid1.iter().copied())
137+
),
138+
139+
Function { params, return_type: optid, body: id, .. } => {
140+
let map_fn: fn(&(String, NodeId)) -> NodeId = |(_, id)| *id;
141+
Children::FuncChain(
142+
std::iter::once(*id)
143+
.chain(optid.iter().copied())
144+
.chain(params.iter().map(map_fn))
145+
)
146+
}
147+
148+
Return { expr: None }
149+
| Break { expr: None }
150+
| Module { items: None, .. }
151+
| Literal(_)
152+
| UnderscoreExpr
153+
| ErrorExpr
154+
| ErrorType
155+
| EmptyStmt
156+
| UseName { .. }
157+
| UseRename { .. }
158+
| UseGlob => Children::None(std::iter::empty()),
159+
}
160+
}
161+
162+
/// returns an iterator over the children of a node for in place mutation
163+
pub fn children_mut(&mut self) -> impl Iterator<Item = &mut NodeId> {
164+
use NodeKind::*;
165+
166+
enum ChildrenMut<'a> {
167+
None(std::iter::Empty<&'a mut NodeId>),
168+
One(std::iter::Once<&'a mut NodeId>),
169+
Two(std::array::IntoIter<&'a mut NodeId, 2>),
170+
Three(std::array::IntoIter<&'a mut NodeId, 3>),
171+
Slice(std::slice::IterMut<'a, NodeId>),
172+
Chain(std::iter::Chain<std::iter::Once<&'a mut NodeId>, std::slice::IterMut<'a, NodeId>>),
173+
ChainOpt(std::iter::Chain<std::option::IterMut<'a, NodeId>, std::option::IterMut<'a, NodeId>>),
174+
FuncChain(
175+
std::iter::Chain<
176+
std::iter::Chain<
177+
std::iter::Once<&'a mut NodeId>,
178+
std::option::IterMut<'a, NodeId>
179+
>,
180+
std::iter::Map<
181+
std::slice::IterMut<'a, (String, NodeId)>,
182+
fn(&'a mut (String, NodeId)) -> &'a mut NodeId
183+
>
184+
>
185+
)
186+
187+
}
188+
189+
impl<'a> Iterator for ChildrenMut<'a> {
190+
type Item = &'a mut NodeId;
191+
fn next(&mut self) -> Option<Self::Item> {
192+
match self {
193+
ChildrenMut::None(i) => i.next(),
194+
ChildrenMut::One(i) => i.next(),
195+
ChildrenMut::Two(i) => i.next(),
196+
ChildrenMut::Three(i) => i.next(),
197+
ChildrenMut::Slice(i) => i.next(),
198+
ChildrenMut::Chain(i) => i.next(),
199+
ChildrenMut::ChainOpt(i) => i.next(),
200+
ChildrenMut::FuncChain(i) => i.next(),
201+
}
202+
}
203+
}
204+
205+
match self {
206+
Unary { expr: id, .. }
207+
| Loop { block: id }
208+
| ExprStmt { expr: id }
209+
| UseDecl { use_tree: id , ..}
210+
| TupleIndexExpression { tuple: id , .. }
211+
| TypeSlice { ty: id }
212+
| Return { expr: Some(id) }
213+
| Break { expr: Some(id) }
214+
| UsePath {tree: id , ..} => ChildrenMut::One(std::iter::once(id)),
215+
216+
Block { nodes: ids }
217+
| Tuple { elements: ids }
218+
| Array { elements: ids }
219+
| PathExpression { segments: ids }
220+
| TypePath { segments: ids }
221+
| TypeTuple { elements: ids }
222+
| UseGroup { trees: ids }
223+
| Module { items: Some(ids) , .. }
224+
| PathSegment {args: ids , ..} => ChildrenMut::Slice(ids.iter_mut()),
225+
226+
Binary { lhs: id0, rhs: id1, .. }
227+
| ArrayRepeat { value: id0, count: id1 }
228+
| IndexExpression { array: id0 , index: id1 }
229+
| If { cond: id0, then_block: id1, else_block: None }
230+
| TypeArray { ty: id0, len: id1 } => ChildrenMut::Two([id0, id1].into_iter()),
231+
232+
Call { callee: id, args: ids } => ChildrenMut::Chain(std::iter::once(id).chain(ids.iter_mut())),
233+
234+
If { cond, then_block, else_block: Some(else_block) } => ChildrenMut::Three(
235+
[cond, then_block, else_block].into_iter()
236+
),
237+
238+
LetStmt {ty: optid0, value: optid1 , .. } => ChildrenMut::ChainOpt(
239+
optid0.iter_mut().chain(optid1.iter_mut())
240+
),
241+
242+
Function { params, return_type: optid, body: id, .. } => {
243+
let map_fn: for<'a> fn(&'a mut (String, NodeId)) -> &'a mut NodeId = |(_, id)| id;
244+
ChildrenMut::FuncChain(
245+
std::iter::once(id)
246+
.chain(optid.iter_mut())
247+
.chain(params.iter_mut().map(map_fn))
248+
)
249+
}
250+
251+
Return { expr: None }
252+
| Break { expr: None }
253+
| Module { items: None, .. }
254+
| Literal(_)
255+
| UnderscoreExpr
256+
| ErrorExpr
257+
| ErrorType
258+
| EmptyStmt
259+
| UseName { .. }
260+
| UseRename { .. }
261+
| UseGlob => ChildrenMut::None(std::iter::empty()),
262+
}
263+
}
264+
}
57265

58266
#[derive(Debug, Clone)]
59267
pub struct Node {
60268
pub kind: NodeKind,
61269
pub span: Span,
62270
}
271+
impl Node {
272+
/// get the children of a node
273+
pub fn children(&self) -> impl Iterator<Item = NodeId> {
274+
self.kind.children()
275+
}
276+
277+
/// get the children of a node for in place mutation
278+
pub fn children_mut(&mut self) -> impl Iterator<Item = &mut NodeId> {
279+
self.kind.children_mut()
280+
}
281+
}
63282

64283
#[derive(Debug, Clone)]
65284
pub enum Literal {

0 commit comments

Comments
 (0)