d
This commit is contained in:
BIN
translator-C++-Swift/__pycache__/process.cpython-37.pyc
Normal file
BIN
translator-C++-Swift/__pycache__/process.cpython-37.pyc
Normal file
Binary file not shown.
59
translator-C++-Swift/process.py
Executable file
59
translator-C++-Swift/process.py
Executable file
@@ -0,0 +1,59 @@
|
|||||||
|
|
||||||
|
def process(FILE_IN):
|
||||||
|
# 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.rstrip()
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
return "\n".join(lines_out)
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from process import *
|
||||||
|
|
||||||
DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
|
DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
|
||||||
|
|
||||||
@@ -11,62 +12,6 @@ if len(sys.argv) < 2:
|
|||||||
|
|
||||||
FILE_IN = sys.argv[1]
|
FILE_IN = sys.argv[1]
|
||||||
|
|
||||||
# Read file.
|
# Translate file.
|
||||||
lines_in = []
|
out = process(FILE_IN)
|
||||||
with open(FILE_IN) as file:
|
print(out)
|
||||||
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)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user