[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Questions about behavior observed in gm2
From: |
Gaius Mulley |
Subject: |
Re: Questions about behavior observed in gm2 |
Date: |
Mon, 04 Dec 2023 08:33:10 +0000 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) |
Jack Perry <devotus@yahoo.com> writes:
> Hello!
>
> I used Modula-2 a lot 25-30 years ago, but haven't been able to use it
> since then until the happy advent of gm2 (13.2.1). I decided to get
> back into it by solving Advent of Code puzzles, and I've encountered
> some questions. I think some of these may be bugs, but since I'm using
> the online reference at modula2.org I may simply be misunderstanding
> what I see. (Some are due to my comparing it with Ada and Rust.)
>
> I would be very grateful for any insight people can give on these; I'm
> pretty sure they're easy to answer!
>
> 1. If I define the type `Constraints = [1..140]` and the variable
> `Idx: Constraints`, then if I increment `Idx` repeatedly until `Idx =
> MAX(Constraints) + 1`, should that be a run-time error? It is in Ada,
> but not in Modula-2 w/gm2. I can provide a fuller example if needed.
Hi Jack and welcome!
currently gm2 defaults to pim settings and does not enable runtime
checking. You can enable ISO and runtime via:
$ cat subrangeoverflow.mod
MODULE subrangeoverflow ;
TYPE
Constraints = [1..140] ;
VAR
c: Constraints ;
BEGIN
c := MIN (Constraints) ;
LOOP
INC (c)
END
END subrangeoverflow.
$ gm2 -fiso -fsoft-check-all subrangeoverflow.mod
./a.out
subrangeoverflow.mod:12:4: In program module subrangeoverflow
subrangeoverflow.mod:12:4:if the INC is ever executed the expression ‘1’ will
cause an overflow error for the designator ‘c’ as it exceeds the type range of
‘Constraints’
Aborted
which raises the question whether now would be a good time to change the
defaults from pim to iso and enable all runtime checking?
>
> 2. I can't seem to define a constant for a variant record type; that is, this
> fails to compile:
>
> TYPE TwoLocations = RECORD
> CASE Valid: BOOLEAN OF
> TRUE:
> First, Second: UsedLocation;
> | ELSE
> END;
> END;
>
> CONST
> InvalidAdjacencies = TwoLocations { FALSE };
>
> 3. If you neglect the parentheses on a function procedure that takes no
> parameters, gm2 treats it as if you want the address(?). Thus, the following
> line gives an unexpected answer:
>
> InOut.WriteInt(Part1, 0); (* needs to be Part1() *)
Interesting - and thanks for the bug report - I'll investigate the above
two issues
> 4. There's no way to obtain a subscript of an array by "slicing", is there?
> For example,
>
> VAR
> A: ARRAY [1..10] OF CHAR;
> BEGIN
> DoSomethignWith(A[3..5]);
As Benjamin mentions slicing isn't part of iso or pim m2 but is part of
m2r10. The gm2 libraries consist of the DynamicStrings module which (if
you choose to use the DynamicStrings.String data type) has the Slice
procedure function which behaves in much the same way as Python
thanks for the bug reports!
regards,
Gaius