d
This commit is contained in:
2
main.cpp
2
main.cpp
@@ -4,7 +4,7 @@
|
|||||||
// L4: Function.
|
// L4: Function.
|
||||||
|
|
||||||
std::map<int, int> memory_generateConstPlayfield(int n) {
|
std::map<int, int> memory_generateConstPlayfield(int n) {
|
||||||
std::map<int, int> idGroups;
|
std::map<int, int> idGroups = { };
|
||||||
auto id = 0;
|
auto id = 0;
|
||||||
for (auto gid = 0; gid < n; ++gid) {
|
for (auto gid = 0; gid < n; ++gid) {
|
||||||
idGroups[id] = gid;
|
idGroups[id] = gid;
|
||||||
|
|||||||
@@ -9,6 +9,57 @@ def translateParameter(s):
|
|||||||
indentation = "".join(" " * indent)
|
indentation = "".join(" " * indent)
|
||||||
return f"{indentation}{t} {name}"
|
return f"{indentation}{t} {name}"
|
||||||
|
|
||||||
|
def translateStatement(s):
|
||||||
|
indent = len(s) - len(s.lstrip())
|
||||||
|
indentation = "".join(" " * indent)
|
||||||
|
ss = s.lstrip()
|
||||||
|
posColon = ss.find(": ")
|
||||||
|
posComma = ss.find(", ")
|
||||||
|
posEqual = ss.find(" = ")
|
||||||
|
posFor = ss.find("for ")
|
||||||
|
posIn = ss.find(" in ")
|
||||||
|
posRange = ss.find("range(")
|
||||||
|
posRangeEnd = ss.find("):")
|
||||||
|
posClosingScope = ss.find("#}")
|
||||||
|
|
||||||
|
# #} -> }
|
||||||
|
if posClosingScope != -1:
|
||||||
|
return f"{indentation}}}"
|
||||||
|
|
||||||
|
# for name in range(x, y): -> for (auto name = x; name < y; ++name) {
|
||||||
|
if (
|
||||||
|
posFor >= 0 and
|
||||||
|
posIn >= 0 and
|
||||||
|
posRange >= 0
|
||||||
|
):
|
||||||
|
name = ss[posFor + len("for "):posIn]
|
||||||
|
x = ss[posRange + len("range("):posComma]
|
||||||
|
y = ss[posComma + len(", "):posRangeEnd]
|
||||||
|
return f"{indentation}for (auto {name} = {x}; {name} < {y}; ++{name}) {{"
|
||||||
|
|
||||||
|
# name: type = value -> type name = value
|
||||||
|
if (
|
||||||
|
posColon >= 0 and
|
||||||
|
posEqual >= 0
|
||||||
|
):
|
||||||
|
name = ss[:posColon]
|
||||||
|
type = ss[posColon + len(": "):posEqual]
|
||||||
|
t = translateType(type)
|
||||||
|
value = ss[posEqual + len(" = "):]
|
||||||
|
return f"{indentation}{t} {name} = {value};"
|
||||||
|
|
||||||
|
# name = value -> auto name = value
|
||||||
|
if (
|
||||||
|
posColon == -1 and
|
||||||
|
posEqual >= 0
|
||||||
|
):
|
||||||
|
name = ss[:posEqual]
|
||||||
|
value = ss[posEqual + len(" = "):]
|
||||||
|
return f"{indentation}auto {name} = {value};"
|
||||||
|
|
||||||
|
# Unknown. Return as is.
|
||||||
|
return s
|
||||||
|
|
||||||
def translateType(s):
|
def translateType(s):
|
||||||
# dict[X, Y] -> std::map<X, Y>
|
# dict[X, Y] -> std::map<X, Y>
|
||||||
if s.startswith("dict["):
|
if s.startswith("dict["):
|
||||||
@@ -25,14 +76,19 @@ class CPP:
|
|||||||
|
|
||||||
def translate(self):
|
def translate(self):
|
||||||
returnType = translateType(self.fn.returnType)
|
returnType = translateType(self.fn.returnType)
|
||||||
|
|
||||||
# Parameters.
|
# Parameters.
|
||||||
params = []
|
params = []
|
||||||
for i in range(0, len(self.fn.parameters)):
|
for i in range(0, len(self.fn.parameters)):
|
||||||
p = translateParameter(self.fn.parameters[i])
|
p = translateParameter(self.fn.parameters[i])
|
||||||
params.append(p)
|
params.append(p)
|
||||||
strparams = "\n".join(params)
|
strparams = "\n".join(params)
|
||||||
|
|
||||||
# Statements.
|
# Statements.
|
||||||
sts = self.fn.statements
|
sts = []
|
||||||
|
for i in range(0, len(self.fn.statements)):
|
||||||
|
s = translateStatement(self.fn.statements[i])
|
||||||
|
sts.append(s)
|
||||||
strstatements = "\n".join(sts)
|
strstatements = "\n".join(sts)
|
||||||
|
|
||||||
return f"""{returnType} {self.fn.name}(
|
return f"""{returnType} {self.fn.name}(
|
||||||
|
|||||||
Reference in New Issue
Block a user