raz / commit 2a226448

commit
2a226448ee9bf3ae23b7adfc5126ede476bb9b5c
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
8465197c
Step 6c: match on integers

MatchStatement takes an integer (an untyped constant as I64); IntArm
reads an arm's labels, each a constant or lo .. hi coerced to the
matched type, refusing an empty range and a value in two labels
(compared unsigned for an unsigned type), and gen's MatchRange tests
each: cmp/je for one value, sub lo + cmp hi - lo + jbe for a range.
No else arm: 'a match on an integer needs an else arm'.
examples/Sh.raz: special, readWord and next use match. AtMatch now
matches an F64; Resync's message follows. conform/run/MatchInt,
conform/err/MatchInt*, MatchReal.

patch · log

22 files changed, +258 -28

PLAN.md (history) modified +1 -1

@@ -125,7 +125,7 @@
125125 - [x] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only
126126 data, used as a variable that can't be changed (section 4).
127127 Done so that a string constant is the same mechanism.
128-- [ ] 6c. `match` on integers: constants and inclusive ranges as
128+- [x] 6c. `match` on integers: constants and inclusive ranges as
129129 labels, no value in two, `else` required (7.4); compares and
130130 branches, no jump table. `examples/Sh.raz`'s chains of `elsif`
131131 become `match`.

conform/err/AtMatch.raz (history) modified +2 -2

@@ -1,7 +1,7 @@
11 module AtMatch;
22 proc main*;
3-var x: I64;
3+var x: F64;
44 begin
5- match x + 1 do end;
5+ match x + 1.0 of end;
66 end;
77 end AtMatch.

conform/err/MatchIntEmpty.raz (history) added +9 -0

@@ -0,0 +1,9 @@
1+module MatchIntEmpty;
2+proc f(n: I64);
3+begin
4+ match n of
5+ | 5 .. 1: n := 0;
6+ | else:
7+ end;
8+end;
9+end MatchIntEmpty.

conform/err/MatchIntFit.raz (history) added +9 -0

@@ -0,0 +1,9 @@
1+module MatchIntFit;
2+proc f(n: U8);
3+begin
4+ match n of
5+ | 0 .. 256: n := 0;
6+ | else:
7+ end;
8+end;
9+end MatchIntFit.

conform/err/MatchIntNoElse.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module MatchIntNoElse;
2+proc f(n: I64);
3+begin
4+ match n of
5+ | 1: n := 0;
6+ end;
7+end;
8+end MatchIntNoElse.

conform/err/MatchIntTwice.raz (history) added +10 -0

@@ -0,0 +1,10 @@
1+module MatchIntTwice;
2+proc f(n: I64);
3+begin
4+ match n of
5+ | 1 .. 5: n := 0;
6+ | 7, 5: n := 1;
7+ | else:
8+ end;
9+end;
10+end MatchIntTwice.

conform/err/MatchIntVar.raz (history) added +9 -0

@@ -0,0 +1,9 @@
1+module MatchIntVar;
2+proc f(n, m: I64);
3+begin
4+ match n of
5+ | m: n := 0;
6+ | else:
7+ end;
8+end;
9+end MatchIntVar.

conform/err/MatchReal.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module MatchReal;
2+proc f(n: F64);
3+begin
4+ match n of
5+ | else:
6+ end;
7+end;
8+end MatchReal.

conform/run/MatchInt.expect (history) added +4 -0

@@ -0,0 +1,4 @@
1+32133120
2+-2 -2 -1 0 1 100 2 2
3+001223
4+seven

conform/run/MatchInt.raz (history) added +65 -0

