trying to make structs work

This commit is contained in:
Danny Yoo 2011-06-30 15:12:25 -04:00
parent a1a1dbe2db
commit 4d5cbef31c
2 changed files with 109 additions and 31 deletions

View File

@ -1998,6 +1998,84 @@
installPrimitiveProcedure(
'make-struct-field-accessor',
makeList(2, 3),
function(MACHINE) {
var accessor, fieldPos, fieldName;
accessor = MACHINE.env[MACHINE.env.length-1];
fieldPos = MACHINE.env[MACHINE.env.length-2];
if (MACHINE.argcount === 2) {
fieldName = 'field' + fieldPos;
} else {
fieldName = MACHINE.env[MACHINE.env.length-3];
}
testArgument(MACHINE,
'accessor procedure that requires a field index',
function(x) {
return (x instanceof types.StructAccessorProc &&
x.numParams > 1);
},
accessor,
0,
'make-struct-field-accessor');
testArgument(MACHINE,
'exact non-negative integer',
isNatural,
fieldPos,
'make-struct-field-accessor',
1)
testArgument(MACHINE,
'symbol or #f',
function(x) {
return x === false || isSymbol(x);
},
'make-struct-field-accessor',
fieldName,
2);
var procName = accessor.type.name + '-' fieldName;
return new types.StructAccessorProc(
accessor.type,
procName,
1,
false,
false,
function(MACHINE) {
testArgument(MACHINE,
'struct:' + accessor.type.name,
accessor.type.predicate,
MACHINE.env[MACHINE.env.length - 1],
procName,
0);
return accessor.impl(x, fieldPos);
});
});

View File

@ -1788,37 +1788,37 @@ String.prototype.toDisplayedString = function(cache) {
};
//////////////////////////////////////////////////////////////////////
var makeOptionPrimitive = function(name,
numArgs,
defaultVals,
usesState,
bodyF) {
var makeNthPrimitive = function(n) {
return new PrimProc(name,
numArgs + n,
false,
usesState,
function() {
var expectedNumArgs = numArgs + n + (usesState ? 1 : 0);
assert.equal(arguments.length,
expectedNumArgs);
var args = [arguments];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
var startDefaults = i - numArgs - (usesState ? 1 : 0);
return bodyF.apply(
bodyF,
args.concat(defaultVals.slice(startDefaults)));
});
};
// var makeOptionPrimitive = function(name,
// numArgs,
// defaultVals,
// usesState,
// bodyF) {
// var makeNthPrimitive = function(n) {
// return new PrimProc(name,
// numArgs + n,
// false,
// usesState,
// function() {
// var expectedNumArgs = numArgs + n + (usesState ? 1 : 0);
// assert.equal(arguments.length,
// expectedNumArgs);
// var args = [arguments];
// for (var i = 0; i < arguments.length; i++) {
// args.push(arguments[i]);
// }
// var startDefaults = i - numArgs - (usesState ? 1 : 0);
// return bodyF.apply(
// bodyF,
// args.concat(defaultVals.slice(startDefaults)));
// });
// };
var cases = [];
for (var i = 0; i <= defaultVals.length; i++) {
cases.push(makeNthPrimitive(i));
}
return new CasePrimitive(name, cases);
};
// var cases = [];
// for (var i = 0; i <= defaultVals.length; i++) {
// cases.push(makeNthPrimitive(i));
// }
// return new CasePrimitive(name, cases);
// };
@ -2149,7 +2149,7 @@ String.prototype.toDisplayedString = function(cache) {
types.CaseLambdaValue = CaseLambdaValue;
types.PrimProc = PrimProc;
types.CasePrimitive = CasePrimitive;
types.makeOptionPrimitive = makeOptionPrimitive;
// 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); };