-
Notifications
You must be signed in to change notification settings - Fork 0
/
Splitter.cs
50 lines (37 loc) · 1.74 KB
/
Splitter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using static propcalc.Syntax;
namespace propcalc;
public static class Splitter {
public static Deque< Deque<string> > splitBrackets(Deque<string> str) {
Deque<int> indexOfBrackets = new(); // Gets trashed after return
Deque< Deque<string> > cutBrackets = new(); // Will return
int replacement = 0; // Intermediate counter
for (int i = 0; i < str.Size(); ++i) {
if (str[i].Equals(strOf(brl)))
indexOfBrackets.Append(i);
else if (str[i].Equals(strOf(brr))) {
int left = indexOfBrackets.PopLast() + 1;
int right = i - 1;
cutBrackets.Append(new());
int amountOfBinOps = 0;
for (int j = left; j <= right; ++j) {
cutBrackets.ReadLast().Append(str[j]); // Create new intermediate
if (isBinOp(str[j]))
++amountOfBinOps;
}
if (amountOfBinOps == 0)
amountOfBinOps = 1; // To make it work with unary not clauses
str.iterator.To(left - 2);
replaceBracket(str, replacement + amountOfBinOps - 1); // Replace bracket with intermediate
replacement += amountOfBinOps;
i = left - 1;
}
}
return cutBrackets;
}
private static void replaceBracket(Deque<string> str, int replacement) {
while (!str.iterator.GetNext().Equals(strOf(brr)))
str.iterator.RemoveNext();
str.iterator.ToNext();
str.iterator.Set(replacement.ToString());
}
}