Skip to content

Commit

Permalink
Internal iterator ref (#41)
Browse files Browse the repository at this point in the history
* make the trait

* try implementing the trait in our AST.

* `InternalIteratorRef` fix (#42)

* I was being `.lock`ed out of testing the code 🙃

* Fix typo in trait/assoc type usage for `.calls_ref()`

* WIP: `git mv src/internal_iterator_re{f,c}.rs`

* WIP: finish renaming `InternalIterator{Ref,Mut}` as `InternalIteratorRec`

* Rename the call-sites (including `Mut`) accordingly

* WIP: Rename `ItemRec` as `Item` to prevent future such mistakes / bad diagnostics

* Adjust the call-sites accordingly

* WIP: Expose new internal `adhoc_internal_iterator_rec!` helper macro

* Profit™

* rustfmt

🥲

---------

Co-authored-by: Daniel Henry-Mantilla <[email protected]>
  • Loading branch information
Lokathor and danielhenrymantilla-codespace authored Jan 13, 2025
1 parent 90af87a commit 36bcd80
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 259 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ logos = "0.14.0"
chumsky = { git = "https://github.com/zesterer/chumsky", features = [
"label",
"pratt",
] }
], rev = "9d1ee8c" }
ariadne = { git = "https://github.com/zesterer/ariadne" }
internal-iterator = "0.2.3"
#rayon = "1.7.0"
74 changes: 19 additions & 55 deletions src/ast/data/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,39 @@ pub struct Function {
impl Function {
pub fn expressions_mut(
&mut self,
) -> impl '_ + InternalIteratorMut<ItemMut = &'_ mut FileSpanned<Expression>>
{
return ExpressionsMut(self);
// where:
struct ExpressionsMut<'r>(&'r mut Function);
impl<'r> InternalIterator for ExpressionsMut<'r> {
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for ExpressionsMut<'r> {
type ItemMut = &'r mut FileSpanned<Expression>;

fn try_for_each_mut<R, F>(self, f: &mut F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.statements.iter_mut() {
stmt.expressions_mut().try_for_each_mut(&mut *f)?;
) -> impl '_ + InternalIteratorRec<Item = &'_ mut FileSpanned<Expression>> {
adhoc_internal_iterator_rec!(
'r, self, |this: &'r mut Function, yield_| -> &'r mut FileSpanned<Expression> {
for stmt in this.statements.iter_mut() {
stmt.expressions_mut().try_for_each_rec(yield_)?;
}
ControlFlow::Continue(())
}
}
)
}

#[cfg(target_os = "none")]
//#[cfg(target_os = "none")]
pub fn calls_ref(
&self,
) -> impl '_ + InternalIterator<Item = &'_ FileSpanned<Call>> {
return CallsRef(self);
// where:
struct CallsRef<'r>(&'r Function);
impl<'r> InternalIterator for CallsRef<'r> {
type Item = &'r FileSpanned<Call>;

fn try_for_each<R, F>(self, mut f: F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.statements.iter() {
stmt.calls_ref().try_for_each(&mut f)?;
) -> impl '_ + InternalIteratorRec<Item = &'_ FileSpanned<Call>> {
adhoc_internal_iterator_rec!(
'r, self, |this: &'r Function, yield_| -> &'r FileSpanned<Call> {
for stmt in this.statements.iter() {
stmt.calls_ref().try_for_each_rec(yield_)?;
}
ControlFlow::Continue(())
}
}
)
}

pub fn calls_mut(
&mut self,
) -> impl '_ + InternalIteratorMut<ItemMut = &'_ mut FileSpanned<Call>> {
return CallsMut(self);
// where:
struct CallsMut<'r>(&'r mut Function);
impl<'r> InternalIterator for CallsMut<'r> {
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for CallsMut<'r> {
type ItemMut = &'r mut FileSpanned<Call>;

fn try_for_each_mut<R, F>(self, f: &mut F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.statements.iter_mut() {
stmt.calls_mut().try_for_each_mut(&mut *f)?;
) -> impl '_ + InternalIteratorRec<Item = &'_ mut FileSpanned<Call>> {
adhoc_internal_iterator_rec!(
'r, self, |this: &'r mut Function, yield_| -> &'r mut FileSpanned<Call> {
for stmt in this.statements.iter_mut() {
stmt.calls_mut().try_for_each_rec(yield_)?;
}
ControlFlow::Continue(())
}
}
)
}

pub fn generate_code(&self) -> Vec<Asm> {
Expand Down
86 changes: 25 additions & 61 deletions src/ast/data/if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,84 +28,48 @@ impl IfElse {

pub fn expressions_mut(
&mut self,
) -> impl '_ + InternalIteratorMut<ItemMut = &'_ mut FileSpanned<Expression>>
{
return ExpressionsMut(self);
// where:
struct ExpressionsMut<'r>(&'r mut IfElse);
impl<'r> InternalIterator for ExpressionsMut<'r> {
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for ExpressionsMut<'r> {
type ItemMut = &'r mut FileSpanned<Expression>;

fn try_for_each_mut<R, F>(self, f: &mut F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
f(&mut self.0.test)?;
for stmt in self.0.if_body.iter_mut() {
stmt.expressions_mut().try_for_each_mut(&mut *f)?;
) -> impl '_ + InternalIteratorRec<Item = &'_ mut FileSpanned<Expression>> {
adhoc_internal_iterator_rec!(
'r, self, |this: &'r mut IfElse, yield_| -> &'r mut FileSpanned<Expression> {
yield_(&mut this.test)?;
for stmt in this.if_body.iter_mut() {
stmt.expressions_mut().try_for_each_rec(yield_)?;
}
for stmt in self.0.else_body.iter_mut() {
stmt.expressions_mut().try_for_each_mut(&mut *f)?;
for stmt in this.else_body.iter_mut() {
stmt.expressions_mut().try_for_each_rec(yield_)?;
}
ControlFlow::Continue(())
}
}
)
}

pub fn calls_ref(
&self,
) -> impl '_ + InternalIterator<Item = &'_ FileSpanned<Call>> {
return CallsRef(self);
// where:
struct CallsRef<'r>(&'r IfElse);
impl<'r> InternalIterator for CallsRef<'r> {
type Item = &'r FileSpanned<Call>;

fn try_for_each<R, F>(self, mut f: F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.if_body.iter() {
stmt.calls_ref().try_for_each(&mut f)?;
) -> impl '_ + InternalIteratorRec<Item = &'_ FileSpanned<Call>> {
adhoc_internal_iterator_rec!(
'r, self, |this: &'r IfElse, yield_| -> &'r FileSpanned<Call> {
for stmt in this.if_body.iter() {
stmt.calls_ref().try_for_each_rec(yield_)?;
}
for stmt in self.0.else_body.iter() {
stmt.calls_ref().try_for_each(&mut f)?;
for stmt in this.else_body.iter() {
stmt.calls_ref().try_for_each_rec(yield_)?;
}
ControlFlow::Continue(())
}
}
)
}

pub fn calls_mut(
&mut self,
) -> impl '_ + InternalIteratorMut<ItemMut = &'_ mut FileSpanned<Call>> {
return CallsMut(self);
// where:
struct CallsMut<'r>(&'r mut IfElse);
impl<'r> InternalIterator for CallsMut<'r> {
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for CallsMut<'r> {
type ItemMut = &'r mut FileSpanned<Call>;

fn try_for_each_mut<R, F>(self, f: &mut F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.if_body.iter_mut() {
stmt.calls_mut().try_for_each_mut(&mut *f)?;
) -> impl '_ + InternalIteratorRec<Item = &'_ mut FileSpanned<Call>> {
adhoc_internal_iterator_rec!(
'r, self, |this: &'r mut IfElse, yield_| -> &'r mut FileSpanned<Call> {
for stmt in this.if_body.iter_mut() {
stmt.calls_mut().try_for_each_rec(yield_)?;
}
for stmt in self.0.else_body.iter_mut() {
stmt.calls_mut().try_for_each_mut(&mut *f)?;
for stmt in this.else_body.iter_mut() {
stmt.calls_mut().try_for_each_rec(yield_)?;
}
ControlFlow::Continue(())
}
}
)
}

pub fn write_code(
Expand Down
72 changes: 18 additions & 54 deletions src/ast/data/loop_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,74 +40,38 @@ impl Loop {

pub fn expressions_mut(
&mut self,
) -> impl '_ + InternalIteratorMut<ItemMut = &'_ mut FileSpanned<Expression>>
{
return ExpressionsMut(self);
// where:
struct ExpressionsMut<'r>(&'r mut Loop);
impl<'r> InternalIterator for ExpressionsMut<'r> {
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for ExpressionsMut<'r> {
type ItemMut = &'r mut FileSpanned<Expression>;

fn try_for_each_mut<R, F>(self, f: &mut F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.statements.iter_mut() {
stmt.expressions_mut().try_for_each_mut(&mut *f)?;
) -> impl '_ + InternalIteratorRec<Item = &'_ mut FileSpanned<Expression>> {
adhoc_internal_iterator_rec!(
'r, self, |this: &'r mut Loop, yield_| -> &'r mut FileSpanned<Expression> {
for stmt in this.statements.iter_mut() {
stmt.expressions_mut().try_for_each_rec(yield_)?;
}
ControlFlow::Continue(())
}
}
)
}

pub fn calls_ref(
&self,
) -> impl '_ + InternalIterator<Item = &'_ FileSpanned<Call>> {
return CallsRef(self);
// where:
struct CallsRef<'r>(&'r Loop);
impl<'r> InternalIterator for CallsRef<'r> {
type Item = &'r FileSpanned<Call>;

fn try_for_each<R, F>(self, mut f: F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.statements.iter() {
stmt.calls_ref().try_for_each(&mut f)?;
) -> impl '_ + InternalIteratorRec<Item = &'_ FileSpanned<Call>> {
adhoc_internal_iterator_rec!(
'r, self, |this: &'r Loop, yield_| -> &'r FileSpanned<Call> {
for stmt in this.statements.iter() {
stmt.calls_ref().try_for_each_rec(yield_)?;
}
ControlFlow::Continue(())
}
}
)
}

pub fn calls_mut(
&mut self,
) -> impl '_ + InternalIteratorMut<ItemMut = &'_ mut FileSpanned<Call>> {
return CallsMut(self);
// where:
struct CallsMut<'r>(&'r mut Loop);
impl<'r> InternalIterator for CallsMut<'r> {
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for CallsMut<'r> {
type ItemMut = &'r mut FileSpanned<Call>;

fn try_for_each_mut<R, F>(self, f: &mut F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.statements.iter_mut() {
stmt.calls_mut().try_for_each_mut(&mut *f)?;
) -> impl '_ + InternalIteratorRec<Item = &'_ mut FileSpanned<Call>> {
adhoc_internal_iterator_rec!(
'r, self, |this: &'r mut Loop, yield_| -> &'r mut FileSpanned<Call> {
for stmt in this.statements.iter_mut() {
stmt.calls_mut().try_for_each_rec(yield_)?;
}
ControlFlow::Continue(())
}
}
)
}

pub fn write_code(
Expand Down
Loading

0 comments on commit 36bcd80

Please sign in to comment.