Course Project - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Main points of this lecture are: Course Project, Oriented Language, Implementation of Modern, Abstraction, Static Typing, Memory Management, Class Definitions, Class Main, Special Method Main, Collection of Attributes

Typology: Slides

2012/2013

Uploaded on 04/25/2013

rajnikanth
rajnikanth 🇮🇳

4.3

(32)

135 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture Outline
Cool
The Course Project
Programming Assignment 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Course Project - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Lecture Outline

• Cool

• The Course Project

• Programming Assignment 1

Cool Overview

• Classroom Object Oriented Language

• Designed to

  • Be implementable in one semester
  • Give a taste of implementation of modern
    • Abstraction
    • Static typing
    • Reuse (inheritance)
    • Memory management
    • And more …

• But many things are left out

Cool Objects

• An object can be thought of as a record

with a slot for each attribute

class Point { x : Int0; y : Int; ( use default value ) };

x y

• The expression “new Point” creates a new

object of class Point

Methods

  • Methods can refer to the current object using self

class Point { x : Int0; y : Int0; movePoint(newx : Int, newy : Int): Point { { xnewx; ynewy; self; } -- close block expression }; -- close method }; -- close class

  • A class can also define methods for manipulating the attributes

Methods

  • Each object knows how to access the code of a method
  • 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

x y 0 0

movePoint

x y 0 0

methods movePoint

Inheritance

• We can extend points to colored points using

subclassing => class hierarchy

class ColorPoint inherits Point { color : Int0; movePoint(newx : Int, newy : Int): Point { { color0; xnewx; ynewy; self; } }; }; x y 0 0

color 0

movePoint

Cool Type Checking

• Is well typed if P is an ancestor of C in the

class hierarchy

  • Anywhere an P is expected a C can be used

• Type safety:

  • A well-typed program cannot result in runtime type errors

x : P; xnew C;

Method Invocation and Inheritance

• Methods are invoked by dispatch

• Understanding dispatch in the presence of

inheritance is a subtle aspect of OO languages

p : Point; pnew ColorPoint; p.movePoint(1,2);

 p has static type Point  p has dynamic type ColorPoint  p.movePoint must invoke the ColorPoint version

Other Expressions

• Expression language (every expression has a

type and a value)

  • Conditionals if E then E else E fi
  • Loops: while E loop E pool
  • 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,…

Cool 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

Programming Assignment I

• Write an interpreter for a stack machine …

• … in Cool

• Due in 2 weeks

• Must be completed individually

Homework for Next Week

• Work on Programming Assignment I

• Read Chapters 1-2 of Textbook

• Continue learning Flex/Jlex