Skip to content

Commit

Permalink
Hago que el asmline se devuelva por movimiento desde el parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-R0 committed Nov 15, 2020
1 parent 3ca0de2 commit 48d823d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
15 changes: 11 additions & 4 deletions asmline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#include <iostream>
#include <utility>

Asmline::Asmline()
: jumpCodes({"jmp", "ja", "jeq", "jneq", "jne", "jlt", "jle", "jgt", "jge",
Asmline::Asmline(std::string label_, std::string opCode_)
: opCode(opCode_),
label(label_),
jumpCodes({"jmp", "ja", "jeq", "jneq", "jne", "jlt", "jle", "jgt", "jge",
"jset"}) {}

bool Asmline::isJump() {
Expand All @@ -23,8 +25,13 @@ std::string Asmline::getLabel() { return this->label; }
std::list<std::string> Asmline::getLabelsToJumpTo() {
return this->labelsToJump;
}
void Asmline::setOpCode(std::string opCodeGiven) { opCode = opCodeGiven; }
void Asmline::setLabelToJump(std::string labelToJumpGiven) {
labelsToJump.push_back(labelToJumpGiven);
}
void Asmline::setLabel(std::string labelGiven) { label = labelGiven; }

Asmline::Asmline(Asmline&& other) {
this->opCode = std::move(other.opCode);
this->label = std::move(other.label);
this->labelsToJump = std::move(other.labelsToJump);
this->jumpCodes = std::move(other.jumpCodes);
}
5 changes: 2 additions & 3 deletions asmline.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ class Asmline {
std::list<std::string> jumpCodes;

public:
Asmline();
void setLabel(std::string labelGiven);
void setOpCode(std::string opCodeGiven);
Asmline(std::string label_, std::string opCode);
void setLabelToJump(std::string labelToJumpGiven);
std::string getOpcode();
std::string getLabel();
std::list<std::string> getLabelsToJumpTo();
void setLabelsToJump(std::list<std::string> labelsToJumpGiven);
bool isJump();
bool esCortante();
Asmline(Asmline&& other);
};

#endif // ASMLINE_H_
3 changes: 1 addition & 2 deletions graphFiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ void Graphfiller::restart() {

void Graphfiller::addInstructionToGraph(std::string line, int lineNumber) {
if (line.size() == 0) return;
Asmline instruction;
parser.parseInstruction(std::move(line), instruction);
Asmline instruction = std::move(parser.parseInstruction(std::move(line)));
opGraph.addVertex(lineNumber);
if (lineNumber != 1) opGraph.connectLast(lineNumber);
std::list<std::string> labelsToJump = instruction.getLabelsToJumpTo();
Expand Down
11 changes: 8 additions & 3 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@ static void parseSpaces(std::string* asmLine) {
*asmLine = noSpacesExtra;
}

void Parser::parseInstruction(std::string asmLine, Asmline& instruction) {
Asmline Parser::parseInstruction(std::string asmLine) {
parseSpaces(&asmLine);
std::string label;
std::string opCode;

int firstDots = asmLine.find_first_of(":");
if (firstDots != -1) {
instruction.setLabel(std::move(asmLine.substr(0, firstDots)));
label = std::move(asmLine.substr(0, firstDots));
asmLine.erase(0, firstDots + 1);
int firstNonSpace = asmLine.find_first_not_of(" ");
asmLine.erase(0, firstNonSpace);
}

int firstSpace = asmLine.find_first_of(" ");
if (firstSpace != -1) {
instruction.setOpCode(std::move(asmLine.substr(0, firstSpace)));
opCode = std::move(asmLine.substr(0, firstSpace));
asmLine.erase(0, firstSpace + 1);
}

Asmline instruction(std::move(label), std::move(opCode));

if (instruction.isJump()) {
int firstComma = asmLine.find_first_of(",");
if (firstComma != -1) {
Expand All @@ -45,4 +49,5 @@ void Parser::parseInstruction(std::string asmLine, Asmline& instruction) {
instruction.setLabelToJump(std::move(asmLine));
}
}
return instruction;
}
2 changes: 1 addition & 1 deletion parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Parser {
public:
void parseInstruction(std::string asmLine, Asmline& instruction);
Asmline parseInstruction(std::string asmLine);
};

#endif // PARSER_H_

0 comments on commit 48d823d

Please sign in to comment.