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

How can I use the visitor pattern? #333

Open
moar55 opened this issue Jan 2, 2023 · 5 comments
Open

How can I use the visitor pattern? #333

moar55 opened this issue Jan 2, 2023 · 5 comments

Comments

@moar55
Copy link

moar55 commented Jan 2, 2023

If I understand correctly, implementing a visitor pattern when traversing the ast is now supported. Is there some example showing how to create the visitor functions and use them?

@Mingun
Copy link
Member

Mingun commented Jan 2, 2023

The visitor pattern always was the way how the AST is processed in Peggy. I think, the best way to understand is to check the passes folder of this repository.

@hildjj
Copy link
Contributor

hildjj commented Feb 22, 2023

I left this open in case we wanted to document the visitor API. I don't currently want to do that because I want to replace the AST and code generation bits. I'm still leaving this open for the moment in case that change doesn't end up working.

@AndrewRayCode
Copy link
Contributor

I don't think the internal visitors file should be documented nor part of the public API. Implementing a visitor module is up to consumers of peggy, and it depends on what kind of AST your parser produces. Babel visitors, for example, have the concept of a "path", with lots of functionality like removing nodes, adding nodes, and skipping nodes, as part of visiting, all of which are up to the consumer to implement.

@hildjj
Copy link
Contributor

hildjj commented Mar 15, 2023

I mostly agree with the above arguments about not documenting the visitor pattern. However, we do provide a plugin capability that is documented, and one of the most obvious things to do in a plugin is to visit the AST.

@mikeaustin
Copy link

When I started using Pegjs/Peggy, I too wasn't sure what the best approach to generate and traverse the AST was. It can be dead simple, this is what I've been doing lately for small projects – a simple evaluate function that invokes nodes by type. I think a short blurb in the documentation about how to write visitors would help get people started more quickly, if there isn't one already.

{
  const visitors = {
    AddExpression: ({ leftExpression, rightExpression }) => {
      return evaluate(leftExpression) + evaluate(rightExpression);
    },

    NumericLiteral: ({ value }) => {
      return value;
    }
  }

  function evaluate(node) {
    return visitors[node.type](node);
  }
}

Program
  = expression:Expression {
      return evaluate(expression);
    }

https://github.com/mikeaustin/kopi/blob/develop/packages/kopi/test/parser3/sample50.pegjs

I've been toying with writing an interactive article about how to write a programming language, using only Peggy (and markdown). At https://mike-austin.com/react-desktop, open the "Let's Build a Language.md" in the Files app. My language, Kopi uses Peggy which you can try in the Terminal app, or in the interactive markdown files such as "Learning Kopi.md"

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

No branches or pull requests

5 participants