https://github.com/eatonphil/jsc
A JavaScript compiler written in TypeScript targeting C++/V8
https://github.com/eatonphil/jsc
cpp javascript javascript-compiler typescript typescript-compiler v8
Last synced: 7 months ago
JSON representation
A JavaScript compiler written in TypeScript targeting C++/V8
- Host: GitHub
- URL: https://github.com/eatonphil/jsc
- Owner: eatonphil
- License: other
- Created: 2018-06-02T15:42:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T15:33:14.000Z (almost 3 years ago)
- Last Synced: 2025-02-27T01:13:46.833Z (8 months ago)
- Topics: cpp, javascript, javascript-compiler, typescript, typescript-compiler, v8
- Language: TypeScript
- Homepage:
- Size: 244 KB
- Stars: 198
- Watchers: 9
- Forks: 10
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Javascript compiler targeting C++/V8
### Building
Requires Node.
```bash
$ yarn
```
### Example
```bash
$ yarn tsc
$ node build/jsc.js tests/tco.js
$ node bin/index.js
12586269025
```
### Features
* Functions and function calls
* Basic tail-call optimization
* Var, const, let declarations
* For, do, while statements
* Basic primitive operations
* Basic import support
* Number, string, boolean and null literals
* Basic value unboxing
#### Not (yet) supported
* Prototype functions
* Nested functions
* Closures
* And much, much more!
### Code produced
The following:
```js
function fib(n: number, a: number, b: number) {
if (n == 0) {
return a;
}
if (n == 1) {
return b;
}
return fib(n - 1, b, a + b);
}
```
Gets compiled to:
```cpp
void tco_fib(const FunctionCallbackInfo &args) {
Isolate *isolate = args.GetIsolate();
double tco_n = toNumber(args[0]);
double tco_a = toNumber(args[1]);
double tco_b = toNumber(args[2]);
tail_recurse_1:
;
bool sym_if_test_58 = (tco_n == 0);
if (sym_if_test_58) {
args.GetReturnValue().Set(Number::New(isolate, tco_a));
return;
}
bool sym_if_test_70 = (tco_n == 1);
if (sym_if_test_70) {
args.GetReturnValue().Set(Number::New(isolate, tco_b));
return;
}
Local sym_arg_83 = Number::New(isolate, (tco_n - 1));
Local sym_arg_92 = Number::New(isolate, (tco_a + tco_b));
tco_n = toNumber(sym_arg_83);
tco_a = tco_b;
tco_b = toNumber(sym_arg_92);
goto tail_recurse_1;
}
```