-
-
Notifications
You must be signed in to change notification settings - Fork 59
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
Conversation
|
||
```ruby | ||
require "syntax_tree" | ||
tree = SyntaxTree.parse("puts 1+1") |
There was a problem hiding this comment.
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:
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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
.
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")))) | ||
``` |
There was a problem hiding this comment.
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"))))]
```
496540d
to
cdf6a9e
Compare
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 |
Thanks for the review friends! I'm closing this for now since I'm not working on it. |
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.