Some renaming; one major bugfix, using cons-lists instead of a local library.

This commit is contained in:
Elf M. Sternberg 2015-05-20 09:41:30 -07:00
parent 86efa8c09a
commit 570fb2b1df
4 changed files with 15 additions and 11 deletions

View File

@ -5,7 +5,7 @@
"main": "bin/lisp",
"dependencies": {
"coffee-script": "^1.9.1",
"git+https://github.com/elfsternberg/cons-lists.git"
"cons-lists": "git+https://github.com/elfsternberg/cons-lists.git"
},
"devDependencies": {
"chai": "^2.0.0",

View File

@ -1,4 +1,4 @@
{listToString, listToVector, pairp, cons, car, cdr, caar, cddr, cdar, cadr, caadr, cadar, caddr, nilp, nil, setcdr, metacadr} = require "./lists"
{listToString, listToVector, pairp, cons, car, cdr, caar, cddr, cdar, cadr, caadr, cadar, caddr, nilp, nil, setcdr, metacadr} = require "cons-lists/lists"
readline = require "readline"
{inspect} = require "util"
@ -12,11 +12,11 @@ definitial = (name, value = nil) ->
env_global = (cons (cons name, value), env_global)
name
defprimitive = (name, value, arity) ->
definitial name, ((values) ->
vvalues = listToVector(values)
if (vvalues.length == arity)
return value(vvalues)
defprimitive = (name, nativ, arity) ->
definitial name, ((args) ->
vmargs = listToVector(args)
if (vmargs.length == arity)
nativ.apply null, vmargs
else
throw "Incorrect arity")
@ -31,7 +31,7 @@ definitial "fib"
definitial "fact"
defpredicate = (name, nativ, arity) ->
defprimitive name, ((args) -> if (nativ.apply null, args) then true else the_false_value), arity
defprimitive name, ((a, b) -> if nativ.call(null, a, b) then true else the_false_value), arity
defprimitive "cons", cons, 2
defprimitive "car", car, 2
@ -60,7 +60,7 @@ make_function = (variables, body, env) ->
(values) -> eprogn body, (extend env, variables, values)
invoke = (fn, args) ->
(fn(args))
fn(args)
# Takes a list of nodes and calls evaluate on each one, returning the
# last one as the value of the total expression. In this example, we
@ -93,7 +93,7 @@ lookup = (id, env) ->
update = (id, env, value) ->
if (pairp env)
if (caar env) == id
setcdr (car env), value
setcdr value, (car env)
value
else
update id, (cdr env), value

View File

@ -1,4 +1,4 @@
{car, cdr, cons, nil, nilp, pairp, vectorToList} = require './lists'
{car, cdr, cons, nil, nilp, pairp, vectorToList} = require 'cons-lists/lists'
NEWLINES = ["\n", "\r", "\x0B", "\x0C"]
WHITESPACE = [" ", "\t"].concat(NEWLINES)

View File

@ -2,4 +2,8 @@ lisp = require './lisp_ch1'
{read, readForms} = require './reader'
{inspect} = require 'util'
ast = read("(begin (set! fact (lambda (x) (if (eq? x 0) 1 (* x (fact (- x 1)))))) (fact 5))")
# ast = read("(begin (if (lt 4 2) (+ 4 1) (+ 2 1)))")
# ast = read("(begin (set! fact 4) fact)")
ast = read("(begin ((lambda (t) (if (lt t 2) (+ 4 1) (+ 2 1))) 4))")
console.log "Result:", (lisp ast)