From 73fbeb237751693ca7fc649645b44b3b347abcf9 Mon Sep 17 00:00:00 2001 From: Onoshko Dan Date: Sat, 16 Aug 2014 22:40:23 +0700 Subject: [PATCH] Some fixes. Class translation spec is done. --- lib/parse.js | 31 ++++++++---- lib/std.js | 10 +++- lib/translate.js | 125 ++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 131 insertions(+), 35 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index c19fa071..f0f0409f 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1438,11 +1438,14 @@ Cola.Parser.prototype.function_ = function(ctor, type, mods) { !type && (type = "dynamic"); !mods && (mods = []); - var in_statement = ctor === Cola.AST_Defun || Cola.AST_Setter || Cola.AST_Getter, _this = this, splatedexist = false; + var in_statement = ctor === Cola.AST_Defun || ctor === Cola.AST_Setter || ctor === Cola.AST_Getter, _this = this, splatedexist = false; var name = this.is("name") ? this.as_symbol(in_statement ? Cola.AST_SymbolDefun : Cola.AST_SymbolLambda) : null; + //var args_skiped = false; + if (name != null) name = this.subscripts(name, false); if (in_statement && !name) this.unexpected(); + this.expect("("); return new ctor({ mods: mods, @@ -1784,23 +1787,31 @@ Cola.Parser.prototype.expr_atom = function(allow_calls) { if (!this.is_js && this.is("name")) { if(this.next_is("name")){ type = this.S.token.value, this.next(); - return this.function_(Cola.AST_Function, type); + + var isfun = false; + + this.dumpS(); + this.subscripts(this.as_symbol(Cola.AST_SymbolLambda), false); + isfun = this.is("punc", "("); + this.restoreS(); + + if(isfun) return this.function_(Cola.AST_Function, type); } var _this = this, balance = 1, isfun = false; this.dumpS(); - this.next(); + this.subscripts(this.as_symbol(Cola.AST_SymbolLambda), false); if(this.is('punc', '(')){ this.next(); - this.next_until(function(){ - if(_this.is('punc', '(')) balance++; - else if(_this.is('punc', ')')) balance--; + this.next_until(function(){ + if(_this.is('punc', '(')) balance++; + else if(_this.is('punc', ')')) balance--; - return balance == 0 || _this.is('eof'); - }); - isfun = (this.next(), (this.is('punc','{') || this.is('punc','=>'))); - } + return balance == 0 || _this.is('eof'); + }); + isfun = (this.next(), (this.is('punc','{') || this.is('punc','=>'))); + } this.restoreS(); if(isfun) return this.function_(Cola.AST_Function); diff --git a/lib/std.js b/lib/std.js index ef6cc22a..5a9d4b00 100644 --- a/lib/std.js +++ b/lib/std.js @@ -145,13 +145,21 @@ Cola._ColaRuntime$$updateProperty = function _ColaRuntime$$updateProperty(_objec }; Cola._ColaRuntime$$updateProperty.i = 14; -Cola._ColaRuntime$$arguments_def = { i : 15 }; +Cola._ColaRuntime$$proto = function _ColaRuntime$$proto(_proto) { + function proto(){} + proto.prototype = _proto.prototype; + return new c(); +}; +Cola._ColaRuntime$$proto.i = 15; + +Cola._ColaRuntime$$arguments_def = { i : 16 }; 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 + Cola._ColaRuntime$$updateProperty + + Cola._ColaRuntime$$proto + "var arguments;"; Cola.Compressor.StdFuncs = { diff --git a/lib/translate.js b/lib/translate.js index 7f302a4c..0e9b29a2 100644 --- a/lib/translate.js +++ b/lib/translate.js @@ -393,13 +393,15 @@ Cola.DefPropWithMods = function(def, mods){ new Cola.AST_ObjectKeyVal({ key : "value", value : def.value - }), - new Cola.AST_ObjectKeyVal({ - key : "configurable", - value : new Cola.AST_True }) ] }; + if(!(def.name.expression instanceof Cola.AST_Symbol && def.name.expression.name == "this" || def.name instanceof Cola.AST_Proto)) + dp.properties.push(new Cola.AST_ObjectKeyVal({ + key : "configurable", + value : new Cola.AST_True + })); + if(mods.indexOf("const") == -1) dp.properties.push(new Cola.AST_ObjectKeyVal({ key : "writable", @@ -427,8 +429,8 @@ Cola.DefPropWithMods = function(def, mods){ }; Cola.DefFunWithMods = function(func, mods){ - if(mods.length == 0 || !mods){ - if(func instanceof Cola.AST_Defun) + if(!mods || mods.length == 0){ + if(func instanceof Cola.AST_Defun || func instanceof Cola.AST_Function) return new Cola.AST_Assign({ start : func.start, end : func.end, @@ -446,7 +448,9 @@ Cola.DefFunWithMods = function(func, mods){ else if(func instanceof Cola.AST_Getter) return new Cola.AST_Call({ expression : new Cola.AST_Dot({ - expression : func.name.expression, + expression : func.name instanceof Cola.AST_Proto + ? new Cola.AST_Dot({ expression: func.name.expression, property: "prototype" }) + : func.name.expression, property : "__defineGetter__" }), args : [ @@ -464,7 +468,9 @@ Cola.DefFunWithMods = function(func, mods){ else if(func instanceof Cola.AST_Setter) return new Cola.AST_Call({ expression : new Cola.AST_Dot({ - expression : node.name.expression, + expression : func.name instanceof Cola.AST_Proto + ? new Cola.AST_Dot({ expression: func.name.expression, property: "prototype" }) + : func.name.expression, property : "__defineSetter__" }), args : [ @@ -499,13 +505,15 @@ Cola.DefFunWithMods = function(func, mods){ }); return new Cola.AST_Function(node); })(func) - }), - new Cola.AST_ObjectKeyVal({ - key : "configurable", - value : new Cola.AST_True }) ] }; + if(!(sname.expression instanceof Cola.AST_Symbol && sname.expression.name == "this" || sname instanceof Cola.AST_Proto)) + dp.properties.push(new Cola.AST_ObjectKeyVal({ + key : "configurable", + value : new Cola.AST_True + })); + if(!(func instanceof Cola.AST_Getter || func instanceof Cola.AST_Setter)) dp.properties.push(new Cola.AST_ObjectKeyVal({ key : "writable", @@ -520,7 +528,9 @@ Cola.DefFunWithMods = function(func, mods){ return new Cola.AST_Call({ args : [ - sname.expression, + sname instanceof Cola.AST_Proto + ? new Cola.AST_Dot({ expression: sname.expression, property: "prototype" }) + : sname.expression, sname instanceof Cola.AST_Sub ? sname.property : new Cola.AST_String({ value: sname.property }), @@ -1113,7 +1123,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){ var defCache = []; newNode = []; node.definitions.forEach(function(def, i){ if(def.name instanceof Cola.AST_SymbolVar){ - if(!def.value) + if(node.mods && node.mods.indexOf("const") != -1 && !def.value) Cola.Parser.prototype.token_error.call(Cola.Parser.prototype, def.start, "`const` var can't have `undefined` value"); defCache.push(def); @@ -1132,7 +1142,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){ } var texpr = def.name; - while(!(texpr.expression instanceof Cola.AST_SymbolVar)) texpr = texpr.expression; + while(!(texpr.expression instanceof Cola.AST_Symbol || texpr.expression instanceof Cola.AST_Constant)) texpr = texpr.expression; texpr.expression = new Cola.AST_SymbolRef(texpr.expression); newNode.push(Cola.DefPropWithMods(def, node.mods)); @@ -1166,16 +1176,17 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){ Math.rand = function rand(){} */ - if((node instanceof Cola.AST_Defun || node instanceof Cola.AST_Setter || node instanceof Cola.AST_Getter) && !(node.name instanceof Cola.AST_SymbolDefun)){ - var texpr = node.name; - while(!(texpr.expression instanceof Cola.AST_SymbolDefun)) texpr = texpr.expression; + if((node instanceof Cola.AST_Defun || node instanceof Cola.AST_Function || node instanceof Cola.AST_Setter || node instanceof Cola.AST_Getter) + && node.name && !(node.name instanceof Cola.AST_SymbolDefun) && !(node.name instanceof Cola.AST_SymbolLambda)){ + var texpr = node.name, notst = node instanceof Cola.AST_Function; + while(!(texpr.expression instanceof Cola.AST_Symbol || texpr.expression instanceof Cola.AST_Constant)) texpr = texpr.expression; texpr.expression = new Cola.AST_SymbolRef(texpr.expression); node = Cola.DefFunWithMods(node, node.mods); if(node instanceof Cola.AST_Call && node.expression instanceof Cola.AST_SymbolRef && node.expression.name == "_ColaRuntime$$updateProperty") _ColaRuntime$$hash[Cola._ColaRuntime$$updateProperty.i] = true; - node = new Cola.AST_SimpleStatement({ + if(!notst) node = new Cola.AST_SimpleStatement({ body : node }); } else @@ -1223,14 +1234,80 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){ } else /* - class Test extends Test2 { + class Test extends ParentTest { - int i = 123; + const int i = 123; - Test(){ - - } + Test(){ // constructor + super(); + } + Test.Test2(){ // named constructor + super.Test2(); + } + + queryAll("ul li a").forEach((el) => + el.onclick = () => console.log("click") + ); + + covert String method1() => console.log("method"); + + get getter() => i; + set setter(int val) => console.log(val); + + superExample(){ + super.superExample(); + } + + selfExample(){ + query("*").onclick = () => console.log(self); + } + + } + + to + + Test(){ + this.pre_constructor(); + + this.super.constructor.call(this); + + this.post_constructor(); + } + Test.prototype = _ColaRuntime$$proto(ParentTest); + + Test::Test2 = Test.Test2(){ + this.pre_constructor(); + + this.super.Test2.call(this); + + this.post_constructor(); + }; + Test.Test2.prototype = Test.prototype; + + Test::pre_constructor(){ + this.super = ParentTest.prototype; + const int this.i = 123; + } + + Test::post_constructor(){ + queryAll("ul li a").forEach((el) => + el.onclick = () => console.log("click") + ); + } + + covert String Test::method1() => console.log("method"); + + get Test::getter() => i; + set Test::setter(int val) => console.log(val); + + Test::superExample(){ + this.super.superExample.call(this); + } + + Test::selfExample(){ + var self = this; + query("*").onclick = () => console.log(self); } */