Cool Overview Classroom Object Oriented Language Designed to Be implementable as a course project in one semester two quarters Give a taste of implementation of a modern programming language with Abstraction and Encapsulation Strong typing Static typing Reuse single inheritance Dynamic Dispatch Automatic Memory management COOL The Language Adapted from material by Prof Alex Aiken UCB But leaves out many features of a production language for tractability CS780 Prasad L2Cool 1 A Simple Example L2Cool 2 Cool Objects class Point x Int 0 y Int 0 class Point x Int 0 y Int use default value The expression new Point creates a new object of class Point Cool programs are sets of class definitions A special class Main with a special method main No separate notion of a subroutine A class is a collection of attributes and methods Instances of a class are objects CS780 Prasad CS780 Prasad L2Cool An object can be thought of as a record with a slot for each attribute 3 CS780 Prasad x y 0 0 L2Cool 4 1 Methods Information Hiding in Cool A class can also define methods for manipulating attributes Methods are global Cf public class Point x Int 0 y Int 0 movePoint newx Int newy Int Point x newx y newy self close block expression close method close class Methods can refer to the current object using self CS780 Prasad L2Cool 5 Methods x 0 Example class Point x Int x setx newx Int Int x newx CS780 Prasad L2Cool 6 y 0 We can extend points to colored points using subclassing class hierarchy class ColorPoint inherits Point color Int 0 movePoint newx Int newy Int Point color 0 x newx y newy SELF TYPE self x y color movePoint 0 0 0 movePoint In reality implementations save space by sharing these pointers among instances of the same class CS780 Prasad They can only be accessed by the class s methods Inheritance Each object knows how to access the code of a method It is as if the object contains a slot pointing to the code x 0 Attributes are local to a class Cf private protected y 0 methods movePoint L2Cool 7 CS780 Prasad L2Cool 8 2 Cool Types Cool Type Checking Every class is a type Base classes Int for integers Bool for boolean values true false String for strings Object root of the class hierarchy Is well typed if A is an ancestor of B in the class hierarchy Anywhere an A instance is expected a B instance can be used Cf polymorphism All variables must be declared Type safety x A x new B A well typed program cannot result in runtime type errors Cf Static type checking Compiler infers types for expressions CS780 Prasad L2Cool 9 CS780 Prasad L2Cool 10 Method Invocation and Inheritance Method Invocation Methods are invoked by dispatch Understanding dispatch in the presence of inheritance is a subtle aspect of OO languages Example invoke one argument method m x 1 p Point p new ColorPoint p movePoint 1 2 L2Cool 4 5 1 Eval e 2 Find class of e 2 5 p has static type Point p has dynamic type ColorPoint p movePoint must invoke the ColorPoint version CS780 Prasad e m e 11 m 3 self x method code method table CS780 Prasad 3 Find code of m 4 Eval arg e 5 Bind self and x 6 Run method 6 L2Cool 12 3 Other Expressions Cool Memory Management Expression language every expression has a type and a value Memory is allocated every time new is invoked Loops while E loop E pool Conditionals if E then E else E fi Case statement case E of x Type E esac Arithmetic logical operations Assignment x E Primitive I O out string s in string Missing features Arrays Floating point operations Interfaces Exceptions CS780 Prasad L2Cool Memory is deallocated automatically when an object is not reachable anymore Done by the garbage collector GC There is a Cool GC Cf Java C Portion of the Run time System 13 CS780 Prasad L2Cool 14 Palindrome recognizer class Main inherits IO pal s String Bool expression A complete COOL program main SELF TYPE statements CS780 Prasad L2Cool 15 CS780 Prasad L2Cool 16 4 pal s String Bool if s length 0 then true else if s length 1 then true else if s substr 0 1 s substr s length 1 1 then pal s substr 1 s length 2 else false fi fi fi CS780 Prasad L2Cool main SELF TYPE out string enter a string if pal in string then out string that was a palindrome n else out string that was not a palindrome n fi 17 CS780 Prasad L2Cool 18 About COOL Cool Installation at WSU The language is defined in the document COOLAID The Cool Reference Manual Lexical Structure Cool Syntax Cool Semantics Cool is installed on gandalf cs wright edu in the directory usr local lib cool Add the following to your PATH to access Cool executables usr local lib cool bin There are several example Cool programs in the directory usr local lib cool examples Static Type System Dynamic Operational Approach To compile a Cool program type Runtime system coolc filename cl Some of the support code for Project 3 and beyond if you take 781 is described in A Tour of Cool Support Code CS780 Prasad L2Cool 19 The compiler produces MIPS assembly code To execute the program use the SPIM simulator spim file filename s CS780 Prasad L2Cool 20 5
View Full Document
Unlocking...