Prepare to typing.

This commit is contained in:
Onoshko Dan 2014-06-12 02:14:36 +07:00
parent a46c1f21a0
commit c72d6f0a4a
4 changed files with 91 additions and 6 deletions

View File

@ -884,7 +884,7 @@ Cola.AST_SymbolConst = Cola.DEFNODE("SymbolConst", null, {
$documentation: "A constant declaration" $documentation: "A constant declaration"
}, Cola.AST_SymbolDeclaration); }, Cola.AST_SymbolDeclaration);
Cola.AST_SymbolFunarg = Cola.DEFNODE("SymbolFunarg", null, { Cola.AST_SymbolFunarg = Cola.DEFNODE("SymbolFunarg", "type", {
$documentation: "Symbol naming a function argument", $documentation: "Symbol naming a function argument",
}, Cola.AST_SymbolVar); }, Cola.AST_SymbolVar);

View File

@ -295,6 +295,25 @@ main();</textarea>
return stream.toString(); 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(); compile();
changeClass(); changeClass();
</script> </script>

View File

@ -1318,7 +1318,7 @@ Cola.Parser.prototype.as_funcarg = function(splatedexist) {
return new Cola.AST_ArgDef({ return new Cola.AST_ArgDef({
name : name, name : name,
type : argtype == "splated" ? "Array" : type == name ? "dynamic" : type.name, type : name.type = (argtype == "splated" ? "Array" : type == name ? "dynamic" : type.name),
argtype : argtype, argtype : argtype,
defval : defval, defval : defval,
start : type.start, start : type.start,
@ -1805,13 +1805,13 @@ Cola.Parser.prototype.array_ = Cola.Parser.embed_tokens(function(is_template, is
is_array = false; is_array = false;
a.push(new Cola.AST_Noop()); a.push(new Cola.AST_Noop());
} else } 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(); if (is_array === true) this.unexpected();
is_array = false; is_array = false;
vardef = true; vardef = true;
a.push(new Cola.AST_VarDef({ a.push(new Cola.AST_VarDef({
start : this.S.token, start : this.S.token,
type : this.S.token.value, type : this.S.token.value == "var" ? "dynamic" : this.S.token.value,
name : (function(_this){ name : (function(_this){
_this.next(); _this.next();
val = _this.as_symbol(Cola.AST_SymbolVar); 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 start = this.S.token;
var type = start.type; var type = start.type;
var name = this.as_property_name(); 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; vardef = true;
ptype = name; ptype = name == "var" ? "dynamic" : name;
name = this.as_name(); name = this.as_name();
} else ptype = false; } else ptype = false;
if (type == "name" && !this.is("punc", ":")) { if (type == "name" && !this.is("punc", ":")) {

66
lib/typecheck.js Normal file
View File

@ -0,0 +1,66 @@
/***********************************************************************
AST-Tree translator, ColaScript -> JavaScript.
Distributed under the BSD license:
Copyright 2014 (c) TrigenSoftware <danon0404@gmail.com>
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<val_type>, Array<key_type, val_type> ).
Deep-level: interfaces, classes, singletones.
Operations to check:
* AST_Return
* AST_Call
* AST_UnaryPrefix
* AST_UnaryPostfix
* AST_Binary
* AST_Conditional
* AST_Assign
*/