• Stars
    star
    1,142
  • Rank 40,813 (Top 0.9 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A miniature model of the Typescript compiler, intended to teach the structure of the real Typescript compiler

mini-typescript

A miniature model of the Typescript compiler, intended to teach the structure of the real Typescript compiler

This project contains two models of the compiler: micro-typescript and centi-typescript.

micro-typescript started when I started reading Modern Compiler Implementation in ML because I wanted to learn more about compiler backends. When I started building the example compiler I found I disagreed with the implementation of nearly everything in the frontend. So I wrote my own, and found that I had just written a small Typescript.

I realised a small Typescript would be useful to others who want to learn how the Typescript compiler works. So I rewrote it in Typescript and added some exercises to let you practise with it. micro-typescript is the smallest compiler I can imagine, implementing just a tiny slice of Typescript: var declarations, assignments and numeric literals. The only two types are string and number.

So that's micro-typescript: a textbook compiler that implements a tiny bit of Typescript in a way that's a tiny bit like the Typescript compiler. centi-typescript, on the other hand, is a 1/100 scale model of the Typescript compiler. It's intended as a reference in code for peopple who want to see how the Typescript compiler actually works, without the clutter caused by real-life compatibility and requirements. Currently centi-typescript is most complete in the checker, because most of Typescript's complexity is there.

To get set up

git clone https://github.com/sandersn/mini-typescript
cd mini-typescript
code .

# Get set up
npm i
npm run build

# Or have your changes instantly happen
npm run build --watch

# Run the compiler:
npm run mtsc ./tests/singleVar.ts

To switch to centi-typescript

git checkout centi-typescript
npm run build

Limitations

  1. This is an example of the way that Typescript's compiler does things. A compiler textbook will help you learn compilers. This project will help you learn Typescript's code.
  2. This is only a tiny slice of the language, also unlike a textbook. Often I only put it one instance of a thing, like nodes that introduce a scope, to keep the code size small.
  3. There is no laziness, caching or node reuse, so the checker and transformer code do not teach you those aspects of the design.
  4. There's no surrounding infrastructure, like a language service or a program builder. This is just a model of tsc.

Exercises

  • Add EmptyStatement.
  • Make semicolon a statement ender, not statement separator.
    • Hint: You'll need a predicate to peek at the next token and decide if it's the start of an element.
    • Bonus: Switch from semicolon to newline as statement ender.
  • Add string literals.
  • Add let.
    • Make sure the binder resolves variables declared with var and let the same way. The simplest way is to add a kind property to Symbol.
    • Add use-before-declaration errors in the checker.
    • Finally, add an ES2015 -> ES5 transform that transforms let to var.
  • Allow var to have multiple declarations.
    • Check that all declarations have the same type.
  • Add objects and object types.
    • Type will need to become more complicated.
  • Add interface.
    • Make sure the binder resolves types declared with type and interface the same way.
    • After the basics are working, allow interface to have multiple declarations.
    • Interfaces should have an object type, but that object type should combine the properties from every declaration.
  • Add an ES5 transformer that converts let -> var.
  • Add function declarations and function calls.
  • Add arrow functions with an appropriate transform in ES5.

More Repositories

1

downlevel-dts

Convert a new d.ts to one that works with older versions of Typescript
JavaScript
229
star
2

vue-ts-plugin

Typescript Language Service Plugin for Vue
TypeScript
62
star
3

manual

Explanatory writing
51
star
4

fing

Fing: F# API Search
F#
38
star
5

minits

A miniature model of the Typescript compiler
F#
13
star
6

nltk

NLTK ported to Javascript
TypeScript
12
star
7

chatgpt-infer-ts-types

Use ChatGPT to infer Typescript types from Javascript code
TypeScript
8
star
8

vscode-wasm-typescript

Language server host for typescript using vscode's sync-api in the browser
TypeScript
7
star
9

dialect

Code for "A Statistical Method For Dialectometry"
Python
6
star
10

what-to-name-your-type

The most popular type names on Definitely Typed
JavaScript
4
star
11

elements-of-ai

Example implementations from Elements of Artificial Intelligence by Steven Tanimoto
TypeScript
4
star
12

dt-package-tester

Install @types packages and run their tests from Definitely Typed
JavaScript
4
star
13

typescript-history

A History of TypeScript
HTML
4
star
14

dt-perf

Scripts for analysing Definitely Typed performance on recent versions of Typescript
JavaScript
4
star
15

playground-dts-plugin

d.ts helper plugin for the Typescript playground
JavaScript
3
star
16

grafs

Cormen, Leiserson and Rivest's Introduction to Algorithms implemented in F#
F#
2
star
17

eoc

Essentials of Compilation
TypeScript
2
star
18

sandersn.github.io

Web site
1
star
19

dt-pr-prettier

Run prettier on Definitely Typed in a series of PRs
JavaScript
1
star
20

measure

Measure type system precision and recall of changes to Typescript
JavaScript
1
star
21

introduction-to-ai

Example implementations from Introduction to Artificial Intelligence by Charniak and McDermott
1
star
22

visual-basic

Ancient Visual Basic code I wrote when I was a teenager
VBA
1
star
23

recipes

Favourite recipes and tools to buy ingredients for them
JavaScript
1
star
24

pursuit-workshop

Pursuit open source workshop
1
star
25

typescript-version-replay

Test code with successive versions of Typescript
JavaScript
1
star
26

type_script

A toy compiler for a toy language
Rust
1
star