Skip to content

Add simple tutorial on common APIs #38

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

Closed
wants to merge 1 commit into from

Conversation

wildmaples
Copy link
Contributor

I know you were looking for documentation on the visitor pattern, but I felt like this simple tutorial could make SyntaxTree more accessible for newbies. I've added a section for the visitor pattern, which I can work on next.

I'm not sure if the content good enough, so please give me feedback on that.

I chose to put it in a separate doc in the root folder because I don't want to crowd the main README doc.


```ruby
require "syntax_tree"
tree = SyntaxTree.parse("puts 1+1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use the name of the node we get here:

Suggested change
tree = SyntaxTree.parse("puts 1+1")
program = SyntaxTree.parse("puts 1+1")

This will help in the following example to explain where statements is coming from.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could also do a pp program and show the output in this doc so readers can follow along which nodes we're accessing? Or just show the output like you did in other code snippets:

# => (program (statements (command (ident "puts") (args ((binary (int "1") :+ (int "1")))))))

tree = SyntaxTree.parse("puts 1+1")
```

Everything with a block of code inside of it has a list of statements represented by a `SyntaxTree::Statements` node.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here for example it's confusing if you don't know what kind of node is tree.

Comment on lines +30 to +40
Using `#child_nodes` we can get an array of child nodes for any particular `SyntaxTree::Node`. In this case, the command node's child nodes are the method name and the arguments.

We are only interested in the arguments, so we can use the instance method `#arguments` to access the `Syntax::Args` node directly.

```ruby
puts_command.child_nodes
# => [(ident "puts"), (args ((binary (int "1") :+ (int "1"))))]

args = puts_command.arguments
# => (args ((binary (int "1") :+ (int "1"))))
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should say this the other way around. The important info here is that you can access #arguments. #child_nodes is more of a helper method.

Here, we are only interested in the arguments, so we can use the instance method `#arguments` to access the `Syntax::Args` node directly.

```ruby
args = puts_command.arguments
# => (args ((binary (int "1") :+ (int "1"))))
```

Note that we can also get an array of child nodes for any particular `SyntaxTree::Node` using `#child_nodes`. In this case, the command node's child nodes would be the method name and the arguments.

```ruby
puts_command.child_nodes
# => [(ident "puts"), (args ((binary (int "1") :+ (int "1"))))]
```

@kddnewton kddnewton force-pushed the main branch 6 times, most recently from 496540d to cdf6a9e Compare April 13, 2022 01:06
@kddnewton
Copy link
Member

I like this PR very much!

I added a bunch of documentation to the README that covers a bit of this, but certainly not all of it. I was wondering what you thought about creating an examples/ directory at the root and including this in it. It would be something like examples/basic.md or examples/operator.md or something. That way we could create other ones for other use cases.

@wildmaples
Copy link
Contributor Author

Thanks for the review friends! I'm closing this for now since I'm not working on it.

@wildmaples wildmaples closed this Jun 10, 2022
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.

3 participants