getting a better handle of the structure definition

This commit is contained in:
Danny Yoo 2011-07-03 19:45:28 -04:00
parent b597932d4c
commit 3693506796
7 changed files with 142 additions and 107 deletions

View File

@ -318,7 +318,7 @@
[(natural? an-arity) [(natural? an-arity)
(number->string an-arity)] (number->string an-arity)]
[(ArityAtLeast? an-arity) [(ArityAtLeast? an-arity)
(format "(new RUNTIME.ArityAtLeast(~a))" (format "(RUNTIME.arityAtLeast(~a))"
(ArityAtLeast-value an-arity))] (ArityAtLeast-value an-arity))]
[(listof-atomic-arity? an-arity) [(listof-atomic-arity? an-arity)
(assemble-listof-assembled-values (assemble-listof-assembled-values
@ -328,7 +328,7 @@
[(natural? atomic-arity) [(natural? atomic-arity)
(number->string atomic-arity)] (number->string atomic-arity)]
[(ArityAtLeast? atomic-arity) [(ArityAtLeast? atomic-arity)
(format "(new RUNTIME.ArityAtLeast(~a))" (format "(RUNTIME.arityAtLeast(~a))"
(ArityAtLeast-value atomic-arity))])) (ArityAtLeast-value atomic-arity))]))
an-arity))])) an-arity))]))

View File

@ -37,6 +37,7 @@
(define-runtime-path baselib.js "runtime-src/baselib.js") (define-runtime-path baselib.js "runtime-src/baselib.js")
(define-runtime-path baselib_unionfind.js "runtime-src/baselib_unionfind.js") (define-runtime-path baselib_unionfind.js "runtime-src/baselib_unionfind.js")
(define-runtime-path baselib_hash.js "runtime-src/baselib_hash.js") (define-runtime-path baselib_hash.js "runtime-src/baselib_hash.js")
(define-runtime-path baselib_symbol.js "runtime-src/baselib_symbol.js")
(define-runtime-path baselib_structs.js "runtime-src/baselib_structs.js") (define-runtime-path baselib_structs.js "runtime-src/baselib_structs.js")
(define-runtime-path baselib_arity.js "runtime-src/baselib_arity.js") (define-runtime-path baselib_arity.js "runtime-src/baselib_arity.js")
@ -69,6 +70,7 @@
baselib.js baselib.js
baselib_unionfind.js baselib_unionfind.js
baselib_hash.js baselib_hash.js
baselib_symbol.js
baselib_structs.js baselib_structs.js
baselib_arity.js baselib_arity.js

View File

@ -4,26 +4,42 @@
baselib.arity = exports; baselib.arity = exports;
var ArityAtLeast = plt.baselib.structs.makeStructureType(
'arity-at-least', false, 1, 0, false, false);
// An arity is either a primitive number, an ArityAtLeast instance, // An arity is either a primitive number, an ArityAtLeast instance,
// or a list of either primitive numbers or ArityAtLeast instances. // or a list of either primitive numbers or ArityAtLeast instances.
var ArityAtLeast = function(n) {
this.value = n;
var isArityAtLeast = ArityAtLeast.predicate;
var arityAtLeastValue = function(x) {
var val = ArityAtLeast.accessor(x, 0);
return val;
}
ArityAtLeast.type.prototype.toString = function() {
return '#<arity-at-least ' + arityAtLeastValue(this) + '>';
}; };
// isArityMatching: arity natural -> boolean // isArityMatching: arity natural -> boolean
// Produces true if n satisfies the arity. // Produces true if n satisfies the arity.
var isArityMatching = function(arity, n) { var isArityMatching = function(arity, n) {
if (typeof(arity) === 'number') { if (typeof(arity) === 'number') {
return arity === n; return arity === n;
} else if (arity instanceof ArityAtLeast) { } else if (isArityAtLeast(arity)) {
return n >= arity.value; return n >= arityAtLeastValue(arity);
} else { } else {
while (arity !== plt.types.EMPTY) { while (arity !== plt.types.EMPTY) {
if (typeof(arity.first) === 'number') { if (typeof(arity.first) === 'number') {
if (arity.first === n) { return true; } if (arity.first === n) { return true; }
} else if (arity instanceof ArityAtLeast) { } else if (isArityAtLeast(arity)) {
if (n >= arity.first.value) { return true; } if (n >= arityAtLeastValue(arity.first)) { return true; }
} }
arity = arity.rest; arity = arity.rest;
} }
@ -33,8 +49,17 @@
exports.ArityAtLeast = ArityAtLeast;
exports.isArityMatching = isArityMatching;
//////////////////////////////////////////////////////////////////////
exports.ArityAtLeast = ArityAtLeast;
exports.arityAtLeast = function() {
var result = ArityAtLeast.constructor.apply(null, arguments);
return result;
};
exports.isArityAtLeast = isArityAtLeast;
exports.isArityMatching = isArityMatching;
exports.arityAtLeastValue = arityAtLeastValue;
})(this['plt'].baselib); })(this['plt'].baselib);

