This commit is contained in:
Михаил Капелько
2024-05-08 22:47:34 +03:00
parent 251da5d3f9
commit 5fbc03fd87
22 changed files with 275 additions and 83 deletions

34
v4/cli.cpp Normal file
View File

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

9
v4/cli.h Normal file
View File

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

View File

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

5
v4/cli_Context.cpp Normal file
View File

@@ -0,0 +1,5 @@
#include "cli_Context.h"
cli_Context cli_createContext() {
return cli_Context();
}

17
v4/cli_Context.h Normal file
View File

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

View File

@@ -3,6 +3,7 @@ class cli_Context:
self.input = ""
self.outputGreeting = ""
self.outputHelp = ""
self.recentField = "none"
def cli_createContext():
return cli_Context()

47
v4/cli_test.cpp Normal file
View File

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

10
v4/cli_test.h Normal file
View File

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

View File

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

View File

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

View File

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

View File

@@ -1,9 +1,11 @@
OPT_HEADER/OPT_INCLUDES: 'None'/'None'
#include <map>
#include <string>
#include <vector>
#include "cli.h"
#include "cli_Context.h"
#include "memory.h"
#include "memory_Context.h"
#include "shell.h"
////////////////
// Client initiated input

View File

@@ -1,9 +1,11 @@
OPT_HEADER/OPT_INCLUDES: 'None'/'None'
#include <map>
#include <string>
#include <vector>
#include "cli.h"
#include "cli_Context.h"
#include "memory.h"
#include "memory_Context.h"
#include "shell.h"
std::string memory_test_generateConstPlayfield(
) {

52
v4/shell.cpp Normal file
View File

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

9
v4/shell.h Normal file
View File

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

View File

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

5
v4/shell_Context.cpp Normal file
View File

@@ -0,0 +1,5 @@
#include "shell_Context.h"
shell_Context shell_createContext() {
return shell_Context();
}

18
v4/shell_Context.h Normal file
View File

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

View File

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

View File

@@ -4,12 +4,15 @@ def includes():
return """#include <map>
#include <string>
#include <vector>
#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)

View File

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

View File

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