Skip to content

Commit

Permalink
have tests working for hashes. Also allow for argumentless lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
jostylr committed May 11, 2012
1 parent 44a3d38 commit 4292626
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
12 changes: 11 additions & 1 deletion scheem/evalScheem.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ var pair = function (elms, stack) {
};

var lambda = function (cur, lex) {
if (cur.length > 3) {
throw "too many arguments for lambda: (lambda _args _body)"
}
if (cur.length === 1) {
return function () {};
}
if (cur.length === 2) { // no arguments
cur[2] = cur[1];
cur[1] = [];
}
return function (args, stack, values, env, self) {
var newenv, v, a, i, n;
stack.push({type: 'envChange', env : env});
Expand Down Expand Up @@ -368,7 +378,7 @@ initenv = function () {
}
return '#t';
},
'<>' : function (arr) {
'!=' : function (arr) {
if (!arr || arr.length === 0 || arr.length === 1) {
throw "insufficient arguments +";
}
Expand Down
24 changes: 24 additions & 0 deletions scheem/pretest/runT.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ module.exports.suites = {
},
cons: function () {
return runT.apply(null, arguments);
},
hash: function () {
return runT.apply(null, arguments);
}
};

Expand Down Expand Up @@ -185,6 +188,27 @@ var data = {
inp: ['(define x 3) (define x 5) x'],
out: ['error', 'variable already defined: x']
}
},
hash: {
'(# x 4 y 6)': {
inp: ['(# x 4 y 6)'],
out: {
y: 6,
x: 4
}
},
'(. (# x 4 y 6) \'y)': {
inp: ['(. (# x 4 y 6) \'y)'],
out: 6
},
'(define x (# y 3 z (lambda (n) (+ (. this \'y) n)))) ((. x \'z) 5)': {
inp: ['(define x (# y 3 z (lambda (n) (+ (. this \'y) n)))) ((. x \'z) 5)'],
out: [8]
},
'(define x (# y 3 z (lambda (+ (. this \'y) (cdr arguments))))) ((. x \'z) 5)': {
inp: ['(define x (# y 3 z (lambda (+ (. this \'y) (cdr arguments))))) ((. x \'z) 5)'],
out: [8]
}
}
};

Expand Down
40 changes: 40 additions & 0 deletions scheem/test/runT.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ var suites = {
},
cons: function () {
return runT.apply(null, arguments);
},
hash: function () {
return runT.apply(null, arguments);
}
};

Expand Down Expand Up @@ -337,4 +340,41 @@ test("(define x 3) (define x 5) x", function () {
if (flag) {
throw new Error("failed to throw error");
}
});

suite("hash");

test("(# x 4 y 6)", function () {
var result = suites.hash.apply(null, ['(# x 4 y 6)']);
var pass = _.isEqual(result, {
y: 6,
x: 4
});
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "{ y: 6, x: 4 }" + "\n Input: ['(#x4y6)']");
}
});

test("(. (# x 4 y 6) 'y)", function () {
var result = suites.hash.apply(null, ['(. (# x 4 y 6) \'y)']);
var pass = _.isEqual(result, 6);
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "6" + "\n Input: ['(.(#x4y6)\'y)']");
}
});

test("(define x (# y 3 z (lambda (n) (+ (. this 'y) n)))) ((. x 'z) 5)", function () {
var result = suites.hash.apply(null, ['(define x (# y 3 z (lambda (n) (+ (. this \'y) n)))) ((. x \'z) 5)']);
var pass = _.isEqual(result, [8]);
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "[ 8 ]" + "\n Input: ['(definex(#y3z(lambda(n)(+(.this\'y)n))))((.x\'z)5)']");
}
});

test("(define x (# y 3 z (lambda (+ (. this 'y) (cdr arguments))))) ((. x 'z) 5)", function () {
var result = suites.hash.apply(null, ['(define x (# y 3 z (lambda (+ (. this \'y) (cdr arguments))))) ((. x \'z) 5)']);
var pass = _.isEqual(result, [8]);
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "[ 8 ]" + "\n Input: ['(definex(#y3z(lambda(+(.this\'y)(cdrarguments)))))((.x\'z)5)']");
}
});

0 comments on commit 4292626

Please sign in to comment.