cool overview - lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · cool É classroom objected oriented...
TRANSCRIPT
Cool OverviewLecture 2
January 8, 2018
Previously
É Kevin’s Office Hours: Thursdays, 2-4pm, BBB 2717É Lawrence’s Office Hours: Tuesdays/Thursdays,
11am-12:30pm, BBB Learning Center
É Compilers are language processing tools1. Lexing2. Parsing3. Type-checking4. Code generation5. Optimization
Compiler Construction 2/47
COOL
É Classroom Objected Oriented Language
É Easy to lex, parse, and type check
É Also COOL AssemblyÉ We’ll cover it in time for PA5É RISC-based fake-o ISA a la MIPS
Compiler Construction 3/47
Why COOL?
Compiler Construction 4/47
Why COOL?
Compiler Construction 4/47
COOL Topics
É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch
Compiler Construction 5/47
COOL Program Structure
É Single fileÉ List of classes
Compiler Construction 6/47
COOL Program Structure
É Single fileÉ List of classes
Compiler Construction 6/47
COOL Program Structure
É No globals, everything is in a class
Compiler Construction 7/47
COOL Classes
É Everything is inside classesÉ classes defined in any orderÉ classes are top level (no nesting!)
Compiler Construction 8/47
COOL Classes
No nested classes!
Compiler Construction 9/47
COOL Classes
É Each Class is a list of featuresÉ Features are attributes or methods
É All attributes are privateÉ All methods are publicÉ No static methods/attributes
Compiler Construction 10/47
COOL Topics
É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch
Compiler Construction 11/47
COOL Attributes and Methods
Compiler Construction 12/47
COOL Attributes and Methods
Compiler Construction 13/47
COOL Topics
É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch
Compiler Construction 14/47
COOL Types and Inheritance
É Everything is a class (no virtual, no interfaces)
É “Primitive” boxed typesÉ Int (default 0)É Bool (default false)É String (default empty string, “”)
É Other TypesÉ Main: Boilerplate for code entryÉ IO: inherit this for I/O functions!É Object: Everything inherits Object
Compiler Construction 15/47
COOL Type Hierarchy
Object
Main
IO
Int Bool String
Class A Class B
Subclass C
can’t inherit!
Compiler Construction 16/47
COOL Type Hierarchy
Object
Main
IO
Int Bool String
Class A Class B
Subclass C
can’t inherit!
Inherit from one at a time...
Compiler Construction 16/47
COOL Type Hierarchy
Object
Main
IO
Int Bool String
Class A Class B
Subclass C
can’t inherit!
...or the other
Compiler Construction 16/47
COOL Type Hierarchy
Object
Main
IO
Int Bool String
Class A Class B
Subclass C
can’t inherit!
but no multiple inheritance!
Compiler Construction 16/47
COOL Inheritance
Compiler Construction 17/47
No Inheritance Cycles!
Compiler Construction 18/47
Looking Ahead...
Object
Main
IO
Int Bool String
Class A
Subclass B
Subsubclass C
How might we detect this?
Compiler Construction 19/47
No attribute redefinition
Compiler Construction 20/47
No method signature changes
Compiler Construction 21/47
No method signature changes
Compiler Construction 22/47
Method Override
Compiler Construction 23/47
COOL Topics
É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch
Compiler Construction 24/47
COOL Expressions
É No statements, only expressionsÉ Expressions have associated types
É Every method contains exactly one expression!
Compiler Construction 25/47
All COOL Expressions
É Constants: 1, true, "Hello"É Arithmetic: 1 + 2, 1 * 3
É Identifiers: xÉ Assignment: x <- exprÉ Comparison: x < 2, 3 <= x
É new: new ClassName
É isvoid: isvoid x
Compiler Construction 26/47
All COOL Expressions (2)
É Conditional:if cond_expr then then_expr else else_expr fi
É Loop:while cond_expr loop body_expr pool
É Blocks:{ expr1 ; expr2 ; ... ; exprn ; }
É Let binding:let x : ClassName <- expr_init in expr_body
É Case:case expr0 of x1 : Class1 => expr1 ; ... ; xn :
Classn => exprn ;
Compiler Construction 27/47
Constant Expression
Compiler Construction 28/47
Arithmetic
Compiler Construction 29/47
Identifier
Compiler Construction 30/47
Assignment
Compiler Construction 31/47
Comparison
Compiler Construction 32/47
new and isvoid
Compiler Construction 33/47
Conditional
Compiler Construction 34/47
Complex Conditional
Compiler Construction 35/47
While Loops
Compiler Construction 36/47
Blocks!
Compiler Construction 37/47
Let
Compiler Construction 38/47
Case
Compiler Construction 39/47
COOL Topics
É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch
Compiler Construction 40/47
Object Creation in Cool
É There is a default constructorÉ Basically, run all initializers for attributes
Compiler Construction 41/47
COOL Topics
É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch
Compiler Construction 42/47
Built in Methods
É IO : Input/OutputÉ Inherit for access toÉ out_string and in_stringÉ out_int and in_int
É Everything comes from ObjectÉ copy (Shallow copy)É abort (give up execution)É type_name (String with dynamic type)
Compiler Construction 43/47
COOL Topics
É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch
Compiler Construction 44/47
Dispatch
É A fancy word for method callsÉ Self, Dynamic, and Static dispatch
Compiler Construction 45/47
Cool List Implementation
É How might we implement a List structure in Cool?
Compiler Construction 46/47
Coming up
É PA1c dueÉ PA1 due 1/17É Lexical Analysis Next
É Brush up on your DFA/NFA/RegExp knowledge
Compiler Construction 47/47