21 lines
578 B
Plaintext
21 lines
578 B
Plaintext
# An assertion says what failed in words of its own, in place of the condition,
|
|||
|
|
# and an `ensures` in a loop's block is checked at each pass's end.
|
||
|
|
import std::io;
|
||
|
|
|
||
|
|
def balanced(lines: span<strview>): void {
|
||
|
|
for line in lines {
|
||
|
|
var depth := 0;
|
||
|
|
ensures(depth == 0);
|
||
|
|
for c in line {
|
||
|
|
continue if c == ' ';
|
||
|
|
depth += c == '(' ? 1 : c == ')' ? -1 : 0;
|
||
|
|
asserts(depth >= 0, "A parenthesis closes one never opened.");
|
||
|
|
}
|
||
|
|
io::println($"{line} closes what it opens");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
def main(): void {
|
||
|
|
balanced(["(a)", "((b) c)"]);
|
||
|
|
balanced(["(d))"]);
|
||
|
|
}
|