This commit is contained in:
Михаил Капелько
2024-03-29 23:23:50 +03:00
parent 9a17647cc2
commit 110b44030d
7 changed files with 11 additions and 3 deletions

View File

@@ -0,0 +1,53 @@
from Type import *
class Function:
def __init__(self):
self.isBody = False
self.isComplete = False
self.isSignature = False
self.name = None
self.parameters = []
self.returnType = None
self.statements = []
def parseLine(self, ln):
parts = ln.split(" ")
count = len(parts)
lastPart = parts[count - 1]
# Beginning of signature.
if (
self.returnType 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]
rawReturnType = ln[:-len(lastPart) - 1] # -1 for space before function name
self.returnType = Type(rawReturnType)
def __repr__(self):
return self.__str__()
def __str__(self):
return f"Function(n/rt: '{self.name}'/'{self.returnType}')"

View File

@@ -0,0 +1,28 @@
class Type:
def __init__(self, s):
# Currently no recursion is allowed,
# so you can't have a dictionary in dictionary.
self.name = None
self.first = None
self.second = None
# Dictionary.
if s.startswith("std::map"):
self.name = "map"
# Remove prefix and suffix.
prefix = "std::map<"
s = s[len(prefix):]
suffix = ">"
s = s[:-len(suffix)]
# Get map types.
parts = s.split(", ")
self.first = parts[0]
self.second = parts[1]
# By default we treat the type as is.
else:
self.name = s
def __repr__(self):
return self.__str__()
def __str__(self):
return f"Type(n/f/s: '{self.name}'/'{self.first}'/'{self.second}')"

View File

@@ -0,0 +1,25 @@
from Function import *
def process(FILE_IN):
# Read file.
lines_in = []
with open(FILE_IN) as file:
for line in file:
lines_in.append(line.rstrip())
f = Function()
# Translate.
for ln in lines_in:
ln = ln.rstrip()
# Ignore includes.
if "#include" in ln:
continue
f.parseLine(ln)
if f.isComplete:
print("Function ready!")
break
return f"Debug: {f}"

17
_translator-C++-Swift/translate Executable file
View File

@@ -0,0 +1,17 @@
#!/usr/bin/env python3
import os
import sys
from process import *
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]
# Translate file.
out = process(FILE_IN)
print(out)