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.
| @@ -125,7 +125,7 @@ | ||
| 125 | 125 | - [x] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only |
| 126 | 126 | data, used as a variable that can't be changed (section 4). |
| 127 | 127 | 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 | |
| 129 | 129 | labels, no value in two, `else` required (7.4); compares and |
| 130 | 130 | branches, no jump table. `examples/Sh.raz`'s chains of `elsif` |
| 131 | 131 | become `match`. |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | module AtMatch; |
| 2 | 2 | proc main*; |
| 3 | -var x: I64; | |
| 3 | +var x: F64; | |
| 4 | 4 | begin |
| 5 | - match x + 1 do end; | |
| 5 | + match x + 1.0 of end; | |
| 6 | 6 | end; |
| 7 | 7 | end AtMatch. |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +32133120 | |
| 2 | +-2 -2 -1 0 1 100 2 2 | |
| 3 | +001223 | |
| 4 | +seven |
| @@ -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. |
| @@ -336,7 +336,10 @@ | ||
| 336 | 336 | Then statements: `Statement` makes each one a recovery point (below) |
| 337 | 337 | and `OneStatement` dispatches; `IfStatement`, |
| 338 | 338 | `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 | |
| 340 | 343 | is compiled where it stands, as a little subroutine of the procedure |
| 341 | 344 | that the code jumps over, and `RunDefers` emits a call to it at every |
| 342 | 345 | way out of its statement list: the end, `return` and `break`. |
| @@ -57,8 +57,10 @@ | ||
| 57 | 57 | |
| 58 | 58 | proc special(c: U8): BOOL; |
| 59 | 59 | 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; | |
| 62 | 64 | end; |
| 63 | 65 | |
| 64 | 66 | (* $NAME or $? at s[at], its value added to b; a $ before anything else |
| @@ -86,11 +88,12 @@ | ||
| 86 | 88 | Buf.init(ADR(b), ADR(arena.base)); |
| 87 | 89 | while (at < LEN(s)) and not special(s[at]) do |
| 88 | 90 | q := s[at]; |
| 89 | - if q = 39 then (* '...': every character as it is *) | |
| 91 | + match q of | |
| 92 | + | 39: (* '...': every character as it is *) | |
| 90 | 93 | INC(at); |
| 91 | 94 | while (at < LEN(s)) and (s[at] # 39) do Buf.add(ADR(b), s[at]); INC(at); end; |
| 92 | 95 | INC(at); |
| 93 | - elsif q = '"' then (* "...": dollars, and \" and \\ *) | |
| 96 | + | '"': (* "...": dollars, and \" and \\ *) | |
| 94 | 97 | INC(at); |
| 95 | 98 | while (at < LEN(s)) and (s[at] # '"') do |
| 96 | 99 | if s[at] = '$' then dollar(ADR(b)); |
| @@ -101,9 +104,9 @@ | ||
| 101 | 104 | end; |
| 102 | 105 | end; |
| 103 | 106 | INC(at); |
| 104 | - elsif q = '$' then | |
| 107 | + | '$': | |
| 105 | 108 | dollar(ADR(b)); |
| 106 | - else | |
| 109 | + | else: | |
| 107 | 110 | if (q = '\\') and (at + 1 < LEN(s)) then INC(at); end; |
| 108 | 111 | Buf.add(ADR(b), s[at]); |
| 109 | 112 | INC(at); |
| @@ -122,16 +125,17 @@ | ||
| 122 | 125 | if at >= LEN(s) then return eof; end; |
| 123 | 126 | c := s[at]; |
| 124 | 127 | 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 | + | '>': | |
| 130 | 134 | if (at < LEN(s)) and (s[at] = '>') then INC(at); return onto; end; |
| 131 | 135 | 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: | |
| 135 | 139 | end; |
| 136 | 140 | DEC(at); |
| 137 | 141 | readWord; |
| @@ -111,6 +111,7 @@ | ||
| 111 | 111 | procedure SetField(const x: TItem; f: PObj; var a: TItem); |
| 112 | 112 | function MatchBegin(var x: TItem): int64; |
| 113 | 113 | procedure MatchTag(p: int64; tag: int64; var L: longint; isEnum: boolean); |
| 114 | +procedure MatchRange(p, lo, hi: int64; var body: longint); | |
| 114 | 115 | procedure CondJump(var x: TItem; var L: longint); |
| 115 | 116 | |
| 116 | 117 | procedure SliceOf(var x: TItem; t: PType; len: int64); |
| @@ -1513,8 +1514,8 @@ | ||
| 1513 | 1514 | function MatchBegin(var x: TItem): int64; |
| 1514 | 1515 | var p: int64; |
| 1515 | 1516 | 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)); | |
| 1518 | 1519 | if x.ro then CopyToTemp(x); { its arms' names stand for a copy's fields } |
| 1519 | 1520 | Address(x, RAX); |
| 1520 | 1521 | p := AllocTemp(8, 8); |
| @@ -1528,6 +1529,22 @@ | ||
| 1528 | 1529 | if not isEnum then Load(RAX, Mem(RAX, 0), 8, false); |
| 1529 | 1530 | CmpI(RAX, tag); |
| 1530 | 1531 | 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); | |
| 1531 | 1548 | end; |
| 1532 | 1549 | |
| 1533 | 1550 | { The end of a round of a for loop: stop if the step would pass the |
| @@ -1303,19 +1303,74 @@ | ||
| 1303 | 1303 | FixLink(lp.brk); |
| 1304 | 1304 | end; |
| 1305 | 1305 | |
| 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 | + | |
| 1306 | 1359 | procedure MatchStatement; |
| 1307 | 1360 | var |
| 1308 | 1361 | 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; | |
| 1310 | 1363 | begin |
| 1311 | 1364 | Next; |
| 1312 | 1365 | Expression(x, nil); |
| 1366 | + if x.typ^.form = fConst then Coerce(x, tI64); | |
| 1313 | 1367 | p := MatchBegin(x); |
| 1314 | 1368 | t := x.typ; |
| 1315 | 1369 | Expect(sOf); |
| 1316 | 1370 | FillChar(seen, SizeOf(seen), 0); |
| 1317 | 1371 | hasElse := false; |
| 1318 | 1372 | endL := NoLabel; |
| 1373 | + ls.n := 0; | |
| 1319 | 1374 | while tok = sBar do begin |
| 1320 | 1375 | Next; |
| 1321 | 1376 | if hasElse then Mark('else must be the last arm'); |
| @@ -1324,7 +1379,9 @@ | ||
| 1324 | 1379 | Expect(sColon); |
| 1325 | 1380 | Statements; |
| 1326 | 1381 | hasElse := true; |
| 1327 | - end else begin | |
| 1382 | + end else if t^.form = fInt then | |
| 1383 | + IntArm(t, p, ls, endL) | |
| 1384 | + else begin | |
| 1328 | 1385 | if tok <> sIdent then Expected('a case'); |
| 1329 | 1386 | c := t^.fields; |
| 1330 | 1387 | while (c <> nil) and (c^.name <> id) do c := c^.next; |
| @@ -1364,6 +1421,8 @@ | ||
| 1364 | 1421 | end; |
| 1365 | 1422 | endAt := TokPos; |
| 1366 | 1423 | 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:'); | |
| 1367 | 1426 | if not hasElse then begin |
| 1368 | 1427 | c := t^.fields; |
| 1369 | 1428 | while c <> nil do begin |
| @@ -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 | + | ^^^^^^^ | |
| 4 | 4 | raz: 1 error |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | conform/err/Resync.raz:15:10: error: expected an expression, found ")" |
| 8 | 8 | 15 | x := ) + 2; |
| 9 | 9 | | ^ |
| 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 | + | ^ | |
| 13 | 13 | conform/err/Resync.raz:23:13: error: expected an expression, found ";" |
| 14 | 14 | 23 | x := 1 +; |
| 15 | 15 | | ^ |