[Oberon] string manipulation

Douglas G. Danforth danforth at greenwoodfarm.com
Wed Jun 23 01:10:15 MEST 2010


Denis,
You can only return pointers from PROCEDUREs
(or primitive data types, such as INTEGER, REAL, etc.).
RECORDs and ARRAYs are not allowed.  You need to think
about memory allocation to appreciate that.

If one implements
    TYPE
         String = POINTER TO ARRAY OF CHAR;
        Text = RECORD string: String END;
then one can crate a method for Text to output its string
    PROCEDURE (VAR text: Text) Out, NEW;
    BEGIN
       Out.String(text.string)
    END Out;
and also a way to set the string
    PROCEDURE (VAR text: Text) Set (IN x: ARRAY OF CHAR), NEW;
    BEGIN
       NEW(text.string, 1+LEN(x$));
       text.string^:=x$;
     END Set;
and then do
    PROCEDURE Do*;  
        VAR   text: Text;
    BEGIN
       text.Set("Hello, World!");
       text.Out;
       Out.Ln
    END Do;

N.B. Component Pascal (a superset of Oberon-2) needs the "NEW" attribute 
on a method.
I don't remember whether Oberon-2 requires this or not.
Other than that the two languages are the same for this example.

-Doug Danforth

spir wrote:
> Thank, this makes things clearer. Now, since I'm a bit blocked in low-level details, I would be glad to watch an implementation of a string *type* (similar to those in most Pascal variants, for instance) in any Oberon implementation.
> In fact, the issue is that even if I can build a string type to manipulate strings, it is unusable in practice since no builtin function/procedure can use it, eg:
> 	Out.String(aText);
> I thus need an additional method to return the char sequence itself:
> 	Out.String(aText.chars());
> But such a method cannot exist since it would have to return an ARRAY OF CHAR. ;-) the impression of beeing lost inside a vicious circle.
>
>
> Side-questions (playing the devil's advocate):
> * Rationale to forbid using open arrays as variables or return values?
> * Why no dynamic array in Oberon?
> * Or even true flexible arrays like the ones data types like lists are commonly based on?
>
>
> Denis


More information about the Oberon mailing list