@@ -0,0 +1,65 @@
1+module MatchInt;
2+import Out;
3+
4+const Max = 100;
5+
6+proc kind(ch: U8): I64;
7+var k: I64;
8+begin
9+ match ch of
10+ | ' ', '\t', '\n': k := 1;
11+ | '0' .. '9': k := 2;
12+ | 'a' .. 'z', 'A' .. 'Z', '_': k := 3;
13+ | else:
14+ end;
15+ return k;
16+end;
17+
18+proc signed(n: I32): I64;
19+begin
20+ match n of
21+ | -2147483648 .. -1000: return -2;
22+ | -999 .. -1: return -1;
23+ | 0: return 0;
24+ | Max: return 100;
25+ | Max + 1 .. 2147483647: return 2;
26+ | else: return 1;
27+ end;
28+end;
29+
30+proc big(n: U64): I64;
31+begin
32+ match n of
33+ | 0FFFFFFFFFFFFFFFFH: return 3;
34+ | 8000000000000000H .. 0FFFFFFFFFFFFFFFEH: return 2;
35+ | 0 .. 0FFFFFFFFH: return 0;
36+ | else: return 1;
37+ end;
38+end;
39+
40+proc main*;
41+var i: INT; s: []U8;
42+begin
43+ s := "a1 _Z\t9?";
44+ for i := 0 to LEN(s) - 1 do Out.int(kind(s[i])); end;
45+ Out.ln;
46+ Out.int(signed(-2147483648)); Out.str(" ");
47+ Out.int(signed(-1000)); Out.str(" ");
48+ Out.int(signed(-999)); Out.str(" ");
49+ Out.int(signed(0)); Out.str(" ");
50+ Out.int(signed(50)); Out.str(" ");
51+ Out.int(signed(100)); Out.str(" ");
52+ Out.int(signed(101)); Out.str(" ");
53+ Out.int(signed(2147483647)); Out.ln;
54+ Out.int(big(0)); Out.int(big(0FFFFFFFFH)); Out.int(big(100000000H));
55+ Out.int(big(8000000000000000H)); Out.int(big(0FFFFFFFFFFFFFFFEH));
56+ Out.int(big(0FFFFFFFFFFFFFFFFH)); Out.ln;
57+ match 7 of
58+ | 1 .. 6: Out.str("low");
59+ | 7: Out.str("seven");
60+ | else: Out.str("high");
61+ end;
62+ Out.ln;
63+end;
64+
65+end MatchInt.

doc/compiler.md (history) modified +4 -1

@@ -336,7 +336,10 @@
336336 Then statements: `Statement` makes each one a recovery point (below)
337337 and `OneStatement` dispatches; `IfStatement`,
338338 `WhileStatement`, `ForStatement` and `MatchStatement` emit their jumps
339-with `x64.pas`'s labels. `Defer` is worth reading: a deferred statement
339+with `x64.pas`'s labels. A `match` on an integer reads each arm's
340+labels in `IntArm`, which refuses a value in two of them, and tests them
341+one after another with `MatchRange`: compares and branches, no jump
342+table. `Defer` is worth reading: a deferred statement
340343 is compiled where it stands, as a little subroutine of the procedure
341344 that the code jumps over, and `RunDefers` emits a call to it at every
342345 way out of its statement list: the end, `return` and `break`.

examples/Sh.raz (history) modified +18 -14

