cisc 672 -- advanced compiler constructioncavazos/cisc672-fall08/lectures/cooloverview.pdf · for...

38
CISC 672 – Advanced Compiler Construction Timo K¨ otzing September 9, 2008

Upload: others

Post on 25-Jan-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

CISC 672 – Advanced Compiler Construction

Timo Kotzing

September 9, 2008

Page 2: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

Disclaimer

The following does not describe the cool-language in depth. It isnot designed to be used as a syntax reference, but rather as anintroduction into programming with cool, and also into objectoriented programming in general.

For the purpose of writing your own cool-compiler, please read thecool-manual carefully.

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 3: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Program?

I a cool-program is a list of cool-classes

I classes may be spread over several files

I one of the classes has to be named “Main”

I this class has to contain a method named “main”

I executing a cool-program is equivalent to evaluating this“Main.main()” function

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 4: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Program?

I a cool-program is a list of cool-classes

I classes may be spread over several files

I one of the classes has to be named “Main”

I this class has to contain a method named “main”

I executing a cool-program is equivalent to evaluating this“Main.main()” function

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 5: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Program?

I a cool-program is a list of cool-classes

I classes may be spread over several files

I one of the classes has to be named “Main”

I this class has to contain a method named “main”

I executing a cool-program is equivalent to evaluating this“Main.main()” function

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 6: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Program?

I a cool-program is a list of cool-classes

I classes may be spread over several files

I one of the classes has to be named “Main”

I this class has to contain a method named “main”

I executing a cool-program is equivalent to evaluating this“Main.main()” function

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 7: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Program?

I a cool-program is a list of cool-classes

I classes may be spread over several files

I one of the classes has to be named “Main”

I this class has to contain a method named “main”

I executing a cool-program is equivalent to evaluating this“Main.main()” function

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 8: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Program?

I a cool-program is a list of cool-classes

I classes may be spread over several files

I one of the classes has to be named “Main”

I this class has to contain a method named “main”

I executing a cool-program is equivalent to evaluating this“Main.main()” function

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 9: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Class?

I a cool-class is a list of features

I features are either attributes or methods

I attributes are local variables (with scope of the class)

I methods are global functions, addressed by“< functionName > . < methodName > (...)”

I attributes have to have a type and may be initialized

I methods have a (possibly empty) list of formal parameters, areturn type and a body

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 10: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Class?

I a cool-class is a list of features

I features are either attributes or methods

I attributes are local variables (with scope of the class)

I methods are global functions, addressed by“< functionName > . < methodName > (...)”

I attributes have to have a type and may be initialized

I methods have a (possibly empty) list of formal parameters, areturn type and a body

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 11: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Class?

I a cool-class is a list of features

I features are either attributes or methods

I attributes are local variables (with scope of the class)

I methods are global functions, addressed by“< functionName > . < methodName > (...)”

I attributes have to have a type and may be initialized

I methods have a (possibly empty) list of formal parameters, areturn type and a body

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 12: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Class?

I a cool-class is a list of features

I features are either attributes or methods

I attributes are local variables (with scope of the class)

I methods are global functions, addressed by“< functionName > . < methodName > (...)”

I attributes have to have a type and may be initialized

I methods have a (possibly empty) list of formal parameters, areturn type and a body

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 13: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Class?

I a cool-class is a list of features

I features are either attributes or methods

I attributes are local variables (with scope of the class)

I methods are global functions, addressed by“< functionName > . < methodName > (...)”

I attributes have to have a type and may be initialized

I methods have a (possibly empty) list of formal parameters, areturn type and a body

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 14: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Class?

I a cool-class is a list of features

I features are either attributes or methods

I attributes are local variables (with scope of the class)

I methods are global functions, addressed by“< functionName > . < methodName > (...)”

I attributes have to have a type and may be initialized

I methods have a (possibly empty) list of formal parameters, areturn type and a body

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 15: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What is a COOL-Class?

I a cool-class is a list of features

I features are either attributes or methods

I attributes are local variables (with scope of the class)

I methods are global functions, addressed by“< functionName > . < methodName > (...)”

I attributes have to have a type and may be initialized

I methods have a (possibly empty) list of formal parameters, areturn type and a body

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 16: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I every class is a type

I the basic types are the classes “Object”, “IO”, “Int”, “String”and “Bool”

I all classes but Object have to be inherited from exactly oneother class (be a child of this class)

I every class that does not specify a class to inherit from isinherited from Object

I the “is child of”-relation has to be a tree, rooted at Object

I basic classes provide several basic functions

I “Int”, “String” and “Bool” may not be inherited from

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 17: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I every class is a type

I the basic types are the classes “Object”, “IO”, “Int”, “String”and “Bool”

I all classes but Object have to be inherited from exactly oneother class (be a child of this class)

I every class that does not specify a class to inherit from isinherited from Object

I the “is child of”-relation has to be a tree, rooted at Object

I basic classes provide several basic functions

I “Int”, “String” and “Bool” may not be inherited from

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 18: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I every class is a type

I the basic types are the classes “Object”, “IO”, “Int”, “String”and “Bool”

I all classes but Object have to be inherited from exactly oneother class (be a child of this class)

I every class that does not specify a class to inherit from isinherited from Object

I the “is child of”-relation has to be a tree, rooted at Object

I basic classes provide several basic functions

I “Int”, “String” and “Bool” may not be inherited from

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 19: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I every class is a type

