StanScript – improved JavaScript based on traceur-compiler

Following on my little half-joke Backtick, I actually wanted to improve some things pointed out to me as bad about it and create a bit more professional JS-derivative. This time it’s based on much more solid foundation – the traceur compiler. It’s pretty cleanly divided into parser and code generator modifications. And there’s a bit of runtime included, offering functions typically used with iterators, like map(), filter(), reduce(). Also a NumStan library trying to lay foundation for a NumPy clone. If I come up with other good ideas, I will also not hesitate to implement them 😉

Here’s a short list of changes relative to JS:

– range operator mimicking the MATLAB one, works like this: 0210, meaning from 0 to 10 (inclusive), with step 2, gets translated to object {‘from’: 0, ‘to’: 10, ‘step’: 2}

– short-hand new operator, e.g. Vector3(1, 2, 3) is new Vector3(1, 2, 3)

– short-hand ‘this’ notation, e.g. .x (this.x) and \ (this)

– operator overloading in variables declared with , e.g.: x = 1; x += 1; gets transpiled to: var x = 1; x.__iadd__(1);

– also member lookup ([]), member (.) and ternary (?:) operators can be overloaded using special methods __index__, __attr__ and __ternary__ respectively

– if last expression in a function is in parentheses it automatically is the return value, e.g. (x) is translated to return x;

– multi-dimensional indexing in variables declared with , e.g. x=ndarray([5,5]); console.log(x[1, 0:2]); is translated to var x = new ndarray([5, 5]); console.log(x.__index__([1, {‘from’: 0, ‘to’: 2, ‘step’: 1}]));

– chaining operator (->) for better functional compositing, e.g. a(1) -> b(2, 3) -> c(); gets transpiled to c(b(2, 3, a(1));

– everything else from ECMAScript 6 is supported thanks to traceur itself; you get short lambda declarations, asynchronous functions, generators, classes and more

– source maps handled also by traceur allow for debugging it in the web browser as if it was a native language

– included NumStan library mimicks closely semantics of NumPy’s ndarray, providing foundations for recreating the rest of its functionality

The project is hosted as traceur fork on Github: HERE. Give it a try 😉

2 Comments

  • Hey Stanisław!

    Great vision about language features I would really like to use in JS since… well, since years. Mainly the pythonish operator overloading blows my mind really.

    I have checked the GitHub repo, do you still maintain the StanScript compiler? The traceur version and the overall project as well is far behind the actual, recent versions of JS libraries, tools and packages.

    I am interested to try out and use the StanScript compiler, but will it work with the recent versions of tools?

    Thank you for your effort and time!

    • Hi Richard,

      Thanks for the kind words.

      Sure if there are people using it I’m kinda maintaining it. Did you have any problems? What do you mean by traceur being behind the recent versions of JS tools? Did you experience some problems with “re-compiling” modern libraries using traceur?

      I wanted to create a better dialect of JS supporting more syntactic sugar like the above. Also support for many ECMAScript6 features is crucial for me. As I modernize my JS codebase, I will probably start using traceur and StanScript myself. For the moment it was a bit of a playground but I hope the situation is going to change soon and first StanScript code will go into production.

      Best,

      S.


Leave a Reply

Your email address will not be published. Required fields are marked *