Some renaming; one major bugfix, using cons-lists instead of a local library.
This commit is contained in:
parent
86efa8c09a
commit
570fb2b1df
|
@ -5,7 +5,7 @@
|
||||||
"main": "bin/lisp",
|
"main": "bin/lisp",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"coffee-script": "^1.9.1",
|
"coffee-script": "^1.9.1",
|
||||||
"git+https://github.com/elfsternberg/cons-lists.git"
|
"cons-lists": "git+https://github.com/elfsternberg/cons-lists.git"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"chai": "^2.0.0",
|
"chai": "^2.0.0",
|
||||||
|
|
|
@ -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"
|
readline = require "readline"
|
||||||
{inspect} = require "util"
|
{inspect} = require "util"
|
||||||
|
|
||||||
|
@ -12,11 +12,11 @@ definitial = (name, value = nil) ->
|
||||||
env_global = (cons (cons name, value), env_global)
|
env_global = (cons (cons name, value), env_global)
|
||||||
name
|
name
|
||||||
|
|
||||||
defprimitive = (name, value, arity) ->
|
defprimitive = (name, nativ, arity) ->
|
||||||
definitial name, ((values) ->
|
definitial name, ((args) ->
|
||||||
vvalues = listToVector(values)
|
vmargs = listToVector(args)
|
||||||
if (vvalues.length == arity)
|
if (vmargs.length == arity)
|
||||||
return value(vvalues)
|
nativ.apply null, vmargs
|
||||||
else
|
else
|
||||||
throw "Incorrect arity")
|
throw "Incorrect arity")
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ definitial "fib"
|
||||||
definitial "fact"
|
definitial "fact"
|
||||||
|
|
||||||
defpredicate = (name, nativ, arity) ->
|
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 "cons", cons, 2
|
||||||
defprimitive "car", car, 2
|
defprimitive "car", car, 2
|
||||||
|
@ -60,7 +60,7 @@ make_function = (variables, body, env) ->
|
||||||
(values) -> eprogn body, (extend env, variables, values)
|
(values) -> eprogn body, (extend env, variables, values)
|
||||||
|
|
||||||
invoke = (fn, args) ->
|
invoke = (fn, args) ->
|
||||||
(fn(args))
|
fn(args)
|
||||||
|
|
||||||
# Takes a list of nodes and calls evaluate on each one, returning the
|
# 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
|
# last one as the value of the total expression. In this example, we
|
||||||
|
@ -93,7 +93,7 @@ lookup = (id, env) ->
|
||||||
update = (id, env, value) ->
|
update = (id, env, value) ->
|
||||||
if (pairp env)
|
if (pairp env)
|
||||||
if (caar env) == id
|
if (caar env) == id
|
||||||
setcdr (car env), value
|
setcdr value, (car env)
|
||||||
value
|
value
|
||||||
else
|
else
|
||||||
update id, (cdr env), value
|
update id, (cdr env), value
|
||||||
|
|
|
@ -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"]
|
NEWLINES = ["\n", "\r", "\x0B", "\x0C"]
|
||||||
WHITESPACE = [" ", "\t"].concat(NEWLINES)
|
WHITESPACE = [" ", "\t"].concat(NEWLINES)
|
||||||
|
|
|
@ -2,4 +2,8 @@ lisp = require './lisp_ch1'
|
||||||
{read, readForms} = require './reader'
|
{read, readForms} = require './reader'
|
||||||
{inspect} = require 'util'
|
{inspect} = require 'util'
|
||||||
ast = read("(begin (set! fact (lambda (x) (if (eq? x 0) 1 (* x (fact (- x 1)))))) (fact 5))")
|
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)
|
console.log "Result:", (lisp ast)
|
||||||
|
|
Loading…
Reference in New Issue