-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.cpp
169 lines (157 loc) · 5.68 KB
/
commands.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// commands.cpp
// HRM-CCPU
//
// Copyright © 2015 Lewis Fox.
// Licenced under The MIT License (MIT)
//
#include "commands.hpp"
#include "commandsgen.ipp"
#include <cstdlib>
#include <string>
#include <unordered_map>
uint8_t getCommand(const std::string &str) {
if (str[0] == '-' && str[1] == '-') return 0xD;
if (str[str.size() - 1] == ':') return 0xC;
const Instruction *i = InstructionLookup::get(str.c_str(), (unsigned int) str.size());
if (i == nullptr) return 0xF;
return i->cmd;
}
int16_t getIndex(const std::string &str) {
std::string::const_iterator begin = str.begin(), end = str.end();
bool deref = *begin == '[' && *(end - 1) == ']';
if (deref) ++begin, --end;
int16_t v = 0;
for (std::string::const_iterator i = begin; i < end; ++i) {
const char c = *i;
if (c < '0' || c > '9') return -1;
v *= 10;
v += c - '0';
if (v >= 0x800) return -1;
}
return (deref << 0xC) | v;
}
int16_t getIndexNoDeref(const std::string &str) {
int16_t v = 0;
for (const char c : str) {
if (c < '0' || c > '9') return -1;
v *= 10;
v += c - '0';
if (v >= 0x800) return -1;
}
return v;
}
int16_t getValue(const std::string &str) {
if (str.size() == 1 && str[0] >= 'A' && str[0] <= 'Z') return SpecialFlag | (str[0] - 'A');
std::string::const_iterator begin = str.begin(), end = str.end();
bool neg = *begin == '-';
if (neg) ++begin;
int16_t v = 0;
for (std::string::const_iterator i = begin; i < end; ++i) {
const char c = *i;
if (c < '0' || c > '9') return Empty;
v *= 10;
v += c - '0';
if (v > 999) return Empty;
}
return (neg ? -1 : 1) * v;
}
bool verifyNumber(const std::string &str) {
for (const char &c : str)
if (c < '0' || c > '9')
return false;
return true;
}
std::pair<Program, RunInfo> setInstructions(std::istream &in) {
Program coms;
RunInfo rinfo;
bool rinfowriten = false;
std::unordered_map<std::string, Program::value_type> locations;
std::unordered_map<std::string, Program::value_type> unfound;
while (in.good()) {
std::string s;
in >> s;
if (s.size() == 0) continue;
uint8_t com = getCommand(s);
if (com < 0xB) {
int16_t index = 0;
if (com >= 0x8) { // Jumps
in >> s;
if (locations.count(s)) index = locations.at(s);
else {
if (unfound.count(s)) index = unfound.at(s);
else index = ~0;
unfound[s] = coms.size();
}
} else if (com >= 0x2) { // Indexed
in >> s;
index = getIndex(s);
if (index < 0)
std::exit(1);
}
coms.push_back((((uint16_t) index) << 4) | ((uint16_t) com));
if (coms.size() >= 0x1000) std::exit(1);
} else { // Special
switch (com) {
case Commands::COMMENT:
in >> s;
if (!verifyNumber(s)) std::exit(1);
break;
case Commands::LABEL:
s = s.substr(0, s.size() - 1);
locations[s] = coms.size();
if (unfound.count(s)) {
uint16_t loc = unfound.at(s);
do {
uint16_t &ins = coms[loc];
loc = (ins >> 4);
ins = (coms.size() << 4) | (ins & 0xF);
} while (loc != ((uint16_t) ~0 >> 4));
unfound.erase(s);
}
break;
case Commands::LCOMMENT:
if (s.size() == 3 && s[2] == '#') { // RunInfo
if (rinfowriten) std::exit(1);
in >> s;
int16_t memsize = getIndexNoDeref(s);
if (memsize < 0) std::exit(1);
rinfo.memory = Values(memsize, Empty);
in >> s;
while (s.size() != 1 || s[0] != ':') {
if (s.compare("#--") == 0) goto done;
int16_t ind = getIndexNoDeref(s);
if (ind < 0) std::exit(1);
in >> s;
uint16_t val = getValue(s);
if (val == Empty) std::exit(1);
rinfo.memory[ind] = val;
in >> s;
}
in >> s;
while (s.compare("#--") != 0) {
uint16_t val = getValue(s);
if (val == Empty) std::exit(1);
rinfo.input.push_back(val);
in >> s;
}
done:
rinfo.input.shrink_to_fit();
rinfowriten = true;
}
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
break;
case Commands::DEFINE:
in.ignore(std::numeric_limits<std::streamsize>::max(), ';');
if (in.eof()) std::exit(1);
break;
default:
std::exit(1);
break;
}
}
}
if (unfound.size() != 0) std::exit(1);
coms.shrink_to_fit();
return std::pair<Program, RunInfo>(coms, rinfo);
}