This commit is contained in:
Elf M. Sternberg 2015-06-17 13:02:37 -07:00
parent cff2d5cb97
commit b8aa463993
3 changed files with 96 additions and 21 deletions

25
Makefile Normal file
View File

@ -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)

View File

@ -1,15 +1,40 @@
lisp = require './lisp_ch1' chai = require 'chai'
{read, readForms} = require './reader' chai.should()
{inspect} = require 'util' 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)))") describe "Core interpreter", ->
# ast = read("(begin (set! fact 4) fact)") it "Should handle if statements", ->
# ast = read("(begin ((lambda (t) (if (lt t 2) (+ 4 1) (+ 2 1))) 1))") 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")
# ast = read("(begin (set! fact (lambda (x) (+ x x))) (fact 5))") it "Should handle basic arithmetic", ->
ast = read("(begin (set! fact (lambda (x) (- x 4))) (fact 5))") expect(lisp read '(begin (+ 5 5))').to.equal(10)
# ast = read("(begin ((lambda () (+ 5 5))))") 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)
it "Should handle some algebra", ->
expect(lisp read '(begin (* (+ 5 5) (* 2 3))').to.equal(60)
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)
console.log "Result:", (lisp ast)

View File

@ -1,15 +1,40 @@
lisp = require './interpreter' chai = require 'chai'
chai.should()
expect = chai.expect
lisp = require '../chapter3/interpreter'
{read, readForms} = require '../chapter1/reader' {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")
# ast = read("(begin (if (lt 4 5) (+ 4 1) (+ 2 1)))") it "Should handle basic arithmetic", ->
# ast = read("(begin (set! fact 4) fact)") expect(lisp read '(begin (+ 5 5))').to.equal(10)
# ast = read("(begin ((lambda (t) (if (lt t 2) (+ 4 1) (+ 2 1))) 1))") 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))") it "Should handle some algebra", ->
ast = read("(begin (set! fact (lambda (x) (- x 4))) (fact 5))") expect(lisp read '(begin (* (+ 5 5) (* 2 3))').to.equal(60)
# ast = read("(begin ((lambda () (+ 5 5))))")
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)
lisp(ast, (r) -> console.log("Result:", r))