-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssembler.h
48 lines (38 loc) · 1.17 KB
/
Assembler.h
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
#ifndef ASSEMBLER_H
#define ASSEMBLER_H
#include "Instruction.h"
#include <vector>
#include <map>
#include <string>
class Assembler {
public:
std::vector<Instruction> assemble(const std::string& code) {
std::vector<Instruction> program;
std::map<std::string, OpCode> opcodes{
{"halt", OpCode::HALT},
{"load", OpCode::LOAD},
{"add", OpCode::ADD},
{"sub", OpCode::SUB},
{"mul", OpCode::MUL},
{"div", OpCode::DIV}
};
size_t pos = 0;
while (pos < code.size()) {
size_t end = code.find('\n', pos);
if (end == std::string::npos) {
end = code.size();
}
std::string line = code.substr(pos, end - pos);
size_t space = line.find(' ');
std::string mnemonic = line.substr(0, space);
Instruction instruction{opcodes[mnemonic], 0};
if (space != std::string::npos) {
instruction.operand = std::stoi(line.substr(space + 1));
}
program.push_back(instruction);
pos = end + 1;
}
return program;
}
};
#endif