Permalink
Browse files

adding isempty, test coverage

  • Loading branch information...
1 parent 526d2a2 commit 92be40b61a573dd299a374de3674382dc06d68ad @ajb committed May 9, 2012
Showing with 74 additions and 38 deletions.
  1. +65 −38 Homework5/scheem.js
  2. +9 −0 Homework5/test/evalScheemTest.js
View
@@ -19,7 +19,7 @@ var update = function (env, v, val) {
};
var add_binding = function (env, v, val) {
- // Your code here
+ if(typeof env.bindings === 'undefined') env.bindings = {};
env.bindings[v] = val;
return 0;
};
@@ -30,22 +30,75 @@ var evalScheem = function (expr, env) {
if(typeof env === 'undefined'){
env = {};
}
-
- if(typeof env.bindings === 'undefined'){
- env.bindings = {};
- }
if(typeof env.outer === 'undefined'){
env.outer = {};
}
- env.bindings.add_one = function (x) {
- return x + 1;
- };
+ try {
+ lookup(env, 'add_one');
+ } catch (error) {
+
+ add_binding(env, 'add_one', function (x) {
+ return x + 1;
+ });
+
+ add_binding(env, 'add_three', function (x, y, z) {
+ return x + y + z;
+ });
+
+ add_binding(env, '+', function(x, y){
+ return x + y;
+ });
+
+ add_binding(env, '-', function(x, y){
+ return x - y;
+ });
+
+ add_binding(env, '*', function(x, y){
+ return x * y;
+ });
+
+ add_binding(env, '/', function(x, y){
+ return x / y;
+ });
+
+ add_binding(env, '<', function(x, y){
+ if(x < y) return '#t';
+ return '#f';
+ });
+
+ add_binding(env, 'car', function(x){
+ return x[0];
+ });
+
+ add_binding(env, 'cons', function(x, y){
+ y.unshift(x);
+ return y;
+ });
+
+ add_binding(env, 'cdr', function(x){
+ x.splice(0, 1);
+ return x;
+ });
+
+ add_binding(env, '=', function(x, y){
+ if(x == y) return '#t';
+ return '#f';
+ });
+
+ add_binding(env, 'alert', function(x){
+ alert(x);
+ return x;
+ });
+
+ add_binding(env, 'isempty?', function(x){
+ if(x.length < 1) return '#t';
+ return '#f';
+ });
- env.bindings.add_three = function (x, y, z) {
- return x + y + z;
- };
+ }
+
// Numbers evaluate to themselves
if (typeof expr === 'number') {
@@ -59,14 +112,6 @@ var evalScheem = function (expr, env) {
// Look at head of list for operation
switch (expr[0]) {
- case '+':
- return evalScheem(expr[1], env) + evalScheem(expr[2], env);
- case '-':
- return evalScheem(expr[1], env) - evalScheem(expr[2], env);
- case '*':
- return evalScheem(expr[1], env) * evalScheem(expr[2], env);
- case '/':
- return evalScheem(expr[1], env) / evalScheem(expr[2], env);
case 'define':
return add_binding(env, expr[1], evalScheem(expr[2], env));
case 'set!':
@@ -84,24 +129,6 @@ var evalScheem = function (expr, env) {
returnVar = evalScheem(theArgs[i], env);
}
return returnVar;
- case '<':
- var predicate = (evalScheem(expr[1], env) < evalScheem(expr[2], env));
- if (predicate) return '#t';
- return '#f';
- case 'cons':
- var cons = evalScheem(expr[2], env);
- cons.unshift(evalScheem(expr[1], env));
- return cons;
- case 'car':
- return evalScheem(expr[1], env)[0];
- case 'cdr':
- var cdr = evalScheem(expr[1], env);
- cdr.splice(0, 1);
- return cdr;
- case '=':
- var eq = (evalScheem(expr[1], env) === evalScheem(expr[2], env));
- if (eq) return '#t';
- return '#f';
case 'if':
if (evalScheem(expr[1], env) == '#t') {
return evalScheem(expr[2], env);
@@ -118,7 +145,7 @@ var evalScheem = function (expr, env) {
return evalScheem(expr[2], newEnv);
};
case 'lambda':
- return function(){ // use arguments object
+ return function(/*arguments*/){
var exprs = expr[1];
var binding = {};
var newEnv = {bindings: binding, outer: env};
@@ -193,6 +193,15 @@ suite('scheem', function(){
assert.deepEqual(
evalSchemeString('(cdr (quote (1 2 3)))'), [2, 3]);
});
+
+ test('isempty?', function () {
+ assert.deepEqual(
+ evalSchemeString('(isempty? (quote ()))', {}), '#t');
+
+ assert.deepEqual(
+ evalSchemeString('(isempty? (quote (1)))', {}), '#f');
+
+ });
});
suite('error checking', function() {

0 comments on commit 92be40b

Please sign in to comment.