Skip to content

Commit

Permalink
recursion works!
Browse files Browse the repository at this point in the history
  • Loading branch information
jostylr committed May 11, 2012
1 parent 4b78bfa commit 1cfa024
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 7 deletions.
32 changes: 29 additions & 3 deletions scheem/evalScheem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ var debugF;
var initenv = function () {
return {
vars : {
'+' : function (arr) {

'+' : function (arr) {
if (!arr || arr.length === 0) {
throw "insufficient arguments +";
}
Expand Down Expand Up @@ -86,8 +85,33 @@ var initenv = function () {
};
}
return cur;
},
'<' : function (arr) {
if (!arr || arr.length === 0 || arr.length === 1) {
throw "insufficient arguments +";
}
var i, sum = 0, n = arr.length;
for (i = 1; i < n; i += 1) {
if (arr[i-1] >= arr[i]) {
return '#f'
}
}
return '#t';
},
'=' : function (arr) {
if (!arr || arr.length === 0 || arr.length === 1) {
throw "insufficient arguments +";
}
var i, sum = 0, n = arr.length;
for (i = 1; i < n; i += 1) {
if (arr[i-1] !== arr[i]) {
return '#f'
}
}
return '#t';
}



},
parent : null
Expand Down Expand Up @@ -166,9 +190,11 @@ var evalScheem = function (expr) {

while (stack.length !== 0) {

debugF(stack)

cur = stack.pop();

//debugF(stack)


if (typeof cur === 'number') {
values[0].push( cur );
Expand Down
77 changes: 77 additions & 0 deletions scheem/pretest/runT.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@ module.exports.suites = {
},
arith: function () {
return runT.apply(null, arguments);
},
begin: function () {
return runT.apply(null, arguments);
},
quote: function () {
return runT.apply({
debugS: 3
}, arguments);
},
lambda: function () {
return runT.apply(null, arguments);
},
ifel: function () {
return runT.apply(null, arguments);
},
def: function () {
return runT.apply(null, arguments);
},
let: function () {
return runT.apply(null, arguments);
},
inequality: function () {
return runT.apply(null, arguments);
},
recursion: function () {
return runT.apply(null, arguments);
},
equality: function () {
return runT.apply(null, arguments);
}
};

Expand Down Expand Up @@ -63,6 +92,54 @@ var data = {
inp: ['(% 3 4)'],
out: 3
}
},
quote: {
'\'(1 2 3)': {
inp: ['\'(1 2 3)'],
out: [1, 2, 3]
},
'\'atom': {
inp: ['\'atom'],
out: 'atom'
}
},
inequality: {
'(< 2 3)': {
inp: ['(< 2 3)'],
out: '#t'
},
'(< 2 3 4)': {
inp: ['(< 2 3 4)'],
out: '#t'
},
'(< 2 5 4)': {
inp: ['(< 2 5 4)'],
out: '#f'
},
'(< 2)': {
inp: ['(< 2)'],
out: ['error', 'insufficient arguments +']
}
},
equality: {
'(= 1 0)': {
inp: ['(= 1 0)'],
out: '#f'
},
'(= 1 1)': {
inp: ['(= 1 1)'],
out: '#t'
}
},
recursion: {
'(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (factorial 3)': {
inp: ['(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (factorial 3)'],
out: [6]
},
'(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (factorial 5)': {
inp: ['(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (factorial 5)'],
out: [120]
}
}
};

Expand Down
4 changes: 3 additions & 1 deletion scheem/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ var parser = scheem.parse;
var evalScheem = require('./evalScheem').evalScheem;

module.exports = function (str) {
return evalScheem(parser(str));
var par = parser(str);
//console.log(par);
return evalScheem(par);
}
7 changes: 4 additions & 3 deletions scheem/scheem.peg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
start =
e: expression+
{ e.unshift('begin'); return e;}
{ if (e.length === 1) {return e[0]; } else { e.unshift('begin'); return e;} }

quote =
_ "'" _ e: expression
Expand All @@ -13,12 +13,13 @@ expression =
{return a}
/ _ s: string
{return s}
/ quote
/ _ q: quote
{return q}
/ _ "(" _ e: expression+ _ ")" _
{return e}

validchar
= [0-9a-zA-Z_?!+=@#$%^&*/.-]
= [0-9a-zA-Z_?!+=<>@#$%^&*/.-]

string =
_ "\"" s :[^\"\n\r]* "\""
Expand Down
125 changes: 125 additions & 0 deletions scheem/test/runT.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@ var suites = {
},
arith: function () {
return runT.apply(null, arguments);
},
begin: function () {
return runT.apply(null, arguments);
},
quote: function () {
return runT.apply({
debugS: 3
}, arguments);
},
lambda: function () {
return runT.apply(null, arguments);
},
ifel: function () {
return runT.apply(null, arguments);
},
def: function () {
return runT.apply(null, arguments);
},
let: function () {
return runT.apply(null, arguments);
},
inequality: function () {
return runT.apply(null, arguments);
},
recursion: function () {
return runT.apply(null, arguments);
},
equality: function () {
return runT.apply(null, arguments);
}
};

Expand Down Expand Up @@ -115,4 +144,100 @@ test("(% 3 4)", function () {
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "3" + "\n Input: ['(%34)']");
}
});

suite("quote");

test("'(1 2 3)", function () {
var result = suites.quote.apply(null, ['\'(1 2 3)']);
var pass = _.isEqual(result, [1, 2, 3]);
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "[ 1, 2, 3 ]" + "\n Input: ['\'(123)']");
}
});

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

