From b8aa4639938066d218f1cc9e5208ec392b116448 Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Wed, 17 Jun 2015 13:02:37 -0700 Subject: [PATCH] Updated. --- Makefile | 25 +++++++++++++++++++++ test/test_chapter1.coffee | 47 ++++++++++++++++++++++++++++++--------- test/test_chapter3.coffee | 45 ++++++++++++++++++++++++++++--------- 3 files changed, 96 insertions(+), 21 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ca219ff --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +.PHONY: test + +# docs: $(patsubst %.md,%.html,$(wildcard *.md)) + +targets = lists.js reduce.js + +all: $(targets) + +%.js: src/%.coffee + node_modules/.bin/mocha coffee -c -o . $< + +%.html: %.md header.html footer.html + cat header.html > $@ + pandoc $< >> $@ + cat footer.html >> $@ + +node_modules: package.json + mkdir -p node_modules + npm install + +test: node_modules + @node_modules/.bin/mocha --compilers coffee:coffee-script/register + +clean: + rm -f $(targets) diff --git a/test/test_chapter1.coffee b/test/test_chapter1.coffee index 5cf563a..02dbb86 100644 --- a/test/test_chapter1.coffee +++ b/test/test_chapter1.coffee @@ -1,15 +1,40 @@ -lisp = require './lisp_ch1' -{read, readForms} = require './reader' -{inspect} = require 'util' +chai = require 'chai' +chai.should() +expect = chai.expect -# ast = read("(begin (set! fact (lambda (x) (if (eq? x 0) 1 (* x (fact (- x 1)))))) (fact 5))") +lisp = require '../chapter1/interpreter' +{read, readForms} = require '../chapter1/reader' -# ast = read("(begin (if (lt 4 5) (+ 4 1) (+ 2 1)))") -# ast = read("(begin (set! fact 4) fact)") -# ast = read("(begin ((lambda (t) (if (lt t 2) (+ 4 1) (+ 2 1))) 1))") +describe "Core interpreter", -> + it "Should handle if statements", -> + expect(lisp read "(begin (if (lt 0 1) #t #f))").to.equal(true) + expect(lisp read "(begin (if (lt 1 0) #t #f))").to.equal(false) + expect(lisp read '(begin (if (lt 1 0) "y" "n"))').to.equal("n") + expect(lisp read '(begin (if (lt 0 1) "y" "n"))').to.equal("y") + expect(lisp read '(begin (if (eq "y" "y") "y" "n"))').to.equal("y") + expect(lisp read '(begin (if (eq "y" "x") "y" "n"))').to.equal("n") + + it "Should handle basic arithmetic", -> + expect(lisp read '(begin (+ 5 5))').to.equal(10) + expect(lisp read '(begin (* 5 5))').to.equal(25) + expect(lisp read '(begin (/ 5 5))').to.equal(1) + expect(lisp read '(begin (- 9 5))').to.equal(4) -# ast = read("(begin (set! fact (lambda (x) (+ x x))) (fact 5))") -ast = read("(begin (set! fact (lambda (x) (- x 4))) (fact 5))") -# ast = read("(begin ((lambda () (+ 5 5))))") + it "Should handle some algebra", -> + expect(lisp read '(begin (* (+ 5 5) (* 2 3))').to.equal(60) -console.log "Result:", (lisp ast) + it "Should handle a basic setting", -> + expect(lisp read '(begin (set! fact 4) fact)').to.equal(4) + + it "Should handle a zero arity thunk", -> + expect(lisp read '(begin (set! fact (lambda () (+ 5 5))) (fact))').to.equal(10) + + it "Should handle a two arity thunk", -> + expect(lisp read '(begin (set! fact (lambda (a b) (+ a b))) (fact 4 6))').to.equal(10) + + it "Should handle a recursive function", -> + expect(lisp read '(begin (set! fact (lambda (x) (if (eq? x 0) 1 (* x (fact (- x 1)))))) (fact 5))').to.equal(120) + + it "Should handle an IIFE", -> + expect(lisp read '(begin ((lambda () (+ 5 5))))').to.equal(10) + diff --git a/test/test_chapter3.coffee b/test/test_chapter3.coffee index ac9458b..358150f 100644 --- a/test/test_chapter3.coffee +++ b/test/test_chapter3.coffee @@ -1,15 +1,40 @@ -lisp = require './interpreter' +chai = require 'chai' +chai.should() +expect = chai.expect + +lisp = require '../chapter3/interpreter' {read, readForms} = require '../chapter1/reader' -{inspect} = require 'util' -ast = read("(begin (set! fact (lambda (x) (if (eq? x 0) 1 (* x (fact (- x 1)))))) (fact 5))") +describe "Core interpreter", -> + it "Should handle if statements", -> + expect(lisp read "(begin (if (lt 0 1) #t #f))").to.equal(true) + expect(lisp read "(begin (if (lt 1 0) #t #f))").to.equal(false) + expect(lisp read '(begin (if (lt 1 0) "y" "n"))').to.equal("n") + expect(lisp read '(begin (if (lt 0 1) "y" "n"))').to.equal("y") + expect(lisp read '(begin (if (eq "y" "y") "y" "n"))').to.equal("y") + expect(lisp read '(begin (if (eq "y" "x") "y" "n"))').to.equal("n") + + it "Should handle basic arithmetic", -> + expect(lisp read '(begin (+ 5 5))').to.equal(10) + expect(lisp read '(begin (* 5 5))').to.equal(25) + expect(lisp read '(begin (/ 5 5))').to.equal(1) + expect(lisp read '(begin (- 9 5))').to.equal(4) -# ast = read("(begin (if (lt 4 5) (+ 4 1) (+ 2 1)))") -# ast = read("(begin (set! fact 4) fact)") -# ast = read("(begin ((lambda (t) (if (lt t 2) (+ 4 1) (+ 2 1))) 1))") + it "Should handle some algebra", -> + expect(lisp read '(begin (* (+ 5 5) (* 2 3))').to.equal(60) -# ast = read("(begin (set! fact (lambda (x) (+ x x))) (fact 5))") -ast = read("(begin (set! fact (lambda (x) (- x 4))) (fact 5))") -# ast = read("(begin ((lambda () (+ 5 5))))") + it "Should handle a basic setting", -> + expect(lisp read '(begin (set! fact 4) fact)').to.equal(4) -lisp(ast, (r) -> console.log("Result:", r)) + it "Should handle a zero arity thunk", -> + expect(lisp read '(begin (set! fact (lambda () (+ 5 5))) (fact))').to.equal(10) + + it "Should handle a two arity thunk", -> + expect(lisp read '(begin (set! fact (lambda (a b) (+ a b))) (fact 4 6))').to.equal(10) + + it "Should handle a recursive function", -> + expect(lisp read '(begin (set! fact (lambda (x) (if (eq? x 0) 1 (* x (fact (- x 1)))))) (fact 5))').to.equal(120) + + it "Should handle an IIFE", -> + expect(lisp read '(begin ((lambda () (+ 5 5))))').to.equal(10) +