From 1161a6e822d2461aff7240503437078a841f62f6 Mon Sep 17 00:00:00 2001 From: "Ben L. Titzer" Date: Thu, 27 Jun 2024 15:46:34 -0400 Subject: [PATCH] [ideas] Ideas for matching ranges --- doc/ideas/MatchRange.v3 | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 doc/ideas/MatchRange.v3 diff --git a/doc/ideas/MatchRange.v3 b/doc/ideas/MatchRange.v3 new file mode 100644 index 000000000..6934b367f --- /dev/null +++ b/doc/ideas/MatchRange.v3 @@ -0,0 +1,36 @@ +//Idea: allow matching within a range and the rest of a range + +type T { + case A(f: int); + case B(g: int); +} + +def m(t: Range) -> (int, Range) { + match (t) { + A(f) ... rest => return (f + 77, rest); // "rest" has type Range + B(g) ... rest => return (g + 99, rest); // "rest" has type Range + } +} + +def keywords(str: Range) -> (int, Range) { + match(str) { + "if" ... rest => return (11, rest); + "case" ... rest => return (12, rest); + "match" ... rest => return (13, rest); + } + return (-1, str); +} + +def parse_literal(str: Range) -> (Lit, Range) { + match (str) { + IntLiteralMatch(x) ... rest => (Lit.Int(x), rest); + BoolLiteralMatch(x) ... rest => (Lit.Bool(x), rest); + "case ${x: int} of foo" ... rest => (Lit.CaseInt(x), rest); + } +} + +def match(str: Range) -> IntLiteralMatch(x: int) { + for (i < str.length) ... + return false; + return (true, length, val) +}