whalesong/js-assembler/runtime-src/baselib_arity.js
2011-07-03 19:12:37 -04:00

40 lines
992 B
JavaScript

// Arity structure
(function(baselib) {
var exports = {};
baselib.arity = exports;
// 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;
};
// 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 {
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; }
}
arity = arity.rest;
}
return false;
}
}
exports.ArityAtLeast = ArityAtLeast;
exports.isArityMatching = isArityMatching;
})(this['plt'].baselib);