|
1 | | - <SECTION WIP="no"> |
2 | | - <NAME>The Explicit-Control Evaluator</NAME> |
| 1 | +<SECTION> |
| 2 | + <NAME>The Explicit-Control Evaluator</NAME> |
3 | 3 |
|
4 | | - <SECTIONCONTENT/> |
| 4 | + <SECTIONCONTENT/> |
5 | 5 |
|
6 | | - <LABEL NAME="sec:eceval"/> |
7 | | - <INDEX>explicit-control evaluator for Scheme|(</INDEX> |
8 | | - <TEXT> |
9 | | - In section<SPACE/><REF NAME="sec:designing-register-machines"/> we saw how to |
10 | | - transform simple <SPLITINLINE><SCHEME>Scheme</SCHEME><JAVASCRIPT>Source</JAVASCRIPT></SPLITINLINE> programs into descriptions of register |
11 | | - machines. We will now perform this transformation on a more complex |
12 | | - program, the metacircular evaluator of |
13 | | - sections <REF NAME="sec:core-of-evaluator"/><ENDASH/><REF NAME="sec:running-eval"/>, |
14 | | - which shows how |
15 | | - the behavior of a <SPLITINLINE><SCHEME>Scheme</SCHEME><JAVASCRIPT>Source</JAVASCRIPT></SPLITINLINE> interpreter can be described in terms of the |
16 | | - <SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE> |
17 | | - <SCHEMEINLINE>eval</SCHEMEINLINE> and <SCHEMEINLINE>apply</SCHEMEINLINE>. |
18 | | - The <EM>explicit-control |
19 | | - evaluator</EM> that we develop in this section shows how the underlying |
20 | | - <SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE>-calling and argument-passing mechanisms used in the |
21 | | - evaluation process can be described in terms of operations on |
22 | | - registers and stacks. In addition, the explicit-control evaluator can |
23 | | - serve as an implementation of a <SPLITINLINE><SCHEME>Scheme</SCHEME><JAVASCRIPT>Source</JAVASCRIPT></SPLITINLINE> interpreter, written in a |
24 | | - language that is very similar to the native machine language of |
25 | | - conventional computers. The evaluator can be executed by the |
26 | | - register-machine simulator of section<SPACE/><REF NAME="sec:simulator"/>. |
27 | | - Alternatively, it can be used as a starting point for building a |
28 | | - machine-language implementation of a <SPLITINLINE><SCHEME>Scheme</SCHEME><JAVASCRIPT>Source</JAVASCRIPT></SPLITINLINE> evaluator, or even a |
29 | | - <INDEX>Scheme chip</INDEX> |
30 | | - <INDEX>integrated-circuit implementation of Scheme</INDEX> |
31 | | - <INDEX>chip implementation of Scheme</INDEX> |
32 | | - special-purpose machine for evaluating <SPLITINLINE><SCHEME>Scheme</SCHEME><JAVASCRIPT>Source</JAVASCRIPT></SPLITINLINE> expressions. |
33 | | - Figure<SPACE/><REF NAME="fig:Scheme-chip"/> shows such a hardware implementation: a |
34 | | - silicon chip that acts as an evaluator for <SPLITINLINE><SCHEME>Scheme</SCHEME><JAVASCRIPT>Scheme, the language for which this book was originally written.</JAVASCRIPT></SPLITINLINE>. The chip designers |
35 | | - started with the data-path and controller specifications for a |
36 | | - register machine similar to the evaluator described in this section |
37 | | - and used design automation programs to construct the |
38 | | - integrated-circuit layout.<FOOTNOTE>See |
39 | | - <CITATION>Batali et al.<SPACE/>1982</CITATION> for more |
40 | | - <INDEX>Batali, John Dean</INDEX> |
41 | | - information on the chip and the method by which it was designed.</FOOTNOTE> |
42 | | - </TEXT> |
43 | | - |
44 | | - <SUBHEADING> |
45 | | - <NAME>Registers and operations</NAME> |
46 | | - </SUBHEADING> |
| 6 | + <LABEL NAME="sec:eceval"/> |
| 7 | + <INDEX>explicit-control evaluator for Scheme|(</INDEX> |
| 8 | + <TEXT> |
| 9 | + In section<SPACE/><REF NAME="sec:designing-register-machines"/> we saw how to |
| 10 | + transform simple |
| 11 | + <SPLITINLINE> |
| 12 | + <SCHEME>Scheme</SCHEME> |
| 13 | + <JAVASCRIPT>JavaScript</JAVASCRIPT> |
| 14 | + </SPLITINLINE> |
| 15 | + programs into descriptions of register |
| 16 | + machines. We will now perform this transformation on a more complex |
| 17 | + program, the metacircular evaluator of |
| 18 | + sections<SPACE/><REF NAME="sec:core-of-evaluator"/><ENDASH/><REF NAME="sec:running-eval"/>, |
| 19 | + which shows how the behavior of a |
| 20 | + <SPLITINLINE> |
| 21 | + <SCHEME>Scheme</SCHEME> |
| 22 | + <JAVASCRIPT>JavaScript</JAVASCRIPT> |
| 23 | + </SPLITINLINE> |
| 24 | + interpreter can be described in terms of the |
| 25 | + <SPLITINLINE> |
| 26 | + <SCHEME> |
| 27 | + procedures |
| 28 | + <SCHEMEINLINE>eval</SCHEMEINLINE> |
| 29 | + </SCHEME> |
| 30 | + <JAVASCRIPT> |
| 31 | + functions |
| 32 | + <SCHEMEINLINE>evaluate</SCHEMEINLINE> |
| 33 | + </JAVASCRIPT> |
| 34 | + </SPLITINLINE> |
| 35 | + and <SCHEMEINLINE>apply</SCHEMEINLINE>. |
| 36 | + The <EM>explicit-control |
| 37 | + evaluator</EM> that we develop in this section shows how the underlying |
| 38 | + <SPLITINLINE> |
| 39 | + <SCHEME>procedure-calling</SCHEME> |
| 40 | + <JAVASCRIPT>function-calling</JAVASCRIPT> |
| 41 | + </SPLITINLINE> |
| 42 | + and argument-passing mechanisms used in the |
| 43 | + evaluation process can be described in terms of operations on |
| 44 | + registers and stacks. In addition, the explicit-control evaluator can |
| 45 | + serve as an implementation of a |
| 46 | + <SPLITINLINE> |
| 47 | + <SCHEME>Scheme</SCHEME> |
| 48 | + <JAVASCRIPT>JavaScript</JAVASCRIPT> |
| 49 | + </SPLITINLINE> |
| 50 | + interpreter, written in a language that is very similar to the native machine |
| 51 | + language of conventional computers. The evaluator can be executed by the |
| 52 | + register-machine simulator of section<SPACE/><REF NAME="sec:simulator"/>. |
| 53 | + Alternatively, it can be used as a starting point for building a |
| 54 | + machine-language implementation of a |
| 55 | + <SPLITINLINE> |
| 56 | + <SCHEME>Scheme</SCHEME> |
| 57 | + <JAVASCRIPT>JavaScript</JAVASCRIPT> |
| 58 | + </SPLITINLINE> |
| 59 | + evaluator, or even a |
| 60 | + <INDEX>Scheme chip</INDEX> |
| 61 | + <INDEX>integrated-circuit implementation of Scheme</INDEX> |
| 62 | + <INDEX>chip implementation of Scheme</INDEX> |
| 63 | + special-purpose machine for evaluating |
| 64 | + <SPLITINLINE> |
| 65 | + <SCHEME>Scheme</SCHEME> |
| 66 | + <JAVASCRIPT>JavaScript</JAVASCRIPT> |
| 67 | + </SPLITINLINE> |
| 68 | + expressions. |
| 69 | + Figure<SPACE/><REF NAME="fig:Scheme-chip"/> shows such a hardware |
| 70 | + implementation: a silicon chip that acts as an evaluator for |
| 71 | + <SPLITINLINE> |
| 72 | + <SCHEME>Scheme.</SCHEME> |
| 73 | + <JAVASCRIPT>Scheme, the language for which this book was originally |
| 74 | + written.</JAVASCRIPT> |
| 75 | + </SPLITINLINE> |
| 76 | + The chip designers started with the data-path and controller specifications |
| 77 | + for a register machine similar to the evaluator described in this section |
| 78 | + and used design automation programs to construct the |
| 79 | + integrated-circuit layout.<FOOTNOTE>See |
| 80 | + <CITATION>Batali et al.<SPACE/>1982</CITATION> for more |
| 81 | + <INDEX>Batali, John Dean</INDEX> |
| 82 | + information on the chip and the method by which it was designed.</FOOTNOTE> |
| 83 | + </TEXT> |
| 84 | + |
| 85 | + <SUBHEADING> |
| 86 | + <NAME>Registers and operations</NAME> |
| 87 | + </SUBHEADING> |
47 | 88 |
|
48 | | - <INDEX>explicit-control evaluator for Scheme<SUBINDEX>data paths|(</SUBINDEX></INDEX> |
49 | | - <INDEX>explicit-control evaluator for Scheme<SUBINDEX>operations</SUBINDEX></INDEX> |
50 | | - <TEXT> |
51 | | - In designing the explicit-control evaluator, we must specify the |
52 | | - operations to be used in our register machine. We described the |
53 | | - metacircular evaluator in terms of abstract syntax, using |
54 | | - <SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE> |
55 | | - such as <SPLITINLINE><SCHEME><SCHEMEINLINE>quoted?</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>is_self_evaluating</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> and <SPLITINLINE><SCHEME><SCHEMEINLINE>make-procedure</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>make_compound_function</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE>. In implementing the |
56 | | - register machine, we could expand these |
57 | | - <SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE> |
58 | | - into sequences of |
59 | | - elementary list-structure memory operations, and implement these |
60 | | - operations on our register machine. However, this would make our |
61 | | - evaluator very long, obscuring the basic structure with |
62 | | - details. To clarify the presentation, we will include as primitive |
63 | | - operations of the register machine the syntax |
64 | | - <SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE> |
65 | | - given in |
66 | | - section<SPACE/><REF NAME="sec:representing-expressions"/> and the |
67 | | - <SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE> |
68 | | - for |
69 | | - representing environments and other run-time data given in |
70 | | - sections<SPACE/><REF NAME="sec:eval-data-structures"/> and<SPACE/><REF NAME="sec:running-eval"/>. |
71 | | - In order to completely specify an evaluator that could be programmed |
72 | | - in a low-level machine language or implemented in hardware, we would |
73 | | - replace these operations by more elementary operations, using the |
74 | | - list-structure implementation we described in |
75 | | - section<SPACE/><REF NAME="sec:storage-allocation"/>. |
76 | | - </TEXT> |
| 89 | + <INDEX>explicit-control evaluator for Scheme |
| 90 | + <SUBINDEX>data paths|(</SUBINDEX></INDEX> |
| 91 | + <INDEX>explicit-control evaluator for Scheme |
| 92 | + <SUBINDEX>operations</SUBINDEX></INDEX> |
| 93 | + <TEXT> |
| 94 | + In designing the explicit-control evaluator, we must specify the |
| 95 | + operations to be used in our register machine. We described the |
| 96 | + metacircular evaluator in terms of abstract syntax, using |
| 97 | + <SPLITINLINE> |
| 98 | + <SCHEME>procedures</SCHEME> |
| 99 | + <JAVASCRIPT>functions</JAVASCRIPT> |
| 100 | + </SPLITINLINE> |
| 101 | + such as |
| 102 | + <SPLITINLINE> |
| 103 | + <SCHEME><SCHEMEINLINE>quoted?</SCHEMEINLINE></SCHEME> |
| 104 | + <JAVASCRIPT><JAVASCRIPTINLINE>is_self_evaluating</JAVASCRIPTINLINE> |
| 105 | + </JAVASCRIPT> |
| 106 | + </SPLITINLINE> |
| 107 | + and |
| 108 | + <SPLITINLINE><SCHEME><SCHEMEINLINE>make-procedure</SCHEMEINLINE>.</SCHEME> |
| 109 | + <JAVASCRIPT> |
| 110 | + <JAVASCRIPTINLINE>make_function</JAVASCRIPTINLINE>. |
| 111 | + </JAVASCRIPT> |
| 112 | + </SPLITINLINE> |
| 113 | + In implementing the |
| 114 | + register machine, we could expand these |
| 115 | + <SPLITINLINE> |
| 116 | + <SCHEME>procedures</SCHEME> |
| 117 | + <JAVASCRIPT>functions</JAVASCRIPT> |
| 118 | + </SPLITINLINE> |
| 119 | + into sequences of |
| 120 | + elementary list-structure memory operations, and implement these |
| 121 | + operations on our register machine. However, this would make our |
| 122 | + evaluator very long, obscuring the basic structure with |
| 123 | + details. To clarify the presentation, we will include as primitive |
| 124 | + operations of the register machine the syntax |
| 125 | + <SPLITINLINE> |
| 126 | + <SCHEME>procedures</SCHEME> |
| 127 | + <JAVASCRIPT>functions</JAVASCRIPT> |
| 128 | + </SPLITINLINE> |
| 129 | + given in |
| 130 | + section<SPACE/><REF NAME="sec:representing-expressions"/> and the |
| 131 | + <SPLITINLINE> |
| 132 | + <SCHEME>procedures</SCHEME> |
| 133 | + <JAVASCRIPT>functions</JAVASCRIPT> |
| 134 | + </SPLITINLINE> |
| 135 | + for representing environments and other run-time data given in |
| 136 | + sections<SPACE/><REF NAME="sec:eval-data-structures"/> |
| 137 | + and<SPACE/><REF NAME="sec:running-eval"/>. |
| 138 | + In order to completely specify an evaluator that could be programmed |
| 139 | + in a low-level machine language or implemented in hardware, we would |
| 140 | + replace these operations by more elementary operations, using the |
| 141 | + list-structure implementation we described in |
| 142 | + section<SPACE/><REF NAME="sec:storage-allocation"/>. |
| 143 | + </TEXT> |
77 | 144 |
|
78 | | - <FIGURE> |
| 145 | + <FIGURE> |
79 | 146 | <FIGURE src="img_original/chip.jpg"/> <!-- FIXME: JS-ify imaga? --> |
80 | | - <INDEX>Scheme chip</INDEX> |
81 | | - <INDEX>integrated-circuit implementation of Scheme</INDEX> |
82 | | - <INDEX>chip implementation of Scheme</INDEX> |
83 | | - <CAPTION>A silicon-chip implementation of an evaluator for Scheme.</CAPTION> |
84 | | - <LABEL NAME="fig:Scheme-chip"/> |
85 | | - </FIGURE> |
| 147 | + <INDEX>Scheme chip</INDEX> |
| 148 | + <INDEX>integrated-circuit implementation of Scheme</INDEX> |
| 149 | + <INDEX>chip implementation of Scheme</INDEX> |
| 150 | + <CAPTION>A silicon-chip implementation of an evaluator for Scheme.</CAPTION> |
| 151 | + <LABEL NAME="fig:Scheme-chip"/> |
| 152 | + </FIGURE> |
86 | 153 |
|
87 | | - <TEXT> |
88 | | - <INDEX>explicit-control evaluator for Scheme<SUBINDEX>registers</SUBINDEX></INDEX> |
89 | | - <!-- \indcodeplus{exp}{register}--> |
90 | | - <!-- \indcodeplus{env}{register}--> |
91 | | - <!-- \indcodeplus{val}{register}--> |
92 | | - <!-- \indcodeplus{continue}{register}[explicit@in explicit-control evaluator]--> |
93 | | - <!-- \indcodeplus{proc}{register}--> |
94 | | - <!-- \indcodeplus{argl}{register}--> |
95 | | - <!-- \indcodeplus{unev}{register}--> |
96 | | - Our <SPLITINLINE><SCHEME>Scheme</SCHEME><JAVASCRIPT>Source</JAVASCRIPT></SPLITINLINE> evaluator register machine includes a stack and seven |
97 | | - registers: <SCHEMEINLINE>exp</SCHEMEINLINE>, <SCHEMEINLINE>env</SCHEMEINLINE>, <SCHEMEINLINE>val</SCHEMEINLINE>, <SCHEMEINLINE>continue</SCHEMEINLINE>, <SCHEMEINLINE>proc</SCHEMEINLINE>, |
98 | | - <SCHEMEINLINE>argl</SCHEMEINLINE>, and <SCHEMEINLINE>unev</SCHEMEINLINE>. <SCHEMEINLINE>Exp</SCHEMEINLINE> is used to hold the expression |
99 | | - to be evaluated, and <SCHEMEINLINE>env</SCHEMEINLINE> contains the environment in which the |
100 | | - evaluation is to be performed. At the end of an evaluation, <SCHEMEINLINE>val</SCHEMEINLINE> |
101 | | - contains the value obtained by evaluating the expression in the |
102 | | - designated environment. The <SCHEMEINLINE>continue</SCHEMEINLINE> register is used to |
103 | | - implement recursion, as explained in |
104 | | - section<SPACE/><REF NAME="sec:stack-recursion"/>. (The evaluator needs to call |
105 | | - itself recursively, since evaluating an expression requires evaluating |
106 | | - its subexpressions.) The registers <SCHEMEINLINE>proc</SCHEMEINLINE>, <SCHEMEINLINE>argl</SCHEMEINLINE>, and <SCHEMEINLINE>unev</SCHEMEINLINE> are used in evaluating combinations. |
107 | | - </TEXT> |
| 154 | + <TEXT> |
| 155 | + <INDEX>explicit-control evaluator for Scheme |
| 156 | + <SUBINDEX>registers</SUBINDEX></INDEX> |
| 157 | + <!-- \indcodeplus{exp}{register}--> |
| 158 | + <!-- \indcodeplus{env}{register}--> |
| 159 | + <!-- \indcodeplus{val}{register}--> |
| 160 | + <!-- \indcodeplus{continue}{register}[explicit@in explicit-control evaluator] |
| 161 | + --> |
| 162 | + <!-- \indcodeplus{proc}{register}--> |
| 163 | + <!-- \indcodeplus{argl}{register}--> |
| 164 | + <!-- \indcodeplus{unev}{register}--> |
| 165 | + Our |
| 166 | + <SPLITINLINE> |
| 167 | + <SCHEME>Scheme</SCHEME> |
| 168 | + <JAVASCRIPT>JavaScript</JAVASCRIPT> |
| 169 | + </SPLITINLINE> |
| 170 | + evaluator register machine includes a stack and seven |
| 171 | + registers: <SCHEMEINLINE>exp</SCHEMEINLINE>, |
| 172 | + <SCHEMEINLINE>env</SCHEMEINLINE>, |
| 173 | + <SCHEMEINLINE>val</SCHEMEINLINE>, <SCHEMEINLINE>continue</SCHEMEINLINE>, |
| 174 | + <SPLITINLINE> |
| 175 | + <SCHEME> |
| 176 | + <SCHEMEINLINE>proc</SCHEMEINLINE>, |
| 177 | + </SCHEME> |
| 178 | + <JAVASCRIPT> |
| 179 | + <SCHEMEINLINE>fun</SCHEMEINLINE>, |
| 180 | + </JAVASCRIPT> |
| 181 | + </SPLITINLINE> |
| 182 | + <SCHEMEINLINE>argl</SCHEMEINLINE>, and <SCHEMEINLINE>unev</SCHEMEINLINE>. |
| 183 | + <SCHEMEINLINE>Exp</SCHEMEINLINE> is used to hold the expression |
| 184 | + to be evaluated, and <SCHEMEINLINE>env</SCHEMEINLINE> contains the |
| 185 | + environment |
| 186 | + in which the evaluation is to be performed. At the end of an evaluation, |
| 187 | + <SCHEMEINLINE>val</SCHEMEINLINE> |
| 188 | + contains the value obtained by evaluating the expression in the |
| 189 | + designated environment. The <SCHEMEINLINE>continue</SCHEMEINLINE> register |
| 190 | + is used to implement recursion, as explained in |
| 191 | + section<SPACE/><REF NAME="sec:stack-recursion"/>. (The evaluator needs to |
| 192 | + call itself recursively, since evaluating an expression requires evaluating |
| 193 | + its subexpressions.) The registers |
| 194 | + <SPLITINLINE> |
| 195 | + <SCHEME> |
| 196 | + <SCHEMEINLINE>proc</SCHEMEINLINE>, |
| 197 | + </SCHEME> |
| 198 | + <JAVASCRIPT> |
| 199 | + <SCHEMEINLINE>fun</SCHEMEINLINE>, |
| 200 | + </JAVASCRIPT> |
| 201 | + </SPLITINLINE> |
| 202 | + <SCHEMEINLINE>argl</SCHEMEINLINE>, and <SCHEMEINLINE>unev</SCHEMEINLINE> are |
| 203 | + used in evaluating combinations. |
| 204 | + </TEXT> |
108 | 205 |
|
109 | | - <TEXT> |
110 | | - We will not provide a data-path diagram to show how the registers and |
111 | | - operations of the evaluator are connected, nor will we give the |
112 | | - complete list of machine operations. These are implicit in the |
113 | | - evaluator<APOS/>s controller, which will be presented in detail. |
114 | | - <INDEX>explicit-control evaluator for Scheme<SUBINDEX>data paths|)</SUBINDEX></INDEX> |
115 | | - </TEXT> |
| 206 | + <TEXT> |
| 207 | + We will not provide a data-path diagram to show how the registers and |
| 208 | + operations of the evaluator are connected, nor will we give the |
| 209 | + complete list of machine operations. These are implicit in the |
| 210 | + evaluator<APOS/>s controller, which will be presented in detail. |
| 211 | + <INDEX>explicit-control evaluator for Scheme |
| 212 | + <SUBINDEX>data paths|)</SUBINDEX></INDEX> |
| 213 | + </TEXT> |
116 | 214 |
|
117 | | - <!-- Subsection 1 : The Core of the Explicit-Control Evaluator --> |
118 | | - &subsection5.4.1; |
119 | | - |
120 | | - <!-- Subsection 2 : Sequence Evaluation and Tail Recursion --> |
121 | | - &subsection5.4.2; |
| 215 | + <!-- Subsection 1 : The Core of the Explicit-Control Evaluator --> |
| 216 | + &subsection5.4.1; |
| 217 | + |
| 218 | + <!-- Subsection 2 : Sequence Evaluation and Tail Recursion --> |
| 219 | + &subsection5.4.2; |
122 | 220 |
|
123 | | - <!-- Subsection 3 : Conditionals, Assignments, and Definitions --> |
124 | | - &subsection5.4.3; |
| 221 | + <!-- Subsection 3 : Conditionals, Assignments, and Definitions --> |
| 222 | + &subsection5.4.3; |
125 | 223 |
|
126 | | - <!-- Subsection 4 : Running the Evaluator --> |
127 | | - &subsection5.4.4; |
| 224 | + <!-- Subsection 4 : Running the Evaluator --> |
| 225 | + &subsection5.4.4; |
128 | 226 |
|
129 | | - </SECTION> |
| 227 | +</SECTION> |
0 commit comments