Skip to content

Commit ded1582

Browse files
committed
deploy: 51e000f
1 parent da1d0c3 commit ded1582

6 files changed

Lines changed: 148 additions & 116 deletions

File tree

404.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ <h1 class="menu-title">Create Your Own Programming Language with Rust</h1>
179179

180180
<div id="content" class="content">
181181
<main>
182-
182+
<h1 id="document-not-found-404"><a class="header" href="#document-not-found-404">Document not found (404)</a></h1>
183+
<p>This URL is invalid, sorry. Please use the navigation bar or search to continue.</p>
184+
183185
</main>
184186

185187
<nav class="nav-wrapper" aria-label="Page navigation">

img/logo_dark_mode.png

1.15 MB
Loading

img/logo_light_mode.png

1.18 MB
Loading

index.html

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -181,50 +181,61 @@ <h1 class="menu-title">Create Your Own Programming Language with Rust</h1>
181181
<div class="license-notice">
182182
<p><em>Materials in this book are distributed under the terms of <a href="https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/LICENSE">Creative Commons BY-NC-SA 4.0</a></em></p>
183183
<img alt="license" src="./img/by-nc-sa.png">
184+
<picture>
185+
<source media="(prefers-color-scheme: dark)" srcset="./img/logo_dark_mode.png">
186+
<source media="(prefers-color-scheme: light)" srcset="./img/logo_light_mode.png">
187+
<img src="./img/logo_dark_mode.png" alt="CreateLang.rs Logo" class="logo-img" width="320">
188+
</picture>
184189
</div>
185-
<p>This book assumes some basic knowledge of the Rust language. Please take a look at the official <a href="https://doc.rust-lang.org/book/">Rust book</a>.</p>
186-
<p>The accompanying code and materials for this book are available on <a href="https://github.com/ehsanmok/create-your-own-lang-with-rust">GitHub</a>. To follow along, make sure you have</p>
187-
<ul>
188-
<li>
189-
<p><a href="https://www.rust-lang.org/tools/install">Rust toolchain installed</a> (stable 1.70+)</p>
190-
</li>
191-
<li>
192-
<p>Cloned the repository</p>
193-
<pre><code class="language-bash">git clone https://github.com/ehsanmok/create-your-own-lang-with-rust
190+
<hr />
191+
<h2 id="getting-started"><a class="header" href="#getting-started">Getting Started</a></h2>
192+
<p>This book assumes basic knowledge of Rust. If you’re new to Rust, start with the official <a href="https://doc.rust-lang.org/book/">Rust book</a>.</p>
193+
<p>The code and materials are available on <a href="https://github.com/ehsanmok/create-your-own-lang-with-rust">GitHub</a>. To follow along:</p>
194+
<pre><code class="language-bash"># Clone the repository
195+
git clone https://github.com/ehsanmok/create-your-own-lang-with-rust
196+
cd create-your-own-lang-with-rust
194197
</code></pre>
195-
<p>Then navigate to the project directory:</p>
196-
<pre><code class="language-bash">cd create-your-own-lang-with-rust
198+
<h3 id="what-works-with-stable-rust"><a class="header" href="#what-works-with-stable-rust">What Works with Stable Rust</a></h3>
199+
<div class="table-wrapper"><table><thead><tr><th>Project</th><th>Command</th></tr></thead><tbody>
200+
<tr><td>Calculator (interpreter/VM)</td><td><code>cargo run</code></td></tr>
201+
<tr><td>Firstlang (interpreter)</td><td><code>cargo run</code></td></tr>
202+
</tbody></table>
203+
</div>
204+
<h3 id="what-requires-nightly-rust--llvm"><a class="header" href="#what-requires-nightly-rust--llvm">What Requires Nightly Rust + LLVM</a></h3>
205+
<div class="table-wrapper"><table><thead><tr><th>Project</th><th>Command</th></tr></thead><tbody>
206+
<tr><td>Calculator JIT</td><td><code>rustup run nightly cargo run --features jit</code></td></tr>
207+
<tr><td>Secondlang</td><td><code>rustup run nightly cargo run</code></td></tr>
208+
<tr><td>Thirdlang</td><td><code>rustup run nightly cargo run</code></td></tr>
209+
</tbody></table>
210+
</div>
211+
<h3 id="llvm-setup"><a class="header" href="#llvm-setup">LLVM Setup</a></h3>
212+
<pre><code class="language-bash"># Install nightly Rust
213+
rustup toolchain install nightly
214+
215+
# Install LLVM
216+
# macOS:
217+
brew install llvm
218+
219+
# Debian/Ubuntu: see https://apt.llvm.org/
197220
</code></pre>
198-
</li>
199-
</ul>
200-
<p><strong>What works with stable Rust:</strong></p>
201-
<ul>
202-
<li><strong>Calculator</strong> (interpreter and VM modes) - <code>cargo run</code></li>
203-
<li><strong>Firstlang</strong> (interpreter) - <code>cargo run</code></li>
204-
</ul>
205-
<p><strong>What requires nightly Rust + LLVM:</strong></p>
206-
<ul>
207-
<li><strong>Calculator JIT</strong> - <code>rustup run nightly cargo run --features jit</code></li>
208-
<li><strong>Secondlang</strong> (compiled to native code) - <code>rustup run nightly cargo run</code></li>
209-
<li><strong>Thirdlang</strong> (classes and OOP) - <code>rustup run nightly cargo run</code></li>
210-
</ul>
211-
<p>To use LLVM features:</p>
212-
<ul>
213-
<li>Install nightly: <code>rustup toolchain install nightly</code></li>
214-
<li>Install LLVM: macOS (<code>brew install llvm</code>), Debian/Ubuntu (<a href="https://apt.llvm.org/">apt.llvm.org</a>)</li>
215-
<li>Check your version: <code>llvm-config --version</code></li>
216-
<li>Update <code>Cargo.toml</code> to match: LLVM 20.x uses <code>llvm20-1</code>, LLVM 19.x uses <code>llvm19-1</code>, LLVM 18.x uses <code>llvm18-1</code></li>
217-
</ul>
221+
<p>Check your LLVM version with <code>llvm-config --version</code> and update <code>Cargo.toml</code>:</p>
222+
<div class="table-wrapper"><table><thead><tr><th>LLVM</th><th>inkwell feature</th></tr></thead><tbody>
223+
<tr><td>20.x</td><td><code>llvm20-1</code></td></tr>
224+
<tr><td>19.x</td><td><code>llvm19-1</code></td></tr>
225+
<tr><td>18.x</td><td><code>llvm18-1</code></td></tr>
226+
</tbody></table>
227+
</div>
228+
<hr />
218229
<h2 id="motivations-and-goals"><a class="header" href="#motivations-and-goals">Motivations and Goals</a></h2>
219230
<p>This book arises from my frustration of not finding modern, clear, and concise teaching materials that are readily accessible to beginners like me who want to learn how to create their own programming language.</p>
220-
<p>The following are my guidelines:</p>
221231
<blockquote>
222-
<p>“If you don’t know how <em>compilers</em> work, then you don’t know how computers work” <sup><a href="http://steve-yegge.blogspot.com/2007/06/rich-programmer-food.html?">1</a></sup></p>
232+
<p><em>“If you don’t know how compilers work, then you don’t know how computers work”</em> <sup><a href="http://steve-yegge.blogspot.com/2007/06/rich-programmer-food.html">1</a></sup></p>
223233
</blockquote>
224234
<blockquote>
225-
<p>“If you can’t explain something in simple terms, you don’t understand it” <sup><a href="https://skeptics.stackexchange.com/questions/8742/did-einstein-say-if-you-cant-explain-it-simply-you-dont-understand-it-well-en">2</a></sup></p>
235+
<p><em>“If you can’t explain something in simple terms, you don’t understand it”</em> <sup><a href="https://skeptics.stackexchange.com/questions/8742/did-einstein-say-if-you-cant-explain-it-simply-you-dont-understand-it-well-en">2</a></sup></p>
226236
</blockquote>
227-
<p><span style="font-family:Trebuchet MS">Pedagogically, one of the most effective methods of teaching is co-creating interactively. Introducing the core aspects around the <em>simplest example</em> (here, our calculator language) helps a lot to build knowledge and confidence. For that, we will use mature technologies instead of spending tons of time partially reinventing the wheel and boring the reader.</span></p>
237+
<p><span style="font-family:Trebuchet MS">Pedagogically, one of the most effective methods of teaching is co-creating interactively. Introducing the core aspects around the <em>simplest example</em> (here, our calculator language) helps build knowledge and confidence. We use mature technologies instead of reinventing the wheel.</span></p>
238+
<hr />
228239
<h2 id="learning-progression"><a class="header" href="#learning-progression">Learning Progression</a></h2>
229240
<p>We build four languages, each building on concepts from the previous:</p>
230241
<div class="table-wrapper"><table><thead><tr><th>Language</th><th>Grammar</th><th>New Concepts</th><th>Execution</th></tr></thead><tbody>
@@ -241,58 +252,62 @@ <h3 id="part-i-calculator"><a class="header" href="#part-i-calculator">Part I: C
241252
Term = _{Int | "(" ~ Expr ~ ")" }
242253
...
243254
</code></pre>
244-
<p>This minimal language lets us focus on the fundamentals without distraction: what is a grammar? How does pest generate a parser? What is an AST? We also explore <em>three different backends</em> (interpreter, bytecode VM, JIT) to show that the same AST can be executed in multiple ways.</p>
255+
<p>This minimal language lets us focus on the fundamentals: what is a grammar? How does pest generate a parser? What is an AST? We explore <em>three different backends</em> (interpreter, bytecode VM, JIT) to show that the same AST can be executed in multiple ways.</p>
245256
<h3 id="part-ii-firstlang"><a class="header" href="#part-ii-firstlang">Part II: Firstlang</a></h3>
246-
<p>With the basics understood, we add the features that make a <em>real</em> programming language. The grammar grows to 70 lines, adding:</p>
247-
<pre><code class="language-text">// New: Statements instead of just expressions
257+
<p>With the basics understood, we add features that make a <em>real</em> programming language. The grammar grows to 70 lines:</p>
258+
<pre><code class="language-text">// Statements instead of just expressions
248259
Stmt = { Function | Return | Assignment | Expr }
249260

