Before we can present a semantics for Cool we need a number
of concepts and a considerable amount of notation.
An environment is a mapping of variable identifiers to
locations. Intuitively, an environment tells us for a given
identifier the address of the memory location where that identifier's
value is stored. For a given expression, the environment must
assign a location to all identifiers to which the expression may refer.
For the expression, e.g., , we need an environment that maps
to some location and to some location. We'll use the following syntax
to describe environments, which is very similar to the syntax of type
assumptions used in Section 12.
The second component of the context for the evaluation of an
expression is the store (memory). The store maps locations to values,
where values in Cool are just objects. Intuitively, a store
tells us what value is stored in a given memory location.
For the moment, assume all values are integers.
A store is similar to an environment:
Given an environment and a store, the value of an
identifier can be found by first looking up the location that the identifier maps
to in the environment and then looking up the location in the store.
Together, the environment and the store define the execution state at a particular step of the evaluation of a Cool expression. The double indirection from identifiers to locations to values allows us to model variables. Consider what happens if the value is assigned variable in the environment and store defined above. Assigning to a variable means changing the value to which it refers but not its location. To perform the assignment, we look up the location for in the environment and then change the mapping for the obtained location to the new value, giving a new store .
The store models the contents of memory of the computer during program execution. Assigning to a variable modifies the store.
There are also situations in which the environment is modified. Consider the following Cool fragment:
let c : Int <- 33 in c
When evaluating this expression, we must introduce the new
identifier into the environment before evaluating the body of
the let. If the current environment and state are and
, then we create a new environment and a new store
The example in this subsection oversimplifies Cool environments and stores a bit, because simple integers are not Cool values. Even integers are full-fledged objects in Cool.