ts2c
1.0.0
从JS/TS代码生成可读的C89代码。
例如,此JavaScript:
console . log ( "Hello world!" ) ;转到以下C代码:
#include <stdio.h>
int main () {
printf ( "Hello world!n" );
return 0 ;
}从来没有生成实际不需要的过多代码。
输出尽可能可读性,并且主要映射到原始代码。
另一个例子:
var obj = { key : "hello" } ;
obj [ "newKey" ] = "test" ;
console . log ( obj ) ;转到以下C代码:
#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 ;
}正在进行的工作:它有效,但目前只支持ES3规范的70% :语句和表达式-95%,内置对象-17%。
值得注意的不支持的功能包括:float和大数字(当前所有数字均为int16_t ), eval , Date , Math等。
有关支持和计划功能的详细信息,请参见Coverage.md。
欢迎捐款!请参阅src/readme.md
您可以自己在线尝试:
该项目的主要动机是解决问题,即目前无法通过JavaScript对物联网和可穿戴设备进行有效编程。
事实是,对于可以在单个电池上长时间工作的可持续物联网设备,诸如Raspberry Pi之类的事情将无法使用。您将必须使用低功率微控制器,这些微控制器通常几乎没有可用的内存。
RAM从字面上的范围从512个字节到120KB,ROM/Flash从1KB到4MB。在这种情况下,即使是优化的JS解释器,例如JerryScript,Espruino或V7有时是太多的开销,通常会导致电池耗尽的增加和/或不会为您的程序留下大量的系统资源。
当然,Transpiler不能绘制JavaScript语言的100%,并且必须遗漏某些内容,特别是eval 。尽管如此,目前的结论是,可以传播大多数语言。
计划的转卸目标:
命令行:
npm install -g ts2c
句法:
ts2c < files to transpile >node.js:
npm install ts2c
const ts2c = require ( "ts2c" ) ;
const cCode = ts2c . transpile ( "console.log('Hello world!')" ) ;
console . log ( cCode ) ;在浏览器中:
< 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 >