@@ -1,5 +1,11 @@ | |||||
from Function import * | from Function import * | ||||
def includes(): | |||||
return """#include <map> | |||||
#include <string> | |||||
#include "entities.h" | |||||
""" | |||||
def replaceAnd(s): | def replaceAnd(s): | ||||
return s.replace("and", "&&") | return s.replace("and", "&&") | ||||
@@ -34,6 +40,7 @@ def translateStatement(s, state): | |||||
ss = s.lstrip() | ss = s.lstrip() | ||||
posColon = ss.find(": ") | posColon = ss.find(": ") | ||||
posComma = ss.find(", ") | posComma = ss.find(", ") | ||||
posCtx = ss.find("c.") | |||||
posEqual = ss.find(" = ") | posEqual = ss.find(" = ") | ||||
posFor = ss.find("for ") | posFor = ss.find("for ") | ||||
posIn = ss.find(" in ") | posIn = ss.find(" in ") | ||||
@@ -70,6 +77,7 @@ def translateStatement(s, state): | |||||
# name = value -> auto name = value | # name = value -> auto name = value | ||||
if ( | if ( | ||||
posCtx == -1 and | |||||
posColon == -1 and | posColon == -1 and | ||||
posOpenSquareBracket == -1 and | posOpenSquareBracket == -1 and | ||||
posEqual >= 0 | posEqual >= 0 | ||||
@@ -120,6 +128,9 @@ class CPP: | |||||
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]) | ||||
# Make Context passed by reference. | |||||
if "Context" in p: | |||||
p = p.replace("Context", "Context&") | |||||
params.append(p) | params.append(p) | ||||
strparams = "\n".join(params) | strparams = "\n".join(params) | ||||
if (len(strparams) > 0): | if (len(strparams) > 0): | ||||
@@ -9,7 +9,7 @@ def process(FILE_IN): | |||||
lines_in.append(line.rstrip()) | lines_in.append(line.rstrip()) | ||||
f = Function() | f = Function() | ||||
out = "" | |||||
out = includes() | |||||
# Parse. | # Parse. | ||||
for ln in lines_in: | for ln in lines_in: | ||||
@@ -0,0 +1,5 @@ | |||||
#include "entities.h" | |||||
MemoryContext memory_createEmptyContext() { | |||||
return MemoryContext(); | |||||
} |
@@ -0,0 +1,9 @@ | |||||
#include <map> | |||||
#include <string> | |||||
struct MemoryContext { | |||||
int playfieldSize = 0; | |||||
std::map<int, int> playfieldItems; | |||||
}; | |||||
MemoryContext memory_createEmptyContext(); |
@@ -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"; | |||||
} | |||||
@@ -0,0 +1,8 @@ | |||||
#include "entities.h" | |||||
std::string memory_generateConstPlayfield( | |||||
MemoryContext& c | |||||
); | |||||
std::string test_memory_generateConstPlayfield( | |||||
); |
@@ -25,14 +25,13 @@ def test_memory_generateConstPlayfield( | |||||
c = memory_createEmptyContext() | c = memory_createEmptyContext() | ||||
c.playfieldSize = 2 | c.playfieldSize = 2 | ||||
fieldName = memory_generateConstPlayfield(c) | fieldName = memory_generateConstPlayfield(c) | ||||
idGroups = c.playfieldItems | |||||
if ( | if ( | ||||
fieldName == "MemoryContext.playfieldItems" and | 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" | return "OK: memory_generateConstPlayfield" | ||||
#} | #} | ||||
@@ -0,0 +1 @@ | |||||
c++ -o test_memory_C++ -std=c++11 -I. entities.cpp functions.cpp main.cpp |
@@ -0,0 +1,8 @@ | |||||
#include <map> | |||||
#include <string> | |||||
#include "functions.h" | |||||
int main() { | |||||
printf("%s\n", test_memory_generateConstPlayfield().c_str()); | |||||
} |