surma / jsxx Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
Consider this
let obj = {};
obj.fib = function (n){
if (n <= 0) return 0;
else if (n == 1) return 1;
else return fib(n - 1) + fib(n - 2);
}
console.log(obj.fib(10));
JavaScript appears to behave as if all variables are keys of an unmentioned scope object. Translating variables of js to c++ doesn't work with recursion. Consider the code in which there is no scope object obj. It still works in js but segfaults when compiled to c++ to a.out using g++.
It seems to me that the jsxx compiler translates line by line without context awareness - it doesn't build a parse tree. If it did, then the generated code would have an explicit scope object. The more general scenario is when a function uses a variable that is defined in a super-scope (not defined in function body). Then any other piece of code can change this variable and the behavior of the function using said variable also changes. Consider the distance variable in the following scenario.
let manhattan = function(p1,p2){
delx = p1.x-p2.x;
dely = p1.y-p2.y;
if(delx<0.0) delx *= -1
if(dely<0.0) dely *= -1
return delx + dely;
}let euclidian = function(p1,p2){
delx = p1.x-p2.x;
dely = p1.y-p2.y;
return delxdelx + delydely;
}let minkowski = function(p1,p2){
delx = p1.x-p2.x;
dely = p1.y-p2.y;
return delxdelx - delydely;
}let proximity = function(p1,p2){
dist = distance(p1,p2);
if(dist < 10) return true;
return false;
}distance = manhattan;
console.log(proximity({x:5,y:5},{x:0,y:1}));
console.log(proximity({x:7,y:3},{x:0,y:1}));
console.log();distance = euclidian;
console.log(proximity({x:5,y:5},{x:0,y:1}));
console.log(proximity({x:7,y:3},{x:0,y:1}));
console.log();distance = minkowski;
console.log(proximity({x:5,y:5},{x:0,y:1}));
console.log(proximity({x:7,y:3},{x:0,y:1}));
This gives different outputs, indicating that the proximity function behaves differently when the distance variable changes. Output is as follows
true
truefalse
falsetrue
false
Is there a plan to add builtin objects and methods like console, process, buffer.Buffer, buffer.Blob, Date, fetch, Promise, and builtin modules like fs, net, utils etc? Is development on this compiler halted or is it expected to continue?
At the moment I am trying to save and use scope information to effect the creation of a local scope variable (including a top level, aka global one), I am not familiar with rust or c++, though i am familiar with c and js, so I am facing trouble. Since iter().map() runs sequentially, would you advise the usage of a global array variable to store the parse tree descent, containing the scopes? So a js variable name will be resolved to its local scope if it exists else next higher level scope if it exists and so on till it reaches the first element. And then once resolved the jsxx will be coded to generate <cpp_scope_var_name>.<js_var_name> in place of the existing <js_var_name>.
There isn't a proper documentation about using this, so do you know how to download the transpiler, and how to use the transpiler itself? I am using Windows for your information.
Hello, I installed Visual Studio 2022 Community, Git and Rustup. I cloned this repo with git clone --recursive https://github.com/surma/jsxx
, then I placed the JavaScript code testprog.js
in that directory (that was cloned), and I ran cat testprog.js | cargo run -- --emit-cpp
, but it gives: error: process didn't exit successfully: \target\debug\jsxx.exe --emit-cpp\ (exit code: 101)
error. Do you know the cause of it? I originally asked it here. By the way, please do not look at the previous edits, since I accidentally sent the wrong file. Here is the file: jsxxButWithTestJSCode.zip
I'm curious how the code quality would compare?
https://github.com/THUDM/CodeGeeX#codegeex-a-multilingual-code-generation-model
Neat project! Seems WASM might be the next wave of containers, to supplant some of Docker's use cases, & create new ones....
Thanks for your blog post - I've been working on a intermediary language transpiler based on this neat project - https://github.com/LingDong-/wax
https://waxc.netlify.app/
It's a very minimal language spec that can transpile into C++/javascript/webassembly and others.
Basically, the language itself is the AST, so very little parsing is needed to handle it.
I wrote a self hosted compiler in it here:
https://github.com/jacoblister/wax/blob/compiler/examples/compiler.wax
https://github.com/jacoblister/rewax
It's pretty tedious to work in though, and I've been thinking about adding a front end to it which can take a higher level language,
and I'm starting to think JavaScript might just be it.
Your blog post mentions using an 'Almost TypeScript' dialect -
I came across a little trick recently which allows static type indications in pure JavaScript by (misusing) default assignments:
int = function(a) {return a | 0}
class Point {
x = int
y = int
constructor(x,y) {
this.x = this.x(x)
this.y = this.y(x)
}
}
This can extend to function signatures as well, (although return type needs to be inferred)
I'm think this could work quite well as a half interpreted / half compiled approach, which could fall back to a JSValue type when static typing is not available. Pretty neat this can be expressed in straight Javascript!
I was using Windows Subsystem for Linux 2, and installed Rustup, and copied the jsxx directory to WSL2 home directory, then ran sudo cat testprog.js | cargo run -- --emit-cpp
, but it gave me this error:
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running `target/debug/jsxx --emit-cpp`
thread 'main' panicked at 'Couldn’t spawn clang-format: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/command_utils.rs:15:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.