-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.cpp
More file actions
208 lines (171 loc) · 6.65 KB
/
Process.cpp
File metadata and controls
208 lines (171 loc) · 6.65 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "Process.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <cctype>
#include <limits>
Process::Process(const std::string& name, const std::vector<Instruction>& ins)
: name(name), finished(false), instructions(ins), current_line(0) {
}
std::string Process::getName() const { return name; }
bool Process::isFinished() const { return finished; }
void Process::setFinished(bool f) { finished = f; }
size_t Process::getCurrentLine() const { return current_line; }
size_t Process::getTotalLines() const { return instructions.size(); }
uint16_t Process::getVariable(const std::string& var) const {
auto it = variables.find(var);
return (it != variables.end()) ? it->second : 0; // auto-declare 0 if missing
}
void Process::setVariable(const std::string& var, uint16_t value) {
variables[var] = clampUint16(value);
}
const std::vector<std::string>& Process::getLogs() const { return logs; }
void Process::addLog(const std::string& msg) { logs.push_back(msg); }
bool Process::isNumber(const std::string& s) const {
if (s.empty()) return false;
for (char c : s) if (!std::isdigit(static_cast<unsigned char>(c))) return false;
return true;
}
uint16_t Process::getValue(const std::string& token) const {
if (isNumber(token)) return std::stoi(token);
auto it = variables.find(token);
return (it != variables.end()) ? it->second : 0;
}
uint16_t Process::clampUint16(int64_t v) const {
if (v < 0) return 0;
if (v > std::numeric_limits<uint16_t>::max()) return std::numeric_limits<uint16_t>::max();
return static_cast<uint16_t>(v);
}
std::string Process::instrTypeAsString(Instruction::Type type) const {
switch (type) {
case Instruction::PRINT: return "PRINT";
case Instruction::DECLARE: return "DECLARE";
case Instruction::ADD: return "ADD";
case Instruction::SUBTRACT: return "SUBTRACT";
case Instruction::SLEEP: return "SLEEP";
case Instruction::FOR: return "FOR";
default: return "UNKNOWN";
}
}
void Process::executeNextInstruction(int nestedLevel) {
if (finished || current_line >= instructions.size()) {
finished = true;
return;
}
executeInstruction(instructions[current_line], nestedLevel);
current_line++;
if (current_line >= instructions.size()) finished = true;
}
void Process::executeInstruction(const Instruction& instr, int nestedLevel) {
if (nestedLevel > 3) {
addLog("Error: FOR loop nesting exceeded 3 levels!");
return;
}
switch (instr.type) {
case Instruction::PRINT: {
std::string out;
if (instr.args.empty()) {
out = "Hello world from " + name + "!";
}
else {
std::string msg = instr.args[0];
if (!msg.empty() && msg.front() == '(' && msg.back() == ')')
msg = msg.substr(1, msg.size() - 2);
if (!msg.empty() && ((msg.front() == '"' && msg.back() == '"') ||
(msg.front() == '\'' && msg.back() == '\''))) {
out = msg.substr(1, msg.size() - 2);
}
else if (variables.find(msg) != variables.end()) {
out = std::to_string(variables.at(msg));
}
else if (isNumber(msg)) {
out = msg;
}
else {
out = "Unknown symbol: " + msg;
}
}
addLog(out);
std::cout << "[" << name << "] " << out << std::endl;
break;
}
case Instruction::DECLARE: {
if (instr.args.size() < 2) break;
long val = std::stol(instr.args[1]);
setVariable(instr.args[0], clampUint16(val));
break;
}
case Instruction::ADD: {
if (instr.args.size() < 3 && instr.args.size() != 2) break;
std::string target = instr.args[0];
std::string src1, src2;
if (instr.args.size() == 3) {
// Format: ADD target src1 src2
src1 = instr.args[1];
src2 = instr.args[2];
uint16_t a = getValue(src1);
uint16_t b = getValue(src2);
setVariable(target, clampUint16(int64_t(a) + int64_t(b)));
}
else if (instr.args.size() == 2) {
// Format: ADD target src
src1 = instr.args[1];
uint16_t oldVal = getVariable(target);
uint16_t addVal = getValue(src1);
setVariable(target, clampUint16(int64_t(oldVal) + int64_t(addVal)));
}
break;
}
case Instruction::SUBTRACT: {
if (instr.args.size() < 3 && instr.args.size() != 2) break;
std::string target = instr.args[0];
std::string src1, src2;
if (instr.args.size() == 3) {
// Format: SUBTRACT target src1 src2
src1 = instr.args[1];
src2 = instr.args[2];
uint16_t a = getValue(src1);
uint16_t b = getValue(src2);
setVariable(target, clampUint16(int64_t(a) - int64_t(b)));
}
else if (instr.args.size() == 2) {
// Format: SUBTRACT target src
src1 = instr.args[1];
uint16_t oldVal = getVariable(target);
uint16_t subVal = getValue(src1);
setVariable(target, clampUint16(int64_t(oldVal) - int64_t(subVal)));
}
break;
}
case Instruction::SLEEP: {
if (instr.args.empty()) break;
uint8_t ticks = static_cast<uint8_t>(std::stoi(instr.args[0]));
addLog("Sleeping for " + std::to_string(ticks) + " ticks...");
std::this_thread::sleep_for(std::chrono::milliseconds(ticks * 100));
break;
}
case Instruction::FOR: {
if (instr.args.size() < 2) break;
std::string innerTypeStr = instr.args[0];
int repeats = std::stoi(instr.args[1]);
std::vector<std::string> innerArgs(instr.args.begin() + 2, instr.args.end());
Instruction innerInstr;
// map string to type
if (innerTypeStr == "PRINT") innerInstr.type = Instruction::PRINT;
else if (innerTypeStr == "DECLARE") innerInstr.type = Instruction::DECLARE;
else if (innerTypeStr == "ADD") innerInstr.type = Instruction::ADD;
else if (innerTypeStr == "SUBTRACT") innerInstr.type = Instruction::SUBTRACT;
else if (innerTypeStr == "SLEEP") innerInstr.type = Instruction::SLEEP;
else if (innerTypeStr == "FOR") innerInstr.type = Instruction::FOR;
innerInstr.args = innerArgs;
for (int i = 0; i < repeats; ++i) {
addLog("[FOR LOOP] Iteration " + std::to_string(i + 1) + "/" + std::to_string(repeats));
executeInstruction(innerInstr, nestedLevel + 1);
}
break;
}
default:
addLog("Unknown instruction type!");
break;
}
}