From b2bffc40cd28921917094bdca83e3293ee76902f Mon Sep 17 00:00:00 2001 From: Onoshko Dan Date: Mon, 21 Apr 2014 01:56:47 +0700 Subject: [PATCH] Array `arr[]` operator added. --- README.md | 3 ++- demo.cola | 8 +++----- lib/index.html | 10 +++++----- lib/parse.js | 13 +++++++++---- lib/std.js | 9 +++++++-- lib/translate.js | 37 ++++++++++++++++++++++++++++++++++++- 6 files changed, 62 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 3031ab6d..ac58f894 100644 --- a/README.md +++ b/README.md @@ -184,10 +184,11 @@ ColaScript is a language that compiles in JavaScript. This language is similar t ### Arrays -- pushing +- pushing and getting last, status: done var arr = [3, 5, 6, 7]; arr[] = 4; // [3, 5, 6, 7, 4] + console.log(arr[]); // 4 - part assigment diff --git a/demo.cola b/demo.cola index 80d530c4..adf18902 100644 --- a/demo.cola +++ b/demo.cola @@ -24,12 +24,10 @@ main(){ Array x, y; x = [123]; y = clone x; - y.push(321); - console.log('original:',x,'cloned:',y); + y[] = 321; + console.log('original:',x,'cloned:',y, y[]); - y.forEach((val){ - console.log(val); - }); + y.forEach((val) => console.log(val)); console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}"); diff --git a/lib/index.html b/lib/index.html index 1003e389..46577da0 100644 --- a/lib/index.html +++ b/lib/index.html @@ -90,12 +90,10 @@ Array x, y; x = [123]; y = clone x; - y.push(321); - console.log('original:',x,'cloned:',y); + y[] = 321; + console.log('original:',x,'cloned:',y, y[]); - y.forEach((val){ - console.log(val); - }); + y.forEach((val) => console.log(val)); console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}"); @@ -166,6 +164,8 @@ main(); } catch(e){ translationArea.value = ''; resultArea.value = ''; + + throw e; } document.querySelector('#lenstat').innerHTML = sourceArea.value.length+" : "+translationArea.value.length+" : "+resultArea.value.length; diff --git a/lib/parse.js b/lib/parse.js index 8b1990da..f9d0e879 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -705,10 +705,15 @@ Cola.Tokenizer.prototype.next_token = function (force_regexp) { 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 (!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(); this.parse_error("Unexpected character '" + ch + "'"); }; @@ -1680,7 +1685,7 @@ Cola.Parser.prototype.subscripts = function(expr, allow_calls) { } if (this.is("punc", "[")) { this.next(); - var prop = this.expression(true); + var prop = !this.is_js && this.is("punc", "]") ? new Cola.AST_Noop() : this.expression(true); this.expect("]"); return this.subscripts(new Cola.AST_Sub({ start : start, diff --git a/lib/std.js b/lib/std.js index 0ddecf0e..c06336ae 100644 --- a/lib/std.js +++ b/lib/std.js @@ -59,7 +59,7 @@ function $_cola_isntset(_object){ } $_cola_isntset.i = 4; -function $_cola_clone(_item) { +function $_cola_clone(_item){ if (_item === undefined || _item === null) return _item; if (_item.__clone__ instanceof Function) return _item.__clone__(); @@ -96,4 +96,9 @@ function $_cola_clone(_item) { } $_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; diff --git a/lib/translate.js b/lib/translate.js index 043bddf8..32659923 100644 --- a/lib/translate.js +++ b/lib/translate.js @@ -43,7 +43,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){ var $_cola_ast = Cola.parse($_cola, { is_js : true}), $_cola_hash = {}, _this, tt = new Cola.TreeTransformer(null, function(node){ - var newNode, props; + var newNode, props, parent = this.parent(); if(node instanceof Cola.AST_Binary && node.operator == '**'){ props = { @@ -207,6 +207,41 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){ node = new Cola.AST_Call(props); } 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){ newNode = new Cola.AST_Binary({ operator : '+',