From 1d297d92488187850ecf69ec30a1a357d66fec26 Mon Sep 17 00:00:00 2001 From: Onoshko Dan Date: Fri, 15 Aug 2014 17:45:46 +0700 Subject: [PATCH] Negate and modulo array accessors are done. arr[-1]; // last element int index = -10; arr[%index] = 34; // arr[index %% arr.length]; --- README.md | 118 +++++++++++++++++++++++------------------------ lib/parse.js | 20 ++++---- lib/std.js | 13 +++++- lib/translate.js | 32 +++++++++++++ 4 files changed, 115 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 1b4e96ae..b42221db 100644 --- a/README.md +++ b/README.md @@ -388,7 +388,7 @@ Future plans int sqrt(int x?) => x ** 2; sqr(); // NaN -- Negate array accessor ( getter ) +- Negate array accessor ( getter ). status: done arr[-1]; // last element @@ -397,6 +397,64 @@ Future plans int index = -10; arr[%index] = 34; // arr[index %% arr.length]; +- classes + + class A { + + int a = 123; + readonly String about = "class"; + + $("button").click(() => console.log("Button Clicked!")); + + A(a){ + about = "some else"; + } + + static Hello() => "hello!"; + + public String about() => about; + } + + class B extends A { + + B(){ + parent(); + about += "!"; + } + + B.anotherConstructor(){ + about = "ups!"; + } + + get some => "some " + about; + set some(val) => about += val; + } + +- singletones + + singleton S { // in fact this is object + int x = 45; + String s = "txt"; + + say(some){ + alert(some); + } + + int operator[](int index) => index + 584; + operator[]=(int index, int val) => x = index + val; + + } + +- injectors + + injector String { + String replaceAll(a, b){ + String res = this; + while(res.indexOf(a) != -1) res = res.replace(a, b); + return res; + } + } + - static typing - `@use` expressions @@ -432,39 +490,6 @@ Future plans String info?; } -- classes - - class A { - - int a = 123; - readonly String about = "class"; - - $("button").click(() => console.log("Button Clicked!")); - - A(a){ - about = "some else"; - } - - static Hello() => "hello!"; - - public String about() => about; - } - - class B extends A { - - B(){ - parent(); - about += "!"; - } - - B.anotherConstructor(){ - about = "ups!"; - } - - get some => "some " + about; - set some(val) => about += val; - } - - classes and typing with templates class A { @@ -474,31 +499,6 @@ Future plans Array arr = [0...10]; Object obj = { name: "Eric" }; -- singletones - - singleton S { // in fact this is object - int x = 45; - String s = "txt"; - - say(some){ - alert(some); - } - - int operator[](int index) => index + 584; - operator[]=(int index, int val) => x = index + val; - - } - -- injectors - - injector String { - String replaceAll(a, b){ - String res = this; - while(res.indexOf(a) != -1) res = res.replace(a, b); - return res; - } - } - - ES6 `for` for(name of names){ diff --git a/lib/parse.js b/lib/parse.js index 6fd3c360..661f909f 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -2059,22 +2059,26 @@ Cola.Parser.prototype.subscripts = function(expr, allow_calls) { var prop, triple; if(this.is_js) prop = this.expression(true); - else if(this.is("punc","]")) prop = new Cola.AST_Noop(); + else if(this.is("punc", "]")) prop = new Cola.AST_Noop(); else { - if((this.is("punc","..") || this.is("punc","...")) && !this.is_js) prop = new Cola.AST_Number({ value : 0 }); - else prop = this.expression(true, false, true); - //this.dumpS(); - if((this.is("punc","..") || this.is("punc","...")) && !this.is_js){ - triple = this.is("punc","..."); + if(this.is("punc", "..") || this.is("punc", "...")) + prop = new Cola.AST_Number({ value : 0 }); + else if(this.is("operator", "%")) + prop = new Cola.AST_UnaryPrefix({ operator : "%", expression : (this.next(), this.expression(true, false, false)) }); + else + prop = this.expression(true, false, true); + + if(!(prop instanceof Cola.AST_UnaryPostfix && prop.operator == "%") && (this.is("punc", "..") || this.is("punc", "..."))){ + triple = this.is("punc", "..."); this.next(); prop = new Cola.AST_ArrayRange({ from : prop, - to : ( this.is("punc","]") ? new Cola.AST_Noop() : this.expression(true, false, true) ), + to : ( this.is("punc", "]") ? new Cola.AST_Noop() : this.expression(true, false, true) ), triple : triple, start : prop.start, end : this.prev() }); - } //else this.restoreS(); + } } this.expect("]"); diff --git a/lib/std.js b/lib/std.js index a5d902d5..72f6421f 100644 --- a/lib/std.js +++ b/lib/std.js @@ -127,12 +127,23 @@ Cola._ColaRuntime$$error = function _ColaRuntime$$error(_error) { }; Cola._ColaRuntime$$error.i = 11; -Cola._ColaRuntime$$arguments_def = { i : 12 }; +Cola._ColaRuntime$$array_negate_access = function _ColaRuntime$$array_negate_access(_array, _index) { + return _array instanceof Array ? _array.length + _index : _index; +}; +Cola._ColaRuntime$$array_negate_access.i = 12; + +Cola._ColaRuntime$$array_modulo_access = function _ColaRuntime$$array_modulo_access(_array, _index) { + return _array instanceof Array ? ((_index % _array.length + +_array.length) % _array.length) : _index; +}; +Cola._ColaRuntime$$array_modulo_access.i = 13; + +Cola._ColaRuntime$$arguments_def = { i : 14 }; Cola.$_cola = Cola._ColaRuntime$$is + Cola._ColaRuntime$$isnt + Cola._ColaRuntime$$modulo + Cola._ColaRuntime$$isset + Cola._ColaRuntime$$isntset + Cola._ColaRuntime$$clone + Cola._ColaRuntime$$array_last + Cola._ColaRuntime$$array_range + Cola._ColaRuntime$$array_asplice + Cola._ColaRuntime$$func_named_args + Cola._ColaRuntime$$func_set_named_args + Cola._ColaRuntime$$error + + Cola._ColaRuntime$$array_negate_access + Cola._ColaRuntime$$array_modulo_access + "var arguments;"; Cola.Compressor.StdFuncs = { diff --git a/lib/translate.js b/lib/translate.js index 70b77939..79a090a8 100644 --- a/lib/translate.js +++ b/lib/translate.js @@ -824,6 +824,38 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){ node = new Cola.AST_Call(props); } else + /* + arr[-1] + + to + + arr[_ColaRuntime$$array_negate_access(arr, -1)] + + */ + if(node instanceof Cola.AST_Sub && node.property instanceof Cola.AST_UnaryPrefix && node.property.operator == "-" && node.property.expression instanceof Cola.AST_Number){ + _ColaRuntime$$hash[Cola._ColaRuntime$$array_negate_access.i] = true; + node.property = new Cola.AST_Call({ + args : [node.expression, node.property], + expression : new Cola.AST_SymbolRef({ name : '_ColaRuntime$$array_negate_access' }) + }); + } else + + /* + arr[%index] + + to + + arr[_ColaRuntime$$array_modulo_access(arr, index)] + + */ + if(node instanceof Cola.AST_Sub && node.property instanceof Cola.AST_UnaryPrefix && node.property.operator == "%"){ + _ColaRuntime$$hash[Cola._ColaRuntime$$array_modulo_access.i] = true; + node.property = new Cola.AST_Call({ + args : [node.expression, node.property.expression], + expression : new Cola.AST_SymbolRef({ name : '_ColaRuntime$$array_modulo_access' }) + }); + } else + /* arr[0..1] = 123