Title: Principles of Programming Languages
1Principles of ProgrammingLanguages
- Lecture 10
- Variables and Assignment
2Variable ? Variable (Math) (CS)
? symbol
definite singular description
real
? referent
? symbol
indefinite singular description
ref real
? referent
real
3Constants
- Algol 68 real pi 3.1416
- C (not quite) define PI 3.1416
- Pascal const pi 3.1416
- Ada pi constant FLOAT 3.1416
3.1416
same logical status as a literal
real
real
3.1416
- No reference available
- No ref real object exists
- pi replaced by literal
- No stack (or any other) space allocated (stored
in instruction) - Pure definition
4Variables
- Algol 68 begin real x,y
- C float x,y
- Pascal var x,y real
- Ada x,y FLOAT
ref real
ref real
alloc on stack
real
real
x 2.0 y 3.0
3.0
2.0
5Assignment (after y gets 3)
- Algol 68 x y
- C x y
- Bliss x ? .y
x
y
ref real
ref real
real
3.0
real
3.0
6Variables Bliss v. C
Bliss
C
x .y .z a .j ?.(a .i)
x y z aj ai Synonym (aj) (ai)
rvalue
lvalue
const
Only lvalue is actually computed
7Algol 68 aside identity declaration
- real x c NOT same as real x c
- Identity declaration real x 2.718 means
- Initialized variable real x 2.718 means
- Why? Implicit local (stack) allocation
x
2.718
x
2.718
8identity declaration (contd)
- real x abbreviates loc real x which abbreviates
the following identity declaration - ref real x loc real
x
Local generator Yields a ref real
ref real (lvalue) Ascribed to identifier x
9Initialized Variables
- Algol 68 begin real p 3.1416
- C float p 3.1416
- Pascal var p real begin p 3.1416
- Ada p FLOAT 3.1416
- What is variable about a variable?
- Not reference (location, lvalue)
- Ref cannot be changed ascribed to or
possessed by ident - Not identifier
- Not type (in most languages)
- The value (rvalue)
p
ref real
real
3.1416
stack
10Algol 68 identity declaration unifies variable
constant declaration
m
- loc real m 3
- real x m
- ref real x m
m
3
m
x
3
11Algol 68 identity declaration (contd)
- real x m ? loc real xm ? ref real x loc
real m - real t xm
ref real
ref real
real
real
3
3
- t const. In new environment
- xm evaluated when declaration encountered
9
12Pointers
- Refs to variable refs
- Algol 68 ref real px
- C float px
- Pascal var px real
- Ada type PTR is access FLOAT
- px PTR (PTRaccess type to
base type)
px
ref ref real
ref real
13Algol 68 pointer declaration
- ref real px ?
- ref ref real px loc ref real
ref ref real
ref real
stack
14Pointers (contd)
- Algol 68 px x
- C px x
- Bliss px ? x
- NOT Pascal, Ada picture. Closest is
- Pascal new(px) px x Ada px
new FLOAT(x)
ref ref real
ref real
ref real
real
2.0
anonymous variable
15Pointers in Pascal/Ada point into heap
- Defeats dangling pointers at block exit
- Pointers cannot point into the stack
- var x real var px, py real
py
x
heap
stack
2.0
16Pascal/Ada pointers (contd)
py
x
heap
stack
2.0
2.0
anonymous variable px Pascal/Ada pointers
point only to unnamed data objects
17Pascal/Ada pointers (contd)
1.
py
x
heap
stack
4.0
18Pascal/Ada pointers (contd)
2.
py
x
heap
stack
4.0
pointer value out of legal range
19Pascal/Ada pointers (contd)
3.
py
x
heap
stack
2.0
2.0
4.0
20Algol68 has a heap allocator
- ref ref real px heap real
- ref real hx heap real
- Abbreviated heap real hx
- Pascal lvalues of all var identifiers are in
stack - lvalues of all anonymous variables are in the
heap - Algol68 no restriction
- allocation orthogonal to declaration
hx
heap
stack
21Refs and Aliasing
- Algol68 ref real py py px
py
stack
px
py
1102
1101
pointer aliasing
2.0
1100
1100
2.0
1100
22Refs and Aliasing (contd)
- Algol 68 ref real y x (identity
declaration)
x
y
ref real
ref real
no alloc!
real
2.0
name aliasing equivalencing (Fortran)
1100
23Bliss untyped bit strings
px
x
- local x x ? 2 local px
- px ? x
px
x
ptr to x stored in px
px
x
picture after assignment
.
24Semantics of Variable Uses C v. Bliss
x
x
/ ?
x
x
C
x
Meaning of x context-sensitive Not
referentially transparent
x
x
x
x
?
Bliss
.x
.x
Meaning of x is referentially transparent
25Variable Uses C v. Bliss (contd)
- C not a unary operator
- (xy) meaningless
- x meaningless (cant reverse a pointer)
- 2 meaningless
- Bliss . is a unary operator
- .(xy) sensible
- ..x sensible
- .2 sensible
2 0010 .2 0010 ..2 1101
1101
1000
26C ? Bliss.
C
x
x
Bliss
x
lvalue of x
x
.x
rvalue of x
x
Bliss variable occurrences are referentially
transparent Semantics of x is independent of
context