From 2ae4a8a72f2b3eeb33b8a17270b4689c4e169f38 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, 21 Mar 2024 22:33:57 +0300 Subject: [PATCH] d --- language-C++/Memory/src/Memory.Aux.cpp | 4 +- translator-C++-Swift/translate | 72 ++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100755 translator-C++-Swift/translate diff --git a/language-C++/Memory/src/Memory.Aux.cpp b/language-C++/Memory/src/Memory.Aux.cpp index 067f7ca..ae4044e 100644 --- a/language-C++/Memory/src/Memory.Aux.cpp +++ b/language-C++/Memory/src/Memory.Aux.cpp @@ -1,7 +1,9 @@ #include #include "Memory.h" -std::map memory_generateConstPlayfield(int n) { +std::map memory_generateConstPlayfield( + int n +) { std::map idGroups; int id = 0; for (int gid = 0; gid < n; ++gid) { diff --git a/translator-C++-Swift/translate b/translator-C++-Swift/translate new file mode 100755 index 0000000..a26c623 --- /dev/null +++ b/translator-C++-Swift/translate @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +import os +import sys + +DIR = os.path.dirname(os.path.realpath(sys.argv[0])) + +# Demand file as input +if len(sys.argv) < 2: + print("Usage: /path/to/translate CPP_FILE") + sys.exit(1) + +FILE_IN = sys.argv[1] + +# Read file. +lines_in = [] +with open(FILE_IN) as file: + for line in file: + lines_in.append(line.rstrip()) + +# Translate. +lines_out = [] + +isDeclaration = False +isDeclarationLine = False +isBody = False +functionName = None +returnType = None +argumentNames = [] +argumentTypes = [] + +for ln in lines_in: + ln = ln.lstrip() + # Ignore includes. + if "#include" in ln: + continue + + parts = ln.split(" ") + count = len(parts) + lastPart = parts[count - 1] + if not isBody and lastPart.endswith("("): + isDeclaration = True + isDeclarationLine = True + isBody = False + + # End of function arguments. + if not isDeclarationLine and not isBody and ln.endswith(") {"): + isDeclaration = False + isBody = True + + # Function argument. + if ( + isDeclaration and + not isDeclarationLine and + not isBody and + not ln.endswith(") {") + ): + print(f"TODO argument: '{ln}'") + + # Function return type and function name. + if isDeclaration and lastPart.endswith("("): + functionName = lastPart[:-1] + returnType = ln[:-len(lastPart) - 1] # -1 for space before function name + print(f"Function name: '{functionName}'") + print(f"Return type: '{returnType}'") + isDeclarationLine = False + + lines_out.append(ln) + + + +# Output result. +print(lines_out)