mirror of
https://github.com/jamiebuilds/the-super-tiny-compiler.git
synced 2024-10-27 20:34:08 +00:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
d8d4013045 | ||
|
f0321d5b1d | ||
|
4f7a85a9f8 | ||
|
386d1efd5b | ||
|
c5979db026 | ||
|
3aa9f63aa8 | ||
|
bc2898670e | ||
|
dee8b924ea | ||
|
a7f50205ee | ||
|
ca64396847 |
@ -1,4 +1,4 @@
|
||||
<a href="the-super-tiny-compiler.js"><img width="731" alt="THE SUPER TINY COMPILER" src="https://cloud.githubusercontent.com/assets/952783/14171276/ed7bf716-f6e6-11e5-96df-80a031c2769d.png"/></a>
|
||||
[](the-super-tiny-compiler.js)
|
||||
|
||||
***Welcome to The Super Tiny Compiler!***
|
||||
|
||||
@ -10,6 +10,8 @@ work from end to end.
|
||||
|
||||
### [Want to jump into the code? Click here](the-super-tiny-compiler.js)
|
||||
|
||||
### [You can also check it out on Glitch](https://the-super-tiny-compiler.glitch.me/)
|
||||
|
||||
---
|
||||
|
||||
### Why should I care?
|
||||
@ -32,10 +34,7 @@ file.
|
||||
|
||||
### I'm back, that didn't make sense
|
||||
|
||||
Ouch, I'm really sorry. I'm planning on doing a lot more work on this to add
|
||||
inline annotations. If you want to come back when that's done, you can either
|
||||
watch/star this repo or follow me on
|
||||
[twitter](https://twitter.com/thejameskyle) for updates.
|
||||
Ouch, I'm really sorry. Let me know how it can be improved.
|
||||
|
||||
### Tests
|
||||
|
||||
|
2
test.js
2
test.js
@ -4,7 +4,7 @@ const {
|
||||
transformer,
|
||||
codeGenerator,
|
||||
compiler,
|
||||
} = require('./super-tiny-compiler');
|
||||
} = require('./the-super-tiny-compiler');
|
||||
const assert = require('assert');
|
||||
|
||||
const input = '(add 2 (subtract 4 2))';
|
||||
|
@ -259,7 +259,7 @@
|
||||
*
|
||||
* If we were manipulating this AST directly, instead of creating a separate AST,
|
||||
* we would likely introduce all sorts of abstractions here. But just visiting
|
||||
* each node in the tree is enough.
|
||||
* each node in the tree is enough for what we're trying to do.
|
||||
*
|
||||
* The reason I use the word "visiting" is because there is this pattern of how
|
||||
* to represent operations on elements of an object structure.
|
||||
@ -275,8 +275,8 @@
|
||||
* CallExpression() {},
|
||||
* };
|
||||
*
|
||||
* When we traverse our AST we will call the methods on this visitor whenever we
|
||||
* encounter a node of a matching type.
|
||||
* When we traverse our AST, we will call the methods on this visitor whenever we
|
||||
* "enter" a node of a matching type.
|
||||
*
|
||||
* In order to make this useful we will also pass the node and a reference to
|
||||
* the parent node.
|
||||
@ -286,10 +286,8 @@
|
||||
* CallExpression(node, parent) {},
|
||||
* };
|
||||
*
|
||||
* We call these functions when we "enter" the node. But there is also the
|
||||
* possibilty of calling things on "exit".
|
||||
*
|
||||
* Imagine our tree structure from before in list form:
|
||||
* However, there also exists the possibility of calling things on "exit". Imagine
|
||||
* our tree structure from before in list form:
|
||||
*
|
||||
* - Program
|
||||
* - CallExpression
|
||||
@ -315,7 +313,7 @@
|
||||
* <- CallExpression (exit)
|
||||
* <- Program (exit)
|
||||
*
|
||||
* In order to supper that, our visitors will look like this:
|
||||
* In order to support that, the final form of our visitor will look like this:
|
||||
*
|
||||
* var visitor = {
|
||||
* NumberLiteral: {
|
||||
@ -335,7 +333,7 @@
|
||||
*
|
||||
* Code generators work several different ways, some compilers will reuse the
|
||||
* tokens from earlier, others will have created a separate representation of
|
||||
* the code so that they can print node linearly, but from what I can tell most
|
||||
* the code so that they can print nodes linearly, but from what I can tell most
|
||||
* will use the same AST we just created, which is what we’re going to focus on.
|
||||
*
|
||||
* Effectively our code generator will know how to “print” all of the different
|
||||
@ -711,16 +709,31 @@ function parser(tokens) {
|
||||
* encounter a node with a matching type.
|
||||
*
|
||||
* traverse(ast, {
|
||||
* Program(node, parent) {
|
||||
* // ...
|
||||
* Program: {
|
||||
* enter(node, parent) {
|
||||
* // ...
|
||||
* },
|
||||
* exit(node, parent) {
|
||||
* // ...
|
||||
* },
|
||||
* },
|
||||
*
|
||||
* CallExpression(node, parent) {
|
||||
* // ...
|
||||
* CallExpression: {
|
||||
* enter(node, parent) {
|
||||
* // ...
|
||||
* },
|
||||
* exit(node, parent) {
|
||||
* // ...
|
||||
* },
|
||||
* },
|
||||
*
|
||||
* NumberLiteral(node, parent) {
|
||||
* // ...
|
||||
* NumberLiteral: {
|
||||
* enter(node, parent) {
|
||||
* // ...
|
||||
* },
|
||||
* exit(node, parent) {
|
||||
* // ...
|
||||
* },
|
||||
* },
|
||||
* });
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user