lboasso / oberonc

An Oberon-07 compiler for the JVM
MIT License
147 stars 17 forks source link

unexpected behavior in CASE ... OF #3

Closed luarockr closed 6 years ago

luarockr commented 6 years ago

when using expressions or calls returning INTEGER in CASE ... OF, compiler gives errors or raises exceptions. e.g.

MODULE CaseTest;

IMPORT Out;

VAR
  arr : ARRAY 3 OF INTEGER;
  i : INTEGER;

BEGIN
  FOR i := 0 TO 2 DO
    arr[i] := i;
  END;
  i := 1;
  CASE arr[i] OF
    1 : Out.String("1");
  END
END CaseTest.

gives CaseTest.Mod:12:10: inadmissible type CaseTest.Mod:13:10: undef CaseTest.Mod:13:11: not a record CaseTest.Mod:13:22: too many params CaseTest.Mod:15:13: compilation FAILED

MODULE CaseTest2;

IMPORT Out;

VAR
  arr : ARRAY 3 OF CHAR;

BEGIN
  arr[0] := 'a';
  arr[1] := 'b';
  arr[2] := 'c';
  i := 1;
  CASE ORD(arr[1]) OF
    97 : Out.String("a");
  END
END CaseTest2.

raises CaseTest2.Mod:9:15: undef CaseTest2.Mod:10:15: undef CaseTest2.Mod:11:15: undef CaseTest2.Mod:12:3: undef CaseTest2.Mod:13:10: OF expected Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at OJG.CaseOut(OJG.Mod:1930) at OJP.CasePart(OJP.Mod:759) at OJP.StatSequence(OJP.Mod:910) at OJP.Module(OJP.Mod:1356) at OJP.Compile(OJP.Mod:1384) at oberonc.Main(oberonc.Mod:41) at oberonc.main(oberonc.Mod)

lboasso commented 6 years ago

Thanks for finding this bug! I fixed it in fdc907cbd67c15ab0843ebe809d154daf3bb455a. The previous code allowed only an identifier (like a variable i) as condition in CASE statements, this was an intentional limitation, but the code was not handling properly unsupported cases like the ones in your examples. With my latest commit, now the compiler supports any integer expression for CASE conditions, your examples work now.