-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellLoop.cpp
More file actions
102 lines (90 loc) · 2.62 KB
/
ShellLoop.cpp
File metadata and controls
102 lines (90 loc) · 2.62 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
/*
** EPITECH PROJECT, 2025
** a
** File description:
** a
*/
#include "ShellLoop.hpp"
nts::ShellLoop::ShellLoop(Circuit &circuit) : _circuit(circuit)
{
_commands.emplace("exit", &ShellLoop::exit);
_commands.emplace("simulate", &ShellLoop::simulate);
_commands.emplace("display", &ShellLoop::display);
_commands.emplace("loop", &ShellLoop::loop);
_running = true;
}
nts::ShellLoop::~ShellLoop()
{
}
void nts::ShellLoop::exit(Circuit &circuit)
{
(void)circuit;
_running = false;
}
void nts::ShellLoop::display(Circuit &circuit)
{
circuit.display();
}
void nts::ShellLoop::simulate(Circuit &circuit)
{
circuit.simulate(circuit.getTick() + 1);
}
void nts::ShellLoop::loop(Circuit &circuit)
{
while (_running) {
circuit.simulate(circuit.getTick() + 1);
circuit.display();
}
}
bool nts::ShellLoop::setComponentValue(const std::string &input, Circuit &circuit)
{
std::regex valuePattern("([a-zA-Z0-9_]+)=([01U])");
std::smatch matches;
if (std::regex_match(input, matches, valuePattern)) {
std::string componentName = matches[1];
std::string valueStr = matches[2];
try {
auto &component = circuit.getComponent(componentName);
auto inputComp = dynamic_cast<nts::InputComponent*>(component.get());
if (!inputComp) {
std::cerr << "Error: " << componentName << " is not an input component" << std::endl;
return false;
}
nts::Tristate state;
if (valueStr == "1")
state = nts::TRUE;
else if (valueStr == "0")
state = nts::FALSE;
else
state = nts::UNDEFINED;
circuit.setInputState(*inputComp, state);
return true;
} catch (const std::out_of_range &e) {
std::cerr << "Error: component " << componentName << " not found" << std::endl;
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
return false;
}
void nts::ShellLoop::run()
{
std::string input;
_circuit.simulate(0);
while (_running) {
std::cout << "> ";
if (!std::getline(std::cin, input)) {
std::cout << std::endl;
break;
}
if (input.empty())
continue;
if (_commands.find(input) != _commands.end())
(this->*_commands[input])(_circuit);
else if (input == "exit")
exit(_circuit);
else if (!setComponentValue(input, _circuit)) {
std::cerr << "Invalid command: " << input << std::endl;
}
}
}