130 likes | 268 Views
Programming Languages 2nd edition Tucker and Noonan. Chapter 10 Function Implementation In theory, there is no difference between theory and practice, but not in practice. Anonymous. Contents. 10.1 Function Declaration and Call in Clite 10.2 Completing the Clite Type System
E N D
Programming Languages2nd editionTucker and Noonan Chapter 10 Function Implementation In theory, there is no difference between theory and practice, but not in practice. Anonymous
Contents 10.1 Function Declaration and Call in Clite 10.2 Completing the Clite Type System 10.3 Semantics of Call and Return 10.4 Formal Treatment of Types and Semantics
10.4 Formal Treatment of Types and Semantics • The type map for a function f, tmf, is a set of pairs and triples, representing global variables (tmG), functions (tmF), f’s parameters, and f’s locals. E.g., for the program in Figure 10.1:
Typing Function • The function typing creates type maps for each individual function f in a program with globals G and functions F.
Validity of a Clite Program • A program is valid if its global variables and function declarations are valid, and each function is valid with respect to their type maps.
Validity of a Clite Function • A function is valid if 1) its parameters and locals have unique names, 2) its statements are valid in its type map, and 3) it contains (does not contain) a return statement if it is a non-void (void) function.
Validity of a Clite Call and Return • Validity of most Clite statements is defined in Chapter 6. Validity of a Call and a Return is defined below:
Formalizing the Semantics of Clite • Memory (), Environment () and State (): • The State of a function f, f , is a triple f a • where a addresses the top of the run-time stack, • is a set of address-value pairs, and • f has f’s visible variables and their addresses.
Allocate and Deallocate Functions • allocate changes the state by adding a group of variable-address pairs to it. • deallocate removes such a group.
Calling a Void Function • Meaning Rule 10.1 in functional form, skipping temporarily the returned result:
Call/Return for a Non-Void Function • The Call returns a Value; the Return identifies it:
Meaning of a Block • Since a block can contain a Return, it must exit as soon as the return is encountered (Meaning Rule 10.3). Here is the functional definition.