-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.cs
155 lines (127 loc) · 5.03 KB
/
Interpreter.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using static propcalc.Compiler;
using static propcalc.Logic;
namespace propcalc;
public class Interpreter {
private readonly bool[] cache; // intermediates ... variables ...
private readonly Statement[] statement;
private readonly string[] varNames;
private readonly int stmtCount;
private readonly int varCount;
private readonly int result; // Index of result statement
public readonly string rawInput;
// 0 - valid, 1 - satisfiable, 2 - invalid
private bool[] props = {true, false, true};
private const int valid = 0;
private const int satisfiable = 1;
private const int invalid = 2;
public Interpreter(string filename) {
BinaryReader file = new(File.OpenRead(filename));
stmtCount = file.ReadInt32();
varCount = file.ReadInt32();
file.ReadBytes(bufferToStatements);
statement = new Statement[stmtCount];
varNames = new string[varCount];
cache = new bool[stmtCount + varCount + 2]; // 2 extra for booleans
cache[stmtCount + varCount + 1] = true; // Constant boolean true
result = stmtCount - 1;
for (int i = 0; i < stmtCount; ++i)
statement[i] = new Statement(
file.ReadInt32(),
file.ReadInt32(),
file.ReadInt32() - interStart,
file.ReadInt32() - interStart,
file.ReadBoolean(),
file.ReadBoolean()
);
for (int i = 0; i < varCount; ++i)
varNames[i] = file.ReadString();
rawInput = file.ReadString();
file.Dispose();
}
public Interpreter(Deque<object> compilation) {
stmtCount = (int) compilation.PopFirst();
varCount = (int) compilation.PopFirst();
statement = new Statement[stmtCount];
varNames = new string[varCount];
cache = new bool[stmtCount + varCount + 2];
cache[stmtCount + varCount + 1] = true;
result = stmtCount - 1;
for (int i = 0; i < stmtCount; ++i)
statement[i] = new Statement(
(int) compilation.PopFirst(),
(int) compilation.PopFirst(),
(int) compilation.PopFirst() - interStart,
(int) compilation.PopFirst() - interStart,
(bool) compilation.PopFirst(),
(bool) compilation.PopFirst()
);
for (int i = 0; i < varCount; ++i)
varNames[i] = (string) compilation.PopFirst();
rawInput = (string) compilation.PopFirst();
}
public void interpret(bool table, bool properties) {
if (table && properties)
interpretTableProps();
else if (table && !properties)
interpretTable();
else if (!table && properties)
interpretProps();
}
private void interpretTable() {
Table table = new(rawInput, varCount, varNames);
int i = default, max = 1 << varCount;
while (i < max) {
executeStatements();
table.writeOutputAndIncrement(cache[result], ++i);
iterateVariables(i);
}
}
private void interpretTableProps() {
Table table = new(rawInput, varCount, varNames);
int i = default, max = 1 << varCount;
while (i < max) {
executeStatements();
table.writeOutputAndIncrement(cache[result], ++i);
iterateVariables(i);
if (cache[result]) {
props[satisfiable] = true;
props[invalid] = false;
} else props[valid] = false;
}
}
private void interpretProps() {
int i = default, max = 1 << varCount;
while (i < max) {
executeStatements();
iterateVariables(++i);
if (cache[result]) {
props[satisfiable] = true;
props[invalid] = false;
}
else props[valid] = false;
}
}
private void executeStatements() {
for (int i = 0; i < stmtCount; ++i)
cache[i] = executeAtom(statement[i]);
}
private bool executeAtom(Statement atom) {
bool varLeft = atom.boolLeft ?
!cache[atom.varLeft] : cache[atom.varLeft];
bool varRight = atom.boolRight ?
!cache[atom.varRight] : cache[atom.varRight];
return gate[atom.binOperator](varLeft, varRight);
}
private void iterateVariables(int n) {
for (int i = 0; i < varCount; ++i)
cache[i + stmtCount] =
((n >> (varCount - i - 1)) & 1) == 1;
}
public void properties() {
Console.WriteLine(
$"\n{"Valid:", -12} {props[valid]}" +
$"\n{"Satisfiable:", -12} {props[satisfiable]}" +
$"\n{"Invalid:", -12} {props[invalid]}"
);
}
}