This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize.js
229 lines (193 loc) · 4.75 KB
/
tokenize.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
let argumentCounts = {
"MOV": 2,
"ADD": 2,
"SUB": 2,
"MUL": 2,
"DIV": 2,
"INC": 1,
"DEC": 1,
"AND": 2,
"OR": 2,
"XOR": 2,
"NOT": 1,
"ROL": 1,
"ROR": 1,
"SHL": 1,
"SHR": 1,
"CMP": 2,
"JMP": 1,
"JZ": 1,
"JNZ": 1,
"JS": 1,
"JNS": 1,
"JO": 1,
"JNO": 1,
"CALL": 1,
"RET": 0,
"INT": 1,
"IRET": 0,
"PUSH": 1,
"POP": 1,
"PUSHF": 0,
"POPF": 0,
"IN": 1,
"OUT": 1,
"CLO": 0,
"HALT": 0,
"NOP": 0,
"STI": 0,
"CLI": 0,
"ORG": 1,
"DB": 1,
"END": 0
};
function isWhitespace(chr) {
return (
// if it's a regular space
chr === " " ||
// if it's a tab
chr === "\t" ||
// if it's a newline
chr === "\n"
);
}
function nextWhitespace(str, pos) {
let char = str[pos];
while (isWhitespace(char)) {
pos++;
char = str[pos];
}
return [pos, char];
}
function nextToken(str, pos) {
let char = str[pos];
let token = "";
if (char === `"`) {
token += char;
pos++;
char = str[pos];
while (char && char !== `"`) {
token += char;
pos++;
char = str[pos];
}
token += char;
} else {
while (char && !isWhitespace(char)) {
token += char;
pos++;
char = str[pos];
}
}
return [pos, char, token];
}
function tokenize(str) {
// tokenizer state
let pos = 0;
let res = [];
while (pos < (str.length - 1)) {
// tokenizer state
let char = "";
let token = "";
// skip all whitespace characters until we find something
// we can tokenize
[pos, char] = nextWhitespace(str, pos);
// grab the next token
[pos, char, token] = nextToken(str, pos);
// ignore comments in the assembly
if (token[0] === ";") {
// find the index of the next newline after the comment
let lastNewline = str.indexOf("\n", pos);
// if there's no new lines, we're done!
if (lastNewline === -1) break;
// otherwise, we skip over the comment and continue tokenizing.
pos = lastNewline + 1;
continue;
}
// convert to uppercase to aid string comparison
token = token.toUpperCase();
// check if this is a label
let lastChar = token.substr(token.length - 1);
// if it's a label, shtick it in there
if (lastChar === ":") {
res.push(token);
continue;
}
// check is the instruction is invalid so we don't go any further
// and throw a useful error
if (!(token in argumentCounts)) {
// grab the start location of the rogue instruction
let loc = pos - token.length;
let errmsg = `Unrecognised instruction "${token}" at position ${loc}`;
let err = new Error(errmsg);
err.location = loc;
throw err;
}
// woo, you've made it to the next stage mr. token!
res.push(token)
// now, we deal with the arguments of the instruction.
// we store an object with the number of expected arguments and
// check against that.
let argumentCount = argumentCounts[token]
// if there's no argument, we can skip along to the next instruction
if (argumentCount === 0) {
pos++;
continue;
}
// if there's one argument, we simply add it to the tokens list as-is.
if (argumentCount === 1) {
let token = "";
[pos, char] = nextWhitespace(str, pos);
[pos, char, token] = nextToken(str, pos);
if (token === "") {
let errmsg = `Expected argument at position ${pos}`
let err = new Error(errmsg);
err.location = pos;
throw err;
}
res.push(token.toUpperCase());
}
// if there're two arguments, we need to check the first to ensure
// it has a trailing comma. otherwise, instruction denied.
if (argumentCount === 2) {
let token = "";
[pos, char] = nextWhitespace(str, pos);
[pos, char, token] = nextToken(str, pos);
if (token === "") {
let errmsg = `Expected first argument at position ${pos}`
let err = new Error(errmsg);
err.location = pos;
throw err;
}
let lastChar = token.substr(token.length - 1)
// check to see if the first argument doesn't have a comma at the end
// if it doesn't, we can't continue assembling; throw a useful err.
if (lastChar !== ",") {
let errmsg = `Expected "," at position ${pos}`;
let err = new Error(errmsg);
err.location = pos;
throw err;
}
// if we reach here, all's well and good -- add the first argument
// to the list of tokens. we strip the comma to make it easier
// for the assembler.
res.push(token.substr(0, token.length - 1).toUpperCase());
token = "";
[pos, char] = nextWhitespace(str, pos);
[pos, char, token] = nextToken(str, pos);
if (token === "") {
let errmsg = `Expected second argument at position ${pos}`
let err = new Error(errmsg);
err.location = pos;
throw err;
}
// add the second argument to the list of tokens.
res.push(token.toUpperCase());
}
pos++;
}
return res;
}
if (typeof module !== "undefined") {
module.exports = tokenize;
}