Giter Club home page Giter Club logo

mini-typescript's Introduction

mini-typescript

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

I started this project as part of 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. The resulting compiler covers a tiny slice of Typescript: just var declarations, assignments and numeric literals. The only two types are string and number.

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.
    • Then add use-before-declaration errors in the checker.
  • Allow var to have multiple declarations.
    • You'll need to convert a Symbol's declaration into a list.
    • Check that all declarations have the same type.
  • Add type aliases.
    • You'll need to convert a Symbol's declaration into a list.
    • Name resolution will need to search a symbol's declarations to see whether it has a type declaration.
  • Add an ES5 transformer that converts let -> var.
  • Add function declarations and function calls.
  • Add arrow functions with an appropriate transform in ES5.

mini-typescript's People

Contributors

sandersn avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.