background

TypeScript is now standard with front-end projects, but TypeScript is really only used for static type checking of front-end code. It doesn’t really have its own compiler or runtime. Front-end projects compile and package TypeScript into JavaScript.

Even deno, which claims to be an alternative to Node.js, has TSC built in, and internally executes TypeScript using TSC compiled to JavaScript and then executed using v8.

Of course, there’s a lot of discussion about the TypeScript runtime, for example

  • As TypeScript continues to grow in popularity, will there be runtimes that run directly to TypeScript

My goal

As a front-end engineer who likes to mess around, I decided to do something fun.

I’m going to build a compiler that compiles TypeScript code into executables, just like GCC compiles C/C++ code into executables.

Feasibility analysis

TypeScript has been around for seven years since its inception in 2013, and the language has a lot of features. And from a programming language perspective, there are some unsound parts of TypeScript, such as cross types and union types, so supporting all of TypeScript’s features is not practical. My goal is simple: enough

Results show

It took four months, but I finally made it.

The code is placed in the repository below

Github.com/StaticScrip…

The installation

Ubuntu

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/StaticScript/StaticScript/master/install-ubuntu.sh)"
Copy the code

or

wget https://raw.githubusercontent.com/StaticScript/StaticScript/master/install-ubuntu.sh
sudo chmod +x install-ubuntu.sh
sudo /bin/bash install-ubuntu.sh
Copy the code

For other Linux distributions, you will need to modify the installation script to install properly

The installation script requires sudo permission

macOS

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/StaticScript/StaticScript/master/install-macos.sh)"
Copy the code

or

wget https://raw.githubusercontent.com/StaticScript/StaticScript/master/install-macos.sh
sudo chmod +x install-macos.sh
sudo /bin/bash install-macos.sh
Copy the code

The installation script requires sudo permission

Windows

Temporary does not support

use

Start by writing a valid StaticScript code file like the one below

// test.ss

let content: string = "Hello World";

ss_println_string(content);
Copy the code

Then execute the following command from the command line

staticscript test.ss -o test.exe
./test.exe
Copy the code

Let’s try to write a Fibonacci column function

Let’s take the sum of 1 minus 100

Great!

Technical analysis

I used Antlr and LLVM when making this compiler

  • Antlr: Lexical analysis & grammatical analysis
  • LLVM: Generate IR and then generate the target object file

The whole project workflow is as follows

The source code of this project is friendly to beginners (I am a beginner myself), interested partners can look at the source code I wrote oh, make suggestions.

Subsequent updates

StaticScript currently only supports basic TypeScript features, with some advanced features being actively developed

Friends are welcome to join the development, also welcome to ask questions and suggestions

Finally finally, saw so much, give a star✨ let me continue to develop more power bar!

Github.com/StaticScrip…