From 40953895787b4e0db776b180ee7ae3dbbf7089da 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: Thu, 11 Apr 2024 16:46:56 +0300 Subject: [PATCH] d --- tr-Python-C++/CPP.py | 11 +++++++++++ tr-Python-C++/process.py | 2 +- v2/entities.cpp | 5 +++++ v2/entities.h | 9 +++++++++ v2/functions.cpp | 42 ++++++++++++++++++++++++++++++++++++++++ v2/functions.h | 8 ++++++++ v2/functions.py | 11 +++++------ v2/gen-C++ | 1 + v2/main.cpp | 8 ++++++++ 9 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 v2/entities.cpp create mode 100644 v2/entities.h create mode 100644 v2/functions.cpp create mode 100644 v2/functions.h create mode 100755 v2/gen-C++ create mode 100644 v2/main.cpp diff --git a/tr-Python-C++/CPP.py b/tr-Python-C++/CPP.py index 6778c58..4d4ab65 100644 --- a/tr-Python-C++/CPP.py +++ b/tr-Python-C++/CPP.py @@ -1,5 +1,11 @@ from Function import * +def includes(): + return """#include +#include +#include "entities.h" +""" + def replaceAnd(s): return s.replace("and", "&&") @@ -34,6 +40,7 @@ def translateStatement(s, state): ss = s.lstrip() posColon = ss.find(": ") posComma = ss.find(", ") + posCtx = ss.find("c.") posEqual = ss.find(" = ") posFor = ss.find("for ") posIn = ss.find(" in ") @@ -70,6 +77,7 @@ def translateStatement(s, state): # name = value -> auto name = value if ( + posCtx == -1 and posColon == -1 and posOpenSquareBracket == -1 and posEqual >= 0 @@ -120,6 +128,9 @@ class CPP: params = [] for i in range(0, len(self.fn.parameters)): p = translateParameter(self.fn.parameters[i]) + # Make Context passed by reference. + if "Context" in p: + p = p.replace("Context", "Context&") params.append(p) strparams = "\n".join(params) if (len(strparams) > 0): diff --git a/tr-Python-C++/process.py b/tr-Python-C++/process.py index a2e496e..b6779ae 100755 --- a/tr-Python-C++/process.py +++ b/tr-Python-C++/process.py @@ -9,7 +9,7 @@ def process(FILE_IN): lines_in.append(line.rstrip()) f = Function() - out = "" + out = includes() # Parse. for ln in lines_in: diff --git a/v2/entities.cpp b/v2/entities.cpp new file mode 100644 index 0000000..9954d8c --- /dev/null +++ b/v2/entities.cpp @@ -0,0 +1,5 @@ +#include "entities.h" + +MemoryContext memory_createEmptyContext() { + return MemoryContext(); +} diff --git a/v2/entities.h b/v2/entities.h new file mode 100644 index 0000000..f5aa527 --- /dev/null +++ b/v2/entities.h @@ -0,0 +1,9 @@ +#include +#include + +struct MemoryContext { + int playfieldSize = 0; + std::map playfieldItems; +}; + +MemoryContext memory_createEmptyContext(); diff --git a/v2/functions.cpp b/v2/functions.cpp new file mode 100644 index 0000000..fbef116 --- /dev/null +++ b/v2/functions.cpp @@ -0,0 +1,42 @@ +#include +#include +#include "entities.h" + + +// L4: Function. + +std::string memory_generateConstPlayfield( + MemoryContext& c +) { + std::map idGroups = { }; + auto id = 0; + for (auto gid = 0; gid < c.playfieldSize; ++gid) { + idGroups[id] = gid; + id += 1; + idGroups[id] = gid; + id += 1; + } + c.playfieldItems = idGroups; + return "MemoryContext.playfieldItems"; +} + +// L20: Test. + +std::string test_memory_generateConstPlayfield( +) { + auto c = memory_createEmptyContext(); + c.playfieldSize = 2; + auto fieldName = memory_generateConstPlayfield(c); + if ( + fieldName == "MemoryContext.playfieldItems" && + c.playfieldItems.size() == 4 && + c.playfieldItems[0] == 0 && + c.playfieldItems[1] == 0 && + c.playfieldItems[2] == 1 && + c.playfieldItems[3] == 1 + ) { + return "OK: memory_generateConstPlayfield"; + } + return "ERR: memory_generateConstPlayfield"; +} + diff --git a/v2/functions.h b/v2/functions.h new file mode 100644 index 0000000..03c1b7f --- /dev/null +++ b/v2/functions.h @@ -0,0 +1,8 @@ +#include "entities.h" + +std::string memory_generateConstPlayfield( + MemoryContext& c +); + +std::string test_memory_generateConstPlayfield( +); diff --git a/v2/functions.py b/v2/functions.py index abcba66..d46aa4d 100644 --- a/v2/functions.py +++ b/v2/functions.py @@ -25,14 +25,13 @@ def test_memory_generateConstPlayfield( c = memory_createEmptyContext() c.playfieldSize = 2 fieldName = memory_generateConstPlayfield(c) - idGroups = c.playfieldItems if ( fieldName == "MemoryContext.playfieldItems" and - len(idGroups) == 4 and - idGroups[0] == 0 and - idGroups[1] == 0 and - idGroups[2] == 1 and - idGroups[3] == 1 + len(c.playfieldItems) == 4 and + c.playfieldItems[0] == 0 and + c.playfieldItems[1] == 0 and + c.playfieldItems[2] == 1 and + c.playfieldItems[3] == 1 ): return "OK: memory_generateConstPlayfield" #} diff --git a/v2/gen-C++ b/v2/gen-C++ new file mode 100755 index 0000000..35dc0a4 --- /dev/null +++ b/v2/gen-C++ @@ -0,0 +1 @@ +c++ -o test_memory_C++ -std=c++11 -I. entities.cpp functions.cpp main.cpp diff --git a/v2/main.cpp b/v2/main.cpp new file mode 100644 index 0000000..90dfbbd --- /dev/null +++ b/v2/main.cpp @@ -0,0 +1,8 @@ +#include +#include +#include "functions.h" + + +int main() { + printf("%s\n", test_memory_generateConstPlayfield().c_str()); +}