[Oberon] CASE without ELSE
joerg.straube at iaeth.ch
Thu Feb 15 22:26:34 CET 2018
Based on Chris‘ example you could write
CASE String.Pos(ch, "ILTQX", 0) OF
-1: (* else case *)
| 0: (* inspect *)
| 1: (* load *)
| 2: (* transfer *)
| 3: (* quit *)
Am 15.02.2018 um 21:51 schrieb Chris Burrows <chris at cfbsoftware.com>:
>> -----Original Message-----
>> From: Oberon [mailto:oberon-bounces at lists.inf.ethz.ch] On Behalf Of
>> Josef Templ
>> Sent: Friday, 16 February 2018 12:58 AM
>> To: oberon at lists.inf.ethz.ch
>> Subject: [Oberon] CASE without ELSE
>> Hi everybody,
>> recently I noticed that there is no ELSE clause in the CASE statement
>> of the latest Oberon language version.
>> Has this been removed intentionally or accidentally? Or is there
>> something new instead of it?
> As far as I know it was removed intentionally. The original programmer has to explicitly handle all cases. This helps a subsequent maintainer of the software to understand the programmers intentions. He does not have to try and guess whether an overlooked case was accidental or not.
>> If it has been removed intentionally:
>> Is there a significant simplification in the compiler or a speedup at
> Not if the CASE statement is efficiently implemented as a jump table (for runtime speed) - assuming the compiler traps missing cases as runtime errors.
>> On the other side, it puts some burden onto the programmer to work
>> around it.
>> As an example: I tried to implement a simple command interpreter that
>> reads a character
>> from a serial line (connected to a keyboard) and executes a related
>> procedure in an embedded target.
>> The natural language construct, I thought, would be a CASE for that
>> kind of problem.
>> Pressing an invalid key could be ignored easily by the ELSE clause of
>> the CASE.
> Suppose you had 5 commands "I", "L", "T", "Q", and "X". In the Astrobe RISC and Cortex-M Oberon compilers on of the simplest (but not the most elegant or efficient) way to do this would be something like
> IF String.Pos(ch, "ILTQX", 0) < 0 THEN
> (* Process error *)
> CASE ch OF
> "L": ....
> Ideally you would map your command characters to symbolic constants at the earliest opportunity and then use those in your logic. Something like this:
> Inspect = 0;
> Load = 1;
> Transfer = 2;
> Exit = 3;
> Invalid = 4;
> Command: INTEGER;
> PROCEDURE ToCommand(ch: CHAR): INTEGER;
> (* Use an array map or a hash table to map your characters to command values *)
> CASE command OF
> Inspect: ... |
> Invalid: (* Process error *)
> Chris Burrows
> CFB Software
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
More information about the Oberon