home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Unsorted BBS Collection
/
thegreatunsorted.tar
/
thegreatunsorted
/
programming
/
misc_programming
/
sather.asc
< prev
next >
Wrap
Text File
|
1993-09-17
|
2KB
|
114 lines
_THE SATHER PROGRAMMING LANGUAGE_
by Stephen M. Omohundro
[LISTING ONE]
class STACK{T} is
-- Stacks of elements of type T.
attr s:ARR{T}; -- An array containing the elements.
attr size:INT; -- The current insertion location.
is_empty:BOOL is
-- True if the stack is empty.
res := (s=void or size=0) end;
pop:T is
-- Return the top element and remove it. Void if empty.
if is_empty then res:=void
else size:=size-1; res:=s[size]; s[size]:=void end end;
push(T) is
-- Push arg onto the stack.
if s=void then s:=#ARR{T}(asize:=5)
elsif size=s.asize then double_size end;
s[size]:=arg; size:=size+1 end;
private double_size is
-- Double the size of `s'.
ns::=#ARR{T}(asize:=2*s.asize); ns.copy_from(s); s:=ns end;
clear is
-- Empty the stack.
size:=0; s.clear end
end; -- class STACK{T}
class FOO is
bar is
s1:STACK{CHAR}; s1.push('a');
s2:STACK{STR}; s2.push("This is a string.") end;
end;
[LISTING TWO]
abstract class $POLYGON is
...
number_of_vertices:INT;
end;
class TRIANGLE is
inherit $POLYGON;
...
number_of_vertices:INT is res:=3 end;
end;
class SQUARE is
inherit $POLYGON;
...
number_of_vertices:INT is res:=4 end;
end;
class FOO2 is
bar2 is
s:STACK{$POLYGON};
...
n:=s.pop.number_of_vertices;
...
end;
end;
[LISTING THREE]
>5+7
12
>40.intinf.factorial
815915283247897734345611269596115894272000000000
>#OUT + "Hello world!"
Hello world!
>v::=#VEC(1.0,2.0,3.0); w::=#VEC(1.0,2.0,3.0);
>v+w
#VEC(2.0, 4.0, 6.0)
>v.dot(w)
14.0
>#ARRAY{STR}("grape", "cherry", "apple", "plum", "orange").sort
#ARRAY{STR}("apple","cherry","grape","orange","plum")
[LISTING FOUR]
>loop #OUT+1.upto!(9) end
123456789
>a::=#ARRAY{INT}(asize:=10)
>loop a.set_elts!(1.upto!(10)) end
>a
#ARRAY{INT}(1,2,3,4,5,6,7,8,9,10)
>loop a.set_elts!(2*a.elts!) end
>a
#ARRAY{INT}(2,4,6,8,10,12,14,16,18,20)