250-
// New: Functions with parameters
261+
// Functions with parameters
251262
Function = { "def" ~ Identifier ~ "(" ~ Params? ~ ")" ~ Block }
252263

253-
// New: Control flow
264+
// Control flow
254265
Conditional = { "if" ~ "(" ~ Expr ~ ")" ~ Block ~ "else" ~ Block }
255266
WhileLoop = { "while" ~ "(" ~ Expr ~ ")" ~ Block }
256267
</code></pre>
257268
<p>We focus on a single backend (tree-walking interpreter) to deeply understand scoping, call stacks, and recursion. The culminating example is computing Fibonacci recursively.</p>
258269
<h3 id="part-iii-secondlang"><a class="header" href="#part-iii-secondlang">Part III: Secondlang</a></h3>
259270
<p>We add <em>static types</em> and compile to native code. The grammar changes are minimal (just 7 more lines), but the compiler grows significantly:</p>
260-
<pre><code class="language-text">// New: Type annotations
271+
<pre><code class="language-text">// Type annotations
261272
Type = { IntType | BoolType }
262273
TypedParam = { Identifier ~ ":" ~ Type }
263274
ReturnType = { "-&gt;" ~ Type }
264275

265-
// Modified: Functions now have types
276+
// Functions now have types
266277
Function = { "def" ~ Identifier ~ "(" ~ TypedParams? ~ ")" ~ ReturnType? ~ Block }
267278
</code></pre>
268-
<p>This demonstrates a key insight: types are primarily a <em>semantic</em> addition, not a syntactic one. The grammar changes are small, but we need new compiler phases (type checking, type inference) and can now generate efficient native code via LLVM.</p>
279+
<p>Types are primarily a <em>semantic</em> addition, not a syntactic one. The grammar changes are small, but we need new compiler phases (type checking, type inference) and can now generate efficient native code via LLVM.</p>
269280
<h3 id="part-iv-thirdlang"><a class="header" href="#part-iv-thirdlang">Part IV: Thirdlang</a></h3>
270281
<p>Finally, we add <em>object-oriented programming</em> with classes, methods, and memory management. The grammar grows to 140 lines:</p>
271-
<pre><code class="language-text">// New: Class definitions
282+
<pre><code class="language-text">// Class definitions
272283
ClassDef = { "class" ~ Identifier ~ "{" ~ ClassBody ~ "}" }
273284
FieldDef = { Identifier ~ ":" ~ Type }
274285
MethodDef = { "def" ~ Identifier ~ "(" ~ SelfParam ~ ... ~ ")" ~ ... ~ Block }
275286

276-
// New: Object operations
287+
// Object operations
277288
NewExpr = { "new" ~ Identifier ~ "(" ~ Args? ~ ")" }
278289
Delete = { "delete" ~ Expr }
279290
</code></pre>
280291
<p>This introduces heap allocation (<code>malloc</code>/<code>free</code>), struct types in LLVM, and the <code>self</code> parameter for methods. We see how OOP features map to lower-level constructs.</p>
292+
<hr />
281293
<h2 id="outline"><a class="header" href="#outline">Outline</a></h2>
282294
<ul>
283-
<li><a href="./crash_course.html">Crash Course on Computing</a> where we briefly set up definitions and foundations</li>
284-
<li><a href="./01_calculator/calc_intro.html"><strong>Calculator</strong></a>: Our first language supporting simple integer addition and subtraction. We use <a href="https://en.wikipedia.org/wiki/Parsing_expression_grammar">PEG</a> to define our grammar, <a href="https://pest.rs/">pest</a> to generate the parser, and explore AST interpretation, JIT compilation with <a href="https://github.com/TheDan64/inkwell">inkwell</a>, and a bytecode VM</li>
285-
<li><a href="./02_firstlang/intro.html"><strong>Firstlang</strong></a>: An interpreted language with variables, functions, control flow, and recursion. We implement <a href="./02_firstlang/fibonacci.html">Fibonacci</a> as the culminating example</li>
286-
<li><a href="./03_secondlang/intro.html"><strong>Secondlang</strong></a>: A statically typed language that compiles to native code via LLVM. We add <a href="./03_secondlang/annotations.html">type annotations</a>, <a href="./03_secondlang/inference.html">type inference</a>, AST <a href="./03_secondlang/optimizations.html">optimization passes</a> with the visitor pattern, and <a href="./03_secondlang/jit_fibonacci.html">JIT compilation</a></li>
287-
<li><a href="./04_thirdlang/intro.html"><strong>Thirdlang</strong></a>: An object-oriented language with <a href="./04_thirdlang/classes_syntax.html">classes</a>, <a href="./04_thirdlang/methods.html">methods</a>, <a href="./04_thirdlang/constructors.html">constructors/destructors</a>, and <a href="./04_thirdlang/memory.html">explicit memory management</a> via <code>new</code> and <code>delete</code></li>
288-
</ul>
289-
<h2 id="donation"><a class="header" href="#donation">Donation</a></h2>
290-
<p>If you have found this book useful, please consider donating to any of the organizations below</p>
291-
<ul>
292-
<li><a href="https://childfoundation.org/">Child Foundation</a></li>
293-
<li><a href="https://blacklivesmatter.com/">Black Lives Matter</a></li>
294-
<li><a href="https://www.foodbankscanada.ca/">Food Bank of Canada</a></li>
295+
<li><a href="./crash_course.html">Crash Course on Computing</a> - definitions and foundations</li>
296+
<li><a href="./01_calculator/calc_intro.html"><strong>Calculator</strong></a> - integer arithmetic with PEG, pest, AST interpretation, JIT compilation, and bytecode VM</li>
297+
<li><a href="./02_firstlang/intro.html"><strong>Firstlang</strong></a> - variables, functions, control flow, recursion, culminating in <a href="./02_firstlang/fibonacci.html">Fibonacci</a></li>
298+
<li><a href="./03_secondlang/intro.html"><strong>Secondlang</strong></a> - <a href="./03_secondlang/annotations.html">type annotations</a>, <a href="./03_secondlang/inference.html">type inference</a>, <a href="./03_secondlang/optimizations.html">optimization passes</a>, and <a href="./03_secondlang/jit_fibonacci.html">JIT compilation</a></li>
299+
<li><a href="./04_thirdlang/intro.html"><strong>Thirdlang</strong></a> - <a href="./04_thirdlang/classes_syntax.html">classes</a>, <a href="./04_thirdlang/methods.html">methods</a>, <a href="./04_thirdlang/constructors.html">constructors/destructors</a>, and <a href="./04_thirdlang/memory.html">memory management</a></li>
295300
</ul>
301+
<hr />
302+
<p align="center">
303+
<strong>Support</strong><br>
304+
If you found this book useful, please consider donating to:
305+
</p>
306+
<p align="center">
307+
<a href="https://mycf.childfoundation.org/s/donate">Child Foundation</a> &bull;
308+
<a href="https://blacklivesmatter.com/">Black Lives Matter</a> &bull;
309+
<a href="https://www.foodbankscanada.ca/">Food Bank of Canada</a>
310+
</p>
296311

297312
</main>
298313

0 commit comments

Comments
 (0)