blakelapierre / TypeScriptCompiler

TypeScript Compiler (by LLVM)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScript Native Compiler

Powered by LLVM|MLIR

Donate

Build

Test Build (Windows) Test Build (Linux)

Demo

(click here)

Demo

Try it

(click here)

Compiler Explorer

Chat Room

Want to chat with other members of the TypeScriptCompiler community?

Join the chat at https://gitter.im/ASDAlexander77/TypeScriptCompiler

Example

abstract class Department {
    constructor(public name: string) {}

    printName(): void {
        print("Department name: " + this.name);
    }

    abstract printMeeting(): void; // must be implemented in derived classes
}

class AccountingDepartment extends Department {
    constructor() {
        super("Accounting and Auditing"); // constructors in derived classes must call super()
    }

    printMeeting(): void {
        print("The Accounting Department meets each Monday at 10am.");
    }

    generateReports(): void {
        print("Generating accounting reports...");
    }
}

function main() {
    let department: Department; // ok to create a reference to an abstract type
    department = new AccountingDepartment(); // ok to create and assign a non-abstract subclass
    department.printName();
    department.printMeeting();
    //department.generateReports(); // error: department is not of type AccountingDepartment, cannot access generateReports
}

Run

tsc --emit=jit example.ts

Result

Department name: Accounting and Auditing
The Accounting Department meets each Monday at 10am.

Compile as JIT

tsc --emit=jit hello.ts

File hello.ts

function main() {
    print("Hello World!");
}

Result

Hello World!

Compile as Binary Executable

On Windows

File tsc-compile.bat

rem set %LLVM% and %TSCBIN%
set FILENAME=%1
set LLVMPATH=C:\TypeScriptCompiler\3rdParty\llvm\release\bin
set TSCPATH=C:\TypeScriptCompiler\__build\tsc\bin
set LIBPATH="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\lib\x64"
set SDKPATH="C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\um\x64"
set UCRTPATH="C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\ucrt\x64"
%TSCPATH%\tsc.exe --emit=jit -nogc -dump-object-file -object-filename=%FILENAME%.o %FILENAME%.ts
%LLVMPATH%\lld.exe -flavor link %FILENAME%.o /libpath:%LIBPATH% /libpath:%SDKPATH% /libpath:%UCRTPATH% /defaultlib:libcmt.lib libvcruntime.lib

Compile

tsc-compile.bat hello

Run

hello.exe

Result

Hello World!

On Linux (Ubuntu 20.04)

File tsc-compile.sh

./tsc --emit=jit -nogc -dump-object-file -object-filename=$1.o $1.ts
gcc -o $1 $1.o

Compile

sh -f tsc-compile.sh hello

Run

./hello

Result

Hello World!

Compiling as WASM

On Windows

File tsc-compile-wasm.bat

rem set %LLVM% and %TSCBIN%
set LLVMPATH=%LLVM%\llvm\release\bin
set TSCPATH=%TSCBIN%\tsc\bin
%TSCPATH%\tsc.exe --emit=llvm -nogc %FILENAME%.ts 2>%FILENAME%.ll
%LLVMPATH%\llc.exe -mtriple=wasm32-unknown-unknown -O3 --filetype=obj -o=%FILENAME%.o %FILENAME%.ll
%LLVMPATH%\wasm-ld.exe %FILENAME%.o -o %FILENAME%.wasm --no-entry --export-all --allow-undefined

Compile

tsc-compile-wasm.bat hello

Run run.html

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <script type="module">
let buffer;

const config = {
    env: {
        memory_base: 0,
        table_base: 0,
        memory : new WebAssembly.Memory({ initial: 256}),
        table: new WebAssembly.Table({
            initial: 0,
            element: 'anyfunc',
        })
    }
};

fetch("./hello.wasm")
    .then(response =>{
        return response.arrayBuffer();
    })
    .then(bytes => {
        return WebAssembly.instantiate(bytes, config); 
    })
    .then(results => { 
       let { main } =  results.instance.exports;
       buffer = new Uint8Array(results.instance.exports.memory.buffer);
       main();
    });
    </script>
  </body>
</html>

Build

On Windows

First, precompile dependencies

prepare_3rdParty.bat 

To build TSC binaries:

cd tsc
config_tsc_debug.bat
build_tsc_debug.bat

On Linux (Ubuntu 20.04)

First, precompile dependencies

sh -f prepare_3rdParty.sh

To build TSC binaries:

cd tsc
sh -f config_tsc_debug.sh
sh -f build_tsc_debug.sh

About

TypeScript Compiler (by LLVM)

License:MIT License


Languages

Language:C++ 91.7%Language:TypeScript 5.5%Language:CMake 1.8%Language:C 0.5%Language:Shell 0.2%Language:Batchfile 0.2%Language:JavaScript 0.0%