-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
52 lines (45 loc) · 992 Bytes
/
parser.js
File metadata and controls
52 lines (45 loc) · 992 Bytes
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
const command_map = {
'push': 'C_PUSH',
'pop': 'C_POP',
'add': 'C_ARITHMETIC',
'sub': 'C_ARITHMETIC',
'neg': 'C_ARITHMETIC',
'eq': 'C_ARITHMETIC',
'gt': 'C_ARITHMETIC',
'lt': 'C_ARITHMETIC',
'and': 'C_ARITHMETIC',
'or': 'C_ARITHMETIC',
'not': 'C_ARITHMETIC',
'label': 'C_LABEL',
'goto': 'C_GOTO',
'if-goto': 'C_IF',
'function': 'C_FUNCTION',
'return': 'C_RETURN',
'call': 'C_CALL',
}
class Parser {
constructor(fileContents) {
this.fileContents = fileContents;
this.currentIndex = 0;
this.command;
}
hasMoreCommands() {
return this.currentIndex < this.fileContents.length;
}
advance() {
this.command = this.fileContents[this.currentIndex].split(' ');
this.currentIndex++;
}
commandType() {
return command_map[this.command[0]];
}
arg1() {
const commandType = this.commandType();
if(commandType === 'C_ARITHMETIC') return this.command[0];
else return this.command[1];
}
arg2() {
return this.command[2];
}
}
module.exports = Parser;