Language Design and Overview of COOL: Instructor: Fredrik Kjolstad Slides Based On Slides Designed by Prof. Alex Aiken
Language Design and Overview of COOL: Instructor: Fredrik Kjolstad Slides Based On Slides Designed by Prof. Alex Aiken
and
Overview of COOL
CS143
Lecture 2
• Project 50%
– 1–2 10% each
– 3–4 15% each
• Midterm 20%
• Final 20%
2
Lecture Outline
3
Programming Language Economics 101
4
Why So Many Languages?
5
Topic: Language Design
6
Language Evaluation Criteria
Features Criteria
Readability Writeability Reliability
Data types
Abstraction
Type checking
Exception handling
7
History of Ideas: Abstraction
• Modes of abstraction
– Via languages/compilers:
• Higher-level code, few machine dependencies
– Via functions and subroutines
• Abstract interface to behavior
– Via modules
• Export interfaces; hide implementation
– Via classes or abstract data types 8
• Bundle data with its operations
History of Ideas: Types
• More recently
– Lots of interest in types
– Experiments with various forms of parameterization
– Best developed in functional programming
9
History of Ideas: Reuse
• Inheritance allows
– Specialization of existing abstraction
– Extension, modification, and hidden behavior
10
Trends
• Language design
– Many new special-purpose languages
– Popular languages stick around (perhaps forever)
• Fortran and Cobol
• Compilers
– Ever more needed and ever more complex
– Driven by increasing gap between
• new languages
• new architectures
– Venerable and healthy area
11
Why Study Languages and Compilers ?
• Designed to
– Be implementable in a short time
– Give a taste of implementation of modern
• Abstraction
• Static typing
• Reuse (inheritance)
• Memory management
• And more …
class Point {
x : Int ¬ 0;
y : Int ¬ 0;
};
• Cool programs are sets of class definitions
– A special class Main with a special method main
– All Cool code lives inside classes
14
Cool Objects
class Point {
x : Int ¬ 0;
y : Int; (* use default value *)
};
• The expression “new Point” creates a new
object of class Point
• An object can be thought of as a record
with a slot for each attribute
x y
0 0
15
Methods
class Point {
...
x () : Int { x };
setx (newx : Int) : Int { x ¬ newx };
};
17
Methods
x y movePoint
0 0 *
x y methods
0 0
movePoint
*
18
Inheritance
20
Cool Type Checking
x : A;
x ¬ new B;
• Is well typed if A is an ancestor of B in the
class hierarchy
– Anywhere an A is expected a B can be used
• Type safety:
– A well-typed program cannot result in runtime type
errors
21
Method Invocation and Inheritance
22
Method Invocation
23
Other 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, exceptions, …
24
Cool Memory Management
25
Course Project
• A complete compiler
– Cool ==> MIPS assembly language
– No optimizations
• Individual or team
– max. 2 students
26