Write chapter 5 review - FIN :)

main
Felix Martin 2021-06-14 12:03:18 -04:00
parent 24c2df3dc6
commit 125fb89df6
2 changed files with 42 additions and 2 deletions

View File

@ -1,7 +1,5 @@
# SICP
**This is currently (2021/06/11) work in progress.**
These are my solutions to the CS classic [Structure and Interpretation of
Computer Programs](https://mitpress.mit.edu/sites/default/files/sicp/index.html).
I have looked up the answer for some exercises on the
@ -243,6 +241,48 @@ programming concepts to Scheme.
# Chapter 5
Chapter 5 combines the previous chapter's concepts, ultimately implementing a
Scheme interpreter running on physical hardware.
The first section introduces the concept of register machines. Register machines
are an abstraction of physical machines that control the flow of data and can
execute computations. For example, the following diagram shows a machine that
computes factorials.
![Factorial Machine Diagram](./shared/draw-factorial-reg-machine.png)
Based on the diagrams, we learn about an assembly-like language that can
represent register-machines. For example, the following listing shows the same
machine as on the previous graph in assembly form.
```scheme
'(controller
(assign product (const 1))
(assign counter (const 1))
test-counter
(test (op >) (reg counter) (reg n))
(branch (label factorial-done))
(assign product (op *) (reg product) (reg counter))
(assign counter (op +) (reg counter) (const 1))
(goto (label test-counter))
```
In the following sections, we implement a simulator that can execute
register-machine programs and use it to run a Scheme interpreter written in the
register-machine language. We call this the Explicit-Control-Evaluator.
Finally, we implement a Scheme compiler that compiles into the register-machine
assembly language. Ultimately, we use the compiler to translate the metacircular
evaluator from chapter 4. We then use the register machine simulator to run the
evaluator. Making that work was indeed a fantastic moment. I cannot explain it
well, but seeing how everything comes together gave me a great sense of
accomplishment.
The last two exercises in the books are programming projects in their own right.
First, I implement a crude [Scheme interpreter in
Rust](https://git.felixm.de/felixm/schemers), and then I modified the compiler
to generate C code. I have implemented the required runtime support, and it
works, at least for simple programs like computing factorials.
# Resources

Binary file not shown.

After

Width:  |  Height:  |  Size: 259 KiB