Skip to content

Commit

Permalink
[ideas] Ideas for matching ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
titzer committed Jun 27, 2024
1 parent f9626a2 commit 1161a6e
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions doc/ideas/MatchRange.v3
Original file line number Diff line number Diff line change
@@ -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<T>) -> (int, Range<T>) {
match (t) {
A(f) ... rest => return (f + 77, rest); // "rest" has type Range<T>
B(g) ... rest => return (g + 99, rest); // "rest" has type Range<T>
}
}

def keywords(str: Range<byte>) -> (int, Range<T>) {
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<byte>) -> (Lit, Range<T>) {
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<byte>) -> IntLiteralMatch(x: int) {
for (i < str.length) ...
return false;
return (true, length, val)
}

0 comments on commit 1161a6e

Please sign in to comment.