Principles of Programming Languages - PowerPoint PPT Presentation

About This Presentation
Title:

Principles of Programming Languages

Description:

Principles of Programming Languages Lecture 10 Variables and Assignment Variable Variable (Math) (CS) Math CS Constants Algol 68: real pi = 3.1416; C: (not ... – PowerPoint PPT presentation

Number of Views:779
Avg rating:3.0/5.0
Slides: 27
Provided by: PeterJ75
Category:

less

Transcript and Presenter's Notes

Title: Principles of Programming Languages


1
Principles of ProgrammingLanguages
  • Lecture 10
  • Variables and Assignment

2
Variable ? Variable (Math) (CS)
  • Math
  • CS

? symbol
definite singular description
real
? referent
? symbol
indefinite singular description
ref real
? referent
real
  • e.g. Prolog FP (IFP)
  • e.g. Pascal Algol 68

3
Constants
  • 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

4
Variables
  • 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
5
Assignment (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
6
Variables Bliss v. C
Bliss
C
  • Explicit dereferencing
  • Context dereferencing

x .y .z a .j ?.(a .i)
x y z aj ai Synonym (aj) (ai)
rvalue
lvalue
const

Only lvalue is actually computed
7
Algol 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
8
identity 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
9
Initialized 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
10
Algol 68 identity declaration unifies variable
constant declaration
m
  • loc real m 3
  • real x m
  • ref real x m

m
3
m
x
3
11
Algol 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
12
Pointers
  • 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
13
Algol 68 pointer declaration
  • ref real px ?
  • ref ref real px loc ref real

ref ref real
ref real
stack
14
Pointers (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
15
Pointers 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
16
Pascal/Ada pointers (contd)
  • new(px) px x

py
x
heap
stack
2.0
2.0
anonymous variable px Pascal/Ada pointers
point only to unnamed data objects
17
Pascal/Ada pointers (contd)
  • x 4.0
  • new(py) py px

1.
py
x
heap
stack
4.0
18
Pascal/Ada pointers (contd)
  • x 4.0
  • py px

2.
py
x
heap
stack
4.0
pointer value out of legal range
19
Pascal/Ada pointers (contd)
  • x 4.0
  • new(py) py px

3.
py
x
heap
stack
2.0
2.0
4.0
20
Algol68 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
21
Refs and Aliasing
  • Algol68 ref real py py px

py
stack
px
py
1102
1101
pointer aliasing
2.0
1100
1100
2.0
1100
22
Refs 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
23
Bliss 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
.
24
Semantics of Variable Uses C v. Bliss
x
x
  • On RHS
  • On LHS

/ ?
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
25
Variable 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
26
C ? Bliss.
C
x
x
Bliss
  • On RHS

x
lvalue of x
x
.x
rvalue of x
x
Bliss variable occurrences are referentially
transparent Semantics of x is independent of
context
Write a Comment
User Comments (0)
About PowerShow.com