I the basic types are the classes “Object”, “IO”, “Int”, “String”and “Bool”

I all classes but Object have to be inherited from exactly oneother class (be a child of this class)

I every class that does not specify a class to inherit from isinherited from Object

I the “is child of”-relation has to be a tree, rooted at Object

I basic classes provide several basic functions

I “Int”, “String” and “Bool” may not be inherited from

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 20: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I every class is a type

I the basic types are the classes “Object”, “IO”, “Int”, “String”and “Bool”

I all classes but Object have to be inherited from exactly oneother class (be a child of this class)

I every class that does not specify a class to inherit from isinherited from Object

I the “is child of”-relation has to be a tree, rooted at Object

I basic classes provide several basic functions

I “Int”, “String” and “Bool” may not be inherited from

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 21: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I every class is a type

I the basic types are the classes “Object”, “IO”, “Int”, “String”and “Bool”

I all classes but Object have to be inherited from exactly oneother class (be a child of this class)

I every class that does not specify a class to inherit from isinherited from Object

I the “is child of”-relation has to be a tree, rooted at Object

I basic classes provide several basic functions

I “Int”, “String” and “Bool” may not be inherited from

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 22: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I every class is a type

I the basic types are the classes “Object”, “IO”, “Int”, “String”and “Bool”

I all classes but Object have to be inherited from exactly oneother class (be a child of this class)

I every class that does not specify a class to inherit from isinherited from Object

I the “is child of”-relation has to be a tree, rooted at Object

I basic classes provide several basic functions

I “Int”, “String” and “Bool” may not be inherited from

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 23: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I every class is a type

I the basic types are the classes “Object”, “IO”, “Int”, “String”and “Bool”

I all classes but Object have to be inherited from exactly oneother class (be a child of this class)

I every class that does not specify a class to inherit from isinherited from Object

I the “is child of”-relation has to be a tree, rooted at Object

I basic classes provide several basic functions

I “Int”, “String” and “Bool” may not be inherited from

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 24: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I Int-constants: 5, 3, 221, . . .

I String-constants: ”Hello World!”, ”\t Hi\n”,. . .

I Bool-constants: true, false

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 25: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I Int-constants: 5, 3, 221, . . .

I String-constants: ”Hello World!”, ”\t Hi\n”,. . .

I Bool-constants: true, false

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 26: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I Int-constants: 5, 3, 221, . . .

I String-constants: ”Hello World!”, ”\t Hi\n”,. . .

I Bool-constants: true, false

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 27: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

What are Types in COOL?

I Int-constants: 5, 3, 221, . . .

I String-constants: ”Hello World!”, ”\t Hi\n”,. . .

I Bool-constants: true, false

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 28: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

Simple Example

class Main {main() : Int { 0 };

};

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 29: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

IO-Example

class Main {myIO : IO < − new IO;myInput : Int;

main() : Int { {myIO.out string(’’How many? ’’);myInput < − myIO.in int();while 0 < myInput loop

myIO.out string(’’Hello world!’’)pool;0;

}};};

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 30: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

Sally

class Silly {f() : Int { 5 };

};

class Sally inherits Silly { };

class Main {x : Int < − (new Sally).f();

main() : Int { x };};

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 31: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

Inheritance

I all features are inherited

I no attribute may be redefined

I methods may be overridden, if the number and all types ofthe formal parameters match, as well as the return type

I static dispatch possible with< object > @ < type > . < methodName > (. . .)

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 32: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

Inheritance

I all features are inherited

I no attribute may be redefined

I methods may be overridden, if the number and all types ofthe formal parameters match, as well as the return type

I static dispatch possible with< object > @ < type > . < methodName > (. . .)

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 33: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

Inheritance

I all features are inherited

I no attribute may be redefined

I methods may be overridden, if the number and all types ofthe formal parameters match, as well as the return type

I static dispatch possible with< object > @ < type > . < methodName > (. . .)

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 34: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

Inheritance

I all features are inherited

I no attribute may be redefined

I methods may be overridden, if the number and all types ofthe formal parameters match, as well as the return type

I static dispatch possible with< object > @ < type > . < methodName > (. . .)

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 35: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

Inheritance

I all features are inherited

I no attribute may be redefined

I methods may be overridden, if the number and all types ofthe formal parameters match, as well as the return type

I static dispatch possible with< object > @ < type > . < methodName > (. . .)

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 36: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

Sally, revised

class Silly { f() : Int { 5 }; };

class Sally inherits Silly {f() : Int { 7 }; };

class Main {mySally : Sally < − new Sally;

main() : Int {mySally.f()

};

alternative() : Int {[email protected]()

};};

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 37: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

The COOL-Manual

Read the cool-manual on if-then-else, case-statements, let,arithmetic operations and so forth.The cool-manual will be your main reference when working on anyof the phases of your cool-compiler.

Timo Kotzing CISC 672 – Advanced Compiler Construction

Page 38: CISC 672 -- Advanced Compiler Constructioncavazos/cisc672-fall08/lectures/CoolOverview.pdf · For the purpose of writing your own cool-compiler, please read the cool-manual carefully

The COOL-Manual

Read the cool-manual on if-then-else, case-statements, let,arithmetic operations and so forth.The cool-manual will be your main reference when working on anyof the phases of your cool-compiler.

Timo Kotzing CISC 672 – Advanced Compiler Construction