-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
83 lines (77 loc) · 2.25 KB
/
Copy pathcommand.js
File metadata and controls
83 lines (77 loc) · 2.25 KB
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
export default class Command {
constructor(rawLine) {
this._rawLine = rawLine;
this.arg1 = '';
this.arg2 = '';
let parts = rawLine.split(/[\s]+/).filter(p => p !== '');
if (parts.length) {
switch (parts[0]) {
case 'add':
case 'sub':
case 'neg':
case 'eq':
case 'gt':
case 'lt':
case 'and':
case 'or':
case 'not':
this.type = CommandType.ARITHMETIC;
this.parseArithmetic(parts);
break;
case 'push':
this.type = CommandType.PUSH;
this.parseBinary(parts);
break;
case 'pop':
this.type = CommandType.POP;
this.parseBinary(parts);
break;
case 'label':
this.type = CommandType.LABEL;
this.parseUnary(parts);
break;
case 'goto':
this.type = CommandType.GOTO;
this.parseUnary(parts);
break;
case 'if-goto':
this.type = CommandType.IF;
this.parseUnary(parts);
break;
case 'function':
this.type = CommandType.FUNCTION;
this.parseBinary(parts);
break;
case 'call':
this.type = CommandType.CALL;
this.parseBinary(parts);
break;
case 'return':
this.type = CommandType.RETURN;
}
}
}
parseArithmetic(parts) {
this.arg1 = parts[0];
}
parseUnary(parts) {
this.arg1 = parts[1];
}
parseBinary(parts) {
this.arg1 = parts[1];
this.arg2 = parts[2];
}
}
export const CommandType = {
UNKNOWN: 'Unknown',
ARITHMETIC: 'Arithmetic',
PUSH: 'Push',
POP: 'Pop',
LABEL: 'Label',
GOTO: 'Goto',
IF: 'If',
FUNCTION: 'Function',
RETURN: 'Return',
CALL: 'Call',
COMMENT: 'Comment'
};