1 / 33

CS 476 – Programming Language Design

CS 476 – Programming Language Design. William Mansky. Java-Like Language: Semantics. Values: ints , objects How should we represent an object?. new Point(3, 5). Java-Like Language: Semantics. Functions: Semantics of Calls. Evaluate the arguments Look up in

ptony
Download Presentation

CS 476 – Programming Language Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 476 – Programming Language Design William Mansky

  2. Java-Like Language: Semantics • Values: ints, objects • How should we represent an object? new Point(3, 5)

  3. Java-Like Language: Semantics

  4. Functions: Semantics of Calls • Evaluate the arguments • Look up in • Execute the body of and produce a return value • Assign the return value to

  5. OO: Semantics of Methods • Evaluate the arguments • Look up in • Execute the body of and produce a return value • Assign the return value to

  6. OO: Semantics of Methods • Evaluate the arguments and the object • Look up in the methods of ’s class • Execute the body of (with set to ) and produce a return value • Assign the return value to

  7. OO: Semantics of Methods • Evaluate the arguments and the object • Look up in the methods of ’s class • Execute the body of (with set to ) and produce a return value • Assign the return value to

  8. Java-Like Language: Contexts • In our simple imperative language, declarations could be stored in the type context and the runtime state • In Java, both typing and semantics need the full declaration of each class • We implicitly store class declarations in a class table

  9. Java-Like Language: Contexts • In Java, both typing and semantics need the full declaration of each class • We implicitly store class declarations in a class table • Java uses type information at runtime! • Every object is tagged with its class • The class determines which fields and methods are defined • The class determines which version of a method is called • Whereas in C, types disappear at runtime

  10. Java-Like Language: Contexts • In Java, both typing and semantics need the full declaration of each class • We implicitly store class declarations in a class table

  11. Java-Like Language: Contexts • In Java, both typing and semantics need the full declaration of each class • We implicitly store class declarations in a class table • is a global structure used by both the type system and the semantics, and computed in preprocessing

  12. Java-Like Language: Casts School s = new School(); s2 = w.build((Building) s); ((School) s2).getCourse(); where build takes a Building and School extends Building

  13. Java-Like Language: Casts School s = new School(); s2 = w.build((Building) s); // upcast from School to Building ((School) s2).getCourse(); // downcast from Building to School where build takes a Building and School extends Building

  14. Java-Like Language: Casts • Upcast: always safe, doesn’t do anything • Downcast: safe only if object actually has the right type • Other casts: ?? B b = new B(); A a = (A) b;

  15. Java-Like Language: Casts • Upcast: always safe, doesn’t do anything • Downcast: safe only if object actually has the right type • Other casts: ?? B b = new B(); A a = (A) ((Object) b);

  16. Java-Like Language: Casts • Upcast: always safe, doesn’t do anything • Downcast: safe only if object actually has the right type • Other casts: ?? B b = new B(); A a = (A) ((Object) b);

  17. Java-Like Language: Casts • Upcast: always safe, doesn’t do anything • Downcast: safe only if object actually has the right type • Other casts: ?? B b = new B(); A a = (A) ((Object) b);

  18. Java-Like Language: Syntax CL ::= class <id> extends <id> { T <id>, …, T <id>; KM … M } K ::= <id>(T <id>, …, T <id>){ super(<id>, …, <id>); this.<id> = <id>; …; this.<id> = <id>; } M ::= T <id>(T <id>, …, T <id>){ C } E ::= … | E.<id> C ::= … | <id> = new <id>(E, …, E); | E.<id> = E; | <id> = E.<id>(E, …, E); T ::= int | <id>

  19. Objects vs. Values • We said “objects are values” Objects: Values: new Point(3, 5) 5, true, etc. can be stored in variables can be stored in variables have pieces that can change can’t change different objects can have the if the value is the same, same values in them they’re equal

  20. Objects vs. Values • We said “objects are values”, but they’re also like variables! Objects: Values: new Point(3, 5) 5, true, etc. can be stored in variables can be stored in variables have pieces that can change can’t change different objects can have the if the value is the same, same values in them they’re equal

  21. Java-Like Language: Mutable Objects • Our language so far has no field-set operation! A a1 = new A(3, 5); A a2 = a1; a1.x = 4; int result = a2.x; // should be 4

  22. Java-Like Language: Mutable Objects • Our language so far has no field-set operation! A a1 = new A(3, 5); {a1 = new A(3, 5)} A a2 = a1; {a1 = new A(3, 5), a2 = new A(3,5)} a1.x = 4; {a1 = new A(4, 5), a2 = new A(3,5)} int result = a2.x; // should be 4

  23. Java-Like Language: Mutable Objects • Our language so far has no field-set operation! A a1 = new A(3, 5); {a1 = r1, r1 -> new A(3, 5)} A a2 = a1; {a1 = r1, a2 = r1, r1 -> new A(3, 5)} a1.x = 4; {a1 = r1, a2 = r1, r1 -> new A(4, 5)} int result = a2.x; // should be 4 a1 = new A(6, 7); {a1 = r2, a2 = r1, r1 -> new A(4, 5), r2 -> new A(6, 7)} • Two-level model: variables hold references, references point to values

  24. Java-Like Language: Mutable Objects • Split the state into two levels • Program state is now a tuple where: • is the currently executing command • is the call stack • is the environment, mapping variables to either primitive values (int, bool) or references • is the store, mapping references to object values

  25. Java-Like Language: Semantics

  26. Java-Like Language: Semantics

  27. Java-Like Language: Semantics

  28. Java-Like Language: Semantics

  29. Java-Like Language: Mutable Objects A a1 = new A(3, 5); {a1 = r1, r1 -> new A(3, 5)} A a2 = a1; {a1 = r1, a2 = r1, r1 -> new A(3, 5)} a1.x = 4; {a1 = r1, a2 = r1, r1 -> new A(4, 5)} int result = a2.x; // should be 4 a1 = new A(6, 7); {a1 = r2, a2 = r1, r1 -> new A(4, 5), r2 -> new A(6, 7)}

More Related