View File

@ -114,7 +114,7 @@
var args = [].slice.call(arguments); var args = [].slice.call(arguments);
return newType.applyGuard( return newType.applyGuard(
args, args,
Symbol.makeInstance(theName), baselib.Symbol.makeInstance(theName),
function(res) { function(res) {
return new rawConstructor(theName, res); }); return new rawConstructor(theName, res); });
}, },

View File

@ -0,0 +1,55 @@
// Structure types
(function(baselib) {
//////////////////////////////////////////////////////////////////////
// Symbols
//////////////////////////////////////////////////////////////////////
var Symbol = function(val) {
this.val = val;
};
var symbolCache = {};
// makeInstance: string -> Symbol.
Symbol.makeInstance = function(val) {
// To ensure that we can eq? symbols with equal values.
if (!(val in symbolCache)) {
symbolCache[val] = new Symbol(val);
} else {
}
return symbolCache[val];
};
Symbol.prototype.equals = function(other, aUnionFind) {
return other instanceof Symbol &&
this.val === other.val;
};
Symbol.prototype.toString = function(cache) {
return this.val;
};
Symbol.prototype.toWrittenString = function(cache) {
return this.val;
};
Symbol.prototype.toDisplayedString = function(cache) {
return this.val;
};
baselib.Symbol = Symbol;
})(this['plt'].baselib);

View File

