-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.js
287 lines (273 loc) · 5.78 KB
/
lexer.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Tokens for lexer. Many are also used by the parser.
const VAR=0,
NUMBER=1,
REGISTER=2,
OPCODE=3,
PTR=4,
SIZE=5,
SIZE_DECL=6,
CONST_DEF=7,
DUP=8;
/*
TODO: Not handling strings
*/
function Token(type, token) {
this.type = type;
this.token = (token===undefined ? type : token);
}
Token.prototype.toString = function() {
return ' ' + this.type + ':' + this.token;
}
function isWhitespace(c) {
return c === ' ' || c === '\t';
}
// String (one command) -> Array of tokens
function lexCommand(cmd) {
var tokens = [],
i = 0,
j = 0,
end = cmd.length,
c = ' ',
t = '',
type;
while (j<end) {
while (isWhitespace(cmd.charAt(i)) && i<end) i++;
j = i+1;
if (i === end) break;
switch (cmd.charAt(i)) {
case '.':
while (j < end && cmd.charAt(j) !== ' ') j++;
tokens.push(new Token('.', cmd.substring(i, j)));
break;
case ',':
tokens.push(new Token(','));
break;
case '+':
tokens.push(new Token('+'));
break;
case '-':
tokens.push(new Token('-'));
break;
case '(':
tokens.push(new Token('('));
break;
case ')':
tokens.push(new Token(')'));
break;
case '[':
tokens.push(new Token('['));
break;
case ']':
tokens.push(new Token(']'));
break;
case ':':
tokens.push(new Token(':'));
break;
case '$':
tokens.push(new Token('$'));
break;
case '?':
tokens.push(new Token('?'));
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
for (; j < end; j++) {
c = cmd.charAt(j);
if (!(c>='0' && c<='9')) break;
}
if (c === 'h' || c === 'd') j++;
t = cmd.substring(i, j);
tokens.push(new Token(NUMBER, t));
break;
default:
for (; j < end; j++) {
c = cmd.charAt(j);
if (!((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c==='_')) break;
}
t = cmd.substring(i, j);
if (isOpCode(t)) {
tokens.push(new Token(OPCODE, t.toLowerCase()));
}
else if (isRegister(t)) {
tokens.push(new Token(REGISTER, t.toLowerCase()));
}
else if (isDirective(t)) {
// Using '.' for the token for directives, even if it was not the start of the directive
tokens.push(new Token('.', t.toLowerCase()));
}
else if (t.toLowerCase() === 'dup') {
tokens.push(new Token(DUP, t.toLowerCase()));
}
else if (t.toLowerCase() === 'equ') {
tokens.push(new Token(CONST_DEF, t.toLowerCase()));
}
else if (t.toLowerCase() === 'ptr') {
tokens.push(new Token(PTR, t.toLowerCase()));
}
else if (isSize(t)) {
tokens.push(new Token(SIZE, t.toLowerCase()));
}
else if (isSizeDecl(t)) {
tokens.push(new Token(SIZE_DECL, t.toLowerCase()));
}
else {
tokens.push(new Token(VAR, t));
}
}
i=j;
}
return tokens;
}
function isOpCode(t) {
/*
switch(t.toLowerCase()) {
case "mov":
case "push":
case "pop":
case "lea":
case "add":
case "sub":
case "inc":
case "dec":
case "imul":
case "idiv":
case "mul":
case "div":
case "and":
case "or":
case "xor":
case "not":
case "neg":
case "shl":
case "shr":
case "sal":
case "sar":
case "rol":
case "ror":
case "rcl":
case "rcr":
case "jmp":
case "je":
case "jne":
case "jz":
case "jnz":
case "jg":
case "jge":
case "jl":
case "jle":
case "ja":
case "jb":
case "jbe":
case "jc":
case "cmp":
case "test":
case "nop":
case "call":
case "ret":
case "retn":
case "movzx":
case "movsd":
case "movsb":
case "stosb":
case "stosd":
case "lodsb":
case "lodsd":
case "invoke":
case "stdcall":
case "xchg":
case "adc":
case "sbb":
case "popad":
case "pushad":
case "rep":
case "fadd":
case "fmul":
case "fsub":
case "fdiv":
case "fstp":
case "fld":
case "fst":
case "clc":
case "stc":
case "org":
case "cld":
case "end":
//case "jumps":
//case "extrn":
return true;
default:
return false;
}
*/
return opcodeLookup[t.toUpperCase()] === undefined;
}
function isRegister(t) {
switch (t.toLowerCase()) {
case "eax":
case "ebx":
case "ecx":
case "edx":
case "esi":
case "edi":
case "esp":
case "ebp":
case "ax":
case "bx":
case "cx":
case "dx":
case "ah":
case "bh":
case "ch":
case "dh":
case "al":
case "bl":
case "cl":
case "dl":
return true;
default:
return false;
}
}
function isDirective(t) {
switch(t.toLowerCase()) {
case "jumps":
case "extrn":
return true;
default:
return false;
}
}
function isSizeDecl(t) {
var token = t.toLowerCase();
return t === 'db'
|| t === 'dd'
|| t === 'dw'
// Not really sure about this one
|| t === 'label';
}
function isSize(t) {
var token = t.toLowerCase();
return t === 'byte'
|| t === 'word'
|| t === 'dword';
}
// Array of strings -> Array of Arrays of tokens
function lexFile(commands) {
var i, s,
commandTokens = [];
for (i=0; i<commands.length; i++) {
s = commands[i];
s = s.replace(/\s*;.*/, ''); // Strip out comments
if (s.match(/^\s*$/)) continue; // Skip empty lines
commandTokens.push(lexCommand(s));
}
return commandTokens;
}