This commit is contained in:
Михаил Капелько
2024-04-29 13:55:36 +03:00
parent 0bd0bf16f2
commit 467ba0ff81
4 changed files with 52 additions and 32 deletions

12
v3/memory.h Normal file
View File

@@ -0,0 +1,12 @@
#include "memory_Context.h"
#ifndef memory_HEADER
#define memory_HEADER
memory_Context memory_generateConstPlayfield(memory_Context);
memory_Context memory_selectItem(memory_Context);
memory_Context memory_shouldDeselectMismatchedItems(memory_Context);
memory_Context memory_shouldDetectVictory(memory_Context);
memory_Context memory_shouldHideMatchingItems(memory_Context);
#endif // memory_HEADER

View File

@@ -16,4 +16,6 @@ struct memory_Context {
bool victory = false; bool victory = false;
}; };
memory_Context memory_createContext();
#endif // memory_Context_HEADER #endif // memory_Context_HEADER

View File

@@ -1,13 +1,14 @@
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include "memory.h"
#include "memory_Context.h" #include "memory_Context.h"
std::string memory_test_generateConstPlayfield( std::string memory_test_generateConstPlayfield(
) { ) {
auto c = memory_createContext(); auto c = memory_createContext();
c.playfieldSize = 2; c.playfieldSize = 2;
auto c = memory_generateConstPlayfield(c); c = memory_generateConstPlayfield(c);
if ( if (
c.recentField == "playfieldItems" && c.recentField == "playfieldItems" &&
c.playfieldItems.size() == 4 && c.playfieldItems.size() == 4 &&
@@ -27,10 +28,10 @@ std::string memory_test_selectItem_1x(
) { ) {
auto c = memory_createContext(); auto c = memory_createContext();
c.playfieldSize = 2; c.playfieldSize = 2;
auto c = memory_generateConstPlayfield(c); c = memory_generateConstPlayfield(c);
# Select the first item.; # Select the first item.;
c.selectedId = 0; c.selectedId = 0;
auto c = memory_selectItem(c); c = memory_selectItem(c);
# See if it's in selectedItems now.; # See if it's in selectedItems now.;
if ( if (
c.recentField == "selectedItems" && c.recentField == "selectedItems" &&
@@ -48,12 +49,12 @@ std::string memory_test_selectItem_2x(
) { ) {
auto c = memory_createContext(); auto c = memory_createContext();
c.playfieldSize = 2; c.playfieldSize = 2;
auto c = memory_generateConstPlayfield(c); c = memory_generateConstPlayfield(c);
# Select the first two items.; # Select the first two items.;
c.selectedId = 0; c.selectedId = 0;
auto c = memory_selectItem(c); c = memory_selectItem(c);
c.selectedId = 1; c.selectedId = 1;
auto c = memory_selectItem(c); c = memory_selectItem(c);
# See if both items are selected now.; # See if both items are selected now.;
if ( if (
c.recentField == "selectedItems" && c.recentField == "selectedItems" &&
@@ -72,14 +73,14 @@ std::string memory_test_selectItem_3x(
) { ) {
auto c = memory_createContext(); auto c = memory_createContext();
c.playfieldSize = 2; c.playfieldSize = 2;
auto c = memory_generateConstPlayfield(c); c = memory_generateConstPlayfield(c);
# Select three items.; # Select three items.;
c.selectedId = 0; c.selectedId = 0;
auto c = memory_selectItem(c); c = memory_selectItem(c);
c.selectedId = 1; c.selectedId = 1;
auto c = memory_selectItem(c); c = memory_selectItem(c);
c.selectedId = 2; c.selectedId = 2;
auto c = memory_selectItem(c); c = memory_selectItem(c);
# See if only one (last) item is selected now.; # See if only one (last) item is selected now.;
if ( if (
c.recentField == "selectedItems" && c.recentField == "selectedItems" &&
@@ -98,14 +99,14 @@ std::string memory_test_shouldDeselectMismatchedItems(
) { ) {
auto c = memory_createContext(); auto c = memory_createContext();
c.playfieldSize = 2; c.playfieldSize = 2;
auto c = memory_generateConstPlayfield(c); c = memory_generateConstPlayfield(c);
# Select two items of different groups.; # Select two items of different groups.;
c.selectedId = 0; c.selectedId = 0;
auto c = memory_selectItem(c); c = memory_selectItem(c);
c.selectedId = 2; c.selectedId = 2;
auto c = memory_selectItem(c); c = memory_selectItem(c);
# Detect mismatching.; # Detect mismatching.;
auto c = memory_shouldDeselectMismatchedItems(c); c = memory_shouldDeselectMismatchedItems(c);
# See if the two selected items do not match.; # See if the two selected items do not match.;
if ( if (
c.recentField == "mismatchedItems" && c.recentField == "mismatchedItems" &&
@@ -125,14 +126,14 @@ std::string memory_test_shouldDeselectMismatchedItems_itemTwice(
) { ) {
auto c = memory_createContext(); auto c = memory_createContext();
c.playfieldSize = 2; c.playfieldSize = 2;
auto c = memory_generateConstPlayfield(c); c = memory_generateConstPlayfield(c);
# Select the same item twice.; # Select the same item twice.;
c.selectedId = 0; c.selectedId = 0;
auto c = memory_selectItem(c); c = memory_selectItem(c);
c.selectedId = 0; c.selectedId = 0;
auto c = memory_selectItem(c); c = memory_selectItem(c);
# Detect mismatching.; # Detect mismatching.;
auto c = memory_shouldDeselectMismatchedItems(c); c = memory_shouldDeselectMismatchedItems(c);
# See if the two selected items do not match.; # See if the two selected items do not match.;
if ( if (
c.recentField == "mismatchedItems" && c.recentField == "mismatchedItems" &&
@@ -154,23 +155,23 @@ std::string memory_test_shouldDetectVictory(
) { ) {
auto c = memory_createContext(); auto c = memory_createContext();
c.playfieldSize = 2; c.playfieldSize = 2;
auto c = memory_generateConstPlayfield(c); c = memory_generateConstPlayfield(c);
# Select the first two items of the same group.; # Select the first two items of the same group.;
c.selectedId = 0; c.selectedId = 0;
auto c = memory_selectItem(c); c = memory_selectItem(c);
c.selectedId = 1; c.selectedId = 1;
auto c = memory_selectItem(c); c = memory_selectItem(c);
# Hide the first pair.; # Hide the first pair.;
auto c = memory_shouldHideMatchingItems(c); c = memory_shouldHideMatchingItems(c);
# Select the last two items of the same group.; # Select the last two items of the same group.;
c.selectedId = 2; c.selectedId = 2;
auto c = memory_selectItem(c); c = memory_selectItem(c);
c.selectedId = 3; c.selectedId = 3;
auto c = memory_selectItem(c); c = memory_selectItem(c);
# Hide the second pair.; # Hide the second pair.;
auto c = memory_shouldHideMatchingItems(c); c = memory_shouldHideMatchingItems(c);
# Detect victory.; # Detect victory.;
auto c = memory_shouldDetectVictory(c); c = memory_shouldDetectVictory(c);
# See if victory has been detected.; # See if victory has been detected.;
if ( if (
c.recentField == "victory" && c.recentField == "victory" &&
@@ -188,14 +189,14 @@ std::string memory_test_shouldHideMatchingItems(
) { ) {
auto c = memory_createContext(); auto c = memory_createContext();
c.playfieldSize = 2; c.playfieldSize = 2;
auto c = memory_generateConstPlayfield(c); c = memory_generateConstPlayfield(c);
# Select two items of the same group.; # Select two items of the same group.;
c.selectedId = 0; c.selectedId = 0;
auto c = memory_selectItem(c); c = memory_selectItem(c);
c.selectedId = 1; c.selectedId = 1;
auto c = memory_selectItem(c); c = memory_selectItem(c);
# Hide matching items.; # Hide matching items.;
auto c = memory_shouldHideMatchingItems(c); c = memory_shouldHideMatchingItems(c);
# See if the two selected items match.; # See if the two selected items match.;
if ( if (
c.recentField == "hiddenItems" && c.recentField == "hiddenItems" &&

View File

@@ -90,8 +90,12 @@ def translateStatement(s, state):
posEqual >= 0 posEqual >= 0
): ):
name = ss[:posEqual] name = ss[:posEqual]
value = ss[posEqual + len(" = "):] # Skip prepending 'auto' each time variable is assigned,
return f"{indentation}auto {name} = {value};" # only do it the first time
if name not in state.varNames:
state.varNames[name] = True
value = ss[posEqual + len(" = "):]
return f"{indentation}auto {name} = {value};"
# Keep "if (" # Keep "if ("
if ss == "if (": if ss == "if (":
@@ -138,6 +142,7 @@ class CPP:
self.fn = fn self.fn = fn
self.isIf = False self.isIf = False
self.isIfNot = False self.isIfNot = False
self.varNames = {}
def translate(self): def translate(self):
returnType = translateType(self.fn.returnType) returnType = translateType(self.fn.returnType)