From 5fbc03fd87cbf96e11d15b28f2671c49273d2436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Wed, 8 May 2024 22:47:34 +0300 Subject: [PATCH] d --- v4/cli.cpp | 34 ++++++++++++++++++++++++++ v4/cli.h | 9 +++++++ v4/cli.py | 2 +- v4/cli_Context.cpp | 5 ++++ v4/cli_Context.h | 17 +++++++++++++ v4/cli_Context.py | 1 + v4/cli_test.cpp | 47 +++++++++++++++++++++++++++++++++++ v4/cli_test.h | 10 ++++++++ v4/gen-C++ | 11 ++++++++- v4/main.cpp | 31 ++++++++++++++++++++--- v4/main.py | 22 ++++++----------- v4/memory.cpp | 4 ++- v4/memory_test.cpp | 4 ++- v4/shell.cpp | 52 +++++++++++++++++++++++++++++++++++++++ v4/shell.h | 9 +++++++ v4/shell.py | 9 +++---- v4/shell_Context.cpp | 5 ++++ v4/shell_Context.h | 18 ++++++++++++++ v4/shell_test.py | 54 ----------------------------------------- v4/tPythonC++/CPP.py | 9 ++++++- v4/tPythonC++/translate | 2 +- v4/translate-py-cxx | 3 +++ 22 files changed, 275 insertions(+), 83 deletions(-) create mode 100644 v4/cli.cpp create mode 100644 v4/cli.h create mode 100644 v4/cli_Context.cpp create mode 100644 v4/cli_Context.h create mode 100644 v4/cli_test.cpp create mode 100644 v4/cli_test.h create mode 100644 v4/shell.cpp create mode 100644 v4/shell.h create mode 100644 v4/shell_Context.cpp create mode 100644 v4/shell_Context.h delete mode 100644 v4/shell_test.py diff --git a/v4/cli.cpp b/v4/cli.cpp new file mode 100644 index 0000000..60aa279 --- /dev/null +++ b/v4/cli.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include "cli.h" +#include "cli_Context.h" +#include "memory.h" +#include "memory_Context.h" +#include "shell.h" + +// Greet the user +cli_Context cli_greetUser( + cli_Context c +) { + c.outputGreeting = "OGS Memory Textual UI"; + c.recentField = "outputGreeting"; + return c; +} + +// Show help (aka commands) +cli_Context cli_showHelp( + cli_Context c +) { + if ( + c.input == "h" || + c.input == "help" + ) { + c.outputHelp = "Commands:\n\te, exit, q, quit\n\t\tExit\n\th, help\n\t\tList commands\n"; + c.recentField = "outputHelp"; + return c; + } + c.recentField = "none"; + return c; +} + diff --git a/v4/cli.h b/v4/cli.h new file mode 100644 index 0000000..b789022 --- /dev/null +++ b/v4/cli.h @@ -0,0 +1,9 @@ +#include "cli_Context.h" + +#ifndef cli_HEADER +#define cli_HEADER + +cli_Context cli_greetUser(cli_Context); +cli_Context cli_showHelp(cli_Context); + +#endif // cli_HEADER diff --git a/v4/cli.py b/v4/cli.py index e18b0d6..666da1b 100644 --- a/v4/cli.py +++ b/v4/cli.py @@ -23,7 +23,7 @@ def cli_showHelp( c.outputHelp = "Commands:\n\te, exit, q, quit\n\t\tExit\n\th, help\n\t\tList commands\n" c.recentField = "outputHelp" return c - + #} c.recentField = "none" return c #} diff --git a/v4/cli_Context.cpp b/v4/cli_Context.cpp new file mode 100644 index 0000000..937bbc8 --- /dev/null +++ b/v4/cli_Context.cpp @@ -0,0 +1,5 @@ +#include "cli_Context.h" + +cli_Context cli_createContext() { + return cli_Context(); +} diff --git a/v4/cli_Context.h b/v4/cli_Context.h new file mode 100644 index 0000000..45b538b --- /dev/null +++ b/v4/cli_Context.h @@ -0,0 +1,17 @@ +#include +#include +#include + +#ifndef cli_Context_HEADER +#define cli_Context_HEADER + +struct cli_Context { + std::string input = ""; + std::string outputGreeting = ""; + std::string outputHelp = ""; + std::string recentField = "none"; +}; + +cli_Context cli_createContext(); + +#endif // cli_Context_HEADER diff --git a/v4/cli_Context.py b/v4/cli_Context.py index fe4ba7e..ef9b43d 100644 --- a/v4/cli_Context.py +++ b/v4/cli_Context.py @@ -3,6 +3,7 @@ class cli_Context: self.input = "" self.outputGreeting = "" self.outputHelp = "" + self.recentField = "none" def cli_createContext(): return cli_Context() diff --git a/v4/cli_test.cpp b/v4/cli_test.cpp new file mode 100644 index 0000000..fe70680 --- /dev/null +++ b/v4/cli_test.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include "cli.h" +#include "cli_Context.h" +#include "memory.h" +#include "memory_Context.h" +#include "shell.h" + +std::string cli_test_greetUser( +) { + auto c = cli_createContext(); + c = cli_greetUser(c); + if ( + c.recentField == "outputGreeting" + ) { + return "OK: cli_greetUser"; + } + return "ERR: cli_greetUser"; +} + +std::string cli_test_showHelp_h( +) { + auto c = cli_createContext(); + c.input = "h"; + c = cli_showHelp(c); + if ( + c.recentField == "outputHelp" + ) { + return "OK: cli_showHelp_h"; + } + return "ERR: cli_showHelp_h"; +} + +std::string cli_test_showHelp_help( +) { + auto c = cli_createContext(); + c.input = "help"; + c = cli_showHelp(c); + if ( + c.recentField == "outputHelp" + ) { + return "OK: cli_showHelp_help"; + } + return "ERR: cli_showHelp_help"; +} + diff --git a/v4/cli_test.h b/v4/cli_test.h new file mode 100644 index 0000000..e4842d1 --- /dev/null +++ b/v4/cli_test.h @@ -0,0 +1,10 @@ +#include + +#ifndef cli_test_HEADER +#define cli_test_HEADER + +std::string cli_test_greetUser(); +std::string cli_test_showHelp_h(); +std::string cli_test_showHelp_help(); + +#endif // cli_test_HEADER diff --git a/v4/gen-C++ b/v4/gen-C++ index d676c37..c26a212 100755 --- a/v4/gen-C++ +++ b/v4/gen-C++ @@ -1 +1,10 @@ -c++ -o test_memory_C++ -std=c++11 -I. memory.cpp memory_Context.cpp memory_test.cpp main.cpp +c++ -o test_memory_C++ -std=c++11 -I. \ + cli.cpp \ + cli_Context.cpp \ + cli_test.cpp \ + memory.cpp \ + memory_Context.cpp \ + memory_test.cpp \ + shell.cpp \ + shell_Context.cpp \ + main.cpp diff --git a/v4/main.cpp b/v4/main.cpp index 561a3f6..16d7801 100644 --- a/v4/main.cpp +++ b/v4/main.cpp @@ -1,13 +1,13 @@ #include #include #include +#include "cli_test.h" +#include "cli_Context.h" #include "memory_test.h" #include "memory_Context.h" - -extern memory_Context memory_createContext(); +#include "shell.h" int main() { - memory_createContext(); std::cout << memory_test_generateConstPlayfield() << std::endl @@ -26,4 +26,29 @@ int main() { << memory_test_shouldHideMatchingItems() << std::endl ; + + std::cout + << cli_test_greetUser() + << std::endl + << cli_test_showHelp_h() + << std::endl + << cli_test_showHelp_help() + << std::endl + ; + + auto c = shell_createContext(); + c.cCLI = cli_createContext(); + c = shell_launch(c); + std::cout << c.output << std::endl; + + while (true) { + std::string line; + getline(std::cin, line); + c.input = line; + c = shell_processInput(c); + if (c.exit) { + break; + } + std::cout << c.output << std::endl; + } } diff --git a/v4/main.py b/v4/main.py index 874bf31..c6aea05 100644 --- a/v4/main.py +++ b/v4/main.py @@ -2,7 +2,6 @@ from cli import * from cli_test import * from memory_test import * from shell import * -from shell_test import * import sys print(memory_test_generateConstPlayfield()) @@ -18,22 +17,15 @@ print(cli_test_greetUser()) print(cli_test_showHelp_h()) print(cli_test_showHelp_help()) -print(shell_test_exit_e()) -print(shell_test_exit_exit()) -print(shell_test_exit_q()) -print(shell_test_exit_quit()) - c = shell_createContext() c.cCLI = cli_createContext() -c = shell_start(c) -if c.recentField == "output": - print(c.output) +c = shell_launch(c) +print(c.output) for line in sys.stdin: - c.input = line.rstrip() - c = shell_processInput(c) - if c.recentField == "output": - print(c.output) - elif c.recentField == "exit": - break + c.input = line.rstrip() + c = shell_processInput(c) + if c.exit: + break + print(c.output) diff --git a/v4/memory.cpp b/v4/memory.cpp index 0ad95fd..0d5a54a 100644 --- a/v4/memory.cpp +++ b/v4/memory.cpp @@ -1,9 +1,11 @@ -OPT_HEADER/OPT_INCLUDES: 'None'/'None' #include #include #include +#include "cli.h" +#include "cli_Context.h" #include "memory.h" #include "memory_Context.h" +#include "shell.h" //////////////// // Client initiated input diff --git a/v4/memory_test.cpp b/v4/memory_test.cpp index 09f0b18..1cc2e77 100644 --- a/v4/memory_test.cpp +++ b/v4/memory_test.cpp @@ -1,9 +1,11 @@ -OPT_HEADER/OPT_INCLUDES: 'None'/'None' #include #include #include +#include "cli.h" +#include "cli_Context.h" #include "memory.h" #include "memory_Context.h" +#include "shell.h" std::string memory_test_generateConstPlayfield( ) { diff --git a/v4/shell.cpp b/v4/shell.cpp new file mode 100644 index 0000000..4577c81 --- /dev/null +++ b/v4/shell.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include "cli.h" +#include "cli_Context.h" +#include "memory.h" +#include "memory_Context.h" +#include "shell.h" + +// Greet the user upon start + +shell_Context shell_launch( + shell_Context c +) { + c.cCLI = cli_greetUser(c.cCLI); + c.cCLI.input = "help"; + c.cCLI = cli_showHelp(c.cCLI); + c.output = c.cCLI.outputGreeting + "\n" + c.cCLI.outputHelp; + return c; +} + +// Switch among CLI functions based on input +// +// Conditions: +// 1. User requested to quit the game +// 2. User plays the game + + + +shell_Context shell_processInput( + shell_Context c +) { + if ( + c.input == "e" || + c.input == "exit" || + c.input == "q" || + c.input == "quit" + ) { + c.exit = true; + return c; + } + c.cCLI.input = c.input; + c.output = ""; + c.cCLI = cli_showHelp(c.cCLI); + if ( + c.cCLI.recentField == "outputHelp" + ) { + c.output += c.cCLI.outputHelp; + } + return c; +} + diff --git a/v4/shell.h b/v4/shell.h new file mode 100644 index 0000000..a06e959 --- /dev/null +++ b/v4/shell.h @@ -0,0 +1,9 @@ +#include "shell_Context.h" + +#ifndef shell_HEADER +#define shell_HEADER + +shell_Context shell_launch(shell_Context); +shell_Context shell_processInput(shell_Context); + +#endif // shell_HEADER diff --git a/v4/shell.py b/v4/shell.py index daa047c..36a78c3 100644 --- a/v4/shell.py +++ b/v4/shell.py @@ -1,10 +1,10 @@ from cli import * -from shell_Context import * from llm import * +from shell_Context import * # Greet the user upon start @llm_by_value -def shell_start( +def shell_launch( c: shell_Context ) -> shell_Context: c.cCLI = cli_greetUser(c.cCLI) @@ -12,7 +12,6 @@ def shell_start( c.cCLI = cli_showHelp(c.cCLI) c.output = c.cCLI.outputGreeting + "\n" + c.cCLI.outputHelp - c.recentField = "output" return c #} @@ -32,8 +31,8 @@ def shell_processInput( c.input == "quit" ): c.exit = True - c.recentField = "exit" return c + #} c.cCLI.input = c.input c.output = "" @@ -43,7 +42,7 @@ def shell_processInput( c.cCLI.recentField == "outputHelp" ): c.output += c.cCLI.outputHelp + #} - c.recentField = "output" return c #} diff --git a/v4/shell_Context.cpp b/v4/shell_Context.cpp new file mode 100644 index 0000000..b36f9a4 --- /dev/null +++ b/v4/shell_Context.cpp @@ -0,0 +1,5 @@ +#include "shell_Context.h" + +shell_Context shell_createContext() { + return shell_Context(); +} diff --git a/v4/shell_Context.h b/v4/shell_Context.h new file mode 100644 index 0000000..f8f5760 --- /dev/null +++ b/v4/shell_Context.h @@ -0,0 +1,18 @@ +#include +#include +#include +#include "cli_Context.h" + +#ifndef shell_Context_HEADER +#define shell_Context_HEADER + +struct shell_Context { + cli_Context cCLI; + bool exit = false; + std::string input = ""; + std::string output = ""; +}; + +shell_Context shell_createContext(); + +#endif // shell_Context_HEADER diff --git a/v4/shell_test.py b/v4/shell_test.py deleted file mode 100644 index 097846a..0000000 --- a/v4/shell_test.py +++ /dev/null @@ -1,54 +0,0 @@ -from shell import * -from shell_Context import * - -def shell_test_exit_e( -) -> str: - c = shell_createContext() - c.input = "e" - c = shell_processInput(c) - if ( - c.recentField == "exit" - ): - return "OK: shell_exit_e" - #} - return "ERR: shell_exit_e" -#} - -def shell_test_exit_exit( -) -> str: - c = shell_createContext() - c.input = "exit" - c = shell_processInput(c) - if ( - c.recentField == "exit" - ): - return "OK: shell_exit_exit" - #} - return "ERR: shell_exit_exit" -#} - -def shell_test_exit_q( -) -> str: - c = shell_createContext() - c.input = "q" - c = shell_processInput(c) - if ( - c.recentField == "exit" - ): - return "OK: shell_exit_q" - #} - return "ERR: shell_exit_q" -#} - -def shell_test_exit_quit( -) -> str: - c = shell_createContext() - c.input = "quit" - c = shell_processInput(c) - if ( - c.recentField == "exit" - ): - return "OK: shell_exit_quit" - #} - return "ERR: shell_exit_quit" -#} diff --git a/v4/tPythonC++/CPP.py b/v4/tPythonC++/CPP.py index 97c551c..315274b 100644 --- a/v4/tPythonC++/CPP.py +++ b/v4/tPythonC++/CPP.py @@ -4,12 +4,15 @@ def includes(): return """#include #include #include +#include "cli.h" +#include "cli_Context.h" #include "memory.h" #include "memory_Context.h" +#include "shell.h" """ def replaceAnd(s): - return s.replace("and", "&&") + return s.replace(" and", " &&") def replaceAppend(s): return s.replace(".append(", ".push_back(") @@ -30,6 +33,9 @@ def replaceLen(s): after = s[posEnd + len(")"):] return f"{before}{name}.size(){after}" +def replaceOr(s): + return s.replace(" or", " ||") + def replaceTrue(s): return s.replace("True", "true") @@ -170,6 +176,7 @@ class CPP: for i in range(0, len(self.fn.statements)): s = translateStatement(self.fn.statements[i], self) s = replaceAnd(s) + s = replaceOr(s) s = replaceAppend(s) # Replace len twice to account for double invocation. s = replaceLen(s) diff --git a/v4/tPythonC++/translate b/v4/tPythonC++/translate index d202e44..0dcc59a 100755 --- a/v4/tPythonC++/translate +++ b/v4/tPythonC++/translate @@ -21,5 +21,5 @@ for arg in sys.argv: OPT_INCLUDES = arg[len(OPT_PREFIX_INCLUDES):] # Translate file. -out = process(FILE_IN, OPT_HEADER, OPT_INCLUDES) +out = process(FILE_IN)#, OPT_HEADER, OPT_INCLUDES) print(out) diff --git a/v4/translate-py-cxx b/v4/translate-py-cxx index 01901fc..6ece697 100755 --- a/v4/translate-py-cxx +++ b/v4/translate-py-cxx @@ -5,3 +5,6 @@ TR=$DIR/tPythonC++/translate $TR $DIR/memory.py > $DIR/memory.cpp $TR $DIR/memory_test.py > $DIR/memory_test.cpp +$TR $DIR/cli.py > $DIR/cli.cpp +$TR $DIR/cli_test.py > $DIR/cli_test.cpp +$TR $DIR/shell.py > $DIR/shell.cpp