Михаил Капелько hace 9 meses
padre
commit
25a2265bf6
Se han modificado 2 ficheros con 59 adiciones y 42 borrados
  1. +51
    -0
      translator-C++-Swift/Function.py
  2. +8
    -42
      translator-C++-Swift/process.py

+ 51
- 0
translator-C++-Swift/Function.py Ver fichero

@@ -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}')"

+ 8
- 42
translator-C++-Swift/process.py Ver fichero

@@ -1,3 +1,4 @@
from Function import *

def process(FILE_IN):
# Read file.
@@ -6,17 +7,9 @@ def process(FILE_IN):
for line in file:
lines_in.append(line.rstrip())

# Translate.
lines_out = []

isDeclaration = False
isDeclarationLine = False
isBody = False
functionName = None
returnType = None
argumentNames = []
argumentTypes = []
f = Function()

# Translate.
for ln in lines_in:
ln = ln.rstrip()

@@ -24,36 +17,9 @@ def process(FILE_IN):
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)
f.parseLine(ln)
if f.isComplete:
print("Function ready!")
break

return "\n".join(lines_out)
return f"Debug: {f}"

Cargando…
Cancelar
Guardar