Skip to content
DevBridge
Module 07 · AST Lab

AST Lab

A from-scratch JS-subset compiler: tokenizer, recursive-descent parser, AST transformer with composable visitors, and code generator. No Babel, no Acorn.

Hand-written. ~900 LoC across 5 core files. Pure functions; no framework.
tokens: 114nodes: 54parse: 2.14mstokenize: 1.57mstransform: 0.01mscodegen: 0.38msin: 340Bout: 336B+0 / -0
rename:
Sourceeditable
Tokens114 tokens
constgreeting="hello";constaudience="world";functionadd(a,b){returna+b;}constsum=add(21,21);console.log(`${greeting}, ${audience} — sum=${sum}`);constitems=[1,2,3].map(x=>x*2);constmeta={lang:"demo",count:items.length};if(sum>40){console.log("big");}else{console.log("small");}eof
AST tree54 nodes · live
Program
VarDecl
greeting
StringLit
hello
VarDecl
audience
StringLit
world
FuncDecl
add
Param
a
Param
b
BlockStmt
ReturnStmt
BinaryExpr
VarDecl
sum
CallExpr
Identifier
add
NumberLit
21
NumberLit
21
ExprStmt
CallExpr
MemberExpr
Identifier
console
TemplateLit
Identifier
greeting
Identifier
audience
Identifier
sum
VarDecl
items
CallExpr
MemberExpr
ArrayLit
ArrowFn
Param
x
BinaryExpr
VarDecl
meta
ObjectLit
Property
StringLit
demo
Property
MemberExpr
IfStmt
BinaryExpr
Identifier
sum
NumberLit
40
BlockStmt
ExprStmt
CallExpr
BlockStmt
ExprStmt
CallExpr
Output336B · +0/-0