From 1c6e0d5f0004a5310d833358f7eb470c28ffec23 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 15:50:52 +0300 Subject: [PATCH] d --- functions.py | 35 +++++++++++++++++++++++++++++++++++ main.py | 38 +------------------------------------- tr-Python-C++/CPP.py | 3 +++ tr-Python-C++/process.py | 21 +++++++++++++++++---- 4 files changed, 56 insertions(+), 41 deletions(-) create mode 100644 functions.py diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..069ebd2 --- /dev/null +++ b/functions.py @@ -0,0 +1,35 @@ + + + +# L4: Function. + +def memory_generateConstPlayfield( + n: int +) -> dict[int, int]: + idGroups: dict[int, int] = { } + id = 0 + for gid in range(0, n): + idGroups[id] = gid + id += 1 + idGroups[id] = gid + id += 1 + #} + return idGroups +#} + +# L20: Test. + +def test_memory_generateConstPlayfield( +) -> str: + idGroups = memory_generateConstPlayfield(2) + if ( + len(idGroups) == 4 and + idGroups[0] == 0 and + idGroups[1] == 0 and + idGroups[2] == 1 and + idGroups[3] == 1 + ): + return "OK: memory_generateConstPlayfield" + #} + return "ERR: memory_generateConstPlayfield" +#} diff --git a/main.py b/main.py index 80e386a..f5989e6 100644 --- a/main.py +++ b/main.py @@ -1,39 +1,3 @@ - - - -# L4: Function. - -def memory_generateConstPlayfield( - n: int -) -> dict[int, int]: - idGroups: dict[int, int] = { } - id = 0 - for gid in range(0, n): - idGroups[id] = gid - id += 1 - idGroups[id] = gid - id += 1 - #} - return idGroups -#} - -# L20: Test. - -def test_memory_generateConstPlayfield( -) -> str: - idGroups = memory_generateConstPlayfield(2) - if ( - len(idGroups) == 4 and - idGroups[0] == 0 and - idGroups[1] == 0 and - idGroups[2] == 1 and - idGroups[3] == 1 - ): - return "OK: memory_generateConstPlayfield" - #} - return "ERR: memory_generateConstPlayfield" -#} - -# L36: Run. +from functions import * print(test_memory_generateConstPlayfield()) diff --git a/tr-Python-C++/CPP.py b/tr-Python-C++/CPP.py index dfbd595..6778c58 100644 --- a/tr-Python-C++/CPP.py +++ b/tr-Python-C++/CPP.py @@ -3,6 +3,9 @@ from Function import * def replaceAnd(s): return s.replace("and", "&&") +def replaceComment(s): + return s.replace("#", "//") + def replaceLen(s): posLen = s.find("len(") posEnd = s.find(")") diff --git a/tr-Python-C++/process.py b/tr-Python-C++/process.py index ad626b8..a2e496e 100755 --- a/tr-Python-C++/process.py +++ b/tr-Python-C++/process.py @@ -9,16 +9,29 @@ def process(FILE_IN): lines_in.append(line.rstrip()) f = Function() + out = "" # Parse. for ln in lines_in: ln = ln.rstrip() - f.parseLine(ln) + + # Empty line. + if ln == "": + out += "\n" + # Comment. + elif ( + ln.startswith("#") and + not f.isBody + ): + out += replaceComment(ln) + "\n" + # Function. + else: + f.parseLine(ln) + if f.isComplete: - print("Function ready! Here it is in C++:") cpp = CPP(f) - print(cpp.translate()) + out += cpp.translate() # Create new function instance. f = Function() - + return out