From 2a226448ee9bf3ae23b7adfc5126ede476bb9b5c Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 09:19:49 +0100 Subject: [PATCH] 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. --- diff --git a/PLAN.md b/PLAN.md index 40c8429..14626fb 100644 --- a/PLAN.md +++ b/PLAN.md @@ -125,7 +125,7 @@ - [x] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only data, used as a variable that can't be changed (section 4). Done so that a string constant is the same mechanism. -- [ ] 6c. `match` on integers: constants and inclusive ranges as +- [x] 6c. `match` on integers: constants and inclusive ranges as labels, no value in two, `else` required (7.4); compares and branches, no jump table. `examples/Sh.raz`'s chains of `elsif` become `match`. diff --git a/conform/err/AtMatch.raz b/conform/err/AtMatch.raz index 43a165a..162f5d8 100644 --- a/conform/err/AtMatch.raz +++ b/conform/err/AtMatch.raz @@ -1,7 +1,7 @@ module AtMatch; proc main*; -var x: I64; +var x: F64; begin - match x + 1 do end; + match x + 1.0 of end; end; end AtMatch. diff --git a/conform/err/MatchIntEmpty.raz b/conform/err/MatchIntEmpty.raz new file mode 100644 index 0000000..8eee559 --- /dev/null +++ b/conform/err/MatchIntEmpty.raz @@ -0,0 +1,9 @@ +module MatchIntEmpty; +proc f(n: I64); +begin + match n of + | 5 .. 1: n := 0; + | else: + end; +end; +end MatchIntEmpty. diff --git a/conform/err/MatchIntFit.raz b/conform/err/MatchIntFit.raz new file mode 100644 index 0000000..ebfbc8a --- /dev/null +++ b/conform/err/MatchIntFit.raz @@ -0,0 +1,9 @@ +module MatchIntFit; +proc f(n: U8); +begin + match n of + | 0 .. 256: n := 0; + | else: + end; +end; +end MatchIntFit. diff --git a/conform/err/MatchIntNoElse.raz b/conform/err/MatchIntNoElse.raz new file mode 100644 index 0000000..03a5af1 --- /dev/null +++ b/conform/err/MatchIntNoElse.raz @@ -0,0 +1,8 @@ +module MatchIntNoElse; +proc f(n: I64); +begin + match n of + | 1: n := 0; + end; +end; +end MatchIntNoElse. diff --git a/conform/err/MatchIntTwice.raz b/conform/err/MatchIntTwice.raz new file mode 100644 index 0000000..b3e86de --- /dev/null +++ b/conform/err/MatchIntTwice.raz @@ -0,0 +1,10 @@ +module MatchIntTwice; +proc f(n: I64); +begin + match n of + | 1 .. 5: n := 0; + | 7, 5: n := 1; + | else: + end; +end; +end MatchIntTwice. diff --git a/conform/err/MatchIntVar.raz b/conform/err/MatchIntVar.raz new file mode 100644 index 0000000..2cf9438 --- /dev/null +++ b/conform/err/MatchIntVar.raz @@ -0,0 +1,9 @@ +module MatchIntVar; +proc f(n, m: I64); +begin + match n of + | m: n := 0; + | else: + end; +end; +end MatchIntVar. diff --git a/conform/err/MatchReal.raz b/conform/err/MatchReal.raz new file mode 100644 index 0000000..c298c00 --- /dev/null +++ b/conform/err/MatchReal.raz @@ -0,0 +1,8 @@ +module MatchReal; +proc f(n: F64); +begin + match n of + | else: + end; +end; +end MatchReal. diff --git a/conform/run/MatchInt.expect b/conform/run/MatchInt.expect new file mode 100644 index 0000000..c24b0f3 --- /dev/null +++ b/conform/run/MatchInt.expect @@ -0,0 +1,4 @@ +32133120 +-2 -2 -1 0 1 100 2 2 +001223 +seven diff --git a/conform/run/MatchInt.raz b/conform/run/MatchInt.raz new file mode 100644 index 0000000..e901b88 --- /dev/null +++ b/conform/run/MatchInt.raz @@ -0,0 +1,65 @@ +module MatchInt; +import Out; + +const Max = 100; + +proc kind(ch: U8): I64; +var k: I64; +begin + match ch of + | ' ', '\t', '\n': k := 1; + | '0' .. '9': k := 2; + | 'a' .. 'z', 'A' .. 'Z', '_': k := 3; + | else: + end; + return k; +end; + +proc signed(n: I32): I64; +begin + match n of + | -2147483648 .. -1000: return -2; + | -999 .. -1: return -1; + | 0: return 0; + | Max: return 100; + | Max + 1 .. 2147483647: return 2; + | else: return 1; + end; +end; + +proc big(n: U64): I64; +begin + match n of + | 0FFFFFFFFFFFFFFFFH: return 3; + | 8000000000000000H .. 0FFFFFFFFFFFFFFFEH: return 2; + | 0 .. 0FFFFFFFFH: return 0; + | else: return 1; + end; +end; + +proc main*; +var i: INT; s: []U8; +begin + s := "a1 _Z\t9?"; + for i := 0 to LEN(s) - 1 do Out.int(kind(s[i])); end; + Out.ln; + Out.int(signed(-2147483648)); Out.str(" "); + Out.int(signed(-1000)); Out.str(" "); + Out.int(signed(-999)); Out.str(" "); + Out.int(signed(0)); Out.str(" "); + Out.int(signed(50)); Out.str(" "); + Out.int(signed(100)); Out.str(" "); + Out.int(signed(101)); Out.str(" "); + Out.int(signed(2147483647)); Out.ln; + Out.int(big(0)); Out.int(big(0FFFFFFFFH)); Out.int(big(100000000H)); + Out.int(big(8000000000000000H)); Out.int(big(0FFFFFFFFFFFFFFFEH)); + Out.int(big(0FFFFFFFFFFFFFFFFH)); Out.ln; + match 7 of + | 1 .. 6: Out.str("low"); + | 7: Out.str("seven"); + | else: Out.str("high"); + end; + Out.ln; +end; + +end MatchInt. diff --git a/doc/compiler.md b/doc/compiler.md index e6e9f4a..f183860 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -336,7 +336,10 @@ Then statements: `Statement` makes each one a recovery point (below) and `OneStatement` dispatches; `IfStatement`, `WhileStatement`, `ForStatement` and `MatchStatement` emit their jumps -with `x64.pas`'s labels. `Defer` is worth reading: a deferred statement +with `x64.pas`'s labels. A `match` on an integer reads each arm's +labels in `IntArm`, which refuses a value in two of them, and tests them +one after another with `MatchRange`: compares and branches, no jump +table. `Defer` is worth reading: a deferred statement is compiled where it stands, as a little subroutine of the procedure that the code jumps over, and `RunDefers` emits a call to it at every way out of its statement list: the end, `return` and `break`. diff --git a/examples/Sh.raz b/examples/Sh.raz index 656e8e3..0e1eedf 100644 --- a/examples/Sh.raz +++ b/examples/Sh.raz @@ -57,8 +57,10 @@ proc special(c: U8): BOOL; begin - return (c = ' ') or (c = 9) or (c = 10) or (c = ';') or (c = '|') or (c = '&') - or (c = '<') or (c = '>'); + match c of + | ' ', 9, 10, ';', '|', '&', '<', '>': return true; + | else: return false; + end; end; (* $NAME or $? at s[at], its value added to b; a $ before anything else @@ -86,11 +88,12 @@ Buf.init(ADR(b), ADR(arena.base)); while (at < LEN(s)) and not special(s[at]) do q := s[at]; - if q = 39 then (* '...': every character as it is *) + match q of + | 39: (* '...': every character as it is *) INC(at); while (at < LEN(s)) and (s[at] # 39) do Buf.add(ADR(b), s[at]); INC(at); end; INC(at); - elsif q = '"' then (* "...": dollars, and \" and \\ *) + | '"': (* "...": dollars, and \" and \\ *) INC(at); while (at < LEN(s)) and (s[at] # '"') do if s[at] = '$' then dollar(ADR(b)); @@ -101,9 +104,9 @@ end; end; INC(at); - elsif q = '$' then + | '$': dollar(ADR(b)); - else + | else: if (q = '\\') and (at + 1 < LEN(s)) then INC(at); end; Buf.add(ADR(b), s[at]); INC(at); @@ -122,16 +125,17 @@ if at >= LEN(s) then return eof; end; c := s[at]; INC(at); - if (c = ';') or (c = 10) then return sep; - elsif c = '|' then return pipe; - elsif c = '&' then return amp; - elsif c = '<' then return from; - elsif c = '>' then + match c of + | ';', 10: return sep; + | '|': return pipe; + | '&': return amp; + | '<': return from; + | '>': if (at < LEN(s)) and (s[at] = '>') then INC(at); return onto; end; return into; - elsif (c = '2') and (at < LEN(s)) and (s[at] = '>') then - INC(at); - return errTo; + | '2': + if (at < LEN(s)) and (s[at] = '>') then INC(at); return errTo; end; + | else: end; DEC(at); readWord; diff --git a/src/gen.pas b/src/gen.pas index 360c80c..6ae2c16 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -111,6 +111,7 @@ procedure SetField(const x: TItem; f: PObj; var a: TItem); function MatchBegin(var x: TItem): int64; procedure MatchTag(p: int64; tag: int64; var L: longint; isEnum: boolean); +procedure MatchRange(p, lo, hi: int64; var body: longint); procedure CondJump(var x: TItem; var L: longint); procedure SliceOf(var x: TItem; t: PType; len: int64); @@ -1513,8 +1514,8 @@ function MatchBegin(var x: TItem): int64; var p: int64; begin - if x.typ^.form = fEnum then exit(ToSlot(x)); - if (x.mode <> mVar) or (x.typ^.form <> fVariant) then MarkAt(x.pos, 'match needs a variant or an enumeration, found ' + What(x)); + if x.typ^.form in [fEnum, fInt] then exit(ToSlot(x)); + 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)); if x.ro then CopyToTemp(x); { its arms' names stand for a copy's fields } Address(x, RAX); p := AllocTemp(8, 8); @@ -1528,6 +1529,22 @@ if not isEnum then Load(RAX, Mem(RAX, 0), 8, false); CmpI(RAX, tag); Jcc(ccNE, L); +end; + +{ To body if the integer in slot p is from lo to hi: less its lo, it is + then at most hi - lo, compared unsigned, which holds for a signed type + or not, with no second jump. } +procedure MatchRange(p, lo, hi: int64; var body: longint); +begin + Load(RAX, Mem(RBP, p), 8, false); + if lo = hi then begin + CmpI(RAX, lo); + Jcc(ccE, body); + exit; + end; + if lo <> 0 then begin MovRI(RCX, lo); Alu(aluSub, RAX, RCX) end; + CmpI(RAX, hi - lo); + Jcc(ccBE, body); end; { The end of a round of a for loop: stop if the step would pass the diff --git a/src/parse.pas b/src/parse.pas index 17175ef..9adca52 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1303,19 +1303,74 @@ FixLink(lp.brk); end; +type + TLabels = record + lo, hi: array[0..1023] of int64; { the ranges of the arms so far } + n: longint; + end; + +{ a < b, for values of an integer type, unsigned or not } +function Below(a, b: int64; unsign: boolean): boolean; +begin + if unsign then Below := qword(a) < qword(b) else Below := a < b; +end; + +{ An arm of a match on an integer of type t, held in slot p: its labels, + each a constant or a range, none overlapping another (report section + 7.4), then its statements. } +procedure IntArm(t: PType; p: int64; var ls: TLabels; var endL: longint); +var lo, hi: TItem; L, body, i: longint; u: boolean; start: TPos; +begin + u := not t^.signed; + body := NoLabel; + repeat + start := TokPos; + Expression(lo, t); + Coerce(lo, t); + ConstValue(lo); + hi := lo; + if tok = sUpto then begin + Next; + Expression(hi, t); + Coerce(hi, t); + ConstValue(hi); + if Below(hi.val, lo.val, u) then MarkAt(SpanFrom(start), 'the range is empty'); + end; + for i := 0 to ls.n - 1 do + if not Below(hi.val, ls.lo[i], u) and not Below(ls.hi[i], lo.val, u) then + if lo.val = hi.val then MarkAt(SpanFrom(start), Source(SpanFrom(start)) + ' is in another label too') + else MarkAt(SpanFrom(start), Source(SpanFrom(start)) + ' overlaps another label'); + if ls.n > High(ls.lo) then MarkAt(SpanFrom(start), 'more than ' + IntStr(High(ls.lo) + 1) + ' labels in one match'); + ls.lo[ls.n] := lo.val; ls.hi[ls.n] := hi.val; + inc(ls.n); + MatchRange(p, lo.val, hi.val, body); + if tok <> sComma then break; + Next; + until false; + L := NoLabel; + Jmp(L); + FixLink(body); + Expect(sColon); + Statements; + Jmp(endL); + FixLink(L); +end; + procedure MatchStatement; var x: TItem; t: PType; c, f, o: PObj; p: int64; L, endL, n: longint; - seen: array[0..255] of boolean; hasElse: boolean; endAt: TPos; + seen: array[0..255] of boolean; hasElse: boolean; endAt: TPos; ls: TLabels; begin Next; Expression(x, nil); + if x.typ^.form = fConst then Coerce(x, tI64); p := MatchBegin(x); t := x.typ; Expect(sOf); FillChar(seen, SizeOf(seen), 0); hasElse := false; endL := NoLabel; + ls.n := 0; while tok = sBar do begin Next; if hasElse then Mark('else must be the last arm'); @@ -1324,7 +1379,9 @@ Expect(sColon); Statements; hasElse := true; - end else begin + end else if t^.form = fInt then + IntArm(t, p, ls, endL) + else begin if tok <> sIdent then Expected('a case'); c := t^.fields; while (c <> nil) and (c^.name <> id) do c := c^.next; @@ -1364,6 +1421,8 @@ end; endAt := TokPos; Expect(sEnd); + if (t^.form = fInt) and not hasElse then + MarkHelp(endAt, 'a match on an integer needs an else arm', 'it may be empty: | else:'); if not hasElse then begin c := t^.fields; while c <> nil do begin diff --git a/test/msg/AtMatch.err b/test/msg/AtMatch.err index 0fbe7c3..4b4bdbd 100644 --- a/test/msg/AtMatch.err +++ b/test/msg/AtMatch.err @@ -1,4 +1,4 @@ -conform/err/AtMatch.raz:5:11: error: match needs a variant or an enumeration, found I64 - 5 | match x + 1 do end; - | ^^^^^ +conform/err/AtMatch.raz:5:11: error: match needs a variant, an enumeration or an integer, found F64 + 5 | match x + 1.0 of end; + | ^^^^^^^ raz: 1 error diff --git a/test/msg/MatchIntEmpty.err b/test/msg/MatchIntEmpty.err new file mode 100644 index 0000000..4bcba1a --- /dev/null +++ b/test/msg/MatchIntEmpty.err @@ -0,0 +1,4 @@ +conform/err/MatchIntEmpty.raz:5:7: error: the range is empty + 5 | | 5 .. 1: n := 0; + | ^^^^^^ +raz: 1 error diff --git a/test/msg/MatchIntFit.err b/test/msg/MatchIntFit.err new file mode 100644 index 0000000..446ab22 --- /dev/null +++ b/test/msg/MatchIntFit.err @@ -0,0 +1,4 @@ +conform/err/MatchIntFit.raz:5:12: error: 256 doesn't fit U8 + 5 | | 0 .. 256: n := 0; + | ^^^ +raz: 1 error diff --git a/test/msg/MatchIntNoElse.err b/test/msg/MatchIntNoElse.err new file mode 100644 index 0000000..a087074 --- /dev/null +++ b/test/msg/MatchIntNoElse.err @@ -0,0 +1,5 @@ +conform/err/MatchIntNoElse.raz:6:5: error: a match on an integer needs an else arm + 6 | end; + | ^^^ +help: it may be empty: | else: +raz: 1 error diff --git a/test/msg/MatchIntTwice.err b/test/msg/MatchIntTwice.err new file mode 100644 index 0000000..f6e1983 --- /dev/null +++ b/test/msg/MatchIntTwice.err @@ -0,0 +1,4 @@ +conform/err/MatchIntTwice.raz:6:10: error: 5 is in another label too + 6 | | 7, 5: n := 1; + | ^ +raz: 1 error diff --git a/test/msg/MatchIntVar.err b/test/msg/MatchIntVar.err new file mode 100644 index 0000000..25de405 --- /dev/null +++ b/test/msg/MatchIntVar.err @@ -0,0 +1,4 @@ +conform/err/MatchIntVar.raz:5:7: error: expected a constant + 5 | | m: n := 0; + | ^ +raz: 1 error diff --git a/test/msg/MatchReal.err b/test/msg/MatchReal.err new file mode 100644 index 0000000..0315d07 --- /dev/null +++ b/test/msg/MatchReal.err @@ -0,0 +1,4 @@ +conform/err/MatchReal.raz:4:11: error: match needs a variant, an enumeration or an integer, found F64 + 4 | match n of + | ^ +raz: 1 error diff --git a/test/msg/Resync.err b/test/msg/Resync.err index 05f1a00..72014b0 100644 --- a/test/msg/Resync.err +++ b/test/msg/Resync.err @@ -7,9 +7,9 @@ conform/err/Resync.raz:15:10: error: expected an expression, found ")" 15 | x := ) + 2; | ^ -conform/err/Resync.raz:16:11: error: match needs a variant or an enumeration, found I64 - 16 | match x - | ^ +conform/err/Resync.raz:17:8: error: expected "of", found ":" + 17 | | 1: x := true; + | ^ conform/err/Resync.raz:23:13: error: expected an expression, found ";" 23 | x := 1 +; | ^