A Javascript-like scripting language for JVM, with coroutine.
Clone project
git clone https://github.com/wssccc/ngscript.git
Run Rose-Render test
./gradlew run -Dexec.mainClass=org.ngscript.examples.RoseRender
Use var
to explicitly define a variable
Examples
Define a variable
var b;
Define a variable and initialize with a value
var a = 1;
Inline definition
for (var i = 0; i < 9; ++i) ...
Examples
function func1 (param1, param2) { println(param1 + "," + param2); }
Named functions were registered in global scope.
Examples
(function (){ println("hello"); })();
var f = function(){ println("hello"); };
val f2 = (x, y) => { x + y; }; val f3 = (x) => { return x + 1; }; val f4 = (x ,y) => { return x + y; }; println("call an arrow func"); println(f3(1));
Examples
//native Java ArrayList var array = new ArrayList(); //add something array.add(1); array.add(2); array.add(3); //reference to method of a Java object var ref_get = array.get; //call the reference to get the element of index 1 println(ref_get(1));
Examples
val f = function(arg) { println("in other routine " + arg); }; go f(123); println("in main routine");
Examples
Define a constructor function
function One(name) { this.hello = function(){ println("I'm " + name); }; }
Create an object with the
new
operatorvar newone = new One("wssccc"); newone.hello();
Examples
Create an ArrayList
var arraylist = new ArrayList(); arraylist.add(1); arraylist.add(2); println(arraylist.toString()); arraylist.remove(0); println(arraylist.toString());
The same as import
statement in Java
java.lang.* and java.util.* classes were imported by default.
Get string representation of the type of an object
Examples
println(typeof println); var a = 1; println(typeof a); println(typeof println);
Print a line.
println(eval("15+20"));
Examples
try { println("will throw excepton"); throw "exception"; } catch (e) { println(e.toString()); }
Examples
function f(a,b) { println("first = " + a); // return 1 and switch to caller yield(1); // resume here println("second = " + b); } // create coroutine var co = new Coroutine(f, "p1", "p2"); println("coroutine status=" + co.status()); // call resume() to run println("resume 1 = " + co.resume()); println("coroutine status=" + co.status()); // call resume() to run to the end of function f println("resume 2 = " + co.resume()); println("coroutine status=" + co.status()); try { println("resume 3:"); println(co.resume()); } catch(e) { println("could not resume, status=" + co.status()); }