Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/visitor macro #211

Merged
merged 16 commits into from
Dec 20, 2022
Merged

Refactor/visitor macro #211

merged 16 commits into from
Dec 20, 2022

Conversation

b-studios
Copy link
Collaborator

@b-studios b-studios commented Dec 18, 2022

Start implementing macro to generate tree traversals by simple structural recursion.

@marzipankaiser we talked about this. I am not yet sure the gains are worth the additional complexity. It was fun to implement, though.

Gains:

  • much more friendly with respect to refactorings
  • much easier to also add rewrites for types
  • impossible to forget cases (it is easily possible to forget whole non-terminals, though)
  • very easy to add a new IR (and rewrites).
  • saved a few lines (though, this is not the main motivation)

Problems:

  • impossible to understand
  • very difficult to debug (I added a couple of comments and commands to help with that, though)

@b-studios b-studios marked this pull request as ready for review December 19, 2022 13:11
@b-studios
Copy link
Collaborator Author

b-studios commented Dec 19, 2022

I think I prefer manually writing recursive functions and then sprinkling-in the recursive macro for boring cases. Like in:

enum Exp { case Add(l: Exp, r: Exp); case Sub(l: Exp, r: Exp); case Lit(n: Int) }
export Exp.*

object Optimizations extends Structural {
  def dropAddZero(e: Exp): Exp = e match {
    case Add(l, Lit(0)) => dropAddZero(l)
    case Add(Lit(0), r) => dropAddZero(r)
    case e => structuralRewrite(e)
  }
}

@main def demo() = println(Optimizations.dropAddZero(Add(Lit(0), Add(Lit(1), Lit(0)))))

@b-studios
Copy link
Collaborator Author

If there are no other voices about this PR, I would go ahead and merge it. You may speak now or forever hold your peace.

@b-studios
Copy link
Collaborator Author

I take this as a "OK" :)

@b-studios b-studios merged commit e1367ba into master Dec 20, 2022
@b-studios b-studios deleted the refactor/visitor-macro branch December 20, 2022 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant