d
This commit is contained in:
51
translator-C++-Swift/Function.py
Normal file
51
translator-C++-Swift/Function.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
|
||||||
|
class Function:
|
||||||
|
def __init__(self):
|
||||||
|
self.isBody = False
|
||||||
|
self.isComplete = False
|
||||||
|
self.isSignature = False
|
||||||
|
self.name = None
|
||||||
|
self.parameters = []
|
||||||
|
self.returnValue = None
|
||||||
|
self.statements = []
|
||||||
|
|
||||||
|
def parseLine(self, ln):
|
||||||
|
parts = ln.split(" ")
|
||||||
|
count = len(parts)
|
||||||
|
lastPart = parts[count - 1]
|
||||||
|
|
||||||
|
# Beginning of signature.
|
||||||
|
if (
|
||||||
|
self.returnValue is None and
|
||||||
|
self.name is None
|
||||||
|
and lastPart.endswith("(")
|
||||||
|
):
|
||||||
|
self.isSignature = True
|
||||||
|
|
||||||
|
# End of parameters.
|
||||||
|
if (
|
||||||
|
self.isSignature and
|
||||||
|
ln.endswith(") {")
|
||||||
|
):
|
||||||
|
self.isSignature = False
|
||||||
|
isBody = True
|
||||||
|
|
||||||
|
# # Parameter.
|
||||||
|
# if (
|
||||||
|
# isSignature and
|
||||||
|
# not ln.endswith(") {")
|
||||||
|
# ):
|
||||||
|
# print(f"TODO argument: '{ln}'")
|
||||||
|
|
||||||
|
# Return type and name.
|
||||||
|
if (
|
||||||
|
self.isSignature and
|
||||||
|
lastPart.endswith("(")
|
||||||
|
):
|
||||||
|
self.name = lastPart[:-1]
|
||||||
|
self.returnType = ln[:-len(lastPart) - 1] # -1 for space before function name
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
|
def __str__(self):
|
||||||
|
return f"Function(n/rt: '{self.name}'/'{self.returnType}')"
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from Function import *
|
||||||
|
|
||||||
def process(FILE_IN):
|
def process(FILE_IN):
|
||||||
# Read file.
|
# Read file.
|
||||||
@@ -6,17 +7,9 @@ def process(FILE_IN):
|
|||||||
for line in file:
|
for line in file:
|
||||||
lines_in.append(line.rstrip())
|
lines_in.append(line.rstrip())
|
||||||
|
|
||||||
|
f = Function()
|
||||||
|
|
||||||
# Translate.
|
# Translate.
|
||||||
lines_out = []
|
|
||||||
|
|
||||||
isDeclaration = False
|
|
||||||
isDeclarationLine = False
|
|
||||||
isBody = False
|
|
||||||
functionName = None
|
|
||||||
returnType = None
|
|
||||||
argumentNames = []
|
|
||||||
argumentTypes = []
|
|
||||||
|
|
||||||
for ln in lines_in:
|
for ln in lines_in:
|
||||||
ln = ln.rstrip()
|
ln = ln.rstrip()
|
||||||
|
|
||||||
@@ -24,36 +17,9 @@ def process(FILE_IN):
|
|||||||
if "#include" in ln:
|
if "#include" in ln:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
parts = ln.split(" ")
|
f.parseLine(ln)
|
||||||
count = len(parts)
|
if f.isComplete:
|
||||||
lastPart = parts[count - 1]
|
print("Function ready!")
|
||||||
if not isBody and lastPart.endswith("("):
|
break
|
||||||
isDeclaration = True
|
|
||||||
isDeclarationLine = True
|
|
||||||
isBody = False
|
|
||||||
|
|
||||||
# End of function arguments.
|
return f"Debug: {f}"
|
||||||
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)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user