jslint on structs
This commit is contained in:
parent
7d89299827
commit
d6cf8236e2
|
@ -1,15 +1,72 @@
|
||||||
// Structure types
|
/*jslint browser: true, unparam: true, vars: true, white: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
|
||||||
|
/*globals $*/
|
||||||
(function(baselib) {
|
(function (baselib) {
|
||||||
|
"use strict";
|
||||||
var exports = {};
|
var exports = {};
|
||||||
baselib.structs = exports;
|
baselib.structs = exports;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
var Struct = function (constructorName, fields) {
|
||||||
|
this._constructorName = constructorName;
|
||||||
|
this._fields = [];
|
||||||
|
};
|
||||||
|
|
||||||
|
Struct.prototype.toWrittenString = function (cache) {
|
||||||
|
var buffer = [], i;
|
||||||
|
cache.put(this, true);
|
||||||
|
buffer.push("(");
|
||||||
|
buffer.push(this._constructorName);
|
||||||
|
for(i = 0; i < this._fields.length; i++) {
|
||||||
|
buffer.push(" ");
|
||||||
|
buffer.push(baselib.format.toWrittenString(this._fields[i], cache));
|
||||||
|
}
|
||||||
|
buffer.push(")");
|
||||||
|
return buffer.join("");
|
||||||
|
};
|
||||||
|
|
||||||
|
Struct.prototype.toDisplayedString = function (cache) {
|
||||||
|
return baselib.format.toWrittenString(this, cache);
|
||||||
|
};
|
||||||
|
|
||||||
|
Struct.prototype.toDomNode = function (params) {
|
||||||
|
var node = document.createElement("span"), i;
|
||||||
|
params.put(this, true);
|
||||||
|
$(node).append(document.createTextNode("("));
|
||||||
|
$(node).append(document.createTextNode(this._constructorName));
|
||||||
|
for(i = 0; i < this._fields.length; i++) {
|
||||||
|
$(node).append(document.createTextNode(" "));
|
||||||
|
$(node).append(baselib.format.toDomNode(this._fields[i], params));
|
||||||
|
}
|
||||||
|
$(node).append(document.createTextNode(")"));
|
||||||
|
return node;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Struct.prototype.equals = function (other, aUnionFind) {
|
||||||
|
var i;
|
||||||
|
if (!(other instanceof this.type)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (i = 0; i < this._fields.length; i++) {
|
||||||
|
if (! baselib.equality.equals(this._fields[i],
|
||||||
|
other._fields[i],
|
||||||
|
aUnionFind)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
var StructType = function(name, // string
|
Struct.prototype.type = Struct;
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
var StructType = function (name, // string
|
||||||
type, // StructType
|
type, // StructType
|
||||||
numberOfArgs, // number
|
numberOfArgs, // number
|
||||||
numberOfFields, // number
|
numberOfFields, // number
|
||||||
|
@ -33,12 +90,12 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
StructType.prototype.toString = function(cache) {
|
StructType.prototype.toString = function (cache) {
|
||||||
return '#<struct-type:' + this.name + '>';
|
return '#<struct-type:' + this.name + '>';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
StructType.prototype.equals = function(other, aUnionFind) {
|
StructType.prototype.equals = function (other, aUnionFind) {
|
||||||
return this === other;
|
return this === other;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -52,199 +109,8 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// makeStructureType: string StructType number number boolean
|
|
||||||
// guard-function -> StructType
|
|
||||||
//
|
|
||||||
// Creates a new structure type.
|
|
||||||
|
|
||||||
var makeStructureType = function(theName,
|
|
||||||
parentType,
|
|
||||||
initFieldCnt,
|
|
||||||
autoFieldCnt,
|
|
||||||
autoV,
|
|
||||||
guard) {
|
|
||||||
// Defaults
|
|
||||||
autoFieldCnt = autoFieldCnt || 0;
|
|
||||||
parentType = parentType || DEFAULT_PARENT_TYPE;
|
|
||||||
guard = guard || DEFAULT_GUARD;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// rawConstructor creates a new struct type inheriting from
|
|
||||||
// the parent, with no guard checks.
|
|
||||||
var rawConstructor = function(name, args) {
|
|
||||||
parentType.type.call(this, name, args);
|
|
||||||
for (var i = 0; i < initFieldCnt; i++) {
|
|
||||||
this._fields.push(args[i+parentType.numberOfArgs]);
|
|
||||||
}
|
|
||||||
for (var i = 0; i < autoFieldCnt; i++) {
|
|
||||||
this._fields.push(autoV);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
rawConstructor.prototype = baselib.heir(parentType.type.prototype);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Set type, necessary for equality checking
|
|
||||||
rawConstructor.prototype.type = rawConstructor;
|
|
||||||
|
|
||||||
// The structure type consists of the name, its constructor, a
|
|
||||||
// record of how many argument it and its parent type contains,
|
|
||||||
// the list of autofields, the guard, and functions corresponding
|
|
||||||
// to the constructor, the predicate, the accessor, and mutators.
|
|
||||||
var newType = new StructType(
|
|
||||||
theName,
|
|
||||||
rawConstructor,
|
|
||||||
initFieldCnt + parentType.numberOfArgs,
|
|
||||||
initFieldCnt + autoFieldCnt,
|
|
||||||
parentType.firstField + parentType.numberOfFields,
|
|
||||||
function(args, name, k) {
|
|
||||||
return guard(args, name,
|
|
||||||
function(result) {
|
|
||||||
var parentArgs = result.slice(0, parentType.numberOfArgs);
|
|
||||||
var restArgs = result.slice(parentType.numberOfArgs);
|
|
||||||
return parentType.applyGuard(
|
|
||||||
parentArgs, name,
|
|
||||||
function(parentRes) {
|
|
||||||
return k( parentRes.concat(restArgs) ); });
|
|
||||||
});
|
|
||||||
},
|
|
||||||
// constructor
|
|
||||||
function() {
|
|
||||||
var args = [].slice.call(arguments);
|
|
||||||
return newType.applyGuard(
|
|
||||||
args,
|
|
||||||
baselib.symbols.Symbol.makeInstance(theName),
|
|
||||||
function(res) {
|
|
||||||
return new rawConstructor(theName, res); });
|
|
||||||
},
|
|
||||||
|
|
||||||
// predicate
|
|
||||||
function(x) {
|
|
||||||
return x instanceof rawConstructor;
|
|
||||||
},
|
|
||||||
|
|
||||||
// accessor
|
|
||||||
function(x, i) { return x._fields[i + this.firstField]; },
|
|
||||||
|
|
||||||
// mutator
|
|
||||||
function(x, i, v) { x._fields[i + this.firstField] = v; });
|
|
||||||
return newType;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var Struct = function(constructorName, fields) {
|
|
||||||
this._constructorName = constructorName;
|
|
||||||
this._fields = [];
|
|
||||||
};
|
|
||||||
|
|
||||||
Struct.prototype.toWrittenString = function(cache) {
|
|
||||||
cache.put(this, true);
|
|
||||||
var buffer = [];
|
|
||||||
buffer.push("(");
|
|
||||||
buffer.push(this._constructorName);
|
|
||||||
for(var i = 0; i < this._fields.length; i++) {
|
|
||||||
buffer.push(" ");
|
|
||||||
buffer.push(plt.baselib.format.toWrittenString(this._fields[i], cache));
|
|
||||||
}
|
|
||||||
buffer.push(")");
|
|
||||||
return buffer.join("");
|
|
||||||
};
|
|
||||||
|
|
||||||
Struct.prototype.toDisplayedString = function(cache) {
|
|
||||||
return plt.baselib.format.toWrittenString(this, cache);
|
|
||||||
};
|
|
||||||
|
|
||||||
Struct.prototype.toDomNode = function(params) {
|
|
||||||
params.put(this, true);
|
|
||||||
var node = document.createElement("span");
|
|
||||||
$(node).append(document.createTextNode("("));
|
|
||||||
$(node).append(document.createTextNode(this._constructorName));
|
|
||||||
for(var i = 0; i < this._fields.length; i++) {
|
|
||||||
$(node).append(document.createTextNode(" "));
|
|
||||||
$(node).append(plt.baselib.format.toDomNode(this._fields[i], params));
|
|
||||||
}
|
|
||||||
$(node).append(document.createTextNode(")"));
|
|
||||||
return node;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
Struct.prototype.equals = function(other, aUnionFind) {
|
|
||||||
if ( other.type == undefined ||
|
|
||||||
this.type !== other.type ||
|
|
||||||
!(other instanceof this.type) ) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < this._fields.length; i++) {
|
|
||||||
if (! equals(this._fields[i],
|
|
||||||
other._fields[i],
|
|
||||||
aUnionFind)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Struct.prototype.type = Struct;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// // Struct Procedure types
|
|
||||||
// var StructProc = function(type, name, numParams, isRest, usesState, impl) {
|
|
||||||
// PrimProc.call(this, name, numParams, isRest, usesState, impl);
|
|
||||||
// this.type = type;
|
|
||||||
// };
|
|
||||||
// StructProc.prototype = baselib.heir(PrimProc.prototype);
|
|
||||||
|
|
||||||
// var StructConstructorProc = function() {
|
|
||||||
// StructProc.apply(this, arguments);
|
|
||||||
// };
|
|
||||||
// StructConstructorProc.prototype = baselib.heir(StructProc.prototype);
|
|
||||||
|
|
||||||
// var StructPredicateProc = function() {
|
|
||||||
// StructProc.apply(this, arguments);
|
|
||||||
// };
|
|
||||||
// StructPredicateProc.prototype = baselib.heir(StructProc.prototype);
|
|
||||||
|
|
||||||
// var StructAccessorProc = function() {
|
|
||||||
// StructProc.apply(this, arguments);
|
|
||||||
// };
|
|
||||||
// StructAccessorProc.prototype = baselib.heir(StructProc.prototype);
|
|
||||||
|
|
||||||
// var StructMutatorProc = function() {
|
|
||||||
// StructProc.apply(this, arguments);
|
|
||||||
// };
|
|
||||||
// StructMutatorProc.prototype = baselib.heir(StructProc.prototype);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Default structure guard just calls the continuation argument.
|
// Default structure guard just calls the continuation argument.
|
||||||
var DEFAULT_GUARD = function(args, name, k) {
|
var DEFAULT_GUARD = function (args, name, k) {
|
||||||
return k(args);
|
return k(args);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -258,8 +124,98 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var isStruct = function(x) { return x instanceof Struct; };
|
// makeStructureType: string StructType number number boolean
|
||||||
var isStructType = function(x) { return x instanceof StructType; };
|
// guard-function -> StructType
|
||||||
|
//
|
||||||
|
// Creates a new structure type.
|
||||||
|
|
||||||
|
var makeStructureType = function (theName,
|
||||||
|
parentType,
|
||||||
|
initFieldCnt,
|
||||||
|
autoFieldCnt,
|
||||||
|
autoV,
|
||||||
|
guard) {
|
||||||
|
|
||||||
|
|
||||||
|
// Defaults
|
||||||
|
autoFieldCnt = autoFieldCnt || 0;
|
||||||
|
parentType = parentType || DEFAULT_PARENT_TYPE;
|
||||||
|
guard = guard || DEFAULT_GUARD;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// RawConstructor creates a new struct type inheriting from
|
||||||
|
// the parent, with no guard checks.
|
||||||
|
var RawConstructor = function (name, args) {
|
||||||
|
var i;
|
||||||
|
parentType.type.call(this, name, args);
|
||||||
|
for (i = 0; i < initFieldCnt; i++) {
|
||||||
|
this._fields.push(args[i+parentType.numberOfArgs]);
|
||||||
|
}
|
||||||
|
for (i = 0; i < autoFieldCnt; i++) {
|
||||||
|
this._fields.push(autoV);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
RawConstructor.prototype = baselib.heir(parentType.type.prototype);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Set type, necessary for equality checking
|
||||||
|
RawConstructor.prototype.type = RawConstructor;
|
||||||
|
|
||||||
|
// The structure type consists of the name, its constructor, a
|
||||||
|
// record of how many argument it and its parent type contains,
|
||||||
|
// the list of autofields, the guard, and functions corresponding
|
||||||
|
// to the constructor, the predicate, the accessor, and mutators.
|
||||||
|
var newType = new StructType(
|
||||||
|
theName,
|
||||||
|
RawConstructor,
|
||||||
|
initFieldCnt + parentType.numberOfArgs,
|
||||||
|
initFieldCnt + autoFieldCnt,
|
||||||
|
parentType.firstField + parentType.numberOfFields,
|
||||||
|
function (args, name, k) {
|
||||||
|
return guard(args, name,
|
||||||
|
function (result) {
|
||||||
|
var parentArgs = result.slice(0, parentType.numberOfArgs);
|
||||||
|
var restArgs = result.slice(parentType.numberOfArgs);
|
||||||
|
return parentType.applyGuard(
|
||||||
|
parentArgs, name,
|
||||||
|
function (parentRes) {
|
||||||
|
return k( parentRes.concat(restArgs) ); });
|
||||||
|
});
|
||||||
|
},
|
||||||
|
// constructor
|
||||||
|
function () {
|
||||||
|
var args = [].slice.call(arguments);
|
||||||
|
return newType.applyGuard(
|
||||||
|
args,
|
||||||
|
baselib.symbols.Symbol.makeInstance(theName),
|
||||||
|
function (res) {
|
||||||
|
return new RawConstructor(theName, res); });
|
||||||
|
},
|
||||||
|
|
||||||
|
// predicate
|
||||||
|
function (x) {
|
||||||
|
return x instanceof RawConstructor;
|
||||||
|
},
|
||||||
|
|
||||||
|
// accessor
|
||||||
|
function (x, i) { return x._fields[i + this.firstField]; },
|
||||||
|
|
||||||
|
// mutator
|
||||||
|
function (x, i, v) { x._fields[i + this.firstField] = v; });
|
||||||
|
return newType;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var isStruct = function (x) { return x instanceof Struct; };
|
||||||
|
var isStructType = function (x) { return x instanceof StructType; };
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
@ -271,16 +227,4 @@
|
||||||
exports.isStruct = isStruct;
|
exports.isStruct = isStruct;
|
||||||
exports.isStructType = isStructType;
|
exports.isStructType = isStructType;
|
||||||
|
|
||||||
// exports.StructProc = StructProc;
|
}(this.plt.baselib));
|
||||||
// exports.StructConstructorProc = StructConstructorProc;
|
|
||||||
// exports.StructPredicateProc = StructPredicateProc;
|
|
||||||
// exports.StructAccessorProc = StructAccessorProc;
|
|
||||||
// exports.StructMutatorProc = StructMutatorProc;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
})(this['plt'].baselib);
|
|
Loading…
Reference in New Issue
Block a user