From ed7d1cd48667d66ff66d8c7c5de481a612286f5f 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: Sun, 26 May 2024 22:39:52 +0300 Subject: [PATCH] d --- v4/Controller.py | 4 ++++ v4/cli.cpp | 5 +++-- v4/cli.py | 4 ++-- v4/cli_test.cpp | 1 + v4/cpp.includes | 1 + v4/cxx-headers | 4 +++- v4/gen-C++ | 7 ++++--- v4/llm.cpp | 11 +++++++++++ v4/llm.h | 9 +++++++++ v4/llm.py | 10 +++++++++- v4/llm_test.cpp | 37 +++++++++++++++++++++++++++++++++++++ v4/llm_test.py | 31 +++++++++++++++++++++++++++++++ v4/llm_test_Python.py | 21 +++++++++++++++++++++ v4/main.cpp | 34 +++++++++++++++++++++++++++++++++- v4/main.h | 26 ++++++++++++++++++++++++++ v4/main.py | 39 +++++++++++++++++++++++---------------- v4/memory.cpp | 1 + v4/memory_Context.h | 10 ++++++++++ v4/memory_Context.py | 1 + v4/memory_test.cpp | 1 + v4/memory_test.h | 15 --------------- v4/tPythonC++/CPP.py | 1 + v4/translate-py-cxx | 2 ++ 23 files changed, 234 insertions(+), 41 deletions(-) create mode 100644 v4/llm.cpp create mode 100644 v4/llm.h create mode 100644 v4/llm_test.cpp create mode 100644 v4/llm_test.py create mode 100644 v4/llm_test_Python.py delete mode 100644 v4/memory_test.h diff --git a/v4/Controller.py b/v4/Controller.py index c469511..21a85e7 100644 --- a/v4/Controller.py +++ b/v4/Controller.py @@ -38,6 +38,10 @@ class Controller: def registerFunction(self, f): self.functions.append(f) + def registerFunctions(self, funcs): + for f in funcs: + self.functions.append(f) + def reportContext(self): for cb in self.callbacks: cb(self.context) diff --git a/v4/cli.cpp b/v4/cli.cpp index 683e509..cdd8a91 100644 --- a/v4/cli.cpp +++ b/v4/cli.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "llm.h" #include "memory_Context.h" #include "main.h" @@ -135,11 +136,11 @@ memory_Context cli_selectItem( ) { if ( c.recentField == "input" && - c.input.isdigit() + llm_isDigit(c.input) ) { // CLI ids start with 1 while memory module has ids starting with 0 // Convert CLI id to memory id - c.selectedId = int(c.input) - 1; + c.selectedId = llm_strToInt(c.input) - 1; c.recentField = "selectedId"; return c; } diff --git a/v4/cli.py b/v4/cli.py index 45b4152..1b6637e 100644 --- a/v4/cli.py +++ b/v4/cli.py @@ -139,11 +139,11 @@ def cli_selectItem( ) -> memory_Context: if ( c.recentField == "input" and - c.input.isdigit() + llm_isDigit(c.input) ): # CLI ids start with 1 while memory module has ids starting with 0 # Convert CLI id to memory id - c.selectedId = int(c.input) - 1 + c.selectedId = llm_strToInt(c.input) - 1 c.recentField = "selectedId" return c #} diff --git a/v4/cli_test.cpp b/v4/cli_test.cpp index 9ddcf50..c24f5bd 100644 --- a/v4/cli_test.cpp +++ b/v4/cli_test.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "llm.h" #include "memory_Context.h" #include "main.h" diff --git a/v4/cpp.includes b/v4/cpp.includes index f055234..2765a38 100644 --- a/v4/cpp.includes +++ b/v4/cpp.includes @@ -1,5 +1,6 @@ #include #include #include +#// file unused #include "memory.h" #include "memory_Context.h" diff --git a/v4/cxx-headers b/v4/cxx-headers index 75726c7..eb8d0fc 100755 --- a/v4/cxx-headers +++ b/v4/cxx-headers @@ -5,7 +5,9 @@ TMP=/tmp/cxx-headers.tmp grep '^memory_Context ' memory.cpp > $TMP grep '^std::string ' memory_test.cpp >> $TMP -#grep '^memory_Context ' cli.cpp >> $TMP +grep '^memory_Context ' cli.cpp >> $TMP +grep '^std::string ' cli_test.cpp >> $TMP +grep '^std::string ' llm_test.cpp >> $TMP IN=$TMP OUT=$DIR/main.h diff --git a/v4/gen-C++ b/v4/gen-C++ index f31100e..4f3d614 100755 --- a/v4/gen-C++ +++ b/v4/gen-C++ @@ -2,7 +2,8 @@ c++ -o test_memory_C++ -std=c++11 -I. \ memory.cpp \ memory_Context.cpp \ memory_test.cpp \ + llm.cpp \ + llm_test.cpp \ + cli.cpp \ + cli_test.cpp \ main.cpp - -# cli.cpp \ -# cli_test.cpp \ diff --git a/v4/llm.cpp b/v4/llm.cpp new file mode 100644 index 0000000..a617851 --- /dev/null +++ b/v4/llm.cpp @@ -0,0 +1,11 @@ +#include "llm.h" + +bool llm_isDigit(const std::string &str) { + return + !str.empty() && + str.find_first_not_of("0123456789") == std::string::npos; +} + +int llm_strToInt(const std::string &str) { + return std::stoi(str); +} diff --git a/v4/llm.h b/v4/llm.h new file mode 100644 index 0000000..645cf30 --- /dev/null +++ b/v4/llm.h @@ -0,0 +1,9 @@ +#ifndef llm_HEADER +#define llm_HEADER + +#include + +bool llm_isDigit(const std::string &str); +int llm_strToInt(const std::string &str); + +#endif // llm_HEADER diff --git a/v4/llm.py b/v4/llm.py index ab3067e..b8848a0 100644 --- a/v4/llm.py +++ b/v4/llm.py @@ -1,6 +1,6 @@ import copy -# Make deep copies of arguments to mimic behaviour of strongly typed languages. +# Make deep copies of arguments to treat the arguments as structs. # https://stackoverflow.com/a/15398021 def llm_by_value(f): def _f(*args, **kwargs): @@ -8,3 +8,11 @@ def llm_by_value(f): kwargsCopy = copy.deepcopy(kwargs) return f(*argsCopy, **kwargsCopy) return _f + +# Tell if string is a digit +def llm_isDigit(s): + return s.isdigit() + +# Convert string to integer +def llm_strToInt(s): + return int(s) diff --git a/v4/llm_test.cpp b/v4/llm_test.cpp new file mode 100644 index 0000000..a0049ba --- /dev/null +++ b/v4/llm_test.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include "llm.h" +#include "memory_Context.h" +#include "main.h" + +std::string llm_test_isDigit_digit( +) { + if ( + llm_isDigit("123") + ) { + return "OK: llm_isDigit_digit"; + } + return "ERR: llm_isDigit_digit"; +} + +std::string llm_test_isDigit_notDigit( +) { + if ( + llm_isDigit("abc") + ) { + return "ERR: llm_isDigit_notDigit"; + } + return "OK: llm_isDigit_notDigit"; +} + +std::string llm_test_strToInt( +) { + if ( + llm_strToInt("123") == 123 + ) { + return "OK: llm_strToInt"; + } + return "ERR: llm_strToInt"; +} + diff --git a/v4/llm_test.py b/v4/llm_test.py new file mode 100644 index 0000000..17ee0ca --- /dev/null +++ b/v4/llm_test.py @@ -0,0 +1,31 @@ +from llm import * + +def llm_test_isDigit_digit( +) -> str: + if ( + llm_isDigit("123") + ): + return "OK: llm_isDigit_digit" + #} + return "ERR: llm_isDigit_digit" +#} + +def llm_test_isDigit_notDigit( +) -> str: + if ( + llm_isDigit("abc") + ): + return "ERR: llm_isDigit_notDigit" + #} + return "OK: llm_isDigit_notDigit" +#} + +def llm_test_strToInt( +) -> str: + if ( + llm_strToInt("123") == 123 + ): + return "OK: llm_strToInt" + #} + return "ERR: llm_strToInt" +#} diff --git a/v4/llm_test_Python.py b/v4/llm_test_Python.py new file mode 100644 index 0000000..8c0e39c --- /dev/null +++ b/v4/llm_test_Python.py @@ -0,0 +1,21 @@ +from llm import * +from memory_Context import * + +def llm_test_Python_copyByValue( +) -> str: + c = memory_createContext() + c.input = "abc" + + @llm_by_value + def alterValue(c): + c.input = "alteredValue" + + alterValue(c) + + if ( + c.input == "abc" + ): + return "OK: llm_Python_copyByValue" + #} + return "ERR: llm_Python_copyByValue" +#} diff --git a/v4/main.cpp b/v4/main.cpp index 06462d4..3a4c0ce 100644 --- a/v4/main.cpp +++ b/v4/main.cpp @@ -5,6 +5,15 @@ #include "memory_Context.h" int main() { + std::cout + << llm_test_isDigit_digit() + << std::endl + << llm_test_isDigit_notDigit() + << std::endl + << llm_test_strToInt() + << std::endl + ; + std::cout << memory_test_detectMismatchedItems() << std::endl @@ -24,16 +33,39 @@ int main() { << std::endl ; - /* std::cout + << cli_test_exit_e() + << std::endl + << cli_test_exit_exit() + << std::endl + << cli_test_exit_victory() + << std::endl + << cli_test_exit_q() + << std::endl + << cli_test_exit_quit() + << std::endl + << cli_test_goOn() + << std::endl << cli_test_greetUser() << std::endl << cli_test_showHelp_h() << std::endl << cli_test_showHelp_help() << std::endl + << cli_test_selectItem() + << std::endl + << cli_test_promptSecondItemSelection() + << std::endl + << cli_test_reportMatchedItems() + << std::endl + << cli_test_reportMismatchedItems() + << std::endl + << cli_test_reportVictory() + << std::endl ; + /* + auto c = shell_createContext(); c.cCLI = cli_createContext(); c = shell_launch(c); diff --git a/v4/main.h b/v4/main.h index 104e3ad..3914930 100644 --- a/v4/main.h +++ b/v4/main.h @@ -17,6 +17,32 @@ std::string memory_test_hideMatchingItems(); std::string memory_test_selectItem_1x(); std::string memory_test_selectItem_2x(); std::string memory_test_selectItem_3x(); +memory_Context cli_exit(memory_Context); +memory_Context cli_goOn(memory_Context); +memory_Context cli_greetUser(memory_Context); +memory_Context cli_promptSecondItemSelection(memory_Context); +memory_Context cli_reportMatchedItems(memory_Context); +memory_Context cli_reportVictory(memory_Context); +memory_Context cli_selectItem(memory_Context); +memory_Context cli_showHelp(memory_Context); +memory_Context cli_reportMismatchedItems(memory_Context); +std::string cli_test_exit_e(); +std::string cli_test_exit_exit(); +std::string cli_test_exit_victory(); +std::string cli_test_exit_q(); +std::string cli_test_exit_quit(); +std::string cli_test_goOn(); +std::string cli_test_greetUser(); +std::string cli_test_promptSecondItemSelection(); +std::string cli_test_reportMatchedItems(); +std::string cli_test_reportMismatchedItems(); +std::string cli_test_selectItem(); +std::string cli_test_showHelp_h(); +std::string cli_test_showHelp_help(); +std::string cli_test_reportVictory(); +std::string llm_test_isDigit_digit(); +std::string llm_test_isDigit_notDigit(); +std::string llm_test_strToInt(); #endif // main_HEADER diff --git a/v4/main.py b/v4/main.py index 6ba71f7..d028108 100644 --- a/v4/main.py +++ b/v4/main.py @@ -1,9 +1,16 @@ from cli import * from cli_test import * +from llm_test import * +from llm_test_Python import * from memory_test import * from Controller import * import sys +print(llm_test_Python_copyByValue()) +print(llm_test_isDigit_digit()) +print(llm_test_isDigit_notDigit()) +print(llm_test_strToInt()) + print(memory_test_detectMismatchedItems()) print(memory_test_detectMismatchedItems_itemTwice()) print(memory_test_detectVictory()) @@ -28,28 +35,28 @@ print(cli_test_reportMatchedItems()) print(cli_test_reportMismatchedItems()) print(cli_test_reportVictory()) - ctrl = Controller(memory_createContext()) -ctrl.registerFunction(cli_exit) -ctrl.registerFunction(cli_goOn) -ctrl.registerFunction(cli_greetUser) -ctrl.registerFunction(cli_promptSecondItemSelection) -ctrl.registerFunction(cli_reportMatchedItems) -ctrl.registerFunction(cli_reportMismatchedItems) -ctrl.registerFunction(cli_reportVictory) -ctrl.registerFunction(cli_selectItem) -ctrl.registerFunction(cli_showHelp) -ctrl.registerFunction(memory_detectMismatchedItems) -ctrl.registerFunction(memory_detectVictory) -ctrl.registerFunction(memory_generateConstPlayfield) -ctrl.registerFunction(memory_hideMatchingItems) -ctrl.registerFunction(memory_selectItem) +ctrl.registerFunctions([ + cli_exit, + cli_goOn, + cli_greetUser, + cli_promptSecondItemSelection, + cli_reportMatchedItems, + cli_reportMismatchedItems, + cli_reportVictory, + cli_selectItem, + cli_showHelp, + memory_detectMismatchedItems, + memory_detectVictory, + memory_generateConstPlayfield, + memory_hideMatchingItems, + memory_selectItem, +]) def printOutput(c): if c.recentField.startswith("output"): print(c.field(c.recentField)) ctrl.registerCallback(printOutput) -#ctrl.registerCallback(lambda c: print(f"ИГР App.dbg ctx: '{c}'")) ctrl.registerFieldCallback("exit", lambda c: sys.exit(0)) ctrl.set("didLaunch", True) diff --git a/v4/memory.cpp b/v4/memory.cpp index 940bcc9..7b731cf 100644 --- a/v4/memory.cpp +++ b/v4/memory.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "llm.h" #include "memory_Context.h" #include "main.h" diff --git a/v4/memory_Context.h b/v4/memory_Context.h index 46595ba..16bd33e 100644 --- a/v4/memory_Context.h +++ b/v4/memory_Context.h @@ -6,8 +6,18 @@ #define memory_Context_HEADER struct memory_Context { + bool didLaunch = false; + bool exit = false; + std::string input = ""; std::vector hiddenItems; std::vector mismatchedItems; + std::string outputGoOn = ""; + std::string outputGreeting = ""; + std::string outputHelp = ""; + std::string outputMatchedItems = ""; + std::string outputMismatchedItems = ""; + std::string outputPromptSelection = ""; + std::string outputVictory = ""; std::map playfieldItems; int playfieldSize = 0; std::string recentField = "none"; diff --git a/v4/memory_Context.py b/v4/memory_Context.py index a7ad78c..1eeed0d 100644 --- a/v4/memory_Context.py +++ b/v4/memory_Context.py @@ -1,6 +1,7 @@ class memory_Context: def __init__(self): self.didLaunch = False + self.exit = False self.hiddenItems = [] self.input = "" self.mismatchedItems = [] diff --git a/v4/memory_test.cpp b/v4/memory_test.cpp index 80c0979..5936364 100644 --- a/v4/memory_test.cpp +++ b/v4/memory_test.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "llm.h" #include "memory_Context.h" #include "main.h" diff --git a/v4/memory_test.h b/v4/memory_test.h deleted file mode 100644 index 313d8c5..0000000 --- a/v4/memory_test.h +++ /dev/null @@ -1,15 +0,0 @@ -#include - -#ifndef memory_test_HEADER -#define memory_test_HEADER - -std::string memory_test_generateConstPlayfield(); -std::string memory_test_selectItem_1x(); -std::string memory_test_selectItem_2x(); -std::string memory_test_selectItem_3x(); -std::string memory_test_shouldDeselectMismatchedItems(); -std::string memory_test_shouldDeselectMismatchedItems_itemTwice(); -std::string memory_test_shouldDetectVictory(); -std::string memory_test_shouldHideMatchingItems(); - -#endif // memory_test_HEADER diff --git a/v4/tPythonC++/CPP.py b/v4/tPythonC++/CPP.py index f9de6db..7516839 100644 --- a/v4/tPythonC++/CPP.py +++ b/v4/tPythonC++/CPP.py @@ -4,6 +4,7 @@ def includes(): return """#include #include #include +#include "llm.h" #include "memory_Context.h" #include "main.h" """ diff --git a/v4/translate-py-cxx b/v4/translate-py-cxx index 2ca0efa..2b29e66 100755 --- a/v4/translate-py-cxx +++ b/v4/translate-py-cxx @@ -7,3 +7,5 @@ $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/llm_test.py > $DIR/llm_test.cpp +$DIR/cxx-headers