All Projects → andrei-markeev → Ts2c

andrei-markeev / Ts2c

Licence: isc
Convert Javascript/TypeScript to C

Programming Languages

javascript
184084 projects - #8 most used programming language
c
50402 projects - #5 most used programming language
typescript
32286 projects
js
455 projects

Projects that are alternatives of or similar to Ts2c

Js2php
JavaScript (ES6) to PHP source-to-source transpiler.
Stars: ✭ 290 (-66.97%)
Mutual labels:  transpiler
Sqlflow
Brings SQL and AI together.
Stars: ✭ 4,412 (+402.51%)
Mutual labels:  transpiler
Onelang
Stars: ✭ 725 (-17.43%)
Mutual labels:  transpiler
Transpiler
A universal translator for programming languages
Stars: ✭ 292 (-66.74%)
Mutual labels:  transpiler
Bytecoder
Rich Domain Model for JVM Bytecode and Framework to interpret and transpile it.
Stars: ✭ 401 (-54.33%)
Mutual labels:  transpiler
Ruby Next
Ruby Next makes modern Ruby code run in older versions and alternative implementations
Stars: ✭ 460 (-47.61%)
Mutual labels:  transpiler
Il2c
IL2C - A translator for ECMA-335 CIL/MSIL to C language.
Stars: ✭ 270 (-69.25%)
Mutual labels:  transpiler
Pas2dart
Object Pascal (Free Pascal 3, Delphi 2007) to Dart (2.5) transpiler
Stars: ✭ 16 (-98.18%)
Mutual labels:  transpiler
Uniter
🎉 PHP in the browser and Node.js => Docs: https://phptojs.com/
Stars: ✭ 405 (-53.87%)
Mutual labels:  transpiler
Pseudo
transpile algorithms/libs to idiomatic JS, Go, C#, Ruby
Stars: ✭ 654 (-25.51%)
Mutual labels:  transpiler
Castl
JavaScript to Lua compiler with runtime library.
Stars: ✭ 323 (-63.21%)
Mutual labels:  transpiler
Wax
A tiny programming language that transpiles to C, C++, Java, TypeScript, Python, C#, Swift, Lua and WebAssembly 🚀
Stars: ✭ 373 (-57.52%)
Mutual labels:  transpiler
Nimporter
Compile Nim Extensions for Python On Import!
Stars: ✭ 474 (-46.01%)
Mutual labels:  transpiler
Pytocs
Converts Python source to C#
Stars: ✭ 288 (-67.2%)
Mutual labels:  transpiler
J2cl
Java to Closure JavaScript transpiler
Stars: ✭ 773 (-11.96%)
Mutual labels:  transpiler
Go On Rails
🚄 Use Rails to Develop or Generate a Golang Application.
Stars: ✭ 275 (-68.68%)
Mutual labels:  transpiler
Haxe
Haxe - The Cross-Platform Toolkit
Stars: ✭ 4,665 (+431.32%)
Mutual labels:  transpiler
Prolog Target Js
Simple Prolog to JS transpiler
Stars: ✭ 19 (-97.84%)
Mutual labels:  transpiler
Typescripttolua
Typescript to lua transpiler. https://typescripttolua.github.io/
Stars: ✭ 783 (-10.82%)
Mutual labels:  transpiler
Rapydscript
Python-inspired, decluttered JavaScript
Stars: ✭ 560 (-36.22%)
Mutual labels:  transpiler

JavaScript/TypeScript to C transpiler

Produces readable C89 code from JS/TS code.

For example, this JavaScript:

console.log("Hello world!");

transpiles to the following C code:

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    return 0;
}

No excessive code that is not actually needed is ever generated.

The output is as readable as possible and mostly maps well to the original code.

Another example:

var obj = { key: "hello" };
obj["newKey"] = "test";
console.log(obj);

transpiles to the following C code:

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

struct obj_t {
    const char * key;
    const char * newKey;
};

static struct obj_t * obj;
int main(void) {

    obj = malloc(sizeof(*obj));
    assert(obj != NULL);
    obj->key = "hello";
    obj->newKey = "test";

    printf("{ ");
    printf("key: \"%s\"", obj->key);
    printf(", ");
    printf("newKey: \"%s\"", obj->newKey);
    printf(" }\n");

    free(obj);

    return 0;
}

Project status

Work in progress: it works, but only about 70% of ES3 specification is currently supported: statements and expressions - 95%, built-in objects - 17%.

Notable NOT supported features include, for example: float and big numbers (all numbers are int16_t currently), eval, Date, Math, etc.

Detailed information about supported and planned features can be found in COVERAGE.md.

Contributions are welcome! See src/README.md

Live demo

You can try it out yourself online:

Rationale

The main motivation behind this project was to solve problem that IoT and wearables cannot be currently efficiently programmed with JavaScript.

The thing is, for sustainable IoT devices that can work for a long time on single battery, things like Raspberry Pi won't do. You'll have to use low-power microcontrollers, which usually have very little memory available.

RAM ranges literally from 512 bytes to 120KB, and ROM/Flash from 1KB to 4MB. In such conditions, even optimized JS interpreters like JerryScript, Espruino or V7 are sometimes too much of an overhead and usually lead to the increased battery drain and/or don't leave a lot of system resources to your program.

Of course, transpiler cannot map 100% of the JavaScript language and some things are have to be left out, notably eval. Still, current conclusion is, that it is possible to transpile most of the language.

Targets

Planned transpilation targets:

Usage

Command line:

npm install -g ts2c

Syntax:

ts2c <files to transpile>

Node.js:

npm install ts2c
const ts2c = require("ts2c");
const cCode = ts2c.transpile("console.log('Hello world!')");
console.log(cCode);

In browser:

<script src="https://unpkg.com/typescript"></script>
<script src="ts2c.bundle.js"></script>
<script>
    var cCode = ts2c.transpile("console.log('Hello world!')");
    alert(cCode);
</script>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].