Skip to content

sadaszewski/stanscript-compiler

 
 

Repository files navigation

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: 0\2\10, 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 ;)

About

StanScript – improved JavaScript based on traceur-compiler

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 98.3%
  • CSS 1.5%
  • Other 0.2%