Skip to content

Commit 7e71bfe

Browse files
author
kr-2003
committed
added undo operation
1 parent 1158ecd commit 7e71bfe

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

src/xinterpreter.cpp

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,17 @@ __get_cxx_version ()
134134

135135
auto input_guard = input_redirection(config.allow_stdin);
136136

137-
// Check for magics
138-
for (auto& pre : preamble_manager.preamble)
137+
if (code.find("%undo") != 0)
139138
{
140-
if (pre.second.is_match(code))
139+
// Check for magics
140+
for (auto& pre : preamble_manager.preamble)
141141
{
142-
pre.second.apply(code, kernel_res);
143-
cb(kernel_res);
144-
return;
142+
if (pre.second.is_match(code))
143+
{
144+
pre.second.apply(code, kernel_res);
145+
cb(kernel_res);
146+
return;
147+
}
145148
}
146149
}
147150

@@ -169,7 +172,32 @@ __get_cxx_version ()
169172
try
170173
{
171174
StreamRedirectRAII R(err);
172-
compilation_result = Cpp::Process(code.c_str());
175+
if (code.rfind("%undo", 0) == 0)
176+
{
177+
int n = 1; // Default value
178+
if (code.length() > 5)
179+
{
180+
try
181+
{
182+
n = std::stoi(code.substr(6));
183+
}
184+
catch (const std::invalid_argument&)
185+
{
186+
throw std::runtime_error(
187+
"Invalid format for %undo. Expected '%undo n' where n is an integer."
188+
);
189+
}
190+
catch (const std::out_of_range&)
191+
{
192+
throw std::runtime_error("Number out of range for %undo.");
193+
}
194+
}
195+
compilation_result = Cpp::Undo(n) ? true : false;
196+
}
197+
else
198+
{
199+
compilation_result = Cpp::Process(code.c_str());
200+
}
173201
}
174202
catch (std::exception& e)
175203
{

0 commit comments

Comments
 (0)