This commit is contained in:
Михаил Капелько
2024-05-26 22:39:52 +03:00
parent d619522f7d
commit ed7d1cd486
23 changed files with 234 additions and 41 deletions

View File

@@ -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)

View File

@@ -1,6 +1,7 @@
#include <map>
#include <string>
#include <vector>
#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;
}

View File

@@ -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
#}

View File

@@ -1,6 +1,7 @@
#include <map>
#include <string>
#include <vector>
#include "llm.h"
#include "memory_Context.h"
#include "main.h"

View File

@@ -1,5 +1,6 @@
#include <map>
#include <string>
#include <vector>
#// file unused
#include "memory.h"
#include "memory_Context.h"

View File

@@ -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

View File

@@ -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 \

11
v4/llm.cpp Normal file
View File

@@ -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);
}

9
v4/llm.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef llm_HEADER
#define llm_HEADER
#include <string>
bool llm_isDigit(const std::string &str);
int llm_strToInt(const std::string &str);
#endif // llm_HEADER

View File

@@ -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)

37
v4/llm_test.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include <map>
#include <string>
#include <vector>
#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";
}

31
v4/llm_test.py Normal file
View File

@@ -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"
#}

21
v4/llm_test_Python.py Normal file
View File

@@ -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"
#}

View File

@@ -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);

View File

@@ -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

View File

@@ -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)

View File

@@ -1,6 +1,7 @@
#include <map>
#include <string>
#include <vector>
#include "llm.h"
#include "memory_Context.h"
#include "main.h"

View File

@@ -6,8 +6,18 @@
#define memory_Context_HEADER
struct memory_Context {
bool didLaunch = false;
bool exit = false;
std::string input = "";
std::vector<int> hiddenItems;
std::vector<int> mismatchedItems;
std::string outputGoOn = "";
std::string outputGreeting = "";
std::string outputHelp = "";
std::string outputMatchedItems = "";
std::string outputMismatchedItems = "";
std::string outputPromptSelection = "";
std::string outputVictory = "";
std::map<int, int> playfieldItems;
int playfieldSize = 0;
std::string recentField = "none";

View File

@@ -1,6 +1,7 @@
class memory_Context:
def __init__(self):
self.didLaunch = False
self.exit = False
self.hiddenItems = []
self.input = ""
self.mismatchedItems = []

View File

@@ -1,6 +1,7 @@
#include <map>
#include <string>
#include <vector>
#include "llm.h"
#include "memory_Context.h"
#include "main.h"

View File

@@ -1,15 +0,0 @@
#include <string>
#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

View File

@@ -4,6 +4,7 @@ def includes():
return """#include <map>
#include <string>
#include <vector>
#include "llm.h"
#include "memory_Context.h"
#include "main.h"
"""

View File

@@ -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