-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
55 lines (48 loc) · 1.6 KB
/
parser.cpp
File metadata and controls
55 lines (48 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "parser.h"
#include <iostream>
#include <string>
#include <utility>
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 std::string makeLabel(int firstDots, std::string* asmLine) {
if (firstDots == -1) return "";
std::string label = asmLine->substr(0, firstDots);
asmLine->erase(0, firstDots + 1);
asmLine->erase(0, asmLine->find_first_not_of(" "));
return label;
}
static std::string makeOpCode(int firstSpace, std::string* asmLine) {
if (firstSpace == -1) return "";
std::string opCode = asmLine->substr(0, firstSpace);
asmLine->erase(0, firstSpace + 1);
return opCode;
}
Asmline Parser::parseInstruction(std::string asmLine) {
parseSpaces(&asmLine);
std::string label(makeLabel(asmLine.find_first_of(":"), &asmLine));
std::string opCode(makeOpCode(asmLine.find_first_of(" "), &asmLine));
Asmline instruction(std::move(label), std::move(opCode));
if (instruction.isJump()) {
int firstComma = asmLine.find_first_of(",");
if (firstComma != -1) {
asmLine.erase(0, firstComma + 2);
int secondComma = asmLine.find_first_of(",");
if (secondComma != -1) {
instruction.setLabelToJump(asmLine.substr(0, secondComma));
asmLine.erase(0, secondComma + 2);
}
instruction.setLabelToJump(asmLine);
} else {
instruction.setLabelToJump(asmLine);
}
}
return instruction;
}