# HG changeset patch # User Mike Pavone # Date 1344524954 25200 # Node ID 5d15b91e738aeb0776f04c38541869aaa1a8a620 # Parent 9db0e3533b235a9dd774e4fa28f124e65bbc60dc Fix paramerized types. Remove debug print calls. diff -r 9db0e3533b23 -r 5d15b91e738a types.js --- a/types.js Wed Aug 08 09:19:14 2012 -0700 +++ b/types.js Thu Aug 09 08:09:14 2012 -0700 @@ -64,13 +64,11 @@ this.satisfies_cache[type.id] = true; var ret = true; if (type.messages === undefined) { - print('type has no messages'); ret = false; } if (ret) { for (var msgname in this.messages) { if (!(msgname in type.messages) || !this.messages[msgname].satisfiedBy(type.messages[msgname])) { - print(msgname, 'missing in type'); ret = false; break; } @@ -158,13 +156,11 @@ this.satisfies_cache[type.id] = true; var ret = true; if (!(type.callable) || this.params.length != type.params.length) { - print('type is not callable or param length mismatch'); ret = false; } if (ret) { for (var i in this.params) { if (i >= type.params.length || !this.params[i].satisfiedBy(type.params[i])) { - print('param ', i, ' is not satisfied'); ret = false; break; } @@ -173,7 +169,6 @@ if (ret) { for (var msgname in this.messages) { if (!(msgname in type.messages) || !this.messages[msgname].satisfiedBy(type.messages[msgname])) { - print('message', msgname, 'is not satisfied'); ret = false; break; } @@ -213,9 +208,13 @@ return visited[this.id]; } var me = visited[this.id] = this.clone(); - for (var msgname in this.messages) { + for (var msgname in me.messages) { me.messages[msgname] = me.messages[msgname].replaceParams(paramtypes, visited); } + for (var i in me.params) { + me.params[i] = me.params[i].replaceParams(paramtypes, visited); + } + me.returntype = me.returntype.replaceParams(paramtypes, visited); return me; }; @@ -239,68 +238,82 @@ this.satisfies_cache = null; } -uniontype.prototype.lazyinit = function() { - if (this.satisfies_cache == null) { - this.satisfies_cache = {}; - this.satisfies_cache[this.id] = true; - this.messages = {}; - if (this.a.messages !== undefined && this.b.messages !== undefined) { - for (var msgname in this.a.messages) { - if (msgname in this.b.messages) { - this.messages[msgname] = mkunion(this.a.messages[msgname], this.b.messages[msgname]); +uniontype.prototype = { + lazyinit: function() { + if (this.satisfies_cache == null) { + this.satisfies_cache = {}; + this.satisfies_cache[this.id] = true; + this._messages = {}; + if (this.a.messages !== undefined && this.b.messages !== undefined) { + for (var msgname in this.a.messages) { + if (msgname in this.b.messages) { + this._messages[msgname] = mkunion(this.a.messages[msgname], this.b.messages[msgname]); + } } } + this._callable = false; + if (this.a.callable && this.b.callable && this.a.params.length == this.b.params.length) { + this._callable = true; + this._params = []; + for (var i = 0; i < this.a.params.length; i++) { + this._params.push(mkunion(this.a.params[i], this.b.params[i])); + } + this._returntype = mkunion(this.a.returntype, this.b.returntype); + } } - this.callable = false; - if (this.a.callable && this.b.callable && this.a.params.length == this.b.params.length) { - this.callable = true; - this.params = []; - for (var i = 0; i < this.a.params.length; i++) { - this.params.push(mkunion(this.a.params[i], this.b.params[i])); - } - this.returntype = mkunion(this.a.returntype, this.b.returntype); + }, + satisfiedBy: function(type) + { + this.lazyinit(); + if (type.id in this.satisfies_cache) { + return this.satisfies_cache[type.id]; + } + this.satisfies_cache[type.id] = true; + var ret = this.a.satisfiedBy(type) || this.b.satisfiedBy(type); + this.satisfies_cache[type.id] = ret; + return ret; + }, + str: function(indent) + { + if (indent === undefined) { + indent = ''; + } + if (indent.length > 6) { + return 'max depth reached\n'; + } + return indent + 'Union {\n\t' + indent + this.a.str(indent+'\t') + '\t' + indent + this.b.str(indent+'\t') + indent + '}\n'; + }, + replaceParams: function(paramtypes, visited) { + if (visited === undefined) { + visited = {}; } - } -}; - -uniontype.prototype.satisfiedBy = function(type) -{ - this.lazyinit(); - if (type.id in this.satisfies_cache) { - return this.satisfies_cache[type.id]; + if (this.id in visited) { + return visited[this.id]; + } + var me = visited[this.id] = this.clone(); + me.a = me.a.replaceParams(paramtypes, visited); + me.b = me.b.replaceParams(paramtypes, visited); + return me; + }, + clone: function() { + return new uniontype(this.a, this.b); + }, + get messages() { + this.lazyinit(); + return this._messages; + }, + get params() { + this.lazyinit(); + return this._params; + }, + get returntype() { + this.lazyinit(); + return this._returntype; + }, + get callable() { + this.lazyinit(); + return this._callable; } - this.satisfies_cache[type.id] = true; - var ret = this.a.satisfiedBy(type) || this.b.satisfiedBy(type); - this.satisfies_cache[type.id] = ret; - return ret; -}; - -uniontype.prototype.str = function(indent) -{ - if (indent === undefined) { - indent = ''; - } - if (indent.length > 6) { - return 'max depth reached\n'; - } - return indent + 'Union {\n\t' + indent + this.a.str(indent+'\t') + '\t' + indent + this.b.str(indent+'\t') + indent + '}\n'; -}; - -uniontype.prototype.replaceParams = function(paramtypes, visited) { - if (visited === undefined) { - visited = {}; - } - if (this.id in visited) { - return visited[this.id]; - } - var me = visited[this.id] = this.clone(); - me.a = me.a.replaceParams(paramtypes, visited); - me.b = me.b.replaceParams(paramtypes, visited); - return me; -}; - -uniontype.prototype.clone = function() { - return new uniontype(this.a, this.b); }; @@ -353,7 +366,6 @@ if (!this.replaced) { var childptypes = {}; for (var i in this.type.typeparams) { - print(this.type.typeparams[i], 'is', this.params[i].str()); childptypes[this.type.typeparams[i]] = this.params[i] } this.type = this.type.replaceParams(childptypes, {});