[graybeards] Simple Simula Example?
Sergio Ruocco
sergio.ruocco at mlab.disco.unimib.it
Tue Oct 9 17:55:58 UTC 2001
Dan Ingalls wrote:
> Folks -
> I'm preparing a talk (for tomorrow ;-), and would really like to show the very simplest class description, such as a stack, and the couple of lines that would instantiate it, push 1, 2, 3, and then print three pops.
> While I'd like it as simple as possible, I'd like it to be complete with any necessary type declarations and, eg, how the array buffer gets allocated.
> Any help will be greatly appreciated. I thought it would be a snap to find some class notes on the Internet, but I failed in my first attempt.
> Thanks in advance
> - Dan
Here is an example taken from
"Programming Language Concepts"
Ghezzi - Jazaeri 2nd edition.
Wiley ISBN 0-471-82173-X
Chapter 4.5 Abstract Data Types (pp. 144-145); since there is a third edition of
this book, at least, page numbers may not coincide, or miss this section.
// these are my comments, taken from the text, to clarify the program
--------------------------
....
Suppose we want to define the abstraction "stack of elements"
[....]
We first describe the class of items that can be stacked.
class stack_member;
begin ref (stack_member) next_member,
next_member:- none
// :- is the reference assignement symbol and read "denotes"
end
This specifies that the only property shared by all stackable objects is the
existence of an attribute that is a reference to the next item in the stack.
[...]
class stack
begin
ref (stack_member) first;
ref (stack_member) procedure top;
top:- first;
procedure pop;
if -, empty then first :- first.next_member;
// -, (an half arrow?) means NOT
procedure push(e); ref (stack_member) e;
begin if first =/= none // =/= means NOT EQUAL
then e.next_member:- first;
first:- e
end push;
boolean procedure empty;
empty := first==none;
first:- none;
end stack
...we can now create stackable objects of a particualr type, for example,
comples, by prefixing class complex
stack_member class complex (...);
...
end complex
specifies that the objects generated by
new complex
have all the atributes of stack_member, as well as the atribute of complex.
In other words, thay are complex numbers that can be stacked.
If s is declared to be of type ref (stack), we can create a stack of complex
numbers by doing
s:- new stack;
Stack s courrently contains no elements, sto that s.empty return true.
If we have several complex objects, c1, c2, c3, we may insert them in the stack
in the following way.
s.push (c1);
s.push(c2);
s.push(c3).
We can look at the top element by doing s.top, which returns a reference; we can
remove the top element by s.pop.
....
---------------------------
I hope this helps
Regards,
Sergio
--
______________________________________________________________________
Sergio Ruocco CS PhD student mailto:sergio.ruocco at disco.unimib.it
Università Statale di Milano-Bicocca http://www.disco.unimib.it
DISCo - Software Architecture Lab. http://www.sal.disco.unimib.it
phone: ++39 02 6448 7874 cell: ++39 347 2519828
More information about the Squeak-dev
mailing list
|