hashes beginning to run.

This commit is contained in:
Danny Yoo 2011-11-03 18:12:30 -04:00
parent dcb94873fe
commit fd28c199e9
8 changed files with 149 additions and 46 deletions

View File

@ -88,6 +88,12 @@
'raise-type-error 'raise-type-error
'struct:exn:fail 'struct:exn:fail
'prop:exn:srclocs 'prop:exn:srclocs
'hash?
'make-hash
'make-hasheqv
'make-hasheq
)) ))
(define-predicate KernelPrimitiveName? KernelPrimitiveName) (define-predicate KernelPrimitiveName? KernelPrimitiveName)

View File

@ -255,6 +255,10 @@
baselib.exceptions.isExn, baselib.exceptions.isExn,
'exn'); 'exn');
var checkHash = makeCheckArgumentType(
baselib.hashes.isHash,
'hash');
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -293,5 +297,6 @@
exports.checkContinuationMarkSet = checkContinuationMarkSet; exports.checkContinuationMarkSet = checkContinuationMarkSet;
exports.checkContinuationPromptTag = checkContinuationPromptTag; exports.checkContinuationPromptTag = checkContinuationPromptTag;
exports.checkExn = checkExn; exports.checkExn = checkExn;
exports.checkHash = checkHash;
}(this.plt.baselib)); }(this.plt.baselib));

View File

@ -79,6 +79,11 @@
}; };
var raiseContractError = function(MACHINE, msg) {
var contMarks = MACHINE.captureContinuationMarks();
raise(MACHINE, ExnFailContract.constructor(msg, contMarks));
};
var raiseUnboundToplevelError = function(MACHINE, name) { var raiseUnboundToplevelError = function(MACHINE, name) {
@ -224,6 +229,7 @@
exceptions.raise = raise; exceptions.raise = raise;
exceptions.raiseContractError = raiseContractError;
exceptions.raiseUnboundToplevelError = raiseUnboundToplevelError; exceptions.raiseUnboundToplevelError = raiseUnboundToplevelError;
exceptions.raiseArgumentTypeError = raiseArgumentTypeError; exceptions.raiseArgumentTypeError = raiseArgumentTypeError;
exceptions.raiseContextExpectedValuesError = raiseContextExpectedValuesError; exceptions.raiseContextExpectedValuesError = raiseContextExpectedValuesError;

View File

@ -21,13 +21,6 @@
var isPrimitiveProcedure = function (x) {
return typeof (x) === 'function';
};
@ -207,9 +200,7 @@
// It assumes that it must begin its own trampoline. // It assumes that it must begin its own trampoline.
var asJavaScriptFunction = function (v, MACHINE) { var asJavaScriptFunction = function (v, MACHINE) {
MACHINE = MACHINE || plt.runtime.currentMachine; MACHINE = MACHINE || plt.runtime.currentMachine;
if (isPrimitiveProcedure(v)) { if (isClosure(v)) {
return coersePrimitiveToJavaScript(v, MACHINE);
} else if (isClosure(v)) {
return coerseClosureToJavaScript(v, MACHINE); return coerseClosureToJavaScript(v, MACHINE);
} else { } else {
baselib.exceptions.raise(MACHINE, baselib.exceptions.raise(MACHINE,
@ -234,18 +225,7 @@
MACHINE.captureContinuationMarks())); MACHINE.captureContinuationMarks()));
} }
if (isPrimitiveProcedure(proc)) { if (isClosure(proc)) {
oldArgcount = MACHINE.a;
MACHINE.a = arguments.length - 4;
for (i = 0; i < arguments.length - 4; i++) {
MACHINE.e.push(arguments[arguments.length - 1 - i]);
}
var result = proc(MACHINE);
for (i = 0; i < arguments.length - 4; i++) {
MACHINE.e.pop();
}
success(result);
} else if (isClosure(proc)) {
oldVal = MACHINE.v; oldVal = MACHINE.v;
oldArgcount = MACHINE.a; oldArgcount = MACHINE.a;
oldProc = MACHINE.p; oldProc = MACHINE.p;
@ -339,20 +319,24 @@
var renameProcedure = function (f, name) { var renameProcedure = function (f, name) {
if (isPrimitiveProcedure(f)) { return makeClosure(name, f.racketArity, f.label, f.closedVals);
return makePrimitiveProcedure(
name,
f.racketArity,
function (MACHINE) {
return f(MACHINE);
});
} else {
return makeClosure(name, f.racketArity, f.label, f.closedVals);
}
}; };
// Applying a procedure.
// Assumptions: the procedure register has been assigned, as has
// the argcount and environment.
// Must be running in the context of a trampoline.
var rawApply = function(M) {
M.cbt--;
if (baselib.arity.isArityMatching(M.p.racketArity, M.a)) {
return M.p.label(M);
} else {
baselib.exceptions.raiseArityMismatchError(M, M.p, M.a);
}
};
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -363,7 +347,6 @@
exports.makePrimitiveProcedure = makePrimitiveProcedure; exports.makePrimitiveProcedure = makePrimitiveProcedure;
exports.makeClosure = makeClosure; exports.makeClosure = makeClosure;
exports.isPrimitiveProcedure = isPrimitiveProcedure;
exports.isClosure = isClosure; exports.isClosure = isClosure;
exports.isProcedure = isProcedure; exports.isProcedure = isProcedure;
@ -371,7 +354,8 @@
exports.renameProcedure = renameProcedure; exports.renameProcedure = renameProcedure;
exports.asJavaScriptFunction = asJavaScriptFunction; exports.asJavaScriptFunction = asJavaScriptFunction;
exports.rawApply = rawApply;
}(this.plt.baselib, this.plt)); }(this.plt.baselib, this.plt));