@@ -57,8 +57,10 @@
5757
5858 proc special(c: U8): BOOL;
5959 begin
60- return (c = ' ') or (c = 9) or (c = 10) or (c = ';') or (c = '|') or (c = '&')
61- or (c = '<') or (c = '>');
60+ match c of
61+ | ' ', 9, 10, ';', '|', '&', '<', '>': return true;
62+ | else: return false;
63+ end;
6264 end;
6365
6466 (* $NAME or $? at s[at], its value added to b; a $ before anything else
@@ -86,11 +88,12 @@
8688 Buf.init(ADR(b), ADR(arena.base));
8789 while (at < LEN(s)) and not special(s[at]) do
8890 q := s[at];
89- if q = 39 then (* '...': every character as it is *)
91+ match q of
92+ | 39: (* '...': every character as it is *)
9093 INC(at);
9194 while (at < LEN(s)) and (s[at] # 39) do Buf.add(ADR(b), s[at]); INC(at); end;
9295 INC(at);
93- elsif q = '"' then (* "...": dollars, and \" and \\ *)
96+ | '"': (* "...": dollars, and \" and \\ *)
9497 INC(at);
9598 while (at < LEN(s)) and (s[at] # '"') do
9699 if s[at] = '$' then dollar(ADR(b));
@@ -101,9 +104,9 @@
101104 end;
102105 end;
103106 INC(at);
104- elsif q = '$' then
107+ | '$':
105108 dollar(ADR(b));
106- else
109+ | else:
107110 if (q = '\\') and (at + 1 < LEN(s)) then INC(at); end;
108111 Buf.add(ADR(b), s[at]);
109112 INC(at);
@@ -122,16 +125,17 @@
122125 if at >= LEN(s) then return eof; end;
123126 c := s[at];
124127 INC(at);
125- if (c = ';') or (c = 10) then return sep;
126- elsif c = '|' then return pipe;
127- elsif c = '&' then return amp;
128- elsif c = '<' then return from;
129- elsif c = '>' then
128+ match c of
129+ | ';', 10: return sep;
130+ | '|': return pipe;
131+ | '&': return amp;
132+ | '<': return from;
133+ | '>':
130134 if (at < LEN(s)) and (s[at] = '>') then INC(at); return onto; end;
131135 return into;
132- elsif (c = '2') and (at < LEN(s)) and (s[at] = '>') then
133- INC(at);
134- return errTo;
136+ | '2':
137+ if (at < LEN(s)) and (s[at] = '>') then INC(at); return errTo; end;
138+ | else:
135139 end;
136140 DEC(at);
137141 readWord;

src/gen.pas modified +19 -2

@@ -111,6 +111,7 @@
111111 procedure SetField(const x: TItem; f: PObj; var a: TItem);
112112 function MatchBegin(var x: TItem): int64;
113113 procedure MatchTag(p: int64; tag: int64; var L: longint; isEnum: boolean);
114+procedure MatchRange(p, lo, hi: int64; var body: longint);
114115 procedure CondJump(var x: TItem; var L: longint);
115116
116117 procedure SliceOf(var x: TItem; t: PType; len: int64);
@@ -1513,8 +1514,8 @@
15131514 function MatchBegin(var x: TItem): int64;
15141515 var p: int64;
15151516 begin
1516- if x.typ^.form = fEnum then exit(ToSlot(x));
1517- if (x.mode <> mVar) or (x.typ^.form <> fVariant) then MarkAt(x.pos, 'match needs a variant or an enumeration, found ' + What(x));
1517+ if x.typ^.form in [fEnum, fInt] then exit(ToSlot(x));
1518+ if (x.mode <> mVar) or (x.typ^.form <> fVariant) then MarkAt(x.pos, 'match needs a variant, an enumeration or an integer, found ' + What(x));
15181519 if x.ro then CopyToTemp(x); { its arms' names stand for a copy's fields }
15191520 Address(x, RAX);
15201521 p := AllocTemp(8, 8);
@@ -1528,6 +1529,22 @@
15281529 if not isEnum then Load(RAX, Mem(RAX, 0), 8, false);
15291530 CmpI(RAX, tag);
15301531 Jcc(ccNE, L);
1532+end;
1533+
1534+{ To body if the integer in slot p is from lo to hi: less its lo, it is
1535+ then at most hi - lo, compared unsigned, which holds for a signed type
1536+ or not, with no second jump. }
1537+procedure MatchRange(p, lo, hi: int64; var body: longint);
1538+begin
1539+ Load(RAX, Mem(RBP, p), 8, false);
1540+ if lo = hi then begin
1541+ CmpI(RAX, lo);
1542+ Jcc(ccE, body);
1543+ exit;
1544+ end;
1545+ if lo <> 0 then begin MovRI(RCX, lo); Alu(aluSub, RAX, RCX) end;
1546+ CmpI(RAX, hi - lo);
1547+ Jcc(ccBE, body);
15311548 end;
15321549
15331550 { The end of a round of a for loop: stop if the step would pass the

src/parse.pas modified +61 -2

@@ -1303,19 +1303,74 @@
13031303 FixLink(lp.brk);
13041304 end;
13051305
1306+type
1307+ TLabels = record
1308+ lo, hi: array[0..1023] of int64; { the ranges of the arms so far }
1309+ n: longint;
1310+ end;
1311+
1312+{ a < b, for values of an integer type, unsigned or not }
1313+function Below(a, b: int64; unsign: boolean): boolean;
1314+begin
1315+ if unsign then Below := qword(a) < qword(b) else Below := a < b;
1316+end;
1317+
1318+{ An arm of a match on an integer of type t, held in slot p: its labels,
1319+ each a constant or a range, none overlapping another (report section
1320+ 7.4), then its statements. }
1321+procedure IntArm(t: PType; p: int64; var ls: TLabels; var endL: longint);
1322+var lo, hi: TItem; L, body, i: longint; u: boolean; start: TPos;
1323+begin
1324+ u := not t^.signed;
1325+ body := NoLabel;
1326+ repeat
1327+ start := TokPos;
1328+ Expression(lo, t);
1329+ Coerce(lo, t);
1330+ ConstValue(lo);
1331+ hi := lo;
1332+ if tok = sUpto then begin
1333+ Next;
1334+ Expression(hi, t);
1335+ Coerce(hi, t);
1336+ ConstValue(hi);
1337+ if Below(hi.val, lo.val, u) then MarkAt(SpanFrom(start), 'the range is empty');
1338+ end;
1339+ for i := 0 to ls.n - 1 do
1340+ if not Below(hi.val, ls.lo[i], u) and not Below(ls.hi[i], lo.val, u) then
1341+ if lo.val = hi.val then MarkAt(SpanFrom(start), Source(SpanFrom(start)) + ' is in another label too')
1342+ else MarkAt(SpanFrom(start), Source(SpanFrom(start)) + ' overlaps another label');
1343+ if ls.n > High(ls.lo) then MarkAt(SpanFrom(start), 'more than ' + IntStr(High(ls.lo) + 1) + ' labels in one match');
1344+ ls.lo[ls.n] := lo.val; ls.hi[ls.n] := hi.val;
1345+ inc(ls.n);
1346+ MatchRange(p, lo.val, hi.val, body);
1347+ if tok <> sComma then break;
1348+ Next;
1349+ until false;
1350+ L := NoLabel;
1351+ Jmp(L);
1352+ FixLink(body);
1353+ Expect(sColon);
1354+ Statements;
1355+ Jmp(endL);
1356+ FixLink(L);
1357+end;
1358+
13061359 procedure MatchStatement;
13071360 var
13081361 x: TItem; t: PType; c, f, o: PObj; p: int64; L, endL, n: longint;
1309- seen: array[0..255] of boolean; hasElse: boolean; endAt: TPos;
1362+ seen: array[0..255] of boolean; hasElse: boolean; endAt: TPos; ls: TLabels;
13101363 begin
13111364 Next;
13121365 Expression(x, nil);
1366+ if x.typ^.form = fConst then Coerce(x, tI64);
13131367 p := MatchBegin(x);
13141368 t := x.typ;
13151369 Expect(sOf);
13161370 FillChar(seen, SizeOf(seen), 0);
13171371 hasElse := false;
13181372 endL := NoLabel;
1373+ ls.n := 0;
13191374 while tok = sBar do begin
13201375 Next;
13211376 if hasElse then Mark('else must be the last arm');
@@ -1324,7 +1379,9 @@
13241379 Expect(sColon);
13251380 Statements;
13261381 hasElse := true;
1327- end else begin
1382+ end else if t^.form = fInt then
1383+ IntArm(t, p, ls, endL)
1384+ else begin
13281385 if tok <> sIdent then Expected('a case');
13291386 c := t^.fields;
13301387 while (c <> nil) and (c^.name <> id) do c := c^.next;
@@ -1364,6 +1421,8 @@
13641421 end;
13651422 endAt := TokPos;
13661423 Expect(sEnd);
1424+ if (t^.form = fInt) and not hasElse then
1425+ MarkHelp(endAt, 'a match on an integer needs an else arm', 'it may be empty: | else:');
13671426 if not hasElse then begin
13681427 c := t^.fields;
13691428 while c <> nil do begin

test/msg/AtMatch.err (history) modified +3 -3

@@ -1,4 +1,4 @@
1-conform/err/AtMatch.raz:5:11: error: match needs a variant or an enumeration, found I64
2- 5 | match x + 1 do end;
3- | ^^^^^
1+conform/err/AtMatch.raz:5:11: error: match needs a variant, an enumeration or an integer, found F64
2+ 5 | match x + 1.0 of end;
3+ | ^^^^^^^
44 raz: 1 error

test/msg/MatchIntEmpty.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/MatchIntEmpty.raz:5:7: error: the range is empty
2+ 5 | | 5 .. 1: n := 0;
3+ | ^^^^^^
4+raz: 1 error

test/msg/MatchIntFit.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/MatchIntFit.raz:5:12: error: 256 doesn't fit U8
2+ 5 | | 0 .. 256: n := 0;
3+ | ^^^
4+raz: 1 error

test/msg/MatchIntNoElse.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/MatchIntNoElse.raz:6:5: error: a match on an integer needs an else arm
2+ 6 | end;
3+ | ^^^
4+help: it may be empty: | else:
5+raz: 1 error

test/msg/MatchIntTwice.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/MatchIntTwice.raz:6:10: error: 5 is in another label too
2+ 6 | | 7, 5: n := 1;
3+ | ^
4+raz: 1 error

test/msg/MatchIntVar.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/MatchIntVar.raz:5:7: error: expected a constant
2+ 5 | | m: n := 0;
3+ | ^
4+raz: 1 error

test/msg/MatchReal.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/MatchReal.raz:4:11: error: match needs a variant, an enumeration or an integer, found F64
2+ 4 | match n of
3+ | ^
4+raz: 1 error

test/msg/Resync.err (history) modified +3 -3

@@ -7,9 +7,9 @@
77 conform/err/Resync.raz:15:10: error: expected an expression, found ")"
88 15 | x := ) + 2;
99 | ^
10-conform/err/Resync.raz:16:11: error: match needs a variant or an enumeration, found I64
11- 16 | match x
12- | ^
10+conform/err/Resync.raz:17:8: error: expected "of", found ":"
11+ 17 | | 1: x := true;
12+ | ^
1313 conform/err/Resync.raz:23:13: error: expected an expression, found ";"
1414 23 | x := 1 +;
1515 | ^