[refactor] Struggling to get self-evaluating components working.

This commit is contained in:
Elf M. Sternberg 2015-07-26 14:59:49 -07:00
parent 5bba101ee2
commit 02f79c4255
2 changed files with 105 additions and 97 deletions

View File

@ -24,7 +24,7 @@ test: clean node_modules
--reporter mocha-jenkins-reporter --compilers coffee:coffee-script/register || true --reporter mocha-jenkins-reporter --compilers coffee:coffee-script/register || true
ltest: node_modules ltest: node_modules
@node_modules/.bin/mocha --compilers coffee:coffee-script/register node_modules/.bin/mocha --compilers coffee:coffee-script/register
watch: watch:
while inotifywait $(SOURCES) ; do make test; done while inotifywait $(SOURCES) ; do make test; done

View File

@ -2,19 +2,15 @@
cdar, cadr, caadr, cadar, caddr, nilp, nil, setcdr, metacadr} = require "cons-lists/lists" 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"
{Symbol} = require "./reader_types"
class LispInterpreterError extends Error class LispInterpreterError extends Error
name: 'LispInterpreterError' name: 'LispInterpreterError'
constructor: (@message) -> constructor: (@message, position = null) ->
env_init = nil env_init = nil
env_global = env_init env_global = env_init
defpredicate = (name, nativ, arity) ->
defprimitive name, ((a, b) -> if nativ.call(null, a, b) then true else the_false_value), arity
definitial = (name, value = nil) -> definitial = (name, value = nil) ->
env_global = (cons (cons name, value), env_global) env_global = (cons (cons name, value), env_global)
name name
@ -27,6 +23,9 @@ defprimitive = (name, nativ, arity) ->
else else
throw (new LispInterpreterError "Incorrect arity")) throw (new LispInterpreterError "Incorrect arity"))
defpredicate = (name, nativ, arity) ->
defprimitive name, ((a, b) -> if nativ.call(null, a, b) then true else the_false_value), arity
definitial "#t", true definitial "#t", true
definitial "#f", the_false_value definitial "#f", the_false_value
definitial "nil", nil definitial "nil", nil
@ -47,32 +46,7 @@ defpredicate "eq?", ((a, b) -> a == b), 2
the_false_value = (cons "false", "boolean") the_false_value = (cons "false", "boolean")
cadddr = metacadr('cadddr') extend = (env, variables, values) ->
metadata_evaluation =
listp: (node) -> (car node) == 'list'
symbolp: (node) -> (car node) == 'symbol'
numberp: (node) -> (car node) == 'number'
stringp: (node) -> (car node) == 'string'
nvalu: (node) -> cadr node
straight_evaluation =
listp: (node) -> node.__type == 'list'
symbolp: (node) -> node instanceOf Symbol
commentp: (node) -> node instanceOf Comment
numberp: (node) -> typeof node == 'number'
stringp: (node) -> typeof node == 'string'
boolp: (node) -> typeof node == 'boolean'
nullp: (node) -> node == null
vectorp: (node) -> (not listp node) and toString.call(node) == '[object Array]'
recordp: (node) -> (not x._prototype?) and toSTring.call(node) == '[object Object]')
objectp: (node) -> (x._prototype?) and toString.call(node) == '[object Object]')
nilp: (node) -> node == nilp
nvalu: (node) -> node
makeEvaluator = (ix = straight_evaluation) ->
(exp, env) ->
extend = (env, variables, values) ->
if (pairp variables) if (pairp variables)
if (pairp values) if (pairp values)
(cons (cons (car variables), (car values)), (cons (cons (car variables), (car values)),
@ -87,18 +61,18 @@ makeEvaluator = (ix = straight_evaluation) ->
else else
nil nil
make_function = (variables, body, env) -> 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
# are hard-coding what ought to be a macro, namely the threading # are hard-coding what ought to be a macro, namely the threading
# macros, "->" # macros, "->"
eprogn = (exps, env) -> eprogn = (exps, env) ->
if (pairp exps) if (pairp exps)
if pairp (cdr exps) if pairp (cdr exps)
evaluate (car exps), env evaluate (car exps), env
@ -108,13 +82,13 @@ makeEvaluator = (ix = straight_evaluation) ->
else else
nil nil
evlis = (exps, env) -> evlis = (exps, env) ->
if (pairp exps) if (pairp exps)
(cons (evaluate (car exps), env), (evlis (cdr exps), env)) (cons (evaluate (car exps), env), (evlis (cdr exps), env))
else else
nil nil
lookup = (id, env) -> lookup = (id, env) ->
if (pairp env) if (pairp env)
if (caar env) == id if (caar env) == id
cdar env cdar env
@ -123,7 +97,7 @@ makeEvaluator = (ix = straight_evaluation) ->
else else
nil nil
update = (id, env, value) -> update = (id, env, value) ->
if (pairp env) if (pairp env)
if (caar env) == id if (caar env) == id
setcdr value, (car env) setcdr value, (car env)
@ -133,11 +107,8 @@ makeEvaluator = (ix = straight_evaluation) ->
else else
nil nil
# This really ought to be the only place where the AST meets the astSymbolsToLispSymbols = (node) ->
# interpreter core. I can't help but think that this design precludes nvalu = (node) -> cadr node
# pluggable interpreter core.
astSymbolsToLispSymbols = (node) ->
return nil if nilp node return nil if nilp node
throw (new LispInterpreterError "Not a list of variable names") if not (ntype(node) is 'list') throw (new LispInterpreterError "Not a list of variable names") if not (ntype(node) is 'list')
handler = (node) -> handler = (node) ->
@ -145,18 +116,49 @@ makeEvaluator = (ix = straight_evaluation) ->
cons (nvalu car node), (handler cdr node) cons (nvalu car node), (handler cdr node)
handler(nvalu node) handler(nvalu node)
cadddr = metacadr('cadddr')
# This is really the only thing that changes behavior between "reader
# nodes" (nodes loaded with debugging metadata) and a standard cons
# object. TODO: astSymbolsToLispSymbols should be deprecated in
# favor of normalizeForm (s?) and Symbol extraction
metadata_evaluation =
listp: (node) -> (car node) == 'list'
symbolp: (node) -> (car node) == 'symbol'
numberp: (node) -> (car node) == 'number'
stringp: (node) -> (car node) == 'string'
nvalu: (node) -> cadr node
straight_evaluation =
listp: (node) -> node.__type == 'list'
symbolp: (node) -> node instanceof Symbol
commentp: (node) -> node instanceof Comment
numberp: (node) -> typeof node == 'number'
stringp: (node) -> typeof node == 'string'
boolp: (node) -> typeof node == 'boolean'
nullp: (node) -> node == null
vectorp: (node) -> (not straight_evaluation.listp node) and toString.call(node) == '[object Array]'
recordp: (node) -> (not node._prototype?) and toSTring.call(node) == '[object Object]'
objectp: (node) -> (node._prototype?) and toString.call(node) == '[object Object]'
nilp: (node) -> nilp(node)
nvalu: (node) -> node
makeEvaluator = (ix = straight_evaluation, ty="straight") ->
(exp, env) ->
console.log("Type:", ty)
# Takes an AST node and evaluates it and its contents. A node may be # Takes an AST node and evaluates it and its contents. A node may be
# ("list" (... contents ...)) or ("number" 42) or ("symbol" x), etc. # ("list" (... contents ...)) or ("number" 42) or ("symbol" x), etc.
[type, exp] = [(ntype e), (nvalu e)] if ix.symbolp(exp)
if type == "symbol" return lookup (ix.nvalu exp).v, env
return lookup exp, env else if ([ix.numberp, ix.stringp].filter (i) -> i(exp)).length > 0
else if type in ["number", "string", "boolean", "vector"]
return exp return exp
else if type == "list" else if ix.listp(exp)
head = car exp head = car ix.nvalu exp
if (ntype head) == 'symbol' if ix.symbolp(head)
switch (nvalu head) switch (ix.nvalu head).v
when "quote" then cdr exp when "quote" then cdr exp
when "if" when "if"
unless (evaluate (cadr exp), env) == the_false_value unless (evaluate (cadr exp), env) == the_false_value
@ -164,7 +166,7 @@ makeEvaluator = (ix = straight_evaluation) ->
else else
evaluate (cadddr exp), env evaluate (cadddr exp), env
when "begin" then eprogn (cdr exp), env when "begin" then eprogn (cdr exp), env
when "set!" then update (nvalu cadr exp), env, (evaluate (caddr exp), env) when "set!" then update (ix.nvalu cadr exp).v, env, (evaluate (caddr exp), env)
when "lambda" then make_function (astSymbolsToLispSymbols cadr exp), (cddr exp), env when "lambda" then make_function (astSymbolsToLispSymbols cadr exp), (cddr exp), env
else invoke (evaluate (car exp), env), (evlis (cdr exp), env) else invoke (evaluate (car exp), env), (evlis (cdr exp), env)
else else
@ -172,4 +174,10 @@ makeEvaluator = (ix = straight_evaluation) ->
else else
throw new LispInterpreterError "Can't handle a #{type}" throw new LispInterpreterError "Can't handle a #{type}"
nodeEval = makeEvaluator(metadata_evaluation, "node")
lispEval = makeEvaluator(straight_evaluation, "lisp")
evaluate = (exp, env) ->
(if exp? and exp.__node then nodeEval else lispEval)(exp, env)
module.exports = (c) -> evaluate c, env_global module.exports = (c) -> evaluate c, env_global