View File

@ -134,18 +134,26 @@
return true; return true;
}; };
WhalesongHashtable.prototype.ref = function(key) {
WhalesongHashtable.prototype.get = function(key) {
return this.hash.get(key); return this.hash.get(key);
}; };
WhalesongHashtable.prototype.set = function(key, value) { WhalesongHashtable.prototype.put = function(key, value) {
return this.hash.put(key, value); this.hash.put(key, value);
}; };
WhalesongHashtable.prototype.remove = function(key) { WhalesongHashtable.prototype.remove = function(key) {
this.hash.remove(key); this.hash.remove(key);
}; };
WhalesongHashtable.prototype.containsKey = function(key) {
return this.hash.containsKey(key);
};
var isHash = function (x) { var isHash = function (x) {
return (x instanceof WhalesongHashtable); return (x instanceof WhalesongHashtable);
}; };

View File

@ -116,6 +116,10 @@
var checkContinuationPromptTag = baselib.check.checkContinuationPromptTag; var checkContinuationPromptTag = baselib.check.checkContinuationPromptTag;
var checkContinuationMarkSet = baselib.check.checkContinuationMarkSet; var checkContinuationMarkSet = baselib.check.checkContinuationMarkSet;
var checkExn = baselib.check.checkExn; var checkExn = baselib.check.checkExn;
var checkHash = baselib.check.checkHash;
var checkAny = baselib.check.makeCheckArgumentType(
function(x) { return true; },
'any');
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -1354,17 +1358,14 @@
M.a--; M.a--;
checkList(M, 'apply', M.a - 1); checkList(M, 'apply', M.a - 1);
M.spliceListIntoStack(M.a - 1); M.spliceListIntoStack(M.a - 1);
M.p = proc;
if (baselib.arity.isArityMatching(proc.racketArity, M.a)) { if (baselib.arity.isArityMatching(proc.racketArity, M.a)) {
M.p = proc; return proc.label(M);
if (baselib.functions.isPrimitiveProcedure(proc)) {
return finalizeClosureCall(M, proc(M));
} else {
return proc.label(M);
}
} else { } else {
raiseArityMismatchError(M, proc, M.a); raiseArityMismatchError(M, proc, M.a);
} }
}; };
installPrimitiveClosure( installPrimitiveClosure(
'apply', 'apply',
baselib.arity.makeArityAtLeast(2), baselib.arity.makeArityAtLeast(2),
@ -2263,14 +2264,14 @@
}); });
}); });
installPrimitiveClosure( installPrimitiveProcedure(
'struct?', 'struct?',
1, 1,
function(M) { function(M) {
return isStruct(M.e[M.e.length - 1]); return isStruct(M.e[M.e.length - 1]);
}); });
installPrimitiveClosure( installPrimitiveProcedure(
'struct-type?', 'struct-type?',
1, 1,
function(M) { function(M) {
@ -2502,6 +2503,13 @@
return hash; return hash;
}; };
installPrimitiveProcedure(
'hash?',
1,
function(M) {
return baselib.hashes.isHash(checkAny(M, 'hash?', 0));
});
installPrimitiveProcedure( installPrimitiveProcedure(
'make-hasheq', 'make-hasheq',
makeList(0, 1), makeList(0, 1),
@ -2535,6 +2543,53 @@
return initializeHash(lst, plt.baselib.hashes.makeEqualHashtable()); return initializeHash(lst, plt.baselib.hashes.makeEqualHashtable());
}); });
installPrimitiveClosure(
'hash-ref',
makeList(2, 3),
function(M) {
var hash = checkHash(M, 'hash-ref', 0);
var key = checkAny(M, 'hash-ref', 1);
var thunk;
if (M.a === 3) {
thunk = checkProcedure(M, 'hash-ref', 2);
}
if (hash.containsKey(key)) {
finalizeClosureCall(M, hash.get(key));
} else {
if (M.a === 2) {
raiseContractError(
plt.baselib.format("hash-ref: no value found for key: ~e",
[key]));
} else {
M.p = thunk;
M.e.length -= M.a;
M.a = 0;
baselib.functions.rawApply();
}
}
});
installPrimitiveProcedure(
'hash-set!',
3,
function(M){
var hash = checkHash(M, 'hash-set!', 0);
var key = checkAny(M, 'hash-set!', 1);
var value = checkAny(M, 'hash-set!', 2);
hash.put(key, value);
return VOID;
});
installPrimitiveProcedure(
'hash-has-key?',
2,
function(M){
var hash = checkHash(M, 'hash-set!', 0);
var key = checkAny(M, 'hash-set!', 1);
return hash.containsKey(key);
});
exports['Primitives'] = Primitives; exports['Primitives'] = Primitives;
exports['installPrimitiveProcedure'] = installPrimitiveProcedure; exports['installPrimitiveProcedure'] = installPrimitiveProcedure;
exports['installPrimitiveClosure'] = installPrimitiveClosure; exports['installPrimitiveClosure'] = installPrimitiveClosure;

View File

@ -149,7 +149,10 @@
let/cc let/cc
with-continuation-mark with-continuation-mark
hash?
make-hash
make-hasheqv
make-hasheq

View File

@ -0,0 +1,36 @@
#lang planet dyoo/whalesong/base
(hash? 1)
(hash? "potatoes")
(hash? (make-hash))
(hash? (make-hash '((1 . one)
(2 . two)
(3 . three)
(4 . four))))
(hash? (make-hasheqv))
(hash? (make-hasheqv '((1 . one)
(2 . two)
(3 . three)
(4 . four))))
(hash? (make-hasheq))
(hash? (make-hasheq '((1 . one)
(2 . two)
(3 . three)
(4 . four))))
(make-hash)
(make-hasheqv)
(make-hasheq)
(make-hash '((1 . one)
(2 . two)
(3 . three)
(4 . four)))
(make-hasheqv '((1 . one)
(2 . two)
(3 . three)
(4 . four)))
(make-hasheq '((1 . one)
(2 . two)
(3 . three)
(4 . four)))