Unformatted text preview:

1CS780(Prasad) L2Cool 1COOL: The LanguageAdapted from material by:Prof. Alex Aiken (UCB) CS780(Prasad) L2Cool 2Cool 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• But leaves out many features of a production language, for tractability.CS780(Prasad) L2Cool 3A Simple Example• 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.class Point {x : Int ← 0;y : Int ← 0;};CS780(Prasad) L2Cool 4Cool Objects• An object can be thought of as a record with a slot for each attribute.class Point {x : Int ← 0;y : Int; (* use default value *)};xy00• The expression “new Point” creates a new object of class Point.2CS780(Prasad) L2Cool 5Methods• Methods can refer to the current object using self.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• A class can also define methods for manipulating attributes. CS780(Prasad) L2Cool 6Information Hiding in Cool• Methods are global. (Cf. public)• Attributes are local to a class. (Cf. private , protected)¾ They can only be accessed by the class’s methods.•Example:class Point {. . .x () : Int { x };setx (newx : Int) : Int { x ← newx };}; CS780(Prasad) L2Cool 7Methods• Each object knows how to access the code of a method. It is as if the object contains a slot pointing to the code.• In reality, implementations save space by sharing these pointers among instances of the same class.xy00movePoint*xy00methodsmovePoint*CS780(Prasad) L2Cool 8class ColorPoint inherits Point {color : Int ← 0;movePoint(newx : Int, newy : Int): Point {{ color ← 0;x ← newx; y ← newy; self;}}; };Inheritance• We can extend points to colored points using subclassing =>class hierarchy.xy00color0movePoint*SELF_TYPE3CS780(Prasad) L2Cool 9Cool Types• 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• All variables must be declared.¾Compiler infers types for expressions. CS780(Prasad) L2Cool 10Cool Type Checking• 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)• Type safety: ¾A well-typed program cannot result in runtime type errors. (Cf. Static type checking)x : A;x ← new B;CS780(Prasad) L2Cool 11Method Invocation and Inheritance• Methods are invoked by dispatch.• Understanding dispatch in the presence of inheritance is a subtle aspect of OO languages.p : Point;p ← new ColorPoint;p.movePoint(1,2); p has static type Point. p has dynamic type ColorPoint. p.movePoint must invoke the ColorPoint version. CS780(Prasad) L2Cool 12Method Invocation• Example: invoke one-argument method m(x)e.m(e’)…1…4m:self ←x ←<method code>35561. Eval. e2. Find class of e3. Find code of m4. Eval. arg. e’5. Bind self and x6. Run method……2methodtable4CS780(Prasad) L2Cool 13Other Expressions• Expression language (every expression has a type and a value)¾ 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 14Cool Memory Management• Memory is allocated every time new is invoked.• 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 SystemCS780(Prasad) L2Cool 15A complete COOL program CS780(Prasad) L2Cool 16Palindrome recognizerclass Main inherits IO {pal(s : String) : Bool {(*...expression…*)};main() : SELF_TYPE {(*…statements…*)};};5CS780(Prasad) L2Cool 17pal(s : String) : Bool {if s.length() = 0then trueelse if s.length() = 1then trueelse if s.substr(0, 1) = s.substr(s.length() - 1, 1)then pal(s.substr(1, s.length() -2))else falsefi fi fi};CS780(Prasad) L2Cool 18main() : 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;}};CS780(Prasad) L2Cool 19About COOL• The language is defined in the document COOLAID: The Cool Reference Manual.¾ Lexical Structure¾ Cool Syntax¾ Cool SemanticsStatic: Type SystemDynamic: Operational Approach¾ Runtime system• 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 20Cool Installation at WSU• 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• To compile a Cool program type: coolc <filename.cl>The compiler produces MIPS assembly code.• To “execute” the program use the SPIM simulator: spim –file


View Full Document

Wright CS 780 - COOL- The Language

Download COOL- The Language
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view COOL- The Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view COOL- The Language 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?