This commit is contained in:
Михаил Капелько
2024-04-11 16:46:56 +03:00
parent 374b21d68e
commit 4095389578
9 changed files with 90 additions and 7 deletions

View File

@@ -1,5 +1,11 @@
from Function import *
def includes():
return """#include <map>
#include <string>
#include "entities.h"
"""
def replaceAnd(s):
return s.replace("and", "&&")
@@ -34,6 +40,7 @@ def translateStatement(s, state):
ss = s.lstrip()
posColon = ss.find(": ")
posComma = ss.find(", ")
posCtx = ss.find("c.")
posEqual = ss.find(" = ")
posFor = ss.find("for ")
posIn = ss.find(" in ")
@@ -70,6 +77,7 @@ def translateStatement(s, state):
# name = value -> auto name = value
if (
posCtx == -1 and
posColon == -1 and
posOpenSquareBracket == -1 and
posEqual >= 0
@@ -120,6 +128,9 @@ class CPP:
params = []
for i in range(0, len(self.fn.parameters)):
p = translateParameter(self.fn.parameters[i])
# Make Context passed by reference.
if "Context" in p:
p = p.replace("Context", "Context&")
params.append(p)
strparams = "\n".join(params)
if (len(strparams) > 0):

View File

@@ -9,7 +9,7 @@ def process(FILE_IN):
lines_in.append(line.rstrip())
f = Function()
out = ""
out = includes()
# Parse.
for ln in lines_in:

5
v2/entities.cpp Normal file
View File

@@ -0,0 +1,5 @@
#include "entities.h"
MemoryContext memory_createEmptyContext() {
return MemoryContext();
}

9
v2/entities.h Normal file
View File

@@ -0,0 +1,9 @@
#include <map>
#include <string>
struct MemoryContext {
int playfieldSize = 0;
std::map<int, int> playfieldItems;
};
MemoryContext memory_createEmptyContext();

42
v2/functions.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include <map>
#include <string>
#include "entities.h"
// L4: Function.
std::string memory_generateConstPlayfield(
MemoryContext& c
) {
std::map<int, int> idGroups = { };
auto id = 0;
for (auto gid = 0; gid < c.playfieldSize; ++gid) {
idGroups[id] = gid;
id += 1;
idGroups[id] = gid;
id += 1;
}
c.playfieldItems = idGroups;
return "MemoryContext.playfieldItems";
}
// L20: Test.
std::string test_memory_generateConstPlayfield(
) {
auto c = memory_createEmptyContext();
c.playfieldSize = 2;
auto fieldName = memory_generateConstPlayfield(c);
if (
fieldName == "MemoryContext.playfieldItems" &&
c.playfieldItems.size() == 4 &&
c.playfieldItems[0] == 0 &&
c.playfieldItems[1] == 0 &&
c.playfieldItems[2] == 1 &&
c.playfieldItems[3] == 1
) {
return "OK: memory_generateConstPlayfield";
}
return "ERR: memory_generateConstPlayfield";
}

8
v2/functions.h Normal file
View File

@@ -0,0 +1,8 @@
#include "entities.h"
std::string memory_generateConstPlayfield(
MemoryContext& c
);
std::string test_memory_generateConstPlayfield(
);

View File

@@ -25,14 +25,13 @@ def test_memory_generateConstPlayfield(
c = memory_createEmptyContext()
c.playfieldSize = 2
fieldName = memory_generateConstPlayfield(c)
idGroups = c.playfieldItems
if (
fieldName == "MemoryContext.playfieldItems" and
len(idGroups) == 4 and
idGroups[0] == 0 and
idGroups[1] == 0 and
idGroups[2] == 1 and
idGroups[3] == 1
len(c.playfieldItems) == 4 and
c.playfieldItems[0] == 0 and
c.playfieldItems[1] == 0 and
c.playfieldItems[2] == 1 and
c.playfieldItems[3] == 1
):
return "OK: memory_generateConstPlayfield"
#}

1
v2/gen-C++ Executable file
View File

@@ -0,0 +1 @@
c++ -o test_memory_C++ -std=c++11 -I. entities.cpp functions.cpp main.cpp

8
v2/main.cpp Normal file
View File

@@ -0,0 +1,8 @@
#include <map>
#include <string>
#include "functions.h"
int main() {
printf("%s\n", test_memory_generateConstPlayfield().c_str());
}