Está en la página 1de 5

Grade Weights

• Project 50%
– I, II 10% each
Language Design – III, IV 15% each
and
Overview of COOL • Midterm 15%

CS143 • Final 25%


Lecture 2
• Written Assignments 10%
– 2.5% each

Prof. Aiken CS 143 Lecture 2 1 Prof. Aiken CS 143 Lecture 2 2

Lecture Outline Programming Language Economics 101

• Today’s topic: language design • Languages are adopted to fill a void


• Why are there new languages? – Enable a previously difficult/impossible application
• Good-language criteria – Orthogonal to language design quality (almost)

• History of ideas:
– Abstraction • Programmer training is the dominant cost
– Types – Languages with many users are replaced rarely
– Reuse – Popular languages become ossified
• Cool – But easy to start in a new niche . . .

• The Course Project


Prof. Aiken CS 143 Lecture 2 3 Prof. Aiken CS 143 Lecture 2 4

Why So Many Languages? Topic: Language Design

• Application domains have distinctive and • No universally accepted metrics for design
conflicting needs
• Claim: “A good language is one people use”
• Examples:

Prof. Aiken CS 143 Lecture 2 5 Prof. Aiken CS 143 Lecture 2 6

1
Language Evaluation Criteria History of Ideas: Abstraction

Characteristic Criteria • Abstraction = detached from concrete details


Readability Writeability Reliability
Simplicity * * * • Abstraction necessary to build software systems
Data types * * *
• Modes of abstraction
Syntax design * * * – Via languages/compilers:
Abstraction * * • Higher-level code, few machine dependencies
– Via subroutines
Expressivity * * • Abstract interface to behavior
Type checking * – Via modules
• Export interfaces; hide implementation
Exception handling * – Via abstract data types
• Bundle data with its operations
Prof. Aiken CS 143 Lecture 2 7 Prof. Aiken CS 143 Lecture 2 8

History of Ideas: Types History of Ideas: Reuse

• Originally, few types • Reuse = exploit common patterns in software systems


– FORTRAN: scalars, arrays – Goal: mass-produced software components
– LISP: no static type distinctions – Reuse is difficult

• Realization: Types help • Two popular approaches


– Allow the programmer to express abstraction – Type parameterization (List(int), List(double))
– Allow the compiler to check against many frequent errors – Classes and inheritance: C++ derived classes
– Sometimes to the point that programs are guaranteed “safe” – Combined in C++, Java

• More recently • Inheritance allows


– Lots of interest in types – Specialization of existing abstraction
– Experiments with various forms of parameterization – Extension, modification, hiding behavior
– Best developed in functional programming
Prof. Aiken CS 143 Lecture 2 9 Prof. Aiken CS 143 Lecture 2 10

Trends Why Study Languages and Compilers ?

• Language design 5. Increase capacity of expression


– Many new special-purpose languages
– Popular languages to stay 4. Improve understanding of program behavior

• Compilers 3. Increase ability to learn new languages


– More needed and more complex
– Driven by increasing gap between 2. Learn to build a large and reliable system
• new languages
• new architectures
– Venerable and healthy area
1. See many basic CS concepts at work
Prof. Aiken CS 143 Lecture 2 11 Prof. Aiken CS 143 Lecture 2 12

2
Cool Overview A Simple Example

• Classroom Object Oriented Language class Point {


x : Int  0;
• Designed to y : Int  0;
– Be implementable in a short time };
– Give a taste of implementation of modern
• Cool programs are sets of class definitions
• Abstraction
– A special class Main with a special method main
• Static typing
• Reuse (inheritance) – No separate notion of subroutine
• Memory management
• And more … • class = a collection of attributes and methods
• Instances of a class are objects
• But many things are left out
Prof. Aiken CS 143 Lecture 2 13 Prof. Aiken CS 143 Lecture 2 14

Cool Objects Methods

class Point { • A class can also define methods for manipulating


x : Int  0; the attributes
y : Int; (* use default value *) class Point {
}; x : Int  0;
y : Int  0;
• The expression “new Point” creates a new movePoint(newx : Int, newy : Int): Point {
object of class Point { x  newx;
y  newy;
• An object can be thought of as a record self;
} -- close block expression
with a slot for each attribute }; -- close method
}; -- close class
x y
0 0 • Methods can refer to the current object using self
Prof. Aiken CS 143 Lecture 2 15 Prof. Aiken CS 143 Lecture 2 16

Information Hiding in Cool Methods

• Methods are global • Each object knows how to access the code of a
method
• Attributes are local to a class • As if the object contains a slot pointing to the code
– They can only be accessed by the class’s methods x y movePoint
0 0 *
• Example: • In reality implementations save space by sharing
class Point { these pointers among instances of the same class
. . .
x y methods
x () : Int { x }; 0 0
setx (newx : Int) : Int { x  newx }; movePoint
}; *
Prof. Aiken CS 143 Lecture 2 17 Prof. Aiken CS 143 Lecture 2 18

3
Inheritance Cool Types

• We can extend points to colored points using • Every class is a type


subclassing => class hierarchy • Base classes:
class ColorPoint inherits Point { – Int for integers
color : Int  0;
– Bool for boolean values: true, false
movePoint(newx : Int, newy : Int): Point {
{ color  0; – String for strings
x  newx; y  newy; – Object root of the class hierarchy
self;
} • All variables must be declared
};
}; x y color movePoint – compiler infers types for expressions
0 0 0 *
Prof. Aiken CS 143 Lecture 2 19 Prof. Aiken CS 143 Lecture 2 20

Cool Type Checking Method Invocation and Inheritance

x : A; • Methods are invoked by dispatch


x  new B;
• Understanding dispatch in the presence of inheritance
• Is well typed if A is an ancestor of B in the is a subtle aspect of OO languages
class hierarchy p : Point;
– Anywhere an A is expected a B can be used p  new ColorPoint;
p.movePoint(1,2);
• Type safety:  p has static type Point
– A well-typed program cannot result in runtime type  p has dynamic type ColorPoint
errors  p.movePoint must invoke the ColorPoint version

Prof. Aiken CS 143 Lecture 2 21 Prof. Aiken CS 143 Lecture 2 22

Method Invocation Other Expressions

• Example: invoke one-argument method m • Expression language


– every expression has a type and a value
e.m(e’)
1 4 – Loops: while E loop E pool
… – Conditionals if E then E else E fi
… 5 1. Eval. e
– Case statement case E of x : Type  E; … esac
2 2. Find class of e – Arithmetic, logical operations
5 3. Find code of m – Assignment x E
… 3 4. Eval. argum. – Primitive I/O out_string(s), in_string(), …
m: self 
… x  5. Bind self and x
method <method code> 6. Run method
• Missing features:
table 6 – arrays, floating point operations, exceptions, …

Prof. Aiken CS 143 Lecture 2 23 Prof. Aiken CS 143 Lecture 2 24

4
Cool Memory Management Course Project

• Memory is allocated every time new is invoked • A complete compiler


– Cool ==> MIPS assembly language
– No optimizations
• Memory is deallocated automatically when an
object is not reachable anymore • Split in 4 programming assignments (PAs)
– Done by the garbage collector (GC)
• There is adequate time to complete assignments
– There is a Cool GC – But start early and please follow directions

• Individual or team
– max. 2 students

Prof. Aiken CS 143 Lecture 2 25 Prof. Aiken CS 143 Lecture 2 26

También podría gustarte