Array arr[] operator added.

This commit is contained in:
Onoshko Dan 2014-04-21 01:56:47 +07:00
parent 514d9e5f01
commit b2bffc40cd
6 changed files with 62 additions and 18 deletions

View File

@ -184,10 +184,11 @@ ColaScript is a language that compiles in JavaScript. This language is similar t
### Arrays ### Arrays
- pushing - pushing and getting last, status: done
var arr = [3, 5, 6, 7]; var arr = [3, 5, 6, 7];
arr[] = 4; // [3, 5, 6, 7, 4] arr[] = 4; // [3, 5, 6, 7, 4]
console.log(arr[]); // 4
- part assigment - part assigment

View File

@ -24,12 +24,10 @@ main(){
Array x, y; Array x, y;
x = [123]; x = [123];
y = clone x; y = clone x;
y.push(321); y[] = 321;
console.log('original:',x,'cloned:',y); console.log('original:',x,'cloned:',y, y[]);
y.forEach((val){ y.forEach((val) => console.log(val));
console.log(val);
});
console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}"); console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}");

View File

@ -90,12 +90,10 @@
Array x, y; Array x, y;
x = [123]; x = [123];
y = clone x; y = clone x;
y.push(321); y[] = 321;
console.log('original:',x,'cloned:',y); console.log('original:',x,'cloned:',y, y[]);
y.forEach((val){ y.forEach((val) => console.log(val));
console.log(val);
});
console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}"); console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}");
@ -166,6 +164,8 @@ main();</textarea>
} catch(e){ } catch(e){
translationArea.value = ''; translationArea.value = '';
resultArea.value = ''; resultArea.value = '';
throw e;
} }
document.querySelector('#lenstat').innerHTML = sourceArea.value.length+" : "+translationArea.value.length+" : "+resultArea.value.length; document.querySelector('#lenstat').innerHTML = sourceArea.value.length+" : "+translationArea.value.length+" : "+resultArea.value.length;

View File

@ -705,10 +705,15 @@ Cola.Tokenizer.prototype.next_token = function (force_regexp) {
return this.token("punc", this.next()); return this.token("punc", this.next());
} }
if (!this.is_js && ch + this.peek(1) == '=>') return this.next(), this.next(), this.token("punc", "=>");
if (!this.is_js && ch == '@') return this.read_at(); if (!this.is_js) {
if (ch == 'r' && (this.peek(1) == '"' || this.peek(1) == "'" || this.peek(1) == '`')) return this.next(), this.read_string(true);
if (ch + this.peek(1) == '=>') return this.next(), this.next(), this.token("punc", "=>");
if (ch + this.peek(1) == '..') return this.next(), this.next(), this.token("punc", "..");
if (ch == '@') return this.read_at();
}
if (Cola.OPERATOR_CHARS(ch)) return this.read_operator(); if (Cola.OPERATOR_CHARS(ch)) return this.read_operator();
if (!this.is_js && ch == 'r' && (this.peek(1) == '"' || this.peek(1) == "'" || this.peek(1) == '`')) return this.next(), this.read_string(true);
if (code == 92 || Cola.is_identifier_start(code)) return this.read_word(); if (code == 92 || Cola.is_identifier_start(code)) return this.read_word();
this.parse_error("Unexpected character '" + ch + "'"); this.parse_error("Unexpected character '" + ch + "'");
}; };
@ -1680,7 +1685,7 @@ Cola.Parser.prototype.subscripts = function(expr, allow_calls) {
} }
if (this.is("punc", "[")) { if (this.is("punc", "[")) {
this.next(); this.next();
var prop = this.expression(true); var prop = !this.is_js && this.is("punc", "]") ? new Cola.AST_Noop() : this.expression(true);
this.expect("]"); this.expect("]");
return this.subscripts(new Cola.AST_Sub({ return this.subscripts(new Cola.AST_Sub({
start : start, start : start,

View File

@ -59,7 +59,7 @@ function $_cola_isntset(_object){
} }
$_cola_isntset.i = 4; $_cola_isntset.i = 4;
function $_cola_clone(_item) { function $_cola_clone(_item){
if (_item === undefined || _item === null) return _item; if (_item === undefined || _item === null) return _item;
if (_item.__clone__ instanceof Function) return _item.__clone__(); if (_item.__clone__ instanceof Function) return _item.__clone__();
@ -96,4 +96,9 @@ function $_cola_clone(_item) {
} }
$_cola_clone.i = 5; $_cola_clone.i = 5;
$_cola = $_cola_is + $_cola_isnt + $_cola_modulo + $_cola_isset + $_cola_isntset + $_cola_clone; function $_cola_array_last(_array){
return _array[_array.length - 1];
}
$_cola_array_last.i = 6;
$_cola = $_cola_is + $_cola_isnt + $_cola_modulo + $_cola_isset + $_cola_isntset + $_cola_clone + $_cola_array_last;

View File

@ -43,7 +43,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){
var $_cola_ast = Cola.parse($_cola, { is_js : true}), $_cola_hash = {}, _this, var $_cola_ast = Cola.parse($_cola, { is_js : true}), $_cola_hash = {}, _this,
tt = new Cola.TreeTransformer(null, function(node){ tt = new Cola.TreeTransformer(null, function(node){
var newNode, props; var newNode, props, parent = this.parent();
if(node instanceof Cola.AST_Binary && node.operator == '**'){ if(node instanceof Cola.AST_Binary && node.operator == '**'){
props = { props = {
@ -207,6 +207,41 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){
node = new Cola.AST_Call(props); node = new Cola.AST_Call(props);
} else } else
if(node instanceof Cola.AST_Assign && node.operator == "=" &&
node.left instanceof Cola.AST_Sub && node.left.property instanceof Cola.AST_Noop){
props = {
property : "push",
start : node.left.start,
end : new Cola.AST_Token({ nlb : false, type : 'name', value : 'push' }),
expression : node.left.expression
};
props = {
args : [node.right],
start : props.start,
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' }),
expression : new Cola.AST_Dot(props)
};
node = new Cola.AST_Call(props);
} else
if(node instanceof Cola.AST_Sub && node.property instanceof Cola.AST_Noop && !(parent instanceof Cola.AST_Assign) && parent.operator != "=" && node != parent.left){
$_cola_hash[$_cola_array_last.i] = true;
props = {
args : [node.expression],
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_array_last' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
};
props.expression = new Cola.AST_SymbolRef({
name : '$_cola_array_last',
start : props.start,
end : props.start
});
node = new Cola.AST_Call(props);
} else
if(node instanceof Cola.AST_StringTemplate){ if(node instanceof Cola.AST_StringTemplate){
newNode = new Cola.AST_Binary({ newNode = new Cola.AST_Binary({
operator : '+', operator : '+',