34
Eiffel Programming Language

Eiffel Programming Language. Chad Frommeyer CSC 407/507 Fall 2005 Dr. Richard Fox

Embed Size (px)

Citation preview

Eiffel Programming Language

Chad Frommeyer

CSC 407/507

Fall 2005

Dr. Richard Fox

History

• 1985 Language Development Began• Developed by Bertrand Meyer of Interactive Software Engineering • 1986 First Compiler Delivered• 1987 Commercial Applications Developed• 1991 NICE Founded (Non-Profit International Consortium for Eiffel)• NICE controls evolution, standardization and basic class definitions

for Eiffel• 1996 Commercial Eiffel based development system becomes

available (iss-base).• Decendent of Algol and ADA

Overview• Pure object oriented language• Strong statically typed• Automatic memory management/Garbage collection• Supports ADTs• Supports Generic Classes• Supports By Value and By reference semantics• Promotes simplicity (Readable, not Writeable)• Non-Object Oriented Capabilities/Low Level Functionality (C-Lang)• Some Compilers only generate C source code• Design By Contract (Reliability)• Exception Handling• Supports separate library compilation• Supports In-Class Documentation• Root class starts execution

Overview (Continued)

• Statement delimiter (;) allowed, not required, typically used in muti-statement line

• Is not case sensitive• Doesn’t support global variables• Doesn’t support union types• Doesn’t support goto instructions• Doesn’t contain side-effect expression operators• Doesn’t support pointers or pointer arithmetic

Hello Worldindexing description: “Basic Hello World author : “Jim Developer” class HELLO_WORLDcreation makefeature make is do io.put_string ("Hello, world!%N") endend

Basic Instructions

• Assignment• Object Creation• Routine Call• Conditional• Iteration• Choice/Switch/Case

Assignment

• i := i + 1

• Assignments cannot happen directly to class attributes. Methods/Routines must be used.

• Shortcut Assignments don’t exist

Object Creation

• Declaration acc:ACCOUNT• acc is “void” when declared• At runtime acc obtains an object by calling• create acc• Which creates a new instance of ACCOUNT and attaches acc• acc can now be used:• acc.open(“John”)• acc.deposit(5000)

Data Types

• The following data types exist, the initialization of variables of these types is automatically initialized

• INTEGER initializes to zero• REAL and DOUBLE initialize to 0.0• BOOLEAN initializes to false• CHARACTER initializes to null• Reference types initialize to a null reference• INTEGER, REAL, DOUBLE, BOOLEAN and

CHARACTER are all considered Expanded types

Reference Types

• A reference is the default object type, when an instance of a class is created it is called a reference, this is created by either calling “create” or using “!!”

Expanded Types

• An expanded type is one that is already initialized• INTEGER, REAL, DOUBLE, BOOLEAN and

CHARACTER are all considered Expanded types• Expanded types don’t require calling “create” or “!!”• If the class is not defined with the keyword “expanded”

then it will be a reference type.

expanded class INTEGER feature ... end -- INTEGER

Routine/Function Callacc.open ("Jill")

acc.deposit (5000)

if acc.may_withdraw(3000) then

acc.withdraw(3000);print(acc.balance)

end

Conditionalif x > 10 then ... statements ... elseif x > 5 then ... elsif statements ... elseif x > 0 then ... more elsif statements ... else ... else statements ... end

Looping/Iteration• Only one looping statement exists, but it could be used to emulate

other types of loops.

For Loop

from i := 1 until i = 10 loop io.put_int (i); io.new_line; i := i + 1; end

While Loop

from node := first; until node = Void; loop io.put_string (node.text); node := node.next; end;

Repeat Loop

from done := False until done and pages = 10; loop done := True; printer.output_next; pages := pages + 1; end

Choice/Switch/Case

State : INTEGER;State_1, State_2, State_3 : INTEGER is unique;... statements ...inspect State when State_1 then ... statements ...when State_2 then ... statements ...when State_3 then ... statements ...else ... statements ...end;

• The inspected value must be enumerable

Generic Classesclass STACK [T] feature push( element : T ) is do storage( pos ) := element; pos := pos + 1; end -- pushend -- STACK

