2015-07-08 22:14:05 +00:00
|
|
|
{listToString, listToVector, pairp, cons, car, cdr, caar, cddr, cdar,
|
|
|
|
cadr, caadr, cadar, caddr, nilp, nil, setcdr,
|
|
|
|
metacadr, setcar} = require "cons-lists/lists"
|
2015-07-30 14:29:39 +00:00
|
|
|
{normalizeForms, normalizeForm} = require "../chapter1/astToList"
|
2015-07-31 14:34:12 +00:00
|
|
|
{Node, Symbol} = require '../chapter1/reader_types'
|
2015-07-01 20:55:22 +00:00
|
|
|
|
2015-07-08 22:14:05 +00:00
|
|
|
class LispInterpreterError extends Error
|
|
|
|
name: 'LispInterpreterError'
|
|
|
|
constructor: (@message) ->
|
|
|
|
|
2015-07-08 02:57:35 +00:00
|
|
|
the_false_value = (cons "false", "boolean")
|
|
|
|
|
2015-07-28 23:51:01 +00:00
|
|
|
# Base class that represents a value. Base class representing a LiSP
|
|
|
|
# value, a primitive, or a function
|
|
|
|
|
2015-07-31 14:34:12 +00:00
|
|
|
eq = (id1, id2) ->
|
|
|
|
if id1 instanceof Symbol and id2 instanceof Symbol
|
|
|
|
return id1.name == id2.name
|
|
|
|
id1 == id2
|
|
|
|
|
2015-07-03 00:21:50 +00:00
|
|
|
class Value
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# Represents the base class of a continuation. Calls to invoke resume
|
|
|
|
# the contained continuation, which is typecast to one of the specific
|
|
|
|
# continuation needs of conditional, sequence, etc...
|
2015-07-08 22:14:05 +00:00
|
|
|
|
2015-07-02 00:38:31 +00:00
|
|
|
class Continuation
|
2015-07-09 03:35:55 +00:00
|
|
|
constructor: (@kont) ->
|
|
|
|
invoke: (value, env, kont) ->
|
|
|
|
if nilp cdr value
|
|
|
|
@kont.resume (car value)
|
2015-07-03 00:21:50 +00:00
|
|
|
else
|
2015-07-08 22:14:05 +00:00
|
|
|
throw new LispInterpreterError "Continuations expect one argument"
|
2015-07-09 03:35:55 +00:00
|
|
|
unwind: (value, ktarget) ->
|
|
|
|
if (@kont == ktarget) then (@kont.resume value) else (@kont.unwind value, ktarget)
|
2015-07-21 23:16:13 +00:00
|
|
|
catchLookup: (tag, kk) ->
|
|
|
|
@kont.catchLookup tag, kk
|
2015-07-03 00:21:50 +00:00
|
|
|
|
|
|
|
# Abstract class representing the environment
|
|
|
|
|
|
|
|
class Environment
|
2015-07-08 22:14:05 +00:00
|
|
|
lookup: -> throw new LispInterpreterError "Nonspecific invocation"
|
|
|
|
update: -> throw new LispInterpreterError "Nonspecific invocation"
|
2015-07-09 03:35:55 +00:00
|
|
|
blockLookup: -> throw new LispInterpreterError "Not an Environment"
|
2015-07-03 00:21:50 +00:00
|
|
|
|
|
|
|
# Base of the environment stack. If you hit this, your variable was
|
|
|
|
# never found for lookup/update. Note that at this time in the
|
2015-07-08 22:14:05 +00:00
|
|
|
# class, you have not
|
2015-07-02 00:38:31 +00:00
|
|
|
|
|
|
|
class NullEnv extends Environment
|
2015-07-08 22:14:05 +00:00
|
|
|
lookup: (e) -> throw new LispInterpreterError "Unknown variable #{e}"
|
|
|
|
update: (e) -> throw new LispInterpreterError "Unknown variable #{e}"
|
2015-07-09 03:35:55 +00:00
|
|
|
blockLookup: (name) -> throw new LispInterpreterError "Unknown block label #{name}"
|
2015-07-03 00:21:50 +00:00
|
|
|
|
|
|
|
# This appears to be an easy and vaguely abstract handle to the
|
|
|
|
# environment. The book is not clear on the distinction between the
|
|
|
|
# FullEnv and the VariableEnv.
|
2015-07-08 22:14:05 +00:00
|
|
|
|
2015-07-02 00:38:31 +00:00
|
|
|
class FullEnv extends Environment
|
|
|
|
constructor: (@others, @name) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "FullEnv"
|
2015-07-02 00:38:31 +00:00
|
|
|
lookup: (name, kont) ->
|
|
|
|
@others.lookup name, kont
|
|
|
|
update: (name, kont, value) ->
|
|
|
|
@others.update name, kont, value
|
2015-07-09 03:35:55 +00:00
|
|
|
blockLookup: (name, kont, value) ->
|
|
|
|
@others.blockLookup(name, kont, value)
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-03 00:21:50 +00:00
|
|
|
# This is the classic environment pair; either it's *this*
|
|
|
|
# environment, or it's a parent environment, until you hit the
|
2015-07-03 22:45:37 +00:00
|
|
|
# NullEnv. Once the name has been found, the continuation is called
|
|
|
|
# with the found value.
|
2015-07-03 00:21:50 +00:00
|
|
|
|
2015-07-02 00:38:31 +00:00
|
|
|
class VariableEnv extends FullEnv
|
|
|
|
constructor: (@others, @name, @value) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "VariableEnv"
|
|
|
|
lookup: (name, kont) ->
|
|
|
|
if name == @name
|
|
|
|
kont.resume @value
|
|
|
|
else
|
|
|
|
@others.lookup name, kont
|
|
|
|
update: (name, kont, value) ->
|
|
|
|
if name == @name
|
|
|
|
@value = value
|
|
|
|
kont.resume value
|
|
|
|
else
|
|
|
|
@others.update name, kont, value
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# "Renders the quote term to the current continuation"; in a more
|
|
|
|
# familiar parlance, calls resume in the current context with the
|
|
|
|
# quoted term uninterpreted.
|
2015-07-02 00:38:31 +00:00
|
|
|
|
|
|
|
evaluateQuote = (v, env, kont) ->
|
2015-07-30 14:29:39 +00:00
|
|
|
kont.resume normalizeForms v
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# Evaluates the conditional expression, creating a continuation with
|
|
|
|
# the current environment that, when resumed, evaluates either the
|
|
|
|
# true or false branch, again in the current enviornment.
|
2015-07-02 00:38:31 +00:00
|
|
|
|
|
|
|
evaluateIf = (exps, env, kont) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
evaluate (car exps), env, new IfCont(kont, (cadr exps), (caddr exps), env)
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-21 23:16:13 +00:00
|
|
|
|
2015-07-02 00:38:31 +00:00
|
|
|
class IfCont extends Continuation
|
2015-07-09 03:35:55 +00:00
|
|
|
constructor: (@kont, @ift, @iff, @env) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "IfCont"
|
|
|
|
resume: (value) ->
|
2015-07-09 03:35:55 +00:00
|
|
|
evaluate (if value == the_false_value then @iff else @ift), @env, @kont
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# Sequences: evaluates the current expression with a continuation that
|
|
|
|
# represents "the next expression" in the sequence. Upon resumption,
|
|
|
|
# calls this function with that next expression.
|
2015-07-02 00:38:31 +00:00
|
|
|
|
|
|
|
evaluateBegin = (exps, env, kont) ->
|
|
|
|
if (pairp exps)
|
|
|
|
if pairp (cdr exps)
|
|
|
|
evaluate (car exps), env, (new BeginCont kont, exps, env)
|
|
|
|
else
|
|
|
|
evaluate (car exps), env, kont
|
|
|
|
else
|
2015-07-03 22:45:37 +00:00
|
|
|
kont.resume("Begin empty value")
|
2015-07-02 00:38:31 +00:00
|
|
|
|
|
|
|
class BeginCont extends Continuation
|
2015-07-09 03:35:55 +00:00
|
|
|
constructor: (@kont, @exps, @env) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "BeginCont"
|
2015-07-09 03:35:55 +00:00
|
|
|
resume: (v) -> evaluateBegin (cdr @exps), @env, @kont
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# In this continuation, we simply pass the continuation and the name
|
|
|
|
# to the environment to look up. The environment knows to call the
|
|
|
|
# continuation with the value.
|
2015-07-02 00:38:31 +00:00
|
|
|
|
|
|
|
evaluateVariable = (name, env, kont) ->
|
|
|
|
env.lookup(name, kont)
|
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# This is the same dance as lookup, only with the continuation being
|
|
|
|
# called after an update has been performed.
|
2015-07-02 00:38:31 +00:00
|
|
|
|
|
|
|
evaluateSet = (name, exp, env, kont) ->
|
2015-07-08 22:14:05 +00:00
|
|
|
evaluate exp, env, (new SetCont(kont, name, env))
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-08 02:57:35 +00:00
|
|
|
class SetCont extends Continuation
|
2015-07-09 03:35:55 +00:00
|
|
|
constructor: (@kont, @name, @env) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "SetCont"
|
2015-07-02 00:38:31 +00:00
|
|
|
resume: (value) ->
|
2015-07-09 03:35:55 +00:00
|
|
|
@env.update @name, @kont, value
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# Calls the current contunation, passing it a new function wrapper.
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-03 00:21:50 +00:00
|
|
|
evaluateLambda = (names, exp, env, kont) ->
|
2015-07-03 22:45:37 +00:00
|
|
|
kont.resume new Function names, exp, env
|
|
|
|
|
|
|
|
# Upon invocation, evaluates the body with a new environment that
|
|
|
|
# consists of the original names, their current values as called, and
|
|
|
|
# the continuation an the moment of invocation, which will continue
|
|
|
|
# (resume) execution once the function is finished.
|
|
|
|
#
|
|
|
|
# By the way: this is pretty much the whole the point.
|
2015-07-02 00:38:31 +00:00
|
|
|
|
|
|
|
class Function extends Value
|
|
|
|
constructor: (@variables, @body, @env) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "Function"
|
2015-07-02 00:38:31 +00:00
|
|
|
invoke: (values, env, kont) ->
|
|
|
|
evaluateBegin @body, (extend @env, @variables, values), kont
|
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# Helper function to build name/value pairs for the current execution
|
|
|
|
# context.
|
|
|
|
|
2015-07-02 00:38:31 +00:00
|
|
|
extend = (env, names, values) ->
|
|
|
|
if (pairp names) and (pairp values)
|
2015-07-08 02:57:35 +00:00
|
|
|
new VariableEnv (extend env, (cdr names), (cdr values)), (car names), (car values)
|
2015-07-02 00:38:31 +00:00
|
|
|
else if (nilp names)
|
2015-07-08 22:14:05 +00:00
|
|
|
if (nilp values) then env else throw new LispInterpreterError "Arity mismatch"
|
2015-07-02 00:38:31 +00:00
|
|
|
else
|
|
|
|
new VariableEnv env, names, values
|
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# Now we start the invocation: this is applying the function. Let's
|
|
|
|
# take it stepwise.
|
|
|
|
|
|
|
|
# Create a function environment. Calls the evaluateArguments(), which
|
|
|
|
# in turns goes down the list of arguments and creates a new
|
|
|
|
# environment, and then the continuation is to actually appy the nev
|
|
|
|
# environment to the existing function.
|
2015-07-02 00:38:31 +00:00
|
|
|
|
|
|
|
evaluateApplication = (exp, exps, env, kont) ->
|
|
|
|
evaluate exp, env, (new EvFunCont kont, exps, env)
|
|
|
|
|
|
|
|
class EvFunCont extends Continuation
|
2015-07-09 03:35:55 +00:00
|
|
|
constructor: (@kont, @exp, @env) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "EvFunCont"
|
2015-07-02 00:38:31 +00:00
|
|
|
resume: (f) ->
|
2015-07-09 03:35:55 +00:00
|
|
|
evaluateArguments @exp, @env, (new ApplyCont(@kont, f, @env))
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# Evaluate the first list, creating a new list of the arguments. Upon
|
|
|
|
# completion, resume the continuation with the gather phase
|
|
|
|
|
2015-07-02 00:38:31 +00:00
|
|
|
evaluateArguments = (exp, env, kont) ->
|
|
|
|
if (pairp exp)
|
|
|
|
evaluate (car exp), env, (new ArgumentCont kont, exp, env)
|
|
|
|
else
|
2015-07-08 02:57:35 +00:00
|
|
|
kont.resume(nil)
|
2015-07-02 00:38:31 +00:00
|
|
|
|
|
|
|
class ArgumentCont extends Continuation
|
2015-07-09 03:35:55 +00:00
|
|
|
constructor: (@kont, @exp, @env) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "ArgumentCont"
|
2015-07-02 00:38:31 +00:00
|
|
|
resume: (v) ->
|
2015-07-09 03:35:55 +00:00
|
|
|
evaluateArguments (cdr @exp), @env, (new GatherCont @kont, v)
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
# Gather the arguments as each ArgumentCont is resumed into a list to
|
|
|
|
# be passed to our next step.
|
|
|
|
|
2015-07-02 00:38:31 +00:00
|
|
|
class GatherCont extends Continuation
|
2015-07-09 03:35:55 +00:00
|
|
|
constructor: (@kont, @value) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "GatherCont"
|
2015-07-09 03:35:55 +00:00
|
|
|
resume: (value) ->
|
|
|
|
@kont.resume (cons @value, value)
|
2015-07-03 22:45:37 +00:00
|
|
|
|
|
|
|
# Upon resumption, invoke the function.
|
2015-07-08 22:14:05 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
class ApplyCont extends Continuation
|
2015-07-09 03:35:55 +00:00
|
|
|
constructor: (@kont, @fn, @env) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "ApplyCont"
|
2015-07-09 03:35:55 +00:00
|
|
|
resume: (value) ->
|
|
|
|
@fn.invoke value, @env, @kont
|
2015-07-03 22:45:37 +00:00
|
|
|
|
|
|
|
# A special continuation that represents what we want the interpreter
|
|
|
|
# to do when it's done processing.
|
2015-07-08 22:14:05 +00:00
|
|
|
|
2015-07-02 00:38:31 +00:00
|
|
|
class BottomCont extends Continuation
|
2015-07-09 03:35:55 +00:00
|
|
|
constructor: (@kont, @func) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "BottomCont"
|
2015-07-09 03:35:55 +00:00
|
|
|
resume: (value) ->
|
|
|
|
@func(value)
|
|
|
|
unwind: (value, ktarget) ->
|
|
|
|
throw new LispInterpreterError "Obsolete continuation"
|
2015-07-21 23:16:13 +00:00
|
|
|
catchLookup: (tag, kk) ->
|
|
|
|
throw new LispInterpreterError "No associated catch"
|
2015-07-09 03:35:55 +00:00
|
|
|
|
|
|
|
evaluateBlock = (label, body, env, kont) ->
|
|
|
|
k = new BlockCont(kont, label)
|
|
|
|
evaluateBegin body, (new BlockEnv env, label, kont), k
|
|
|
|
|
|
|
|
class BlockCont extends Continuation
|
|
|
|
constructor: (@kont, @label) ->
|
|
|
|
@_type = "BlockCont"
|
|
|
|
resume: (value) ->
|
|
|
|
@kont.resume value
|
|
|
|
|
|
|
|
class BlockEnv extends FullEnv
|
|
|
|
constructor: (@others, @name, @kont) ->
|
|
|
|
blockLookup: (name, kont, value) ->
|
|
|
|
if (name == @name)
|
|
|
|
kont.unwind value, @kont
|
|
|
|
else
|
|
|
|
@others.blockLookup(name, kont, value)
|
|
|
|
|
|
|
|
evaluateReturnFrom = (label, form, env, kont) ->
|
|
|
|
evaluate form, env, (new ReturnFromCont kont, env, label)
|
|
|
|
|
|
|
|
class ReturnFromCont extends Continuation
|
|
|
|
constructor: (@kont, @env, @label) ->
|
2015-07-02 00:38:31 +00:00
|
|
|
resume: (v) ->
|
2015-07-09 03:35:55 +00:00
|
|
|
@env.blockLookup @label, @kont, v
|
2015-07-08 22:14:05 +00:00
|
|
|
|
2015-07-21 23:16:13 +00:00
|
|
|
evaluateCatch = (tag, body, env, kont) ->
|
|
|
|
evaluate tag, env, (new CatchCont kont, body, env)
|
|
|
|
|
|
|
|
class CatchCont extends Continuation
|
|
|
|
constructor: (@kont, @body, @env) ->
|
|
|
|
resume: (value) ->
|
2015-07-31 14:34:12 +00:00
|
|
|
evaluateBegin @body, @env, (new LabeledCont @kont, value)
|
2015-07-21 23:16:13 +00:00
|
|
|
|
|
|
|
class LabeledCont extends Continuation
|
|
|
|
constructor: (@kont, @tag) ->
|
|
|
|
resume: (value) ->
|
|
|
|
@kont.resume value
|
|
|
|
catchLookup: (tag, kk) ->
|
2015-07-31 14:34:12 +00:00
|
|
|
if eq tag, @tag
|
2015-07-21 23:16:13 +00:00
|
|
|
evaluate kk.form, kk.env, (new ThrowingCont kk, tag, this)
|
|
|
|
else
|
|
|
|
@kont.catchLookup tag, kk
|
|
|
|
|
|
|
|
class ThrowCont extends Continuation
|
|
|
|
constructor: (@kont, @form, @env) ->
|
|
|
|
resume: (value) ->
|
2015-07-31 14:34:12 +00:00
|
|
|
@catchLookup value, @
|
2015-07-21 23:16:13 +00:00
|
|
|
|
|
|
|
evaluateThrow = (tag, form, env, kont) ->
|
|
|
|
evaluate tag, env, (new ThrowCont kont, form, env)
|
|
|
|
|
|
|
|
class ThrowingCont extends Continuation
|
|
|
|
constructor: (@kont, @tag, @resumecont) ->
|
|
|
|
resume: (value) ->
|
|
|
|
@resumecont.resume value
|
|
|
|
|
|
|
|
evaluateUnwindProtect = (form, cleanup, env, kont) ->
|
|
|
|
evaluate form, env, (new UnwindProtectCont kont, cleanup, env)
|
|
|
|
|
|
|
|
class UnwindProtectCont extends Continuation
|
|
|
|
constructor: (@kont, @cleanup, @env) ->
|
|
|
|
resume: (value) ->
|
|
|
|
evaluateBegin @cleanup, @env, (new ProtectReturnCont @kont, value)
|
|
|
|
|
2015-07-02 00:38:31 +00:00
|
|
|
class Primitive extends Value
|
2015-07-03 00:21:50 +00:00
|
|
|
constructor: (@name, @nativ) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@_type = "Primitive"
|
2015-07-03 00:21:50 +00:00
|
|
|
invoke: (args, env, kont) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
@nativ.apply null, [args, env, kont]
|
|
|
|
|
|
|
|
env_init = new NullEnv()
|
2015-07-02 00:38:31 +00:00
|
|
|
|
2015-07-01 20:55:22 +00:00
|
|
|
definitial = (name, value = nil) ->
|
2015-07-03 00:21:50 +00:00
|
|
|
env_init = new VariableEnv env_init, name, value
|
2015-07-01 20:55:22 +00:00
|
|
|
name
|
|
|
|
|
|
|
|
defprimitive = (name, nativ, arity) ->
|
2015-07-03 00:21:50 +00:00
|
|
|
definitial name, new Primitive name, (args, env, kont) ->
|
2015-07-01 20:55:22 +00:00
|
|
|
vmargs = listToVector(args)
|
|
|
|
if (vmargs.length == arity)
|
2015-07-03 22:45:37 +00:00
|
|
|
kont.resume (nativ.apply null, vmargs)
|
2015-08-03 14:31:22 +00:00
|
|
|
else
|
|
|
|
throw new LispInterpreterError "Incorrect arity"
|
2015-07-01 20:55:22 +00:00
|
|
|
|
2015-07-03 19:58:08 +00:00
|
|
|
defpredicate = (name, nativ, arity) ->
|
|
|
|
defprimitive name, ((a, b) -> if nativ.call(null, a, b) then true else the_false_value), arity
|
|
|
|
|
2015-07-01 20:55:22 +00:00
|
|
|
definitial "#t", true
|
|
|
|
definitial "#f", the_false_value
|
|
|
|
definitial "nil", nil
|
2015-07-31 14:34:12 +00:00
|
|
|
|
|
|
|
# FIXME: All of these things dereference to the same value!!!!
|
|
|
|
|
2015-07-03 00:21:50 +00:00
|
|
|
for i in [
|
|
|
|
"x", "y", "z", "a", "b", "c", "foo", "bar", "hux",
|
|
|
|
"fib", "fact", "visit", "primes", "length"]
|
|
|
|
definitial i
|
|
|
|
|
2015-07-01 20:55:22 +00:00
|
|
|
defprimitive "cons", cons, 2
|
|
|
|
defprimitive "car", car, 2
|
2015-07-03 22:45:37 +00:00
|
|
|
defprimitive "cdr", cdr, 2
|
2015-07-01 20:55:22 +00:00
|
|
|
defprimitive "set-cdr!", setcdr, 2
|
2015-07-03 22:45:37 +00:00
|
|
|
defprimitive "set-car!", setcar, 2
|
2015-07-01 20:55:22 +00:00
|
|
|
defprimitive "+", ((a, b) -> a + b), 2
|
|
|
|
defprimitive "*", ((a, b) -> a * b), 2
|
|
|
|
defprimitive "-", ((a, b) -> a - b), 2
|
|
|
|
defprimitive "/", ((a, b) -> a / b), 2
|
|
|
|
defpredicate "lt", ((a, b) -> a < b), 2
|
2015-07-03 22:45:37 +00:00
|
|
|
defpredicate "gt", ((a, b) -> a > b), 2
|
|
|
|
defpredicate "lte", ((a, b) -> a <= b), 2
|
|
|
|
defpredicate "gte", ((a, b) -> a >= b), 2
|
2015-07-01 20:55:22 +00:00
|
|
|
defpredicate "eq?", ((a, b) -> a == b), 2
|
2015-07-08 02:57:35 +00:00
|
|
|
defpredicate "pair?", ((a) -> pairp a), 1
|
|
|
|
defpredicate "nil?", ((a) -> nilp a), 1
|
|
|
|
defpredicate "symbol?", ((a) -> /\-?[0-9]+$/.test(a) == false), 1
|
2015-07-01 20:55:22 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
definitial "call/cc", new Primitive "call/cc", (values, env, kont) ->
|
|
|
|
if nilp cdr values
|
|
|
|
(car values).invoke (cons kont), env, kont
|
2015-07-01 20:55:22 +00:00
|
|
|
else
|
2015-07-08 22:14:05 +00:00
|
|
|
throw new LispInterpreterError "Incorrect arity for call/cc"
|
2015-07-01 20:55:22 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
definitial "apply", new Primitive "apply", (values, env, kont) ->
|
|
|
|
if pairp cdr values
|
|
|
|
f = car values
|
|
|
|
args = (() ->
|
|
|
|
(flat = (args) ->
|
2015-07-08 02:57:35 +00:00
|
|
|
if nilp (cdr args) then (car args) else (cons (car args), (flat cdr args)))(cdr values))()
|
2015-07-03 22:45:37 +00:00
|
|
|
f.invoke args, env, kont
|
2015-07-01 20:55:22 +00:00
|
|
|
|
2015-07-21 23:16:13 +00:00
|
|
|
definitial "funcall", new Primitive "funcall", (args, env, kont) ->
|
2015-08-03 14:31:22 +00:00
|
|
|
if not nilp cdr args
|
|
|
|
@kont.invoke (car args), (cdr args)
|
|
|
|
else
|
|
|
|
throw new LispInterpreterError "Invoke requires a function name and arguments"
|
2015-07-21 23:16:13 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
definitial "list", new Primitive "list", (values, env, kont) ->
|
|
|
|
(values, env, kont) -> kont.resume(values)
|
2015-07-01 20:55:22 +00:00
|
|
|
|
2015-07-28 04:53:14 +00:00
|
|
|
# Only called in rich node mode...
|
|
|
|
|
|
|
|
astSymbolsToLispSymbols = (node) ->
|
|
|
|
return nil if nilp node
|
2015-07-28 23:51:01 +00:00
|
|
|
throw (new LispInterpreterError "Not a list of variable names") if not node.type == 'list'
|
|
|
|
handler = (cell) ->
|
|
|
|
return nil if nilp cell
|
|
|
|
cons (car cell).value, (handler cdr cell)
|
|
|
|
handler node.value
|
2015-07-28 04:53:14 +00:00
|
|
|
|
|
|
|
metadata_evaluation =
|
2015-07-28 23:51:01 +00:00
|
|
|
listp: (node) -> node.type == 'list'
|
|
|
|
symbolp: (node) -> node.type == 'symbol'
|
|
|
|
numberp: (node) -> node.type == 'number'
|
|
|
|
stringp: (node) -> node.type == 'string'
|
|
|
|
commentp: (node) -> node.type == 'comment'
|
|
|
|
nvalu: (node) -> node.value
|
|
|
|
mksymbols: (list) -> astSymbolsToLispSymbols(list)
|
2015-07-28 04:53:14 +00:00
|
|
|
|
2015-08-03 14:31:22 +00:00
|
|
|
straight_evaluation =
|
2015-07-28 23:51:01 +00:00
|
|
|
listp: (cell) -> cell.__type == 'list'
|
|
|
|
symbolp: (cell) -> typeof cell == 'string' and cell.length > 0 and cell[0] not in ["\"", ";"]
|
|
|
|
commentp: (cell) -> typeof cell == 'string' and cell.length > 0 and cell[0] == ";"
|
|
|
|
numberp: (cell) -> typeof cell == 'number'
|
|
|
|
stringp: (cell) -> typeof cell == 'string' and cell.length > 0 and cell[0] == "\""
|
|
|
|
boolp: (cell) -> typeof cell == 'boolean'
|
|
|
|
nullp: (cell) -> cell == null
|
|
|
|
vectorp: (cell) -> (not straight_evaluation.listp cell) and toString.call(cell) == '[object Array]'
|
|
|
|
recordp: (cell) -> (not cell._prototype?) and toSTring.call(cell) == '[object Object]'
|
|
|
|
objectp: (cell) -> (cell._prototype?) and toString.call(cell) == '[object Object]'
|
|
|
|
nilp: (cell) -> nilp(cell)
|
|
|
|
nvalu: (cell) -> cell
|
|
|
|
mksymbols: (cell) -> cell
|
2015-07-28 04:53:14 +00:00
|
|
|
|
|
|
|
makeEvaluator = (ix = straight_evaluation, ty="straight") ->
|
|
|
|
(exp, env, kont) ->
|
|
|
|
if ix.symbolp exp
|
2015-07-28 23:51:01 +00:00
|
|
|
return evaluateVariable (ix.nvalu exp), env, kont
|
2015-07-28 04:53:14 +00:00
|
|
|
else if ([ix.numberp, ix.stringp].filter (i) -> i(exp)).length > 0
|
|
|
|
return kont.resume ix.nvalu exp
|
|
|
|
else if ix.listp exp
|
|
|
|
body = ix.nvalu exp
|
|
|
|
head = car body
|
|
|
|
if ix.symbolp head
|
2015-07-28 23:51:01 +00:00
|
|
|
switch (ix.nvalu head)
|
2015-07-31 14:34:12 +00:00
|
|
|
when "quote" then evaluateQuote (cadr body), env, kont
|
2015-07-28 04:53:14 +00:00
|
|
|
when "if" then evaluateIf (cdr body), env, kont
|
|
|
|
when "begin" then evaluateBegin (cdr body), env, kont
|
2015-07-28 23:51:01 +00:00
|
|
|
when "set!" then evaluateSet (ix.nvalu cadr body), (caddr body), env, kont
|
2015-07-28 04:53:14 +00:00
|
|
|
when "lambda" then evaluateLambda (ix.mksymbols cadr body), (cddr body), env, kont
|
|
|
|
when "block" then evaluateBlock (ix.nvalu cadr body), (cddr body), env, kont
|
|
|
|
when "return-from" then evaluateReturnFrom (ix.nvalu cadr body), (caddr body), env, kont
|
|
|
|
when "catch" then evaluateCatch (cadr body), (cddr body), env, kont
|
|
|
|
when "throw" then evaluateThrow (cadr body), (caddr body), env, kont
|
|
|
|
when "unwind-protect" then evaluateUnwindProtect (cadr body), (cddr body), env, kont
|
|
|
|
else evaluateApplication (car body), (cdr body), env, kont
|
|
|
|
else
|
|
|
|
evaluateApplication (car body), (cdr body), env, kont
|
|
|
|
else
|
|
|
|
throw new LispInterpreterError("Can't handle a '#{type}'")
|
|
|
|
|
|
|
|
nodeEval = makeEvaluator(metadata_evaluation, "node")
|
|
|
|
lispEval = makeEvaluator(straight_evaluation, "lisp")
|
|
|
|
|
|
|
|
evaluate = (exp, env, kont) ->
|
2015-07-28 23:51:01 +00:00
|
|
|
(if exp? and (exp instanceof Node) then nodeEval else lispEval)(exp, env, kont)
|
2015-07-28 04:53:14 +00:00
|
|
|
|
2015-07-03 22:45:37 +00:00
|
|
|
interpreter = (ast, kont) ->
|
|
|
|
evaluate ast, env_init, new BottomCont null, kont
|
2015-07-08 22:14:05 +00:00
|
|
|
|
2015-07-08 02:57:35 +00:00
|
|
|
module.exports = interpreter
|