|
1 | 1 | ## Abstract Syntax Tree (AST)
|
2 | 2 |
|
3 | | -AST comes into picture when we want to go from the string representation of our program like `"-1"` or `"1 + 2"` to something more manageable and easier to work with. Since our program is not a random string (the grammar is for), we can use the structure within the expressions like `"-1"` and `"1 + 2"` to our own advantage and come up with a *new representation* like a [tree](https://en.wikipedia.org/wiki/Tree_structure) |
| 3 | +AST comes into picture when we want to go from the string representation of our program like `"-1"` or `"1 + 2"` to something more manageable and easier to work with. Since our program is not a random string (the grammar is for), we can use the structure within the expressions `"-1"` and `"1 + 2"` to our own advantage and come up with a *new representation* like a [tree](https://en.wikipedia.org/wiki/Tree_structure) |
4 | 4 |
|
5 | 5 | <p align="center">
|
6 | 6 | </br>
|
@@ -73,13 +73,13 @@ in later stages of compilation.
|
73 | 73 |
|
74 | 74 | ## Interpreter
|
75 | 75 |
|
76 | | -CPU is the *ultimate interpreter*. That is, it executes opcodes as it goes. After we have changed the representation (aka *lowered* the representation) of our source code `&str` to AST `Node` the a basic interpreter looks and each node of the AST (via any [tree traversal methods](https://en.wikipedia.org/wiki/Tree_traversal)) and simply **evaluates** it *recursively* |
| 76 | +CPU is the *ultimate interpreter*. That is, it executes opcodes as it goes. To do that, after we have changed the representation (aka *lowered* the representation) of our source code `&str` to AST `Node`, a basic interpreter looks and each node of the AST (via any [tree traversal methods](https://en.wikipedia.org/wiki/Tree_traversal)) and simply **evaluates** it *recursively* |
77 | 77 |
|
78 | 78 | ```rust,ignore
|
79 | 79 | {{#include ../../../calculator/src/compiler/interpreter.rs:interpreter_eval}}
|
80 | 80 | ```
|
81 | 81 |
|
82 | | -To sum up, we can define a `Compile` trait |
| 82 | +To sum up, we define a `Compile` trait |
83 | 83 |
|
84 | 84 | ```rust,ignore
|
85 | 85 | {{#include ../../../calculator/src/lib.rs:compile_trait}}
|
|
0 commit comments