Skip to content

Instantly share code, notes, and snippets.

@kernigh
Created December 10, 2016 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kernigh/add79662bb3c63ffb7c46d01dc8ae788 to your computer and use it in GitHub Desktop.
Save kernigh/add79662bb3c63ffb7c46d01dc8ae788 to your computer and use it in GitHub Desktop.
test for Modula-2 set operations in Amsterdam Compiler Kit
MODULE SetDemo;
FROM InOut IMPORT WriteLn, WriteString;
PROCEDURE Puts(s: ARRAY OF CHAR);
BEGIN
WriteString(s);
WriteLn;
END Puts;
TYPE
Num = [1..100];
NumSet = SET OF Num;
VAR
(* VAR, not CONST, to defeat constant folding. *)
primes, teens, lowevens, eighties, nineties: NumSet;
CONST
primeteen = NumSet{13, 17, 19};
compeighties = NumSet{80..82, 84..88};
teenxoreven = NumSet{2, 4, 6, 8, 10, 12, 13, 15, 17, 19, 20};
eightiesnineties = NumSet{80..99};
PROCEDURE Check(set: NumSet; what: INTEGER);
BEGIN
(*
* The compiler uses cms in EM to check set equality. For each
* a # b below, the compiler emits
* ... teq ... cms cmu
* Each cms must pop both sets from the EM stack.
*)
IF (what = 1) # (set = primeteen) THEN
Puts("@@FAIL 1");
END;
IF (what = 2) # (set = compeighties) THEN
Puts("@@FAIL 2");
END;
IF (what = 3) # (set = teenxoreven) THEN
Puts("@@FAIL 3");
END;
IF (what = 4) # (set = eightiesnineties) THEN
Puts("@@FAIL 4");
END;
END Check;
PROCEDURE Range(min: Num; max: Num): NumSet;
BEGIN
(* The compiler calls LtoUset in lang/m2/libm2/LtoUset.e *)
RETURN NumSet{min..max}
END Range;
BEGIN
primes := NumSet{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
teens := NumSet{13, 14, 15, 16, 17, 18, 19};
lowevens := NumSet{2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
eighties := Range(80, 89);
nineties := Range(90, 99);
Check(primes * teens, 1);
Check(eighties - primes, 2);
Check(teens / lowevens, 3);
Check(eighties + nineties, 4);
Puts("@@FINISHED");
END SetDemo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment