Negate and modulo array accessors are done.

arr[-1]; // last element

int index = -10;
arr[%index] = 34; // arr[index %% arr.length];
This commit is contained in:
Onoshko Dan 2014-08-15 17:45:46 +07:00
parent aec283f0b4
commit 1d297d9248
4 changed files with 115 additions and 68 deletions

118
README.md
View File

@ -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<T> {
@ -474,31 +499,6 @@ Future plans
Array<int> arr = [0...10];
Object<String, String> 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){

View File

@ -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("]");

View File

@ -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 = {

View File

@ -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