class LUNCH_COUNTER feature tray_stack : STACK[ TRAY ]; napkin_dispenser : STACK[ NAPKIN ] ... init is do ... napkin_dispenser.pop( end -- initend -- LUNCH_COUNTER

Creation Routines/Constructors

class CONVERTERcreation make;feature temperature : DOUBLE make is do temperature := 98.6; end -- make ... end -- CONVERTER

• A creation routine is called when the reference type is created with “create” or “!!”

Exception Handling

• Exceptions can be thrown in few cases:

• Design by contract failures

• Low level functions

• Rescue (Catch)

• Retry (can only exist within a rescue clause)

• Retry starts execution of the routine again without the variable initialization

Exception Handling (Cont.)read_next_character (f: FILE) is -- Make next character available in last_character ; -- if impossible, set failed to True. require readable: file . readable local impossible: BOOLEAN do if impossible then failed := True else last_character := low_level_read_function ( f ) end rescue impossible := True retry end

Design By Contract

• Facilitates more reliable techniques• Defines correct usage• Assertions (Boolean Expression):• Precondition (require)• Postcondition (ensure)• Class invariants (invariant)• Class invariant must be satisfied upon exit

of the creation procedure (constructor)

Assertion Monitoring

• Assertion monitoring levels are set at compile time

• The levels vary from no monitoring to monitoring all require/ensure/invariant

• Exceptions will be thrown if assertions are being monitored

• If the exception isn’t handled, a run-time error will result

Sample DBCindexing description: "Simple bank accounts" class ACCOUNT feature -- Access balance: INTEGER -- Current balance deposit_count: INTEGER is -- Number of deposits made since opening do … As before … end feature -- Element change deposit (sum: INTEGER) is -- Add sum to account. require non_negative: sum >= 0 do … As before … ensure one_more_deposit: deposit_count = old deposit_count + 1 updated: balance = old balance + sum end

Sample DBC (Cont.)feature { NONE } -- Implementation all_deposits: DEPOSIT_LIST invariant consistent_balance: (all_deposits /= Void) implies (balance = all_deposits . total) zero_if_no_deposits: (all_deposits = Void) implies (balance = 0) end -- class ACCOUNT

Object Oriented

• Purely Object Oriented

• Everything is an object

• Information Hiding

• Encapsulation

• Polymorphism/Dynamic Binding

• Inheritance (Including Multiple)

Object Oriented (Cont.)

• Supports information hiding/encapsulation

• No attributes of a class are allowed to be modified directly. All modifications must happen within a subroutine

• No global variables are allowed

• No static functions are allowed

• Secret attributes/routines are equivalent to private variables/methods

Polymorphism

• Dynamic binding allows polymorphic object to call the appropriate versioned routine

acc : ACCOUNT ; sav : SAVINGS_ACCOUNT acc := sav acc.deposit( 1000 )

Inheritance

• Inheritance is supported

• Multiple inheritance is supported

• Routine can be overridden (redefine)

• Parent is known as a precursor

• Keyword precursor can be used to reference the parent

• Renaming allows conflicts with multiple inheritance to be resolved

Inheritance (Cont.)

indexing description: "Savings accounts" class SAVINGS_ACCOUNT inherit ACCOUNT redefine deposit end feature -- Element change deposit ( sum : INTEGER ) is -- Add sum to account. do precursor (sum) … end … Other features … end -- class SAVINGS_ACCOUNT

Inheritance (Cont.)

• The implementation of inheritance supplies many keywords to define how the parents members are recognized

• Rename allows renaming of a parent attribute/routine

• Export allows changing protection of a parent attribute/routine

• Redefine allows overriding a routine• Undefine allows removing a routine

Inheritance (Cont.)class D inherit A rename g as f -- g was effective in A export {X, Y, …} feature1, feature2 undefine f end B undefine f end -- f was effective in B C -- C also has an effective feature f , which will serve as -- implementation for the result of the join. feature …

In-Class Documentation

• Keyword Indexing

• Indexing Items (author, description, other)

• Developer defined indexing items

• Developer tools exist to generate documentation

class ACCOUNT feature balance: INTEGER -- Attribute owner: PERSON minimum_balance: INTEGER is 1000 open (who: PERSON) is -- Routine -- Assign the account to owner who. do owner := who end deposit (sum: INTEGER) is -- Deposit sum into the account. do add (sum) end withdraw (sum: INTEGER) is -- Withdraw sum from the account. do add (-sum) end may_withdraw (sum: INTEGER): BOOLEAN is -- Function -- Is there enough money to withdraw sum? do Result := (balance >= sum + minimum_balance) –- Return value end feature {NONE} add (sum: INTEGER) is –- Private/Secret Routine -- Add sum to the balance. do balance := balance + sum end end -- class ACCOUNT

Bibliography

• http://en.wikipedia.org/wiki/Eiffel_programming_language

• http://archive.eiffel.com/doc/online/eiffel50/intro/language/

• http://www.eiffel.com/index.html

• http://www.comp.ufla.br/~monserrat/eiffel/advanced_introduction/eiffel.html