From e5d3cb56c0de4b2e9d1a40282d8aba549344ac3b Mon Sep 17 00:00:00 2001 From: Onoshko Dan Date: Thu, 3 Jul 2014 01:08:24 +0700 Subject: [PATCH] in progress... --- lib/typecheck.js | 143 +++++++++++++++++++++++++++++++---------------- 1 file changed, 96 insertions(+), 47 deletions(-) diff --git a/lib/typecheck.js b/lib/typecheck.js index 83452867..6a4e7df2 100644 --- a/lib/typecheck.js +++ b/lib/typecheck.js @@ -39,90 +39,78 @@ /* 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 : interfaces, classes (prototypes), singletones. - - Operations to check: - * AST_Return - * AST_Call - * AST_UnaryPrefix - * AST_UnaryPostfix - * AST_Binary - * AST_Conditional - * AST_Assign - - Warnings: - * type mismatch - * redefinition of variable + */ -// TypedObject +// RefsStorage -Cola.TypedObject = function (name, vartype, isfunc) { - this.name = name; - this.vartype = vartype; - this.isfunc = isfunc; -}; - - -// TypeStorage - -Cola.TypeStorage = function (parentStorage) { +Cola.RefsStorage = function (parentStorage) { this.parentStorage = parentStorage; + this.singletype = false; this.objs = []; }; -Cola.TypeStorage.prototype.contains = function (name, isfunc) { +Cola.RefsStorage.Element = function (name, vartype, callable) { + this.name = name; + this.vartype = vartype; + this.callable = callable; + this.props = new Cola.RefsStorage(); +}; + +Cola.RefsStorage.prototype.setSingleType = function (type) { + if(this.parentStorage) return false; + + this.singletype = true; + this.objs = [type]; +}; + +Cola.RefsStorage.prototype.contains = function (name, callable) { return this.objs.some(function(el){ - return el.name == name && el.isfunc == isfunc; + return el.name == name && el.callable == callable; }); }; -Cola.TypeStorage.prototype.remove = function (name, isfunc) { +Cola.RefsStorage.prototype.remove = function (name, callable) { var pre = this.objs.length; this.objs = this.objs.filter(function(el){ - return !(el.name == name && el.isfunc == isfunc); + return !(el.name == name && el.callable == callable); }); return pre != this.objs.length; }; -Cola.TypeStorage.prototype.uniqueAdd = function (name, vartype, isfunc) { - if(!this.contains(name, isfunc)){ - this.objs.push(new Cola.TypedObject(name, vartype, isfunc)); +Cola.RefsStorage.prototype.uniqueAdd = function (name, vartype, callable) { + if(!this.contains(name, callable)){ + this.objs.push(new Cola.RefsStorage.Element(name, vartype, callable)); return true; } return false; }; -Cola.TypeStorage.prototype.owerwriteAdd = function (name, vartype, isfunc) { - var removed = this.remove(name, isfunc); - this.objs.push(new Cola.TypedObject(name, vartype, isfunc)); +Cola.RefsStorage.prototype.owerwriteAdd = function (name, vartype, callable) { + var removed = this.remove(name, callable); + this.objs.push(new Cola.RefsStorage.Element(name, vartype, callable)); return removed; }; -Cola.TypeStorage.prototype.get = function (name) { +Cola.RefsStorage.prototype.get = function (name) { + if(this.singletype) return this.objs[0]; + var obj = false; this.objs.some(function(el){ return el.name == name && (obj = el); }); if(!obj && this.parentStorage) obj = this.parentStorage.get(name); + + return obj; }; @@ -130,9 +118,21 @@ Cola.TypeStorage.prototype.get = function (name) { Cola.TypeChecker = function (parentStorage, om) { this.outputMode = om == undefined ? Cola.TypeChecker.WARNING : om; - this.storage = new Cola.TypeStorage(parentStorage, this.outputMode); + this.storage = new Cola.RefsStorage(parentStorage, this.outputMode); }; +/* + Defs: + first step ( simple ) + - vars definition * AST_VarDef - done + - function definition * AST_Defun - done + - function argument definition * AST_SymbolFunarg - done + second step + - object's property assignment * AST_Assign + - prototypes ( classes methods ) * AST_Assign - after classes + - vars definition in constructor ( vars of class ) * AST_Assign - after classes + +*/ Cola.TypeChecker.prototype.reg = function (def) { if(def.forEach) def.forEach(function(d){ if(d instanceof Cola.AST_VarDef && !this.storage.uniqueAdd(d.name.name, d.type, false)){ @@ -150,6 +150,55 @@ Cola.TypeChecker.prototype.reg = function (def) { } }; +/* + Nodes to check: + * AST_Binary + * AST_UnaryPrefix + * AST_UnaryPostfix + * AST_Return + * AST_Conditional + + * AST_VarDef + * AST_ForIn - `in` left argument + * AST_Switch + +*/ +Cola.TypeChecker.prototype.check = function () { + +}; + +/* + Nodes to getting type: + * AST_Binary + * AST_UnaryPrefix + * AST_UnaryPostfix + * AST_Return + * AST_Conditional + + * AST_Call + * AST_Seq + * AST_Dot + * AST_Sub + + * AST_Symbol + + * AST_Atom + * AST_Constant + * AST_Array + * AST_Object + +*/ +Cola.TypeChecker.prototype.get = function () { + +}; + +/* + Warnings: + - type mismatch + - redefinition of variable + - redefinition of function + +*/ Cola.TypeChecker.prototype.printMessage = function (token, msg) { switch(this.outputMode){ case Cola.TypeChecker.WARNING: