about summary refs log tree commit diff stats
path: root/js/games/nluqo.github.io/~bh/61a-pages/Lectures/3.2/envrules
blob: ce2b12424edb6c6abd1ab8b3183ce854e940f749 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
CS 61A                Environment Model of Evaluation

Every expression is either atomic or compound.

At any time there is a CURRENT FRAME, initially the global frame.

1.  atomic expressions

    (a)  Numbers, strings, #T, and #F are self-evaluating.

    (b)  If the expression is a symbol, find the FIRST AVAILABLE binding.
         (That is, look in the current frame; if not found there, look in
         the frame "behind" the current frame; and so on until the global
         frame is reached.)

2.  Compound expressions

If the car of the expression is a symbol that names a special form, then
follow its rules (2(b) below).  Otherwise the expression is a procedure
invocation.

    (a)  Procedure invocation

        Step 1:  Evaluate all the subexpressions (using these same rules).
        Step 2:  Apply the procedure (the value of the first subexpression)
                 to the arguments (the values of the other subexpressions).

            i.  If the procedure is compound (user-defined):
                A.  Create a frame with the formal parameters of the procedure
                    bound to the actual argument values.
                B.  Extend the procedure's defining environment with this
                    new frame.
                C.  Evaluate the procedure body, using the new frame as
                    the current frame.

                *** ONLY COMPOUND PROCEDURE INVOCATION CREATES A FRAME ***

           ii.  If the procedure is primitive:
                        Apply it by magic.

    (b)  Special forms

        i.  LAMBDA creates a procedure.  The left circle points to the text
            of the lambda expression; the right circle points to the
            defining environment, i.e., to the current environment at the
            time the lambda is seen.

            *** ONLY LAMBDA CREATES A PROCEDURE ***

       ii.  DEFINE adds a NEW binding to the CURRENT FRAME.

      iii.  SET! changes the FIRST AVAILABLE binding (see 1(b) for the
            definition of "first available").

       iv.  LET = LAMBDA (2(b)i) + invocation (2(a))

        v.  (define (...) ...) = LAMBDA (2(b)i) + DEFINE (2(b)ii)

       vi.  Other special forms follow their own rules (e.g. COND, IF).