4 ms·
With concepts like these it can sometimes be difficult to form an appropriate mental model of how the machine is actually working (aka the implementations of th
by lukesandberg 16y ago
With concepts like these it can sometimes be difficult to form an appropriate mental model of how the machine is actually working (aka the implementations of the algorithm).
So if you were to implement a scheme interpreter heres how i would represent these things.
scope: map from symbols to objects.
environment: linked list of scopes, where each scope is linked to its parent scope.
(so to look up a variable value you just walk up the scope chain looking for where the symbol is defined).
closure: pair of environment and lambda. where the environment is the one in which the lambda was defined.
(so to execute a closure you just link the lambda scope to the given environment and then run the lambda)
continuation: just a closure. continuations would most likely be implemented by rewriting the code after the method call as a lambda and passing the closure as the continuation.
if you really want to see how the bits fit together you can read this blog series: http://michaux.ca/index#Scheme%20from%20Scratch http://michaux.ca/index#Scheme%20from%20Scratch and check out the corresponding github implementations:
https://github.com/petermichaux/bootstrap-scheme https://github.com/petermichaux/bootstrap-scheme
https://github.com/lukesandberg/bootstrap-scheme https://github.com/lukesandberg/bootstrap-scheme (shameless plug for my implementation)