[Oberon] Bit-fiddling: SETs and type casts in Oberon-07
thutt at harp-project.com
thutt at harp-project.com
Tue Aug 9 05:09:54 CEST 2022
Hans Klaver writes:
> 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).
EXCL is a built-in procedure. It doesn't return a value, but updates
the first argument directly. So, because the compiler must store the
result, the first argument must be a variable.
You can do what you want using the set operators in an expression.
--
Pickled beets. Pickled beaks. What's the difference?
More information about the Oberon
mailing list