Scheme from Scratch - Bootstrap v0.19 - Eval

The eval form is a bit like apply. They are both procedures with an unusual tail call requirements.

The second argument to eval is an environment, so exposing eval also requires exposing some environment-related procedures. All together I implemented the following procedures for this version:

interaction-environment
Returns the top-level environment of the current REPL session.
null-environment
Returns an empty environment with only syntactic keywords like if, define, etc.
environment
Returns a new environment like the environment present when the REPL starts.
eval
Evaluates an expression in a given environment.

Sample REPL session:

$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
> (define env (environment))
ok
> (eval '(define z 25) env)
ok
> (eval 'z env)
25

I used the same trick for eval as I did for apply yesterday. There is a dummy primitive procedure but the exposed Scheme eval procedure lives inside the C eval function.

The null-environment and environment procedures required a bit of refactoring in the model initialization. At some point I may backport those changes to previous versions with environments as it is just good programming anyway.

It feels like we are on the home stretch. There isn't much remaining to complete the bootstrap interpreter and none of it is very difficult.

There is a v0.19 branch on github for this version.

Previous article: Apply
Next article: I/O

Comments

Have something to write? Comment on this article.