-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
191 lines (169 loc) · 5.59 KB
/
main.cpp
File metadata and controls
191 lines (169 loc) · 5.59 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <bits/stdc++.h>
using namespace std;
#define PUSH 0
#define SWAP 1
#define N 20200
map<string,int> mp;
string var[N];
queue<bool> input, output;
struct ins{
int op, x, y;
void print(){
if(op == PUSH) printf("PUSH %s %s", var[x].c_str(), var[y].c_str());
else printf("SWAP %s", var[x].c_str());
}
};
struct node{
ins i;
int p0, p1;
};
struct program{
node d[N];
int S, I, O0, O1, n, size;
void step(){
if(n == I){
// assumes input is non-empty when visiting I
bool x = input.front();
input.pop();
n = x ? d[I].p1 : d[I].p0;
return;
}else if(n == O0){
output.push(0);
}else if(n == O1){
output.push(1);
}else{
ins cur_i = d[n].i;
if(cur_i.op == PUSH){
d[cur_i.x].p0 = d[cur_i.x].p1;
d[cur_i.x].p1 = cur_i.y;
}else{ // SWAP
swap(d[cur_i.x].p0, d[cur_i.x].p1);
}
}
n = d[n].p0;
}
void print(bool print_machine){
if(print_machine){
for(int i = 0; i < size; i++){
printf("NODE %s(", var[i].c_str());
d[i].i.print();
printf("), p0 = %s, p1 = %s \n", var[d[i].p0].c_str(), var[d[i].p1].c_str());
}
printf("END\n");
}
printf("CUR = %s \n", var[n].c_str());
printf("-------------------------\n");
}
void load(){
ifstream in("input.glf");
if(!in){ perror("input.glf"); return; }
// reset state
while(!input.empty()) input.pop();
while(!output.empty()) output.pop();
mp.clear();
size = 0;
auto trim = [](string &s){
size_t a = s.find_first_not_of(" \t\r\n");
size_t b = s.find_last_not_of(" \t\r\n");
if (a == string::npos) { s.clear(); return; }
s = s.substr(a, b - a + 1);
};
// Create/get id for a name; default to safe node (self-loops, SWAP self)
auto getId = [&](const string& s)->int{
auto it = mp.find(s);
if(it != mp.end()) return it->second;
int id = size++;
mp[s] = id;
var[id] = s;
d[id].p0 = d[id].p1 = id; // self-loop
d[id].i.op = SWAP;
d[id].i.x = id; // SWAP self
d[id].i.y = 0;
return id;
};
S = getId("S");
I = getId("I");
O0 = getId("O0");
O1 = getId("O1");
string line;
// ---- Read NODE lines until END
while (std::getline(in, line)) {
string t = line; trim(t);
if (t.empty()) continue;
if (t == "END") break;
if (t.rfind("NODE ", 0) != 0) continue;
// Format: NODE <name>(<op...>) <p0> <p1>
size_t pos = 5; // after "NODE "
size_t lpar = t.find('(', pos);
size_t rpar = (lpar == string::npos) ? string::npos : t.find(')', lpar+1);
if (lpar == string::npos || rpar == string::npos) continue;
string name = t.substr(pos, lpar - pos); trim(name);
int idx = getId(name);
string inside = t.substr(lpar+1, rpar - lpar - 1); trim(inside);
string rest = t.substr(rpar+1); trim(rest);
string p0s, p1s;
{ stringstream rs(rest); rs >> p0s >> p1s; }
int p0id = p0s.empty() ? idx : getId(p0s);
int p1id = p1s.empty() ? idx : getId(p1s);
// parse instruction
ins insn{};
{
stringstream is(inside);
string op; is >> op;
if (op == "PUSH") {
string xs, ys; is >> xs >> ys;
insn.op = PUSH; insn.x = getId(xs); insn.y = getId(ys);
} else if (op == "SWAP") {
string xs; is >> xs;
insn.op = SWAP; insn.x = getId(xs); insn.y = 0;
} else {
// unknown => SWAP self as a safe default
insn.op = SWAP; insn.x = idx; insn.y = 0;
}
}
d[idx].i = insn;
d[idx].p0 = p0id;
d[idx].p1 = p1id;
}
// ---- After END: read CUR and INPUT until EOF (order-insensitive)
while (std::getline(in, line)) {
string t = line; trim(t);
if (t.empty()) continue;
if (t.rfind("CUR", 0) == 0) {
size_t eq = t.find('=');
if (eq != string::npos) {
string cur = t.substr(eq+1); trim(cur);
n = getId(cur);
}
} else if (t.rfind("INPUT", 0) == 0) {
size_t eq = t.find('=');
if (eq != string::npos) {
string bits = t.substr(eq+1); trim(bits);
for (char c : bits) {
if (c == '0') input.push(false);
else if (c == '1') input.push(true);
}
}
}
}
// Fill conventional special nodes if present
if (mp.count("S")) S = mp["S"];
if (mp.count("I")) I = mp["I"];
if (mp.count("O0")) O0 = mp["O0"];
if (mp.count("O1")) O1 = mp["O1"];
}
void init(){ n = S; }
} p;
int main(){
p.load();
p.init();
p.print(true);
int halt = mp["*"];
// demo: step a bit and show CUR only
for(int i = 0; i < 1000; i++){
p.step();
p.print(false);
if(p.n==halt)break;
}
return 0;
}