Михаил Капелько 3 weeks ago
parent
commit
4095389578
9 changed files with 90 additions and 7 deletions
  1. +11
    -0
      tr-Python-C++/CPP.py
  2. +1
    -1
      tr-Python-C++/process.py
  3. +5
    -0
      v2/entities.cpp
  4. +9
    -0
      v2/entities.h
  5. +42
    -0
      v2/functions.cpp
  6. +8
    -0
      v2/functions.h
  7. +5
    -6
      v2/functions.py
  8. +1
    -0
      v2/gen-C++
  9. +8
    -0
      v2/main.cpp

+ 11
- 0
tr-Python-C++/CPP.py 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):


+ 1
- 1
tr-Python-C++/process.py 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
- 0
v2/entities.cpp View File

@@ -0,0 +1,5 @@
#include "entities.h"

MemoryContext memory_createEmptyContext() {
return MemoryContext();
}

+ 9
- 0
v2/entities.h View File

@@ -0,0 +1,9 @@
#include <map>
#include <string>

struct MemoryContext {
int playfieldSize = 0;
std::map<int, int> playfieldItems;
};

MemoryContext memory_createEmptyContext();

+ 42
- 0
v2/functions.cpp 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
- 0
v2/functions.h View File

@@ -0,0 +1,8 @@
#include "entities.h"

std::string memory_generateConstPlayfield(
MemoryContext& c
);

std::string test_memory_generateConstPlayfield(
);

+ 5
- 6
v2/functions.py 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
- 0
v2/gen-C++ View File

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

+ 8
- 0
v2/main.cpp View File

@@ -0,0 +1,8 @@
#include <map>
#include <string>
#include "functions.h"


int main() {
printf("%s\n", test_memory_generateConstPlayfield().c_str());
}

Loading…
Cancel
Save