-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.js
39 lines (33 loc) · 842 Bytes
/
compiler.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
const { INS_CODE, hex } = require('./opcode.js');
exports.parse = function compile(source) {
return source
.trim()
.split(/[\n\r]/)
.map(line => {
const [command, param] = line.trim().split(' ');
if (command === '') {
return [];
}
if (param === undefined) {
return [Number(command)];
}
return [command, Number(param)];
});
};
exports.compile = function(source) {
const parsed = exports.parse(source);
var s = [];
for (let i = 0; i < parsed.length; i++) {
let line = '';
if (parsed[i].length == 1) {
line = parsed[i][0];
} else {
const [instruction, parameter = 0] = parsed[i];
const opCode = INS_CODE[instruction];
line = (opCode << 4) | parameter;
}
// console.log(b(line, 8));
s.push(line);
}
return s;
};