cool overview - lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · cool É classroom objected oriented...

52
Cool Overview Lecture 2

Upload: others

Post on 05-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Cool OverviewLecture 2

January 8, 2018

Page 2: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

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

Page 3: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

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

Page 4: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Why COOL?

Compiler Construction 4/47

Page 5: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Why COOL?

Compiler Construction 4/47

Page 6: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Topics

É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch

Compiler Construction 5/47

Page 7: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Program Structure

É Single fileÉ List of classes

Compiler Construction 6/47

Page 8: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Program Structure

É Single fileÉ List of classes

Compiler Construction 6/47

Page 9: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Program Structure

É No globals, everything is in a class

Compiler Construction 7/47

Page 10: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Classes

É Everything is inside classesÉ classes defined in any orderÉ classes are top level (no nesting!)

Compiler Construction 8/47

Page 11: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Classes

No nested classes!

Compiler Construction 9/47

Page 12: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

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

Page 13: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Topics

É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch

Compiler Construction 11/47

Page 14: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Attributes and Methods

Compiler Construction 12/47

Page 15: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Attributes and Methods

Compiler Construction 13/47

Page 16: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Topics

É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch

Compiler Construction 14/47

Page 17: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

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

Page 18: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Type Hierarchy

Object

Main

IO

Int Bool String

Class A Class B

Subclass C

can’t inherit!

Compiler Construction 16/47

Page 19: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

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

Page 20: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

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

Page 21: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

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

Page 22: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Inheritance

Compiler Construction 17/47

Page 23: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

No Inheritance Cycles!

Compiler Construction 18/47

Page 24: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Looking Ahead...

Object

Main

IO

Int Bool String

Class A

Subclass B

Subsubclass C

How might we detect this?

Compiler Construction 19/47

Page 25: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

No attribute redefinition

Compiler Construction 20/47

Page 26: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

No method signature changes

Compiler Construction 21/47

Page 27: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

No method signature changes

Compiler Construction 22/47

Page 28: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Method Override

Compiler Construction 23/47

Page 29: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Topics

É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch

Compiler Construction 24/47

Page 30: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Expressions

É No statements, only expressionsÉ Expressions have associated types

É Every method contains exactly one expression!

Compiler Construction 25/47

Page 31: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

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

Page 32: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

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

Page 33: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Constant Expression

Compiler Construction 28/47

Page 34: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Arithmetic

Compiler Construction 29/47

Page 35: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Identifier

Compiler Construction 30/47

Page 36: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Assignment

Compiler Construction 31/47

Page 37: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Comparison

Compiler Construction 32/47

Page 38: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

new and isvoid

Compiler Construction 33/47

Page 39: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Conditional

Compiler Construction 34/47

Page 40: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Complex Conditional

Compiler Construction 35/47

Page 41: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

While Loops

Compiler Construction 36/47

Page 42: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Blocks!

Compiler Construction 37/47

Page 43: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Let

Compiler Construction 38/47

Page 44: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Case

Compiler Construction 39/47

Page 45: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Topics

É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch

Compiler Construction 40/47

Page 46: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Object Creation in Cool

É There is a default constructorÉ Basically, run all initializers for attributes

Compiler Construction 41/47

Page 47: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Topics

É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch

Compiler Construction 42/47

Page 48: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

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

Page 49: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

COOL Topics

É Program structureÉ ClassesÉ Attributes/MethodsÉ Types/InheritanceÉ ExpressionsÉ Object creationÉ Built in methodsÉ Dispatch

Compiler Construction 44/47

Page 50: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Dispatch

É A fancy word for method callsÉ Self, Dynamic, and Static dispatch

Compiler Construction 45/47

Page 51: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Cool List Implementation

É How might we implement a List structure in Cool?

Compiler Construction 46/47

Page 52: Cool Overview - Lecture 2kjleach.eecs.umich.edu/c18/l2.pdf · COOL É Classroom Objected Oriented Language É Easy to lex, parse, and type check É Also COOL Assembly É We’ll cover

Coming up

É PA1c dueÉ PA1 due 1/17É Lexical Analysis Next

É Brush up on your DFA/NFA/RegExp knowledge

Compiler Construction 47/47