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

View File

@ -37,6 +37,7 @@
(define-runtime-path baselib.js "runtime-src/baselib.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_symbol.js "runtime-src/baselib_symbol.js")
(define-runtime-path baselib_structs.js "runtime-src/baselib_structs.js")
(define-runtime-path baselib_arity.js "runtime-src/baselib_arity.js")
@ -69,6 +70,7 @@
baselib.js
baselib_unionfind.js
baselib_hash.js
baselib_symbol.js
baselib_structs.js
baselib_arity.js

View File

@ -4,26 +4,42 @@
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,
// 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
// Produces true if n satisfies the arity.
var isArityMatching = function(arity, n) {
if (typeof(arity) === 'number') {
return arity === n;
} else if (arity instanceof ArityAtLeast) {
return n >= arity.value;
} else if (isArityAtLeast(arity)) {
return n >= arityAtLeastValue(arity);
} else {
while (arity !== plt.types.EMPTY) {
if (typeof(arity.first) === 'number') {
if (arity.first === n) { return true; }
} else if (arity instanceof ArityAtLeast) {
if (n >= arity.first.value) { return true; }
} else if (isArityAtLeast(arity)) {
if (n >= arityAtLeastValue(arity.first)) { return true; }
}
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);

View File

@ -114,7 +114,7 @@
var args = [].slice.call(arguments);
return newType.applyGuard(
args,
Symbol.makeInstance(theName),
baselib.Symbol.makeInstance(theName),
function(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(
'format',
new plt.baselib.arity.ArityAtLeast(1),
plt.baselib.arity.arityAtLeast(1),
function(MACHINE) {
var args = [], i, formatString;
testArgument(MACHINE,
@ -689,7 +689,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'printf',
new plt.baselib.arity.ArityAtLeast(1),
plt.baselib.arity.arityAtLeast(1),
function(MACHINE) {
var args = [], i, formatString;
testArgument(MACHINE,
@ -711,7 +711,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'fprintf',
new plt.baselib.arity.ArityAtLeast(2),
plt.baselib.arity.arityAtLeast(2),
function(MACHINE) {
var args = [], i, formatString;
testArgument(MACHINE,
@ -772,7 +772,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'=',
new plt.baselib.arity.ArityAtLeast(2),
plt.baselib.arity.arityAtLeast(2),
function(MACHINE) {
var firstArg = MACHINE.env[MACHINE.env.length-1];
testArgument(MACHINE, 'number', isNumber, firstArg, 0, '=');
@ -826,7 +826,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'<',
new plt.baselib.arity.ArityAtLeast(2),
plt.baselib.arity.arityAtLeast(2),
function(MACHINE) {
var firstArg = MACHINE.env[MACHINE.env.length-1];
testArgument(MACHINE,
@ -849,7 +849,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'>',
new plt.baselib.arity.ArityAtLeast(2),
plt.baselib.arity.arityAtLeast(2),
function(MACHINE) {
var firstArg = MACHINE.env[MACHINE.env.length-1];
testArgument(MACHINE,
@ -871,7 +871,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'<=',
new plt.baselib.arity.ArityAtLeast(2),
plt.baselib.arity.arityAtLeast(2),
function(MACHINE) {
var firstArg = MACHINE.env[MACHINE.env.length-1];
testArgument(MACHINE,
@ -894,7 +894,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'>=',
new plt.baselib.arity.ArityAtLeast(2),
plt.baselib.arity.arityAtLeast(2),
function(MACHINE) {
var firstArg = MACHINE.env[MACHINE.env.length-1];
testArgument(MACHINE,
@ -917,7 +917,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'+',
new plt.baselib.arity.ArityAtLeast(0),
plt.baselib.arity.arityAtLeast(0),
function(MACHINE) {
var result = 0;
var i = 0;
@ -936,7 +936,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'*',
new plt.baselib.arity.ArityAtLeast(0),
plt.baselib.arity.arityAtLeast(0),
function(MACHINE) {
var result = 1;
var i = 0;
@ -954,7 +954,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'-',
new plt.baselib.arity.ArityAtLeast(1),
plt.baselib.arity.arityAtLeast(1),
function(MACHINE) {
if (MACHINE.argcount === 1) {
testArgument(MACHINE,
@ -980,7 +980,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'/',
new plt.baselib.arity.ArityAtLeast(1),
plt.baselib.arity.arityAtLeast(1),
function(MACHINE) {
testArgument(MACHINE,
'number',
@ -1053,7 +1053,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'list',
new plt.baselib.arity.ArityAtLeast(0),
plt.baselib.arity.arityAtLeast(0),
function(MACHINE) {
var result = NULL;
for (var i = 0; i < MACHINE.argcount; i++) {
@ -1153,7 +1153,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'vector',
new plt.baselib.arity.ArityAtLeast(0),
plt.baselib.arity.arityAtLeast(0),
function(MACHINE) {
var i;
var result = [];
@ -1295,7 +1295,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'string-append',
new plt.baselib.arity.ArityAtLeast(0),
plt.baselib.arity.arityAtLeast(0),
function(MACHINE) {
var buffer = [];
var i;
@ -1342,7 +1342,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'void',
new plt.baselib.arity.ArityAtLeast(0),
plt.baselib.arity.arityAtLeast(0),
function(MACHINE) {
return VOID;
});
@ -1597,7 +1597,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'gcd',
new plt.baselib.arity.ArityAtLeast(1),
plt.baselib.arity.arityAtLeast(1),
function(MACHINE) {
var args = [], i, x;
for (i = 0; i < MACHINE.argcount; i++) {
@ -1616,7 +1616,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'lcm',
new plt.baselib.arity.ArityAtLeast(1),
plt.baselib.arity.arityAtLeast(1),
function(MACHINE) {
var args = [], i, x;
for (i = 0; i < MACHINE.argcount; i++) {
@ -2033,7 +2033,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
installPrimitiveProcedure(
'format',
new plt.baselib.arity.ArityAtLeast(1),
plt.baselib.arity.arityAtLeast(1),
function(MACHINE) {
var args = [], i, formatString;
testArgument(MACHINE,
@ -2517,6 +2517,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
exports['toDisplayedString'] = toDisplayedString;
exports['ArityAtLeast'] = plt.baselib.arity.ArityAtLeast;
exports['arityAtLeast'] = plt.baselib.arity.arityAtLeast;
exports['isArityMatching'] = plt.baselib.arity.isArityMatching;
exports['heir'] = heir;
@ -2526,4 +2527,6 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
scope.link.announceReady('runtime');
})(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;
};
//////////////////////////////////////////////////////////////////////
// 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
@ -1353,20 +1318,20 @@ String.prototype.toDisplayedString = function(cache) {
// INTERNAL_CALL
// used for interaction between the Primitives and the interpreter (callPrimitiveProcedure).
// Don't confuse this with CallControl.
var INTERNAL_CALL = function(operator, operands, k) {
this.operator = operator;
this.operands = operands;
this.k = k;
};
// var INTERNAL_CALL = function(operator, operands, k) {
// this.operator = operator;
// this.operands = operands;
// this.k = k;
// };
// INTERNAL_PAUSE
// used for interaction between the Primitive functions and the
// interpreter.
// Halts the interpreter, but passing onPause the functions necessary
// to restart computation.
var INTERNAL_PAUSE = function(onPause) {
this.onPause = onPause;
};
// // INTERNAL_PAUSE
// // used for interaction between the Primitive functions and the
// // interpreter.
// // Halts the interpreter, but passing onPause the functions necessary
// // to restart computation.
// var INTERNAL_PAUSE = function(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 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.symbol = Symbol.makeInstance;
types.rational = jsnums.makeRational;
types.floatpoint = jsnums.makeFloat;
@ -1569,8 +1522,8 @@ String.prototype.toDisplayedString = function(cache) {
types.pair = function(x, y) { return Cons.makeInstance(x, y); };
types.hash = makeHashEqual;
types.hashEq = makeHashEq;
types.jsValue = function(name, val) { return new JsValue(name, val); };
types.wrappedSchemeValue = function(val) { return new WrappedSchemeValue(val); };
// types.jsValue = function(name, val) { return new JsValue(name, val); };
// types.wrappedSchemeValue = function(val) { return new WrappedSchemeValue(val); };
types.color = Color.constructor;
@ -1578,8 +1531,6 @@ String.prototype.toDisplayedString = function(cache) {
types.colorGreen = function(x) { return Color.accessor(x, 1); };
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;
@ -1610,7 +1561,6 @@ String.prototype.toDisplayedString = function(cache) {
x instanceof EqualHashTable); };
types.isByteString = function(x) { return x instanceof Bytes; };
types.isStruct = function(x) { return x instanceof Struct; };
types.isArityAtLeast = ArityAtLeast.predicate;
types.isColor = Color.predicate;
// types.isFunction = function(x) {
@ -1618,8 +1568,8 @@ String.prototype.toDisplayedString = function(cache) {
// };
types.isJsValue = function(x) { return x instanceof JsValue; };
types.isWrappedSchemeValue = function(x) { return x instanceof WrappedSchemeValue; };
// types.isJsValue = function(x) { return x instanceof JsValue; };
// types.isWrappedSchemeValue = function(x) { return x instanceof WrappedSchemeValue; };
types.cons = Cons.makeInstance;
@ -1632,10 +1582,10 @@ String.prototype.toDisplayedString = function(cache) {
// types.defaultContinuationPromptTagHandler = defaultContinuationPromptTagHandler;
// types.makeOptionPrimitive = makeOptionPrimitive;
types.internalCall = function(op, args, k) { return new INTERNAL_CALL(op, args, k); };
types.isInternalCall = function(x) { return (x instanceof INTERNAL_CALL); };
types.internalPause = function(onPause) { return new INTERNAL_PAUSE(onPause) };
types.isInternalPause = function(x) { return (x instanceof INTERNAL_PAUSE); };
// types.internalCall = function(op, args, k) { return new INTERNAL_CALL(op, args, k); };
// types.isInternalCall = function(x) { return (x instanceof INTERNAL_CALL); };
// types.internalPause = function(onPause) { return new INTERNAL_PAUSE(onPause) };
// types.isInternalPause = function(x) { return (x instanceof INTERNAL_PAUSE); };
types.contMarkRecordControl = function(dict) { return new ContMarkRecordControl(dict); };
types.isContMarkRecordControl = function(x) { return x instanceof ContMarkRecordControl; };