suite("inequality");

test("(< 2 3)", function () {
var result = suites.inequality.apply(null, ['(< 2 3)']);
var pass = _.isEqual(result, '#t');
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "'#t'" + "\n Input: ['(<23)']");
}
});

test("(< 2 3 4)", function () {
var result = suites.inequality.apply(null, ['(< 2 3 4)']);
var pass = _.isEqual(result, '#t');
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "'#t'" + "\n Input: ['(<234)']");
}
});

test("(< 2 5 4)", function () {
var result = suites.inequality.apply(null, ['(< 2 5 4)']);
var pass = _.isEqual(result, '#f');
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "'#f'" + "\n Input: ['(<254)']");
}
});

test("(< 2)", function () {
var flag = true;
try {
suites.inequality.apply(null, ['(< 2)']);
}
catch (e) {
flag = false;
if (!_.isEqual(e.toString(), "insufficient arguments +")) {
throw new Error("wrong error", e);
}
}
if (flag) {
throw new Error("failed to throw error");
}
});

suite("equality");

test("(= 1 0)", function () {
var result = suites.equality.apply(null, ['(= 1 0)']);
var pass = _.isEqual(result, '#f');
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "'#f'" + "\n Input: ['(=10)']");
}
});

test("(= 1 1)", function () {
var result = suites.equality.apply(null, ['(= 1 1)']);
var pass = _.isEqual(result, '#t');
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "'#t'" + "\n Input: ['(=11)']");
}
});

suite("recursion");

test("(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (factorial 3)", function () {
var result = suites.recursion.apply(null, ['(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (factorial 3)']);
var pass = _.isEqual(result, [6]);
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "[ 6 ]" + "\n Input: ['(definefactorial(lambda(n)(if(=n0)1(*n(factorial(-n1))))))(factorial3)']");
}
});

test("(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (factorial 5)", function () {
var result = suites.recursion.apply(null, ['(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (factorial 5)']);
var pass = _.isEqual(result, [120]);
if (!pass) {
throw new Error(util.inspect(result) + " not equal to " + "[ 120 ]" + "\n Input: ['(definefactorial(lambda(n)(if(=n0)1(*n(factorial(-n1))))))(factorial5)']");
}
});

0 comments on commit 1cfa024

Please sign in to comment.