Permalink
Browse files

working on test suite

  • Loading branch information...
1 parent 4fb150d commit e966b6153035947bf32f29bceeda175ecf312c6d @ajb committed May 7, 2012
Showing with 21 additions and 3 deletions.
  1. +6 −3 Homework5/scheem.js
  2. +15 −0 Homework5/test/evalScheemTest.js
View
@@ -112,22 +112,25 @@ var evalScheem = function (expr, env) {
// New code here
return function(arg){
var binding = {};
- binding[expr[1]] = evalScheem(arg, env);
var newEnv = {bindings: binding, outer: env};
+
+ binding[expr[1]] = evalScheem(arg, newEnv);
return evalScheem(expr[2], newEnv);
};
case 'lambda':
return function(){ // use arguments object
var exprs = expr[1];
var binding = {};
+ var newEnv = {bindings: binding, outer: env};
+
for(var i = 0; i < arguments.length; ++i){
- binding[exprs[i]] = evalScheem(arguments[i], env);
+ binding[exprs[i]] = evalScheem(arguments[i], newEnv);
}
- var newEnv = {bindings: binding, outer: env};
return evalScheem(expr[2], newEnv);
}
+
default:
var func = evalScheem(expr[0], env);
var args = expr;
@@ -59,6 +59,21 @@ suite('scheem', function(){
evalSchemeString('((lambda (x y) (+ x y)) ((lambda (x y) (+ x y)) 0 2) 2)', {}), 4);
});
+ test('inner function uses value from enclosing function', function() {
+ assert.deepEqual(
+ evalSchemeString('( ( lambda (x y) (+ x y) ) 2 ((lambda (z) (+ x z)) 0) )', {}), 4);
+ });
+
+ test('argument to a function shadows a global variable', function() {
+ assert.deepEqual(
+ evalSchemeString('(begin (define globalvar 9) ( ( lambda (x y globalvar) (+ globalvar (* x y)) ) 2 3 ) )', {}), 15);
+ });
+
+ test('a function modifies a global variable', function() {
+ assert.deepEqual(
+ evalSchemeString('(begin (define globalvar 9) ( ( lambda (x y globalvar) (set! globalvar (* x y)) ) 2 3 ) globalvar )', {}), 6);
+ });
+
});
suite('arithmetic', function () {

0 comments on commit e966b61

Please sign in to comment.