|
17 | 17 | <link rel="stylesheet" href="../css/variables.css"> |
18 | 18 | <link rel="stylesheet" href="../css/general.css"> |
19 | 19 | <link rel="stylesheet" href="../css/chrome.css"> |
20 | | - <link rel="stylesheet" href="../css/print.css" media="print"> |
21 | 20 |
|
22 | 21 | <!-- Fonts --> |
23 | 22 | <link rel="stylesheet" href="../FontAwesome/css/font-awesome.css"> |
@@ -145,9 +144,6 @@ <h2 class="mdbook-help-title">Keyboard shortcuts</h2> |
145 | 144 | <h1 class="menu-title">Create Your Own Programming Language with Rust</h1> |
146 | 145 |
|
147 | 146 | <div class="right-buttons"> |
148 | | - <a href="../print.html" title="Print this book" aria-label="Print this book"> |
149 | | - <i id="print-button" class="fa fa-print"></i> |
150 | | - </a> |
151 | 147 | <a href="https://github.com/ehsanmok/create-your-own-lang-with-rust" title="Git repository" aria-label="Git repository"> |
152 | 148 | <i id="git-repository-button" class="fa fa-github"></i> |
153 | 149 | </a> |
@@ -183,6 +179,9 @@ <h1 class="menu-title">Create Your Own Programming Language with Rust</h1> |
183 | 179 | <div id="content" class="content"> |
184 | 180 | <main> |
185 | 181 | <h2 id="abstract-syntax-tree-ast"><a class="header" href="#abstract-syntax-tree-ast">Abstract Syntax Tree (AST)</a></h2> |
| 182 | +<blockquote> |
| 183 | +<p>Think of source code as a <em>sentence</em> and the AST as its <em>diagram</em>. Just like “The cat sat on the mat” can be diagrammed into subject/verb/object, <code>1 + 2</code> can be diagrammed into left/operator/right. The AST is that diagram - it captures <em>structure</em>, not just text.</p> |
| 184 | +</blockquote> |
186 | 185 | <p>AST comes into the picture when we want to go from the string representation of our program like <code>"-1"</code> or <code>"1 + 2"</code> to something more manageable and easier to work with. Since our program is not a random string (that’s what the grammar ensures), we can use the structure within the expressions <code>"-1"</code> and <code>"1 + 2"</code> to our advantage and come up with a <em>new representation</em> like a <a href="https://en.wikipedia.org/wiki/Tree_structure">tree</a>:</p> |
187 | 186 | <p align="center"> |
188 | 187 | </br> |
@@ -318,6 +317,24 @@ <h2 id="interpreter"><a class="header" href="#interpreter">Interpreter</a></h2> |
318 | 317 | <p>Run such tests locally with</p> |
319 | 318 | <pre><code class="language-bash">cargo test interpreter --tests |
320 | 319 | </code></pre> |
| 320 | +<div class="checkpoint"> |
| 321 | +<strong>Checkpoint</strong> |
| 322 | +<p>At this point, you should be able to:</p> |
| 323 | +<ul> |
| 324 | +<li>Parse <code>1 + 2</code> and build an AST with a <code>Binary</code> node</li> |
| 325 | +<li>Evaluate <code>1 + 2 - 3</code> and get <code>0</code></li> |
| 326 | +<li>Handle nested expressions like <code>-1 + (2 + 3)</code></li> |
| 327 | +</ul> |
| 328 | +</div> |
| 329 | +<div class="related-topics"> |
| 330 | +<strong>Related Topics</strong> |
| 331 | +<ul> |
| 332 | +<li><a href="./grammar_lexer_parser.html">Grammar and Parser</a> - How input becomes tokens</li> |
| 333 | +<li><a href="./jit_intro.html">JIT Compilation</a> - Compile AST to machine code</li> |
| 334 | +<li><a href="./vm.html">Virtual Machine</a> - Compile AST to bytecode</li> |
| 335 | +<li><a href="../02_firstlang/functions.html">Firstlang AST</a> - AST for a full language</li> |
| 336 | +</ul> |
| 337 | +</div> |
321 | 338 |
|
322 | 339 | </main> |
323 | 340 |
|
|
0 commit comments