Giter VIP home page Giter VIP logo

ts2c's Introduction

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 48% of ES3 syntax is currently supported.

Overview of currently supported language features (compared to ES3 Standard):

  • statements [76%]: var, if-else, do-while, while, for, for-of, for-in, continue, break, return, function, block, empty statement, expression statement
  • expressions [56%]:
    • primary expressions [81%]: variables; number, string, regex and boolean literals; array and object initializers; grouping operator
    • left-hand-side expressions [60%]: property accessors, function calls
    • postfix expressions [100%]: ++, --
    • unary operators [44%]: ++, --, +, !
    • multiplicative operators [16%]: *
    • additive operators [50%]: +, -
    • bitwise shift operators [0%]
    • relational operators [33%]: <, >, <=, >=
    • equality operators [50%]: ==, !=, ===, !==
    • binary bitwise operators [0%]
    • binary logical operators [100%]: &&, ||
    • conditional operator [100%]: ?-:
    • assignment operators [52%]: =, +=
    • comma operator [100%]
  • built-in objects [13%]:
    • Global [0%]
    • Object [0%]
    • Function [0%]
    • Array [78%]: push(), pop(), shift(), unshift(), splice(), slice(), concat(), join(), toString(), sort(), reverse(), indexOf(), lastIndexOf(), length
    • String [52%]: indexOf(), lastIndexOf(), search(), charAt(), charCodeAt(), concat(), substring(), slice(), toString(), valueOf(), length
    • Boolean [0%]
    • Number [0%]
    • Math [0%]
    • Date [0%]
    • RegExp [0%]

Note: some of these features supported only partially. Detailed information about supported and planned features can be found in COVERAGE.md.

Memory management is done via escape analysis.

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, for example eval and try-catch. Still, current conclusion is, that it is possible to transpile most of the language.

These are some examples of planned target platforms for using with TS2C:

Usage

Syntax:

ts2c <files to transpile>

In browser (also see index.html file):

<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>

ts2c's People

Contributors

andrei-markeev avatar endel avatar npmcdn-to-unpkg-bot avatar

Watchers

 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.