C SC 520 Principles of Programming Languages1Principles of ProgrammingLanguagesLecture 10Variables and AssignmentC SC 520 Principles of Programming Languages2Variable ≠ Variable(Math) (CS)• Math • CS3.1415828…ππππRRRRxRRRRref real3.14pirealdefinitesingulardescriptionindefinitesingulardescription←←←← symbol←←←← referentx…←←←← symbol←←←← referent• e.g. Prolog & FP (IFP)• e.g. Pascal & Algol 68realC SC 520 Principles of Programming Languages3Constants• Algol 68: real pi = 3.1416;• C: (not quite) #define PI 3.1416• Pascal: const pi = 3.1416;• Ada: pi: constant FLOAT := 3.1416;real3.14163.14163.1416pireal• No reference available•Noref real object exists•pi replaced by literal•No stack (or any other) space allocated (stored in instruction)•Pure definitionsame logical statusas a literalC SC 520 Principles of Programming Languages4Variables• Algol 68: begin real x,y;• C: { float x,y;• Pascal: var x,y: real;• Ada: x,y: FLOAT;yxref realref realrealrealalloc on stackx := 2.0; y := 3.0;3.02.0C SC 520 Principles of Programming Languages5Assignment (after y gets 3)• Algol 68: x := y;• C: x = y;• Bliss: x ← .yyxref realref realrealreal3.03.0C SC 520 Principles of Programming Languages6Variables: Bliss v. C• Explicit dereferencing• Context dereferencingxBlissCxx (lvalue)x (rvalue).xx = y + za[j] = a[i]Synonym: *(a+j) = *(a+i)x = .y + .za +.j ←.(a +.i)lvaluervalueconstOnly lvalue is actually computed{C SC 520 Principles of Programming Languages7Algol 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 2.718xx2.718C SC 520 Principles of Programming Languages8identity declaration (cont’d)• real x; abbreviates loc real x; which abbreviates the following identity declaration:• ref real x = loc real ;realref realLocal generatorYields a ref realref real (lvalue)Ascribed to identifier xxC SC 520 Principles of Programming Languages9Initialized 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)p3.1416ref realrealstackC SC 520 Principles of Programming Languages10Algol 68 identity declaration: unifies variable & constant declaration1. loc real m := 3;2. real x = m;3. ref real x = m;3xmm33x3mC SC 520 Principles of Programming Languages11Algol 68 identity declaration (cont’d)4. real x := m; ⇒⇒⇒⇒ loc real x:=m; ⇒⇒⇒⇒ref real x = loc real := m;5. real t = x*m;3realref realref realreal39• t const. In new environment•x*m evaluated when declaration encounteredC SC 520 Principles of Programming Languages12Pointers• Refs to variable refs• Algol 68: ref real px ;• C: float *px ;• Pascal: var px: ^real; • Ada: type PTR is access FLOAT;px : PTR; (PTR=``access type’’ to ``base type’’)ref realref ref realpx〉〉〉〉〉〉〉〉C SC 520 Principles of Programming Languages13Algol 68 pointer declarationref real px ⇒⇒⇒⇒ref ref real px = loc ref real;ref ref realref real〉〉〉〉〉〉〉〉stackC SC 520 Principles of Programming Languages14Pointers (cont’d)• 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 realref ref realrealref realstack2.0px〉〉〉〉〉〉〉〉x``anonymous variable’’C SC 520 Principles of Programming Languages15Pointers 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;x px〉〉〉〉〉〉〉〉py〉〉〉〉〉〉〉〉stackheap2.0C SC 520 Principles of Programming Languages16Pascal/Ada pointers (cont’d)• new(px); px^ := x;x px〉〉〉〉〉〉〉〉py〉〉〉〉〉〉〉〉stackheap2.02.0``anonymous variable px^’’Pascal/Ada pointers point only to ``unnamed’’ data objectsC SC 520 Principles of Programming Languages17Pascal/Ada pointers (cont’d)• x := 4.0;• new(py); py := px;x px〉〉〉〉〉〉〉〉py〉〉〉〉〉〉〉〉stackheap4.02.01.C SC 520 Principles of Programming Languages18Pascal/Ada pointers (cont’d)• x := 4.0;• py^ := px^;x px〉〉〉〉〉〉〉〉py〉〉〉〉〉〉〉〉stackheap4.02.02.``pointer value out of legal range’’C SC 520 Principles of Programming Languages19Pascal/Ada pointers (cont’d)• x := 4.0;• new(py); py^ := px^;x px〉〉〉〉〉〉〉〉py〉〉〉〉〉〉〉〉stackheap4.02.03.2.0C SC 520 Principles of Programming Languages20Algol68 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 declarationpx〉〉〉〉〉〉〉〉hxstackheapC SC 520 Principles of Programming Languages21Refs and Aliasing• Algol68: ref real py; py := px;px〉〉〉〉〉〉〉〉py〉〉〉〉〉〉〉〉stack2.0``pointer aliasing’’2.0pxpy11001100〉〉〉〉〉〉〉〉1100:〉〉〉〉〉〉〉〉11011102C SC 520 Principles of Programming Languages22Refs and Aliasing (cont’d)• Algol 68: ref real y = x; (identity declaration)yxref realref realno alloc!real2.02.0xy110111011100:``name aliasing’’``equivalencing (Fortran)’’C SC 520 Principles of Programming Languages23Bliss: untyped bit strings• local x; x ← 2; local px;• px ← x.0010x0010=pxx``ptr to x stored in px’’px0010xpxpicture after assignmentC SC 520 Principles of Programming Languages24Semantics of Variable Uses: C v. Bliss• On LHS• On RHSxBliss:C:xxx= / ←←←←=x&xx←←←←x.xx.xMeaning of x context-sensitiveNot ``referentially transparent’’Meaning of x is ``referentially transparent’’C SC 520 Principles of Programming Languages25Variable Uses: C v. Bliss (cont’d)• C: & not a unary operator! &(x+y) meaningless! &&x meaningless (can’t reverse a pointer)! &2 meaningless• Bliss: . is a unary operator! .(x+y) sensible! ..x sensible! .2 sensible2 = 0010.2 0010:..2 1101:11011000C SC 520
View Full Document