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