Skip to content

Commit

Permalink
Fix echo state during script compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Serious-senpai committed Jun 12, 2024
1 parent 7a473fd commit 300e026
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
26 changes: 11 additions & 15 deletions src/commands/for.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,28 @@ class ForCommand : public liteshell::BaseCommand

stream_ptr->consume_last();
auto lines = _get_lines(raw_context);
auto loop_label = _make_new_label(), end_label = _make_new_label();

const auto restore_echo = stream_ptr->echo() ? liteshell::InputStream::ECHO_ON : liteshell::InputStream::ECHO_OFF;
const auto loop_label = _make_new_label(), end_label = _make_new_label();

lines.push_front(restore_echo);
lines.push_front("endif");
lines.push_front(utils::format("jump %s", end_label.c_str()));
lines.push_front(utils::format("if -m \"$%s\" == \"%s\"", loop_var.c_str(), end.c_str()));
lines.push_front(loop_label);
lines.push_front(utils::format("eval -ms \"%s\" \"%s\"", loop_var.c_str(), start.c_str()));
lines.push_front(liteshell::InputStream::ECHO_OFF);

lines.push_back(liteshell::InputStream::ECHO_OFF);
lines.push_back(utils::format("if -m \"%s\" < \"%s\"", start.c_str(), end.c_str()));
lines.push_back(utils::format("eval -ms \"%s\" \"$%s + 1\"", loop_var.c_str(), loop_var.c_str()));
lines.push_back(
utils::format(
"_if -m \"$%s\" < \"%s\" \"%s\" \"%s\"",
loop_var.c_str(),
end.c_str(),
loop_label.c_str(),
end_label.c_str()));
lines.push_back("else");
lines.push_back(utils::format("eval -ms \"%s\" \"$%s - 1\"", loop_var.c_str(), loop_var.c_str()));
lines.push_back(
utils::format(
"_if -m \"$%s\" > \"%s\" \"%s\" \"%s\"",
loop_var.c_str(),
end.c_str(),
loop_label.c_str(),
end_label.c_str()));
lines.push_back("endif");
lines.push_back(utils::format("jump %s", loop_label.c_str()));

lines.push_back(end_label);
lines.push_back(restore_echo);

stream_ptr->write(lines.begin(), lines.end());

Expand Down
13 changes: 11 additions & 2 deletions src/commands/if.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,20 @@ class IfCommand : public liteshell::BaseCommand
}
}

auto true_label = _make_new_label(), false_label = _make_new_label(), end_label = _make_new_label();
const auto restore_echo = stream_ptr->echo() ? liteshell::InputStream::ECHO_ON : liteshell::InputStream::ECHO_OFF;
const auto true_label = _make_new_label(), false_label = _make_new_label(), end_label = _make_new_label();

if_true.push_front(restore_echo);
if_true.push_front(true_label);
if_false.push_front(false_label);
if_true.push_back(liteshell::InputStream::ECHO_OFF);
if_true.push_back("jump " + end_label);

if_false.push_front(restore_echo);
if_false.push_front(false_label);
if_false.push_back(liteshell::InputStream::ECHO_OFF);
if_false.push_back("jump " + end_label);

stream_ptr->write(restore_echo);
stream_ptr->write(end_label);
stream_ptr->write(if_false.begin(), if_false.end());
stream_ptr->write(if_true.begin(), if_true.end());
Expand All @@ -130,6 +138,7 @@ class IfCommand : public liteshell::BaseCommand
raw_context.present.count("-m") ? "-m" : "",
x.c_str(), op.c_str(), y.c_str(),
true_label.c_str(), false_label.c_str()));
stream_ptr->write(liteshell::InputStream::ECHO_OFF);

return 0;
}
Expand Down
36 changes: 22 additions & 14 deletions src/include/stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ namespace liteshell
class InputStream
{
private:
std::list<std::string> _list;

/** @brief Mimic the [instruction pointer](https://en.wikipedia.org/wiki/Program_counter) */
std::list<std::string>::iterator _iterator = _list.begin();

InputStream(const InputStream &) = delete;
InputStream &operator=(const InputStream &) = delete;

/** @brief The current echo state */
bool _echo = true;

public:
/**
* @brief A special command to turn off echo.
*
Expand All @@ -32,18 +44,6 @@ namespace liteshell
*/
static const std::string STREAM_EOF;

std::list<std::string> _list;

/** @brief Mimic the [instruction pointer](https://en.wikipedia.org/wiki/Program_counter) */
std::list<std::string>::iterator _iterator = _list.begin();

InputStream(const InputStream &) = delete;
InputStream &operator=(const InputStream &) = delete;

/** @brief The current echo state */
bool _echo = true;

public:
/** @brief A flag indicating that `getline` must echo the input to stdout */
static const int FORCE_ECHO = 1 << 0;

Expand All @@ -59,7 +59,7 @@ namespace liteshell
InputStream() {}

/** @brief The echo state after the next command */
bool peek_echo()
bool peek_echo() const
{
if (peek() == ECHO_ON)
{
Expand All @@ -74,6 +74,14 @@ namespace liteshell
return _echo;
}

/**
* @brief The current echo state
*/
bool echo() const
{
return _echo;
}

/**
* @brief Peek the next command in the stream.
*
Expand All @@ -82,7 +90,7 @@ namespace liteshell
* @return The next command in the input stream, or `std::nullopt` if the stream
* reaches EOF
*/
std::optional<std::string> peek()
std::optional<std::string> peek() const
{
for (auto iter = _iterator; iter != _list.end(); iter++)
{
Expand Down

0 comments on commit 300e026

Please sign in to comment.