<<

IMPERATIVE PROGRAMMING LANGUAGES

(or: why the actual choice of doesn’ really matter)

University of Waterloo

based on David A. Schmidt: The Structure of Typed Programming Languages MIT Press, 1994 LIST OF SLIDES 1-1

List of Slides

1 2 Outline 3 The Core Language 4 Abstract Syntax Tree 5 What happens? 6 What happens? (cont.) 7 What happens with commands? 8 What happens with expressions? 9 Example 10 How to it into a full-blown PL? 11 Naming Things 12 Parameters 13 Local Definitions (hiding) 14 Storage Management 15 Pointer Types 16 The Result 17 Syntax Summary 18 Syntax Summary (cont.) 19 Syntax Summary (cont.) Imperative Programming Languages: 2

Outline

• A core language ⇒ syntax ⇒ understanding what happens

• Extensions ⇒ giving things names ⇒ giving things parameters ⇒ allowing local definitions ⇒ “real” variables

• Syntax Summary

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 3

The Core Language

cmd ::= skip

| locint := intexp | cmd ; cmd | if boolexp then cmd else cmd fi | while boolexp do cmd od

intexp ::= int

| locint | intexp + intexp

boolexp ::= intexp = intexp

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 4

Abstract Syntax Tree

loc1 := 0; while loc1 = 0 do loc1 := loc1 + 1 od

C : comm

C : comm C : comm

L : intloc := E : intexp while E : boolexp doC : comm od loc 1

N : int E : intexp =E : intexp L : intloc := E : intexp 0 loc 2

@ L : intloc N : int E : intexp + E : intexp loc 0 1

@ L : intloc N : int loc 1 1

Figure 1.3 (corrected)

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 5

What happens?

IDEA: Programs (commands) use memory (store) to keep intermediate results and the of the computation

⇒ store (memory) is an array of locations

loc0 loc1 loc2 ··· loc256M−1

⇒ executing a changes values in this array — given a location we can retrieve a value — given a location and a value we can store it

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 6

What happens? (cont.)

Now we can say precisely what it means to execute a command (defined by cmd)asa that transforms the store S

• commands are functions from store to store

• expressions are functions from store to a value

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 7 What happens with commands?

(a no operation) F (skip,S)= S

(an assignment) F (locint := intexp, S)= assign(locint,F (intexp, S),S)

(sequential composition) F (cmd1 ; cmd2,S)= F (cmd2,F (cmd1,S))

(if-conditional) F (if boolexp then cmd1 else cmd2 fi,S)= F (cmd1,S) if F (boolexp, S)= true F (cmd2,S) if F (boolexp, S)= false

(while-loop) F (while boolexp do cmd od,S)= S if F (boolexp, S)= false F (while boolexp do cmd od,F (cmd,S)) otherwise

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 8

What happens with expressions?

(an integer constant int) F (int, S)= int

(store lookup) F (locint,S)= lookup(locint,S)

(integer addition) F (intexp1 + intexp2,S)= plus(F (intexp1,S),F (intexp2,S))

(integer comparison) F (intexp1 = intexp2,S)= true if F (intexp1,S)= F (intexp2,S) false if F (intexp1,S) 6= F (intexp2,S)

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 9

Example

F (loc1 := 0; while loc1 =0 do loc1 := loc1 +1 od, [7, 5,...]) =

F (while loc1 =0 do loc1 := loc1 +1 od,

F (loc1 := 0, [7, 5,...])) =

F (while loc1 =0 do loc1 := loc1 +1 od, [7, 0,...]) =

F (while loc1 =0 do loc1 := loc1 +1 od,

F (loc1 := loc1 +1, [7, 0,...]) =

F (while loc1 =0 do loc1 := loc1 +1 od, [7, 1,...]=

[7, 1,...]

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 10

How to make it into a full-blown PL?

How do we get named constants, functions, procedures, modules (all of these parametrized), and local values/hiding mechanisms?

⇒ by applying three simple principles:

1. every syntactic construction can be given a name

2. every syntactic construction can be given a parameter

3. every syntactic construction can be given local definitions

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 11

Naming Things

Generic form for a declaration:

D ::= define id = V | ; D

Depending on what V is we get:

• a procedure for V a command

• a function and constant for V an expression

• a module for V a declaration

• a class/record for V a type structure (coming)

Use: “D in V ” where V can “use” identifiers defined in D

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 12

Parameters

Generic form: λp.V ... takes a value for p, substitutes it into V and then does whatever V used to do. Depending on what V is we get parametrized versions of the above constructs, in particular ⇒ parametrized procedures ⇒ parametrized functions In most languages parameters can be given only to named things; the usual form then is

define id(p1,...,pk)= V where V contains identifiers p1,...,pk.

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 13

Local Definitions (hiding)

Generic form: begin D in V end . . . declarations D are “visible” only in V

• command blocks (most common; V is a command)

• local (private) variables in classes/modules/ADTs

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 14

Storage Management

ONE MAJOR missing ingredient:

⇒ variable declarations!!! Problem: a variable declaration

var i: integer does two things:

1. allocates space (an unused location) for an integer

2. names the obtained location i

⇒ type structures (essentially records) ⇒ space allocation routines (essentially commands)

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 15

Pointer Types

IDEA: (1) locations are indexed (essentially) by integers (2) integers can be stored in locations ⇒ pointer types

• type of a pointer variable tells us what is the data type of the location of whose index is stored in this location

• anonymous locations (without explicit names) ⇒ dynamic storage allocation (new) ⇒ new’s parameter is a type-structure ⇒ new returns a location of the appropriate type

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 16

The Result

Leads to a C or JAVA (w/o inheritance) like language; the only thing remaining is to change “generic” constructions into specific keywords:

feature name in JAVA named command (void) method (class) named expression method (class) module static class type structure class . . .

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 17

Syntax Summary

Types: ::= integer | char | array [m..n] of | record id1:, ..., idk: end | ^

Expressions: ::= | + | ... | ID(, ..., )

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 18

Syntax Summary (cont.)

Commands: ::= := | begin ; ... ; end | if then else | while do | for := to do | new(p) | ID(, ..., ) repeat-until-loop is special case of while-loop; case-statement is special case of if statements (nested).

Data Types and Structures CS 234 University of Waterloo Imperative Programming Languages: 19 Syntax Summary (cont.)

Declarations: type = ;

var : ;

function (:,..., :): ; begin ; := end;

procedure (:,..., :); begin end;

Data Types and Structures CS 234 University of Waterloo