GithubHelp home page GithubHelp logo

strogo / typescriptcompiler Goto Github PK

View Code? Open in Web Editor NEW

This project forked from asdalexander77/typescriptcompiler

0.0 0.0 0.0 7.37 MB

TypeScript Compiler (by LLVM)

License: MIT License

Batchfile 0.17% CMake 1.46% C++ 92.60% C 0.65% TypeScript 4.82% JavaScript 0.05% Shell 0.24%

typescriptcompiler's Introduction

TypeScript Native Compiler

Powered by LLVM|MLIR

Test Build (Windows) Test Build (Linux)

Demo

Demo

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

typescriptcompiler's People

Contributors

asdalexander77 avatar oduzhar 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.