[Oberon] Eliminating RETURN statements
Chris Burrows
chris at cfbsoftware.com
Wed Apr 8 07:29:56 CEST 2020
> -----Original Message-----
> From: Oberon [mailto:oberon-bounces at lists.inf.ethz.ch] On Behalf Of Hans
> Klaver
> Sent: Wednesday, 8 April 2020 9:00 AM
> To: oberon at lists.inf.ethz.ch
> Subject: [Oberon] Eliminating RETURN statements
>
> In my experience eliminating RETURN statements from function bodies to comply
> with Oberon-07 syntax is not entirely trivial.
>
> The loop needs an extra guard, and an Oberon FOR loop cannot do that, so we
> also need to transform the FOR loop into a WHILE loop:
>
> (* Oberon-07, right output *)
> PROCEDURE Fit (i, j: INTEGER): BOOLEAN;
> VAR k: INTEGER; OK: BOOLEAN;
> BEGIN
> k := 0; OK := TRUE;
> WHILE (k <= piecemax[i]) & OK DO
> IF p[i][k] THEN
> IF puzzl[j + k] THEN OK := FALSE END
> END;
> INC(k)
> END
> RETURN OK
> END Fit;
>
I prefer something that clarifies that there are actually two terminating conditions but a different result for each. For example, something like:
(* Untested *)
PROCEDURE Fit (i, j: INTEGER): BOOLEAN;
CONST
continue = 0;
failed = 1;
terminated = 2;
VAR k, status: INTEGER;
BEGIN
k := 0;
REPEAT
IF k > piecemax[i] THEN status := terminated
ELSIF p[i][k] & puzzl[j + k] THEN status := failed
ELSE INC(k); status := continue
END
UNTIL status # continue
RETURN status # failed
END Fit;
>
> Maybe the Oberon-07 compiler should give a *warning* and not an error message
> if a function procedure has more RETURN statements than one single RETURN at
> its end. Then legacy code could be ported more easily and more clearly to the
> latest version of the language.
>
Aargh! No, no, no! That reminds me of a question I once saw on a programming forum:
Q: How can I fix the following warnings when I compile my program ...
A: Add the directive (*$WARNINGS OFF*) to the top of your program.
:-)
Just spend a week or so trying to maintain an application with 100,000's of lines of Delphi code if you really want to understand the horrendous problems associated with code that has multiple exits from thousands procedures.
Regards,
Chris Burrows
CFB Software
https://www.astrobe.com
More information about the Oberon
mailing list