• Stars
    star
    1,220
  • Rank 36,936 (Top 0.8 %)
  • Language
    C
  • License
    ISC License
  • Created almost 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convert Javascript/TypeScript to C

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>

More Repositories

1

camljs

Library for creating SharePoint CAML queries client-side. For JSOM, REST or SPServices.
TypeScript
76
star
2

cisar

SharePoint CSR Live Editor (Chrome extension)
JavaScript
73
star
3

camljs-console

Chrome extension, that provides a CamlJs console for creating SharePoint CAML queries right in browser, with live data preview.
JavaScript
36
star
4

finnlingo

Duolingo-like application tailored for learning Finnish
JavaScript
29
star
5

ddpserver

Server endpoint for Meteor's DDP protocol in C++
C++
19
star
6

ngx_http_jwted_module

Nginx JWT authentication module with Ed25519 algorithm support
C
9
star
7

ts2c-target-esp-idf

ESP-IDF target for TS2C transpiler
7
star
8

pebble-onenote

Display OneNote notebooks on Pebble smartwatch
JavaScript
6
star
9

vue2jsx

Small utility for converting Vue templates to jsx.
TypeScript
5
star
10

openresty-lua-types

Typescript types for openresty (ngx_http_lua_module), designed to be used with TypeScriptToLua.
5
star
11

skype4b

Example code for authenticating with Skype for Business Online
JavaScript
3
star
12

mongodb-livedata-server

Standalone live data server implementation extracted from Meteor, Fibers removed and converted to TypeScript.
TypeScript
3
star
13

spribbon

Fluent API for Ribbon creation in SharePoint 2010. Migrated from https://spribbon.codeplex.com
C#
2
star
14

pascal-lsp

LSP for Pascal, built from scratch with focus on performance and DX.
Pascal
2
star
15

pebble-outlook-tasks

See and complete Outlook tasks from a Pebble watch!
JavaScript
2
star
16

pebble-outlook-events

Displays Office 365 Outlook calendar events on Pebble watch and allows sending "I'm late" messages.
C
1
star
17

jsom-azure-function

Example of running JSOM from Azure Function
JavaScript
1
star