diff --git a/README.md b/README.md index b2ba3045..3031ab6d 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,7 @@ ColaScript is a language that compiles in JavaScript. This language is similar t String str = r"\n \r"; // "\\n \\r" -- templating, status : done +- templating, status: done String name = "dan"; @@ -222,7 +222,7 @@ ColaScript is a language that compiles in JavaScript. This language is similar t console.log('Hello World!'); } -- arrow functions +- arrow functions, status: done print(str) => console.log(str); @@ -320,4 +320,4 @@ ColaScript is a language that compiles in JavaScript. This language is similar t ### Statistic - 30 feature ( without classes ) -- 15 done +- 18 done diff --git a/lib/index.html b/lib/index.html index 1c860aca..0b8708ac 100644 --- a/lib/index.html +++ b/lib/index.html @@ -139,8 +139,6 @@ main(); } catch(e){ translationArea.value = ''; resultArea.value = ''; - - throw e; } } diff --git a/lib/parse.js b/lib/parse.js index c83720e6..8b1990da 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -705,6 +705,7 @@ 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 (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); @@ -1010,7 +1011,7 @@ Cola.Parser.prototype.statement = Cola.Parser.embed_tokens(function() { return balance == 0 || _this.is('eof'); }); - isfun = (this.next(), this.is('punc','{')); + isfun = (this.next(), (this.is('punc','{') || this.is('punc','=>'))); } this.restoreS(); @@ -1238,7 +1239,22 @@ Cola.Parser.prototype.function_ = function(ctor, type) { _this.S.in_directives = true; _this.S.in_loop = 0; _this.S.labels = []; - var a = _this.block_(); + + var tmp, a = !_this.is_js && _this.is("punc", "=>") + ? (_this.next(), [new Cola.AST_Return({ + start: new Cola.AST_Token({ nlb : false, type : 'keyword', value : 'return' }), + value: (function(){ + tmp = _this.expression(true); + if ( ctor === Cola.AST_Defun ) { + _this.semicolon(); + _this.next(); + } + return tmp; + })(), + end : _this.prev() + })]) + : _this.block_(); + --_this.S.in_function; _this.S.in_loop = loop; _this.S.labels = labels; @@ -1504,7 +1520,7 @@ Cola.Parser.prototype.expr_atom = function(allow_calls) { return balance == 0 || _this.is('eof'); }); - isfun = (this.next(), this.is('punc','{')); + isfun = (this.next(), (this.is('punc','{') || this.is('punc','=>'))); this.restoreS();