diff --git a/v3/memory.h b/v3/memory.h new file mode 100644 index 0000000..c234c97 --- /dev/null +++ b/v3/memory.h @@ -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 diff --git a/v3/memory_Context.h b/v3/memory_Context.h index e74cde1..46595ba 100644 --- a/v3/memory_Context.h +++ b/v3/memory_Context.h @@ -16,4 +16,6 @@ struct memory_Context { bool victory = false; }; +memory_Context memory_createContext(); + #endif // memory_Context_HEADER diff --git a/v3/memory_test.cpp b/v3/memory_test.cpp index daf32b2..e5d65dd 100644 --- a/v3/memory_test.cpp +++ b/v3/memory_test.cpp @@ -1,13 +1,14 @@ #include #include #include +#include "memory.h" #include "memory_Context.h" std::string memory_test_generateConstPlayfield( ) { auto c = memory_createContext(); c.playfieldSize = 2; - auto c = memory_generateConstPlayfield(c); + c = memory_generateConstPlayfield(c); if ( c.recentField == "playfieldItems" && c.playfieldItems.size() == 4 && @@ -27,10 +28,10 @@ std::string memory_test_selectItem_1x( ) { auto c = memory_createContext(); c.playfieldSize = 2; - auto c = memory_generateConstPlayfield(c); + c = memory_generateConstPlayfield(c); # Select the first item.; c.selectedId = 0; - auto c = memory_selectItem(c); + c = memory_selectItem(c); # See if it's in selectedItems now.; if ( c.recentField == "selectedItems" && @@ -48,12 +49,12 @@ std::string memory_test_selectItem_2x( ) { auto c = memory_createContext(); c.playfieldSize = 2; - auto c = memory_generateConstPlayfield(c); + c = memory_generateConstPlayfield(c); # Select the first two items.; c.selectedId = 0; - auto c = memory_selectItem(c); + c = memory_selectItem(c); c.selectedId = 1; - auto c = memory_selectItem(c); + c = memory_selectItem(c); # See if both items are selected now.; if ( c.recentField == "selectedItems" && @@ -72,14 +73,14 @@ std::string memory_test_selectItem_3x( ) { auto c = memory_createContext(); c.playfieldSize = 2; - auto c = memory_generateConstPlayfield(c); + c = memory_generateConstPlayfield(c); # Select three items.; c.selectedId = 0; - auto c = memory_selectItem(c); + c = memory_selectItem(c); c.selectedId = 1; - auto c = memory_selectItem(c); + c = memory_selectItem(c); c.selectedId = 2; - auto c = memory_selectItem(c); + c = memory_selectItem(c); # See if only one (last) item is selected now.; if ( c.recentField == "selectedItems" && @@ -98,14 +99,14 @@ std::string memory_test_shouldDeselectMismatchedItems( ) { auto c = memory_createContext(); c.playfieldSize = 2; - auto c = memory_generateConstPlayfield(c); + c = memory_generateConstPlayfield(c); # Select two items of different groups.; c.selectedId = 0; - auto c = memory_selectItem(c); + c = memory_selectItem(c); c.selectedId = 2; - auto c = memory_selectItem(c); + c = memory_selectItem(c); # Detect mismatching.; - auto c = memory_shouldDeselectMismatchedItems(c); + c = memory_shouldDeselectMismatchedItems(c); # See if the two selected items do not match.; if ( c.recentField == "mismatchedItems" && @@ -125,14 +126,14 @@ std::string memory_test_shouldDeselectMismatchedItems_itemTwice( ) { auto c = memory_createContext(); c.playfieldSize = 2; - auto c = memory_generateConstPlayfield(c); + c = memory_generateConstPlayfield(c); # Select the same item twice.; c.selectedId = 0; - auto c = memory_selectItem(c); + c = memory_selectItem(c); c.selectedId = 0; - auto c = memory_selectItem(c); + c = memory_selectItem(c); # Detect mismatching.; - auto c = memory_shouldDeselectMismatchedItems(c); + c = memory_shouldDeselectMismatchedItems(c); # See if the two selected items do not match.; if ( c.recentField == "mismatchedItems" && @@ -154,23 +155,23 @@ std::string memory_test_shouldDetectVictory( ) { auto c = memory_createContext(); c.playfieldSize = 2; - auto c = memory_generateConstPlayfield(c); + c = memory_generateConstPlayfield(c); # Select the first two items of the same group.; c.selectedId = 0; - auto c = memory_selectItem(c); + c = memory_selectItem(c); c.selectedId = 1; - auto c = memory_selectItem(c); + c = memory_selectItem(c); # Hide the first pair.; - auto c = memory_shouldHideMatchingItems(c); + c = memory_shouldHideMatchingItems(c); # Select the last two items of the same group.; c.selectedId = 2; - auto c = memory_selectItem(c); + c = memory_selectItem(c); c.selectedId = 3; - auto c = memory_selectItem(c); + c = memory_selectItem(c); # Hide the second pair.; - auto c = memory_shouldHideMatchingItems(c); + c = memory_shouldHideMatchingItems(c); # Detect victory.; - auto c = memory_shouldDetectVictory(c); + c = memory_shouldDetectVictory(c); # See if victory has been detected.; if ( c.recentField == "victory" && @@ -188,14 +189,14 @@ std::string memory_test_shouldHideMatchingItems( ) { auto c = memory_createContext(); c.playfieldSize = 2; - auto c = memory_generateConstPlayfield(c); + c = memory_generateConstPlayfield(c); # Select two items of the same group.; c.selectedId = 0; - auto c = memory_selectItem(c); + c = memory_selectItem(c); c.selectedId = 1; - auto c = memory_selectItem(c); + c = memory_selectItem(c); # Hide matching items.; - auto c = memory_shouldHideMatchingItems(c); + c = memory_shouldHideMatchingItems(c); # See if the two selected items match.; if ( c.recentField == "hiddenItems" && diff --git a/v3/tPythonC++/CPP.py b/v3/tPythonC++/CPP.py index c923c74..27a503a 100644 --- a/v3/tPythonC++/CPP.py +++ b/v3/tPythonC++/CPP.py @@ -90,8 +90,12 @@ def translateStatement(s, state): posEqual >= 0 ): name = ss[:posEqual] - value = ss[posEqual + len(" = "):] - return f"{indentation}auto {name} = {value};" + # Skip prepending 'auto' each time variable is assigned, + # 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 (" if ss == "if (": @@ -138,6 +142,7 @@ class CPP: self.fn = fn self.isIf = False self.isIfNot = False + self.varNames = {} def translate(self): returnType = translateType(self.fn.returnType)