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;
}
Work in progress: it works, but only about 50% 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 [63%]:
- primary expressions [80%]: 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 [50%]:
*
,/
,%
- additive operators [50%]:
+
,-
- bitwise shift operators [33%]:
<<
,>>
- relational operators [33%]:
<
,>
,<=
,>=
- equality operators [50%]:
==
,!=
,===
,!==
- binary bitwise operators [33%]:
&
,|
- binary logical operators [100%]:
&&
,||
- conditional operator [100%]:
?
-:
- assignment operators [52%]:
=
,+=
- comma operator [100%]
- built-in objects [14%]:
- Global [0%]
- Object [0%]
- Function [0%]
- Array [78%]:
push()
,pop()
,shift()
,unshift()
,splice()
,slice()
,concat()
,join()
,toString()
,sort()
,reverse()
,indexOf()
,lastIndexOf()
,length
- String [61%]:
indexOf()
,lastIndexOf()
,search()
,match()
,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.
Notable NOT supported features include, for example: float numbers, this
, new
, function
inside expression, typeof
, etc.
Memory management is done via escape analysis.
You can try it out yourself online:
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:
- ESP8266
- Pebble watch
- Atmel AVR family (used in Arduino boards)
- TI MSP430 family
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>