Clean up temporary variables properly

This commit is contained in:
2025-12-22 19:38:06 +11:00
parent 957e0fd95a
commit 43310c70bf

View File

@@ -9,6 +9,10 @@
namespace Solstice { namespace Solstice {
namespace Parser { namespace Parser {
bool isTemp(std::string id) {
return id.rfind("tmp_", 0) == 0;
}
// SolData Implementation // SolData Implementation
SolData::SolData(int64_t in) : data(in), type(SolDataType::Int) {} SolData::SolData(int64_t in) : data(in), type(SolDataType::Int) {}
SolData::SolData(double in) : data(in), type(SolDataType::Double) {} SolData::SolData(double in) : data(in), type(SolDataType::Double) {}
@@ -73,7 +77,6 @@ namespace Solstice {
case SolNodeType::Value: { case SolNodeType::Value: {
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
SolGroundCodeBlock codeBlock; SolGroundCodeBlock codeBlock;
codeBlock.toBeDropped.push_back(outputId);
GroundInstruction gi = groundCreateInstruction(SET); GroundInstruction gi = groundCreateInstruction(SET);
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
switch (data.type) { switch (data.type) {
@@ -94,7 +97,9 @@ namespace Solstice {
case SolDataType::String: { case SolDataType::String: {
auto dataopt = data.getString(); auto dataopt = data.getString();
if (dataopt) { if (dataopt) {
groundAddValueToInstruction(&gi, groundCreateValue(STRING, dataopt.value().c_str())); char* str = (char*) malloc(dataopt.value().size() + 1);
strcpy(str, dataopt.value().c_str());
groundAddValueToInstruction(&gi, groundCreateValue(STRING, str));
} }
break; break;
} }
@@ -120,7 +125,6 @@ namespace Solstice {
case SolNodeType::Add: { case SolNodeType::Add: {
SolGroundCodeBlock codeBlock; SolGroundCodeBlock codeBlock;
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
codeBlock.toBeDropped.push_back(outputId);
GroundInstruction gi = groundCreateInstruction(ADD); GroundInstruction gi = groundCreateInstruction(ADD);
if (children.size() < 2) { if (children.size() < 2) {
std::cout << "Need more stuff to add\n"; std::cout << "Need more stuff to add\n";
@@ -129,13 +133,14 @@ namespace Solstice {
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data()));
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
codeBlock.code.push_back(gi); codeBlock.code.push_back(gi);
if (isTemp(children[0].outputId)) codeBlock.toBeDropped.push_back(children[0].outputId);
if (isTemp(children[1].outputId)) codeBlock.toBeDropped.push_back(children[1].outputId);
code.push_back(codeBlock); code.push_back(codeBlock);
break; break;
} }
case SolNodeType::Equal: { case SolNodeType::Equal: {
SolGroundCodeBlock codeBlock; SolGroundCodeBlock codeBlock;
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
codeBlock.toBeDropped.push_back(outputId);
GroundInstruction gi = groundCreateInstruction(EQUAL); GroundInstruction gi = groundCreateInstruction(EQUAL);
if (children.size() < 2) { if (children.size() < 2) {
std::cout << "Need more stuff to equal\n"; std::cout << "Need more stuff to equal\n";
@@ -144,13 +149,14 @@ namespace Solstice {
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data()));
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
codeBlock.code.push_back(gi); codeBlock.code.push_back(gi);
if (isTemp(children[0].outputId)) codeBlock.toBeDropped.push_back(children[0].outputId);
if (isTemp(children[1].outputId)) codeBlock.toBeDropped.push_back(children[1].outputId);
code.push_back(codeBlock); code.push_back(codeBlock);
break; break;
} }
case SolNodeType::Inequal: { case SolNodeType::Inequal: {
SolGroundCodeBlock codeBlock; SolGroundCodeBlock codeBlock;
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
codeBlock.toBeDropped.push_back(outputId);
GroundInstruction gi = groundCreateInstruction(INEQUAL); GroundInstruction gi = groundCreateInstruction(INEQUAL);
if (children.size() < 2) { if (children.size() < 2) {
std::cout << "Need more stuff to inequal\n"; std::cout << "Need more stuff to inequal\n";
@@ -159,13 +165,14 @@ namespace Solstice {
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data()));
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
codeBlock.code.push_back(gi); codeBlock.code.push_back(gi);
if (isTemp(children[0].outputId)) codeBlock.toBeDropped.push_back(children[0].outputId);
if (isTemp(children[1].outputId)) codeBlock.toBeDropped.push_back(children[1].outputId);
code.push_back(codeBlock); code.push_back(codeBlock);
break; break;
} }
case SolNodeType::Greater: { case SolNodeType::Greater: {
SolGroundCodeBlock codeBlock; SolGroundCodeBlock codeBlock;
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
codeBlock.toBeDropped.push_back(outputId);
GroundInstruction gi = groundCreateInstruction(GREATER); GroundInstruction gi = groundCreateInstruction(GREATER);
if (children.size() < 2) { if (children.size() < 2) {
std::cout << "Need more stuff to greater\n"; std::cout << "Need more stuff to greater\n";
@@ -174,13 +181,14 @@ namespace Solstice {
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data()));
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
codeBlock.code.push_back(gi); codeBlock.code.push_back(gi);
if (isTemp(children[0].outputId)) codeBlock.toBeDropped.push_back(children[0].outputId);
if (isTemp(children[1].outputId)) codeBlock.toBeDropped.push_back(children[1].outputId);
code.push_back(codeBlock); code.push_back(codeBlock);
break; break;
} }
case SolNodeType::Lesser: { case SolNodeType::Lesser: {
SolGroundCodeBlock codeBlock; SolGroundCodeBlock codeBlock;
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
codeBlock.toBeDropped.push_back(outputId);
GroundInstruction gi = groundCreateInstruction(LESSER); GroundInstruction gi = groundCreateInstruction(LESSER);
if (children.size() < 2) { if (children.size() < 2) {
std::cout << "Need more stuff to lesser\n"; std::cout << "Need more stuff to lesser\n";
@@ -189,6 +197,8 @@ namespace Solstice {
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data()));
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
codeBlock.code.push_back(gi); codeBlock.code.push_back(gi);
if (isTemp(children[0].outputId)) codeBlock.toBeDropped.push_back(children[0].outputId);
if (isTemp(children[1].outputId)) codeBlock.toBeDropped.push_back(children[1].outputId);
code.push_back(codeBlock); code.push_back(codeBlock);
break; break;
} }
@@ -198,13 +208,9 @@ namespace Solstice {
std::cout << "Need more stuff to inequal\n"; std::cout << "Need more stuff to inequal\n";
} }
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
codeBlock.toBeDropped.push_back(outputId);
std::string trueLabelIdString = "internal_true" + std::to_string(labelIterator++); std::string trueLabelIdString = "internal_true" + std::to_string(labelIterator++);
codeBlock.toBeDropped.push_back(trueLabelIdString);
std::string falseLabelIdString = "internal_false" + std::to_string(labelIterator); std::string falseLabelIdString = "internal_false" + std::to_string(labelIterator);
codeBlock.toBeDropped.push_back(falseLabelIdString);
std::string endLabelIdString = "internal_end" + std::to_string(labelIterator); std::string endLabelIdString = "internal_end" + std::to_string(labelIterator);
codeBlock.toBeDropped.push_back(endLabelIdString);
char* trueLabelId = (char*) malloc(sizeof(char) * (trueLabelIdString.size() + 1)); char* trueLabelId = (char*) malloc(sizeof(char) * (trueLabelIdString.size() + 1));
strcpy(trueLabelId, trueLabelIdString.data()); strcpy(trueLabelId, trueLabelIdString.data());
char* falseLabelId = (char*) malloc(sizeof(char) * (falseLabelIdString.size() + 1)); char* falseLabelId = (char*) malloc(sizeof(char) * (falseLabelIdString.size() + 1));
@@ -263,6 +269,8 @@ namespace Solstice {
gi = groundCreateInstruction(CREATELABEL); gi = groundCreateInstruction(CREATELABEL);
groundAddReferenceToInstruction(&gi, groundCreateReference(LABEL, endLabelId)); groundAddReferenceToInstruction(&gi, groundCreateReference(LABEL, endLabelId));
codeBlock.code.push_back(gi); codeBlock.code.push_back(gi);
if (isTemp(children[0].outputId)) codeBlock.toBeDropped.push_back(children[0].outputId);
if (isTemp(children[1].outputId)) codeBlock.toBeDropped.push_back(children[1].outputId);
code.push_back(codeBlock); code.push_back(codeBlock);
break; break;
} }
@@ -272,13 +280,9 @@ namespace Solstice {
std::cout << "Need more stuff to inequal\n"; std::cout << "Need more stuff to inequal\n";
} }
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
codeBlock.toBeDropped.push_back(outputId);
std::string trueLabelIdString = "internal_true" + std::to_string(labelIterator++); std::string trueLabelIdString = "internal_true" + std::to_string(labelIterator++);
codeBlock.toBeDropped.push_back(trueLabelIdString);
std::string falseLabelIdString = "internal_false" + std::to_string(labelIterator); std::string falseLabelIdString = "internal_false" + std::to_string(labelIterator);
codeBlock.toBeDropped.push_back(falseLabelIdString);
std::string endLabelIdString = "internal_end" + std::to_string(labelIterator); std::string endLabelIdString = "internal_end" + std::to_string(labelIterator);
codeBlock.toBeDropped.push_back(endLabelIdString);
char* trueLabelId = (char*) malloc(sizeof(char) * (trueLabelIdString.size() + 1)); char* trueLabelId = (char*) malloc(sizeof(char) * (trueLabelIdString.size() + 1));
strcpy(trueLabelId, trueLabelIdString.data()); strcpy(trueLabelId, trueLabelIdString.data());
char* falseLabelId = (char*) malloc(sizeof(char) * (falseLabelIdString.size() + 1)); char* falseLabelId = (char*) malloc(sizeof(char) * (falseLabelIdString.size() + 1));
@@ -337,6 +341,8 @@ namespace Solstice {
gi = groundCreateInstruction(CREATELABEL); gi = groundCreateInstruction(CREATELABEL);
groundAddReferenceToInstruction(&gi, groundCreateReference(LABEL, endLabelId)); groundAddReferenceToInstruction(&gi, groundCreateReference(LABEL, endLabelId));
codeBlock.code.push_back(gi); codeBlock.code.push_back(gi);
if (isTemp(children[0].outputId)) codeBlock.toBeDropped.push_back(children[0].outputId);
if (isTemp(children[1].outputId)) codeBlock.toBeDropped.push_back(children[1].outputId);
code.push_back(codeBlock); code.push_back(codeBlock);
break; break;
} }
@@ -348,6 +354,7 @@ namespace Solstice {
} }
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data()));
codeBlock.code.push_back(gi); codeBlock.code.push_back(gi);
if (isTemp(children[0].outputId)) codeBlock.toBeDropped.push_back(children[0].outputId);
code.push_back(codeBlock); code.push_back(codeBlock);
break; break;
} }
@@ -357,6 +364,7 @@ namespace Solstice {
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
SolGroundCodeBlock codeBlock; SolGroundCodeBlock codeBlock;
codeBlock.toBeDropped.push_back(outputId); codeBlock.toBeDropped.push_back(outputId);
if (isTemp(children[0].outputId)) codeBlock.toBeDropped.push_back(children[0].outputId);
GroundInstruction gi = groundCreateInstruction(NOT); GroundInstruction gi = groundCreateInstruction(NOT);
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data()));
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
@@ -374,6 +382,7 @@ namespace Solstice {
code.insert(code.end(), childCode.begin(), childCode.end()); code.insert(code.end(), childCode.begin(), childCode.end());
} }
codeBlock.code.clear(); codeBlock.code.clear();
codeBlock.toBeDropped.clear();
GroundInstruction gi3 = groundCreateInstruction(CREATELABEL); GroundInstruction gi3 = groundCreateInstruction(CREATELABEL);
groundAddReferenceToInstruction(&gi3, groundCreateReference(LABEL, labelId)); groundAddReferenceToInstruction(&gi3, groundCreateReference(LABEL, labelId));
codeBlock.code.push_back(gi3); codeBlock.code.push_back(gi3);
@@ -400,6 +409,7 @@ namespace Solstice {
SolGroundCodeBlock checkBlock; SolGroundCodeBlock checkBlock;
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
checkBlock.toBeDropped.push_back(outputId); checkBlock.toBeDropped.push_back(outputId);
if (isTemp(children[0].outputId)) checkBlock.toBeDropped.push_back(children[0].outputId);
GroundInstruction gi = groundCreateInstruction(NOT); GroundInstruction gi = groundCreateInstruction(NOT);
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data()));
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data())); groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
@@ -434,6 +444,7 @@ namespace Solstice {
groundAddReferenceToInstruction(&setInstruction, groundCreateReference(DIRREF, children[0].outputId.data())); groundAddReferenceToInstruction(&setInstruction, groundCreateReference(DIRREF, children[0].outputId.data()));
groundAddReferenceToInstruction(&setInstruction, groundCreateReference(VALREF, children[1].outputId.data())); groundAddReferenceToInstruction(&setInstruction, groundCreateReference(VALREF, children[1].outputId.data()));
codeBlock.code.push_back(setInstruction); codeBlock.code.push_back(setInstruction);
if (isTemp(children[1].outputId)) codeBlock.toBeDropped.push_back(children[1].outputId);
code.push_back(codeBlock); code.push_back(codeBlock);
break; break;
} }
@@ -445,10 +456,10 @@ namespace Solstice {
GroundInstruction printInstruction = groundCreateInstruction(PRINT); GroundInstruction printInstruction = groundCreateInstruction(PRINT);
groundAddReferenceToInstruction(&printInstruction, groundCreateReference(VALREF, children[1].outputId.data())); groundAddReferenceToInstruction(&printInstruction, groundCreateReference(VALREF, children[1].outputId.data()));
inputCodeBlock.code.push_back(printInstruction); inputCodeBlock.code.push_back(printInstruction);
if (isTemp(children[1].outputId)) inputCodeBlock.toBeDropped.push_back(children[1].outputId);
} }
GroundInstruction inputInstruction = groundCreateInstruction(INPUT); GroundInstruction inputInstruction = groundCreateInstruction(INPUT);
outputId = "tmp_" + std::to_string(tmpIdIterator++); outputId = "tmp_" + std::to_string(tmpIdIterator++);
inputCodeBlock.toBeDropped.push_back(outputId);
groundAddReferenceToInstruction(&inputInstruction, groundCreateReference(DIRREF, outputId.data())); groundAddReferenceToInstruction(&inputInstruction, groundCreateReference(DIRREF, outputId.data()));
inputCodeBlock.code.push_back(inputInstruction); inputCodeBlock.code.push_back(inputInstruction);
code.push_back(inputCodeBlock); code.push_back(inputCodeBlock);
@@ -871,14 +882,13 @@ namespace Solstice {
for (const auto& inst : code[i].code) { for (const auto& inst : code[i].code) {
groundAddInstructionToProgram(&gp, inst); groundAddInstructionToProgram(&gp, inst);
} }
// Fix this later
/*
for (auto& tmpVar : code[i].toBeDropped) { for (auto& tmpVar : code[i].toBeDropped) {
GroundInstruction gi = groundCreateInstruction(DROP); GroundInstruction gi = groundCreateInstruction(DROP);
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, tmpVar.data())); char* droppedVar = (char*) malloc(tmpVar.size() + 1);
strcpy(droppedVar, tmpVar.c_str());
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, droppedVar));
groundAddInstructionToProgram(&gp, gi); groundAddInstructionToProgram(&gp, gi);
} }
*/
} }
return gp; return gp;
} }