diff --git a/lib/ast.js b/lib/ast.js index 6c27cbaf..2843bbb9 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -884,7 +884,7 @@ Cola.AST_SymbolConst = Cola.DEFNODE("SymbolConst", null, { $documentation: "A constant declaration" }, Cola.AST_SymbolDeclaration); -Cola.AST_SymbolFunarg = Cola.DEFNODE("SymbolFunarg", null, { +Cola.AST_SymbolFunarg = Cola.DEFNODE("SymbolFunarg", "type", { $documentation: "Symbol naming a function argument", }, Cola.AST_SymbolVar); diff --git a/lib/index.html b/lib/index.html index 537334e0..f369a916 100644 --- a/lib/index.html +++ b/lib/index.html @@ -295,6 +295,25 @@ main(); return stream.toString(); } + function Mangle(){ + var ast; + + try { + // 1. compile + ast = Cola.parse(source, { is_js : isjs.checked }); + if(!isjs.checked) ast = ast.toJavaScript({ main_binding : mainbinding.checked }); + + // 3. mangle + //ast.figure_out_scope(); + //ast.compute_char_frequency(); + //ast.mangle_names({ sort : true, toplevel : true }); + + return ast; + } catch(e){ + throw e; + } + } + compile(); changeClass(); diff --git a/lib/parse.js b/lib/parse.js index 25d7325f..6f2c9be1 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1318,7 +1318,7 @@ Cola.Parser.prototype.as_funcarg = function(splatedexist) { return new Cola.AST_ArgDef({ name : name, - type : argtype == "splated" ? "Array" : type == name ? "dynamic" : type.name, + type : name.type = (argtype == "splated" ? "Array" : type == name ? "dynamic" : type.name), argtype : argtype, defval : defval, start : type.start, @@ -1805,13 +1805,13 @@ Cola.Parser.prototype.array_ = Cola.Parser.embed_tokens(function(is_template, is is_array = false; a.push(new Cola.AST_Noop()); } else - if (!is_var && this.is("name") && this.next_is("name") && !Cola.cKEYWORDS(this.peek().value)) { + if (!is_var && this.is("name") && this.next_is("name") && (!Cola.cKEYWORDS(this.peek().value) || this.peek().value == "var")) { if (is_array === true) this.unexpected(); is_array = false; vardef = true; a.push(new Cola.AST_VarDef({ start : this.S.token, - type : this.S.token.value, + type : this.S.token.value == "var" ? "dynamic" : this.S.token.value, name : (function(_this){ _this.next(); val = _this.as_symbol(Cola.AST_SymbolVar); @@ -1875,9 +1875,9 @@ Cola.Parser.prototype.object_ = Cola.Parser.embed_tokens(function(is_template, i var start = this.S.token; var type = start.type; var name = this.as_property_name(); - if (!this.is_js && !is_var && this.is("name") && !this.next_is("punc", "(") && !Cola.cKEYWORDS(name)) { + if (!this.is_js && !is_var && this.is("name") && !this.next_is("punc", "(") && (!Cola.cKEYWORDS(name) || name == "var")) { vardef = true; - ptype = name; + ptype = name == "var" ? "dynamic" : name; name = this.as_name(); } else ptype = false; if (type == "name" && !this.is("punc", ":")) { diff --git a/lib/typecheck.js b/lib/typecheck.js new file mode 100644 index 00000000..7c73ee35 --- /dev/null +++ b/lib/typecheck.js @@ -0,0 +1,66 @@ +/*********************************************************************** + + AST-Tree translator, ColaScript -> JavaScript. + + Distributed under the BSD license: + + Copyright 2014 (c) TrigenSoftware + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above + copyright notice, this list of conditions and the following + disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials + provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + + ***********************************************************************/ + +"use strict"; +!this.Cola && (this.Cola = {}); + +/* + MD: + + Defs: + - var definition + - function definition + - function argument definition + + * AST_VarDef + * AST_Defun + * AST_Function + * AST_SymbolFunarg + + Levels of follow: + 1 level: Simple types: String, Number, Boolean, Function. + 2 level: Arrays and Object ( Array, Array ). + Deep-level: interfaces, classes, singletones. + + Operations to check: + * AST_Return + * AST_Call + * AST_UnaryPrefix + * AST_UnaryPostfix + * AST_Binary + * AST_Conditional + * AST_Assign +*/ \ No newline at end of file