MTMS main
Modern Turing Machine Simulator
Loading...
Searching...
No Matches
cli.cpp
Go to the documentation of this file.
1
12#include "cli.hpp"
13
14#include <cstdlib>
15#include <iostream>
16#include <print>
17
23inline void clear_viewport() { std::print("\033[2J\033[1;1H"); }
24
28void render_dashboard(const TuringMachine &machine, int steps)
29{
31 std::print(
32 "==================================================\n"
33 " MODERN TURING MACHINE SIMULATOR (mtms) \n"
34 "==================================================\n"
35 " Current State : [{}]\n"
36 " Steps Executed: {}\n"
37 "--------------------------------------------------\n\n",
38 machine.get_current_state().get_label(),
39 steps
40 );
41
42 // Cascade rendering streams across all structurally active tapes
43 const auto &tapes = machine.get_tapes();
44 for (std::size_t i = 0; i < tapes.size(); ++i)
45 {
46 std::println(" [Tape {}] ", i);
47 tapes[i].print(std::cout);
48 std::println("");
49 }
50 std::println("==================================================");
51}
52
53int run_cli_session(TuringMachine &machine, std::string_view input_word, bool interactive)
54{
55 // Bootstrap validation pipeline over input alphabet parameters
56 if (!machine.load_input(input_word))
57 {
58 std::println(
59 stderr,
60 "\n\033[1;31m[!] Input string contains symbols outside the validation alphabet."
61 );
62 return EXIT_FAILURE;
63 }
64
65 int step_counter = 0;
66
67 // Interactive Mode
68 if (interactive)
69 {
70 render_dashboard(machine, step_counter);
71 std::print("\n Press [ENTER] to initialize computation step sequences...");
72 std::cin.ignore();
73
74 while (machine.step())
75 {
76 render_dashboard(machine, ++step_counter);
77 std::print("\n Press [ENTER] to advance execution ticks...");
78 std::cin.get();
79 }
80
81 render_dashboard(machine, step_counter);
82 std::print("\n Execution Halted. ");
83 std::println(
84 "{}",
86 ? "\033[1;32m[STRING ACCEPTED]\033[0m"
87 : "\033[1;31m[STRING REJECTED / HALT CRASH]\033[0m"
88 );
89 std::println("==================================================");
90 }
91 // Batch Mode
92 else
93 {
94 while (machine.step())
95 {
96 ++step_counter;
97 }
98
99 if (machine.get_current_state().is_accept())
100 {
101 std::println(
102 "STATUS=ACCEPTED;STEPS={};FINAL_STATE={}",
103 step_counter,
104 machine.get_current_state().get_label()
105 );
106 }
107 else
108 {
109 std::println(
110 "STATUS=REJECTED;STEPS={};FINAL_STATE={}",
111 step_counter,
112 machine.get_current_state().get_label()
113 );
114 return EXIT_FAILURE;
115 }
116 }
117
118 return EXIT_SUCCESS;
119}
std::string_view get_label() const noexcept
Getter for the state's unique label.
Definition state.hpp:50
bool is_accept() const noexcept
Checks if this state is an accepting (final) state.
Definition state.hpp:56
The central execution engine representing a deterministic Multi-Tape Turing Machine.
Definition tm.hpp:33
bool step() noexcept(false)
Executes a single computational transition step across all parallel tapes.
Definition tm.cpp:61
bool load_input(std::string_view input) noexcept(false)
Validates an input word against the input alphabet and loads it onto Tape 0.
Definition tm.cpp:41
const std::vector< Tape > & get_tapes() const noexcept
Retrieves the entire collection layout of parallel tape tracks.
Definition tm.hpp:122
const State & get_current_state() const noexcept
Retrieves the active operational state context marker.
Definition tm.hpp:116
int run_cli_session(TuringMachine &machine, std::string_view input_word, bool interactive)
Executes the simulation via the terminal view layer.
Definition cli.cpp:53
void clear_viewport()
Flushes the active terminal frame using standard ANSI escape codes.
Definition cli.cpp:23
void render_dashboard(const TuringMachine &machine, int steps)
Renders the consolidated simulation status metrics and stacked tapes layout.
Definition cli.cpp:28
Header definition for the interactive CLI dashboard interface.