From 2f11d32a70344ee27d99c7e83f76b771ec49acd3 Mon Sep 17 00:00:00 2001 From: Onoshko Dan Date: Fri, 18 Apr 2014 13:10:20 +0700 Subject: [PATCH] Adding std functions if need. --- lib/index.html | 6 +++--- lib/parse.js | 15 ++++++++------- lib/std.js | 13 ++++++++++--- lib/translate.js | 15 +++++++++++++-- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/index.html b/lib/index.html index 4dd04b01..22cbd656 100644 --- a/lib/index.html +++ b/lib/index.html @@ -64,11 +64,11 @@ localStorage.isjs = isjs.checked ? "t" : "f"; stream = new Cola.OutputStream({ beautify : true, is_js : isjs.checked }); - compressor = Cola.Compressor({ is_js : isjs.checked }); + compressor = new Cola.Compressor({ is_js : isjs.checked }); try { // 1. compile - ast = Cola.parse(source, null, isjs.checked); + ast = Cola.parse(source, { is_js : isjs.checked }); if(!isjs.checked) ast = ast.toJavaScript(); ast.print(stream); translationArea.value = stream.toString(); @@ -82,7 +82,7 @@ ast.compute_char_frequency(); ast.mangle_names({ is_js : isjs.checked, sort : true, toplevel : true }); - stream = new Cola.OutputStream(); + stream = new Cola.OutputStream({ is_js : isjs.checked }); ast.print(stream); resultArea.value = stream.toString(); } catch(e){ diff --git a/lib/parse.js b/lib/parse.js index 340c1f7d..9be82dc7 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -780,24 +780,25 @@ Cola.ATOMIC_START_TOKEN = Cola.array_to_hash([ "atom", "num", "string", "regexp" /* -----[ Parser ]----- */ -Cola.parse = function ($TEXT, options, is_js) { - return (new Cola.Parser($TEXT, options, is_js)).parse(); +Cola.parse = function ($TEXT, options) { + return (new Cola.Parser($TEXT, options)).parse(); }; -Cola.Parser = function ($TEXT, options, is_js) { +Cola.Parser = function ($TEXT, options) { var _this = this; - this.is_js = !!is_js; + this.is_js = !!options.is_js; this.options = Cola.defaults(options, { strict : false, filename : null, toplevel : null, expression : false, - html5_comments : true + html5_comments : true, + is_js : false }); - this.tokenizer = typeof $TEXT == "string" ? new Cola.Tokenizer($TEXT, this.options.filename, is_js, this.options.html5_comments) : $TEXT; + this.tokenizer = typeof $TEXT == "string" ? new Cola.Tokenizer($TEXT, this.options.filename, this.is_js, this.options.html5_comments) : $TEXT; this.S = { input : function(){ return _this.tokenizer.next_token() }, @@ -812,7 +813,7 @@ Cola.Parser = function ($TEXT, options, is_js) { this.S.input.context = function(){ return _this.tokenizer.context() }; this.S.token = this.next(); - if(is_js){ + if(this.is_js){ this.UNARY_PREFIX = Cola.UNARY_PREFIX; this.PRECEDENCE = Cola.PRECEDENCE; this.ASSIGNMENT = Cola.ASSIGNMENT; diff --git a/lib/std.js b/lib/std.js index a902b348..dbbc53af 100644 --- a/lib/std.js +++ b/lib/std.js @@ -35,21 +35,28 @@ ***********************************************************************/ function $_cola_is(_object, _type){ - return _object instanceof _type || _object.__proto__ == _type.prototype; + return _object instanceof _type || _object.__proto__ === _type.prototype; } +$_cola_is.i = 0; function $_cola_isnt(_object, _type){ - return !(_object instanceof _type || _object.__proto__ == _type.prototype); + return !(_object instanceof _type || _object.__proto__ === _type.prototype); } +$_cola_isnt.i = 1; function $_cola_modulo(_a, _b){ return (_a % _b + +_b) % _b; } +$_cola_modulo.i = 2; function $_cola_isset(_object){ return !(typeof _object === "undefined" || _object === null); } +$_cola_isset.i = 3; function $_cola_isntset(_object){ return (typeof _object === "undefined" || _object === null); -} \ No newline at end of file +} +$_cola_isntset.i = 4; + +$_cola = $_cola_is + $_cola_isnt + $_cola_modulo + $_cola_isset + $_cola_isntset; diff --git a/lib/translate.js b/lib/translate.js index d8dd26eb..a8469355 100644 --- a/lib/translate.js +++ b/lib/translate.js @@ -41,7 +41,8 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){ if(this.language == 'js') return this; this.language = 'js'; - var tt = new Cola.TreeTransformer(null, function(node){ + var $_cola_ast = Cola.parse($_cola, { is_js : true}), $_cola_hash = {}, _this, + tt = new Cola.TreeTransformer(null, function(node){ var newNode, props; if(node instanceof Cola.AST_Binary && node.operator == '**'){ @@ -61,6 +62,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){ } else if(node instanceof Cola.AST_Binary && node.operator == '%%'){ + $_cola_hash[$_cola_modulo.i] = true; props = { args : [node.left, node.right], start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_modulo' }), @@ -76,6 +78,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){ } else if(node instanceof Cola.AST_SimpleStatement && node.body instanceof Cola.AST_Assign && node.body.operator == '?='){ + $_cola_hash[$_cola_isntset.i] = true; node.body.operator = '='; props = { @@ -98,6 +101,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){ } else if(node instanceof Cola.AST_Assign && node.operator == '?='){ + $_cola_hash[$_cola_isntset.i] = true; node.operator = '='; props = { @@ -121,6 +125,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){ } else if(node instanceof Cola.AST_Binary && node.operator == 'is'){ + $_cola_hash[$_cola_is.i] = true; props = { args : [node.left, node.right], start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_is' }), @@ -136,6 +141,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){ } else if(node instanceof Cola.AST_Binary && node.operator == 'isnt'){ + $_cola_hash[$_cola_isnt.i] = true; props = { args : [node.left, node.right], start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_isnt' }), @@ -177,5 +183,10 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){ return node; }); - return this.transform(tt); + _this = this.transform(tt); + + for(var i in $_cola_hash) if($_cola_hash.hasOwnProperty(i)) + _this.body.unshift($_cola_ast.body[i]); + + return _this; }; \ No newline at end of file