Skip to content

Commit

Permalink
Refactor del parser, evito aun mas allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-R0 committed Nov 15, 2020
1 parent ded2539 commit 6ca8cf9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 64 deletions.
14 changes: 7 additions & 7 deletions asmline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
#include <algorithm>
#include <iostream>

Asmline::Asmline() {
label = {};
opCode = {};
labelsToJump = {};
jumpCodes = {"jmp", "ja", "jeq", "jneq", "jne",
"jlt", "jle", "jgt", "jge", "jset"};
}
Asmline::Asmline()
: jumpCodes({"jmp", "ja", "jeq", "jneq", "jne", "jlt", "jle", "jgt", "jge",
"jset"}) {}

bool Asmline::isJump() {
return (find(jumpCodes.begin(), jumpCodes.end(), opCode) != jumpCodes.end());
Expand All @@ -29,6 +25,10 @@ std::list<std::string> Asmline::getLabelsToJumpTo() {
}
void Asmline::setLabel(std::string labelGiven) { label = labelGiven; }
void Asmline::setOpCode(std::string opCodeGiven) { opCode = opCodeGiven; }
void Asmline::setLabelToJump(std::string labelToJumpGiven) {
labelsToJump.push_back(labelToJumpGiven);
}

void Asmline::setLabelsToJump(std::list<std::string> labelsToJumpGiven) {
if (!this->isJump()) return;
if (labelsToJumpGiven.size() == 1) {
Expand Down
1 change: 1 addition & 0 deletions asmline.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Asmline {
Asmline();
void setLabel(std::string labelGiven);
void setOpCode(std::string opCodeGiven);
void setLabelToJump(std::string labelToJumpGiven);
std::string getOpcode();
std::string getLabel();
std::list<std::string> getLabelsToJumpTo();
Expand Down
2 changes: 0 additions & 2 deletions graphFiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ Graphfiller::Graphfiller() {

void Graphfiller::addInstructionToGraph(std::string line, int lineNumber) {
if (line.size() == 0) return;

Asmline instruction;
parser.parseInstruction(std::move(line), instruction);
opGraph.addVertex(lineNumber);
if (lineNumber != 1) opGraph.connectLast(lineNumber);

std::list<std::string> labelsToJump = instruction.getLabelsToJumpTo();
if (instruction.esCortante()) aristaACortar.push_front(lineNumber);
if (instruction.getLabel().size() != 0)
Expand Down
74 changes: 24 additions & 50 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,43 @@
#include <iostream>
#include <string>

#define SUCCESS 0
#define FAILURE (SUCCESS - 1)

Parser::Parser() : delims({':', ',', ' ', '\n', '\t', '\0'}) {}

bool Parser::isDelim(char c) {
for (auto i : delims) {
if (i == c) return true;
}
return false;
}

static void parseSpaces(std::string* asmLine) {
std::string noSpacesExtra = {};
char anterior = 0;
for (auto c : *asmLine) {
if (!(c == ' ' && anterior == ' ')) noSpacesExtra.push_back(c);
anterior = c;
}
if (noSpacesExtra.front() == ' ') noSpacesExtra.erase(0, 1);
*asmLine = noSpacesExtra;
}

static void parseLabel(Asmline& instruction, bool hasLabel,
std::list<std::string>& parts) {
if (parts.size() == 0) return;
if (hasLabel) {
instruction.setLabel(std::move(parts.front()));
parts.pop_front();
void Parser::parseInstruction(std::string asmLine, Asmline& instruction) {
parseSpaces(&asmLine);
int firstDots = asmLine.find_first_of(":");
if (firstDots != -1) {
instruction.setLabel(asmLine.substr(0, firstDots));
asmLine.erase(0, firstDots + 1);
int firstNonSpace = asmLine.find_first_not_of(" ");
asmLine.erase(0, firstNonSpace);
}
}

static int parseData(Parser* p, std::string asmLine, bool& hasLabel,
std::list<std::string>& parts, Asmline& instruction) {
std::string placeHolder;
char lastChar = 0;
for (auto c : asmLine) {
lastChar = c;
if (p->isDelim(c)) {
if (c == ':') hasLabel = true;
if (placeHolder.size() != 0) {
parts.push_back(placeHolder);
}
if (c != '\n') placeHolder.erase();
} else if (!p->isDelim(c)) {
placeHolder.push_back(c);
}
int firstSpace = asmLine.find_first_of(" ");
if (firstSpace != -1) {
instruction.setOpCode(asmLine.substr(0, firstSpace));
asmLine.erase(0, firstSpace + 1);
}
if (!p->isDelim(lastChar)) parts.push_back(placeHolder);
if (parts.size() == 0) return FAILURE; // Empty line.
parseLabel(instruction, hasLabel, parts);
return SUCCESS;
}

void Parser::parseInstruction(std::string asmLine, Asmline& instruction) {
bool hasLabel = false;
std::list<std::string> parts;

parseSpaces(&asmLine);

if (parseData(this, std::move(asmLine), hasLabel, parts, instruction) ==
SUCCESS) {
instruction.setOpCode(std::move(parts.front()));
parts.pop_front();
instruction.setLabelsToJump(std::move(parts));
if (instruction.isJump()) {
int firstComma = asmLine.find_first_of(",");
std::string firstArg = asmLine.substr(0, firstComma);
asmLine.erase(0, firstComma + 2);
if ((firstComma = asmLine.find_first_of(",")) != -1) {
instruction.setLabelToJump(asmLine.substr(0, firstComma));
asmLine.erase(0, firstComma + 2);
if (!asmLine.empty()) instruction.setLabelToJump(asmLine);
} else {
instruction.setLabelToJump(asmLine);
}
}
}
5 changes: 0 additions & 5 deletions parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
#include "asmline.h"

class Parser {
private:
std::list<char> delims;

public:
Parser();
void parseInstruction(std::string asmLine, Asmline& instruction);
bool isDelim(char c);
};

#endif // PARSER_H_

0 comments on commit 6ca8cf9

Please sign in to comment.