[Oberon] Bit-fiddling: SETs and type casts in Oberon-07
Hans Klaver
hklaver at dds.nl
Sat Aug 6 23:29:15 CEST 2022
Hi all,
While trying to make an Oberon-07 version of the classic predeclared function CAP I noticed some differences in the behaviour of the two compilers I use regularly.
One compiler can compile this:
PROCEDURE CAP1 (ch: CHAR): CHAR;
(*
Capitalizes ASCII lower-case letters while leaving capital letters unchanged.
Beware: digits and other characters *are* changed.
*)
VAR chSet: SET;
BEGIN
chSet := SYSTEM.VAL(SET, ch); (* cast CHAR to SET *)
EXCL(chSet, 5) (* clear bit 5 *)
RETURN SYSTEM.VAL(CHAR, chSet) (* cast SET back to CHAR *)
END CAP1;
The other compiler does not swallow the above version (casting of CHAR to SET not allowed), but does compile the following:
PROCEDURE CAP2 (ch: CHAR): CHAR;
(*
Same semantics as CAP1.
*)
VAR i: INTEGER;
BEGIN
i := SYSTEM.VAL(BYTE, ch); (* cast CHAR to BYTE and assign to INTEGER *)
EXCL(SYSTEM.VAL(SET, i), 5) (* clear bit 5 *)
RETURN SYSTEM.VAL(CHAR, i) (* cast INTEGER directly back to CHAR *)
END CAP2;
The interesting thing is that the first compiler compiles CAP1 but does not compile CAP2 (its EXCL procedure always needs a variable as first parameter).
It would be nice (imho) to have a compiler that combines the behaviours of the two, i.e. accepts a cast of CHAR to SET, and doesn't need a variable as the first parameter of EXCL. Then it would have no trouble with the next simplest version of CAP:
PROCEDURE CAP0 (ch: CHAR): CHAR;
(*
Neither compilers can cope with this one.
*)
BEGIN
EXCL(SYSTEM.VAL(SET, ch), 5) (* cast CHAR to SET and clear bit 5 *)
RETURN ch
END CAP0;
My questions are:
- the behaviour of which of the two above mentioned existing compilers do you like best?
- do you know an Oberon-07 compiler that can compile CAP0, and what do you think of it?
Regards,
Hans Klaver
More information about the Oberon
mailing list