@ -669,7 +669,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'format', 'format',
new plt.baselib.arity.ArityAtLeast(1), plt.baselib.arity.arityAtLeast(1),
function(MACHINE) { function(MACHINE) {
var args = [], i, formatString; var args = [], i, formatString;
testArgument(MACHINE, testArgument(MACHINE,
@ -689,7 +689,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'printf', 'printf',
new plt.baselib.arity.ArityAtLeast(1), plt.baselib.arity.arityAtLeast(1),
function(MACHINE) { function(MACHINE) {
var args = [], i, formatString; var args = [], i, formatString;
testArgument(MACHINE, testArgument(MACHINE,
@ -711,7 +711,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'fprintf', 'fprintf',
new plt.baselib.arity.ArityAtLeast(2), plt.baselib.arity.arityAtLeast(2),
function(MACHINE) { function(MACHINE) {
var args = [], i, formatString; var args = [], i, formatString;
testArgument(MACHINE, testArgument(MACHINE,
@ -772,7 +772,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'=', '=',
new plt.baselib.arity.ArityAtLeast(2), plt.baselib.arity.arityAtLeast(2),
function(MACHINE) { function(MACHINE) {
var firstArg = MACHINE.env[MACHINE.env.length-1]; var firstArg = MACHINE.env[MACHINE.env.length-1];
testArgument(MACHINE, 'number', isNumber, firstArg, 0, '='); testArgument(MACHINE, 'number', isNumber, firstArg, 0, '=');
@ -826,7 +826,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'<', '<',
new plt.baselib.arity.ArityAtLeast(2), plt.baselib.arity.arityAtLeast(2),
function(MACHINE) { function(MACHINE) {
var firstArg = MACHINE.env[MACHINE.env.length-1]; var firstArg = MACHINE.env[MACHINE.env.length-1];
testArgument(MACHINE, testArgument(MACHINE,
@ -849,7 +849,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'>', '>',
new plt.baselib.arity.ArityAtLeast(2), plt.baselib.arity.arityAtLeast(2),
function(MACHINE) { function(MACHINE) {
var firstArg = MACHINE.env[MACHINE.env.length-1]; var firstArg = MACHINE.env[MACHINE.env.length-1];
testArgument(MACHINE, testArgument(MACHINE,
@ -871,7 +871,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'<=', '<=',
new plt.baselib.arity.ArityAtLeast(2), plt.baselib.arity.arityAtLeast(2),
function(MACHINE) { function(MACHINE) {
var firstArg = MACHINE.env[MACHINE.env.length-1]; var firstArg = MACHINE.env[MACHINE.env.length-1];
testArgument(MACHINE, testArgument(MACHINE,
@ -894,7 +894,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'>=', '>=',
new plt.baselib.arity.ArityAtLeast(2), plt.baselib.arity.arityAtLeast(2),
function(MACHINE) { function(MACHINE) {
var firstArg = MACHINE.env[MACHINE.env.length-1]; var firstArg = MACHINE.env[MACHINE.env.length-1];
testArgument(MACHINE, testArgument(MACHINE,
@ -917,7 +917,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'+', '+',
new plt.baselib.arity.ArityAtLeast(0), plt.baselib.arity.arityAtLeast(0),
function(MACHINE) { function(MACHINE) {
var result = 0; var result = 0;
var i = 0; var i = 0;
@ -936,7 +936,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'*', '*',
new plt.baselib.arity.ArityAtLeast(0), plt.baselib.arity.arityAtLeast(0),
function(MACHINE) { function(MACHINE) {
var result = 1; var result = 1;
var i = 0; var i = 0;
@ -954,7 +954,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'-', '-',
new plt.baselib.arity.ArityAtLeast(1), plt.baselib.arity.arityAtLeast(1),
function(MACHINE) { function(MACHINE) {
if (MACHINE.argcount === 1) { if (MACHINE.argcount === 1) {
testArgument(MACHINE, testArgument(MACHINE,
@ -980,7 +980,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'/', '/',
new plt.baselib.arity.ArityAtLeast(1), plt.baselib.arity.arityAtLeast(1),
function(MACHINE) { function(MACHINE) {
testArgument(MACHINE, testArgument(MACHINE,
'number', 'number',
@ -1053,7 +1053,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'list', 'list',
new plt.baselib.arity.ArityAtLeast(0), plt.baselib.arity.arityAtLeast(0),
function(MACHINE) { function(MACHINE) {
var result = NULL; var result = NULL;
for (var i = 0; i < MACHINE.argcount; i++) { for (var i = 0; i < MACHINE.argcount; i++) {
@ -1153,7 +1153,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'vector', 'vector',
new plt.baselib.arity.ArityAtLeast(0), plt.baselib.arity.arityAtLeast(0),
function(MACHINE) { function(MACHINE) {
var i; var i;
var result = []; var result = [];
@ -1295,7 +1295,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'string-append', 'string-append',
new plt.baselib.arity.ArityAtLeast(0), plt.baselib.arity.arityAtLeast(0),
function(MACHINE) { function(MACHINE) {
var buffer = []; var buffer = [];
var i; var i;
@ -1342,7 +1342,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'void', 'void',
new plt.baselib.arity.ArityAtLeast(0), plt.baselib.arity.arityAtLeast(0),
function(MACHINE) { function(MACHINE) {
return VOID; return VOID;
}); });
@ -1597,7 +1597,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'gcd', 'gcd',
new plt.baselib.arity.ArityAtLeast(1), plt.baselib.arity.arityAtLeast(1),
function(MACHINE) { function(MACHINE) {
var args = [], i, x; var args = [], i, x;
for (i = 0; i < MACHINE.argcount; i++) { for (i = 0; i < MACHINE.argcount; i++) {
@ -1616,7 +1616,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'lcm', 'lcm',
new plt.baselib.arity.ArityAtLeast(1), plt.baselib.arity.arityAtLeast(1),
function(MACHINE) { function(MACHINE) {
var args = [], i, x; var args = [], i, x;
for (i = 0; i < MACHINE.argcount; i++) { for (i = 0; i < MACHINE.argcount; i++) {
@ -2033,7 +2033,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure( installPrimitiveProcedure(
'format', 'format',
new plt.baselib.arity.ArityAtLeast(1), plt.baselib.arity.arityAtLeast(1),
function(MACHINE) { function(MACHINE) {
var args = [], i, formatString; var args = [], i, formatString;
testArgument(MACHINE, testArgument(MACHINE,
@ -2517,6 +2517,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
exports['toDisplayedString'] = toDisplayedString; exports['toDisplayedString'] = toDisplayedString;
exports['ArityAtLeast'] = plt.baselib.arity.ArityAtLeast; exports['ArityAtLeast'] = plt.baselib.arity.ArityAtLeast;
exports['arityAtLeast'] = plt.baselib.arity.arityAtLeast;
exports['isArityMatching'] = plt.baselib.arity.isArityMatching; exports['isArityMatching'] = plt.baselib.arity.isArityMatching;
exports['heir'] = heir; exports['heir'] = heir;
@ -2526,4 +2527,6 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
scope.link.announceReady('runtime'); scope.link.announceReady('runtime');
})(this['plt']); })(this['plt']);

View File

@ -47,6 +47,10 @@ if (! this['plt']) { this['plt'] = {}; }
}; };
var Symbol = plt.baselib.Symbol;
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -362,45 +366,6 @@ if (! this['plt']) { this['plt'] = {}; }
return other instanceof Char && this.val == other.val; return other instanceof Char && this.val == other.val;
}; };
//////////////////////////////////////////////////////////////////////
// Symbols
//////////////////////////////////////////////////////////////////////
var Symbol = function(val) {
this.val = val;
};
var symbolCache = {};
// makeInstance: string -> Symbol.
Symbol.makeInstance = function(val) {
// To ensure that we can eq? symbols with equal values.
if (!(val in symbolCache)) {
symbolCache[val] = new Symbol(val);
} else {
}
return symbolCache[val];
};
Symbol.prototype.equals = function(other, aUnionFind) {
return other instanceof Symbol &&
this.val == other.val;
};
Symbol.prototype.toString = function(cache) {
return this.val;
};
Symbol.prototype.toWrittenString = function(cache) {
return this.val;
};
Symbol.prototype.toDisplayedString = function(cache) {
return this.val;
};
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Keywords // Keywords
@ -1353,20 +1318,20 @@ String.prototype.toDisplayedString = function(cache) {
// INTERNAL_CALL // INTERNAL_CALL
// used for interaction between the Primitives and the interpreter (callPrimitiveProcedure). // used for interaction between the Primitives and the interpreter (callPrimitiveProcedure).
// Don't confuse this with CallControl. // Don't confuse this with CallControl.
var INTERNAL_CALL = function(operator, operands, k) { // var INTERNAL_CALL = function(operator, operands, k) {
this.operator = operator; // this.operator = operator;
this.operands = operands; // this.operands = operands;
this.k = k; // this.k = k;
}; // };
// INTERNAL_PAUSE // // INTERNAL_PAUSE
// used for interaction between the Primitive functions and the // // used for interaction between the Primitive functions and the
// interpreter. // // interpreter.
// Halts the interpreter, but passing onPause the functions necessary // // Halts the interpreter, but passing onPause the functions necessary
// to restart computation. // // to restart computation.
var INTERNAL_PAUSE = function(onPause) { // var INTERNAL_PAUSE = function(onPause) {
this.onPause = onPause; // this.onPause = onPause;
}; // };
@ -1464,19 +1429,6 @@ String.prototype.toDisplayedString = function(cache) {
var Color = plt.baselib.structs.makeStructureType('color', false, 3, 0, false, false); var Color = plt.baselib.structs.makeStructureType('color', false, 3, 0, false, false);
var ArityAtLeast = plt.baselib.structs.makeStructureType(
'arity-at-least', false, 1, 0, false,
function(args, name, k) {
// helpers.check(args[0],
// function(x) {
// return ( jsnums.isExact(x) &&
// jsnums.isInteger(x) &&
// jsnums.greaterThanOrEqual(x, 0) ); },
// name,
// 'exact non-negative integer', 1);
return k(args);
});
@ -1544,9 +1496,10 @@ String.prototype.toDisplayedString = function(cache) {
types.exceptionHandlerKey = new Symbol("exnh"); types.exceptionHandlerKey = new Symbol("exnh");
types.symbol = Symbol.makeInstance; types.symbol = Symbol.makeInstance;
types.rational = jsnums.makeRational; types.rational = jsnums.makeRational;
types.floatpoint = jsnums.makeFloat; types.floatpoint = jsnums.makeFloat;
@ -1569,8 +1522,8 @@ String.prototype.toDisplayedString = function(cache) {
types.pair = function(x, y) { return Cons.makeInstance(x, y); }; types.pair = function(x, y) { return Cons.makeInstance(x, y); };
types.hash = makeHashEqual; types.hash = makeHashEqual;
types.hashEq = makeHashEq; types.hashEq = makeHashEq;
types.jsValue = function(name, val) { return new JsValue(name, val); }; // types.jsValue = function(name, val) { return new JsValue(name, val); };
types.wrappedSchemeValue = function(val) { return new WrappedSchemeValue(val); }; // types.wrappedSchemeValue = function(val) { return new WrappedSchemeValue(val); };
types.color = Color.constructor; types.color = Color.constructor;
@ -1578,8 +1531,6 @@ String.prototype.toDisplayedString = function(cache) {
types.colorGreen = function(x) { return Color.accessor(x, 1); }; types.colorGreen = function(x) { return Color.accessor(x, 1); };
types.colorBlue = function(x) { return Color.accessor(x, 2); }; types.colorBlue = function(x) { return Color.accessor(x, 2); };
types.arityAtLeast = ArityAtLeast.constructor;
types.arityAtLeastValue = function(arity) { return ArityAtLeast.accessor(arity, 0); };
types.FALSE = false; types.FALSE = false;
@ -1610,7 +1561,6 @@ String.prototype.toDisplayedString = function(cache) {
x instanceof EqualHashTable); }; x instanceof EqualHashTable); };
types.isByteString = function(x) { return x instanceof Bytes; }; types.isByteString = function(x) { return x instanceof Bytes; };
types.isStruct = function(x) { return x instanceof Struct; }; types.isStruct = function(x) { return x instanceof Struct; };
types.isArityAtLeast = ArityAtLeast.predicate;
types.isColor = Color.predicate; types.isColor = Color.predicate;
// types.isFunction = function(x) { // types.isFunction = function(x) {
@ -1618,8 +1568,8 @@ String.prototype.toDisplayedString = function(cache) {
// }; // };
types.isJsValue = function(x) { return x instanceof JsValue; }; // types.isJsValue = function(x) { return x instanceof JsValue; };
types.isWrappedSchemeValue = function(x) { return x instanceof WrappedSchemeValue; }; // types.isWrappedSchemeValue = function(x) { return x instanceof WrappedSchemeValue; };
types.cons = Cons.makeInstance; types.cons = Cons.makeInstance;
@ -1632,10 +1582,10 @@ String.prototype.toDisplayedString = function(cache) {
// types.defaultContinuationPromptTagHandler = defaultContinuationPromptTagHandler; // types.defaultContinuationPromptTagHandler = defaultContinuationPromptTagHandler;
// types.makeOptionPrimitive = makeOptionPrimitive; // types.makeOptionPrimitive = makeOptionPrimitive;
types.internalCall = function(op, args, k) { return new INTERNAL_CALL(op, args, k); }; // types.internalCall = function(op, args, k) { return new INTERNAL_CALL(op, args, k); };
types.isInternalCall = function(x) { return (x instanceof INTERNAL_CALL); }; // types.isInternalCall = function(x) { return (x instanceof INTERNAL_CALL); };
types.internalPause = function(onPause) { return new INTERNAL_PAUSE(onPause) }; // types.internalPause = function(onPause) { return new INTERNAL_PAUSE(onPause) };
types.isInternalPause = function(x) { return (x instanceof INTERNAL_PAUSE); }; // types.isInternalPause = function(x) { return (x instanceof INTERNAL_PAUSE); };
types.contMarkRecordControl = function(dict) { return new ContMarkRecordControl(dict); }; types.contMarkRecordControl = function(dict) { return new ContMarkRecordControl(dict); };
types.isContMarkRecordControl = function(x) { return x instanceof ContMarkRecordControl; }; types.isContMarkRecordControl = function(x) { return x instanceof ContMarkRecordControl; };