Conditional Statements |
if cond
then stmt
end
|
|
if a<10
then b := 35
end
|
|
if cond
then stmt
else stmt
end
|
|
if n<=0
then res := 0
else res := sum / n
end
|
|
if cond
then stmt
elsif cond
then stmt
elsif ...
else stmt
end
|
|
if m.empty
then p := m.cons(x)
elsif x |
|
inspect [int-expr | char-expr]
when value,value
then stmt
when value..value
then stmt
else stmt
end
|
|
inspect percent // 10
when 1,2,3 then grade := 'F'
when 4..6 then grade := 'P'
else grade := 'D'
end
|
|
Loop Statements |
from stmt
until cond
loop stmt
end
|
|
from i:=1
until i>n
loop sum := sum + i
i := i + 1
end
|
|
from stmt
invariant cond
until cond
loop stmt
end
|
|
from log := 0
exp := 1
invariant power2(log)=exp
until exp >= n
loop
log := log + 1
exp := exp * 2
end
|
|
Routines (procedures and functions) |
name [parameters] is
do stmt
end
|
|
deposit( sum:INTEGER ) is
do
balance := balance + sum
end
|
|
name [parameters] : type is
do stmt
end
|
|
available(sum:INTEGER):BOOLEAN is
do
Result := (balance-sum)>=min
end
|
|
name [parameters] is
require cond
do stmt
ensure cond
end
|
|
withdraw( sum:INTEGER ) is
require available(sum)
do balance := balance - sum
ensure balance = old balance - sum
end
|
|
Attributes (variables and constants) |
name ,... : type
|
|
count,n : INTEGER
|
|
name ,... : like name
|
|
costs : like prices
|
|
name ,... : type is value
|
|
pi : REAL is 3.1415
|
|
name ,... : type is unique
|
|
yes,no,any : INTEGER is unique
|
|
name ,... : generic-type[type]
|
|
prices : LIST[ INTEGER ]
|
|
name ,... : expanded type
|
|
names : expanded ARRAY[ STRING ]
|
|
Simple Class |
class name
creation routine-name,...
feature
[attribute | routine]...
invariant cond
end
|
|
class HELLO
creation make
feature
m1 : STRING is "hello%N"
make is
do print(m1)
end
invariant m1.count = 5
end
|
|
Class with Nominated Clients |
class name
creation {class-name,...}
routine-name,...
feature (class-name,...}
[attribute | routine]...
feature (class-name,...}
[attribute | routine]...
invariant cond
end
|
|
class MARBLES
creation {PLAYER} make
feature {NONE}
make is
do count := 100
end
feature {ANY}
count : INTEGER
feature {PLAYER}
lose( n:INTEGER ) is
require n<=count
do count := count - n
end
win( n:INTEGER ) is
do count := count + n
end
invariant count >= 0
end
|
|
Object Creation Statements |
!!var
|
|
!!bookset
|
|
!!var.creation[ parameters ]
|
|
!!myvector.make(1,100)
|
|
!class!var
|
|
!BST_SET!bookset |
|
Abstract Classes |
deferred class name
feature {class-name,...}
[attribute
| routine
| deferred-routine]
invariant cond
end
|
|
deferred class SHAPE
feature {ANY}
visible : BOOLEAN
pos : COORD
feature {PICTURE,DRAWER}
shift(dist:COORD) is
deferred
end
scale(x:REAL)is
require x>0
deferred
end
end
|
|
Derived Classes |
class name
inherit class-name
feature {class-name}
[attribute | routine],...
end
|
|
class LINE
inherit SHAPE
creation {ANY} make
feature
other : COORD
shift(dist:COORD) is
do pos.add(dist)
other.add(dist)
end;
scale(factor:REAL) is
local p : COORD
do pos.mul(factor)
other.mul(factor)
end
end
|
|
class name
inherit
class
rename name as name,...
redefine name,...
end
class
rename name as name,...
redefine name,...
end
feature
[attribute | routine]
end
|
|
class VECTOR[T]
inherit
ARRAY[T] rename enter as store,
end
ARRAY[T] redefine enter
end
creation make
feature
enter( x:T; pos:INTEGER ) is
do grow( 1 )
from i := count until i=pos
loop store( item(i-1), i )
i := i - 1
end
store( x, pos )
end
end
|
|
Programming by Contract |
name [parameters] is
require cond
do stmt
ensure cond
rescue stmt
end
|
|
add( name:STRING; room:INTEGER ) is
require name /= Void
room > 0
do staff.put( room, name )
ensure location(name) = room
rescue -- maybe table full
purge_staff
if staff.capacity > staff.count
then retry
end
|
|
require failure leads to fail in client routine
ensure failure leads to fail in supplier routine
fail is caught by closest enclosing rescue
rescue may fail, or retry its containing routine
|
|
Documentation |
indexing
key: value value ...;
key: value value ...;
...
class name
...
end
|
|
indexing
description: "Store items by key";
keys: table, indexed, hash;
date: 23, July, 1994
class HASH_TABLE[ H, H->HASHABLE ]...
end
|
|
Indexing clauses define a useful format for header comments,
and can be used in the creation of source code processing tools.
|
|
name [parameters] is
obsolete "message"
require cond
do stmt
ensure cond
end
|
|
store( pos:INTEGER; val:T ) is
obsolete "use put(val,pos)"
require pos <= count
do area.put(val,pos-lower)
end
|
|
Obsolete clause is triggered at compile time if a client uses
the obsolete routine. Thus routines can be deleted in two
stages. |
|