-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ts
39 lines (33 loc) · 870 Bytes
/
code.ts
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
import { IParser, ICode } from './interface'
import Logger from './logger'
import constants from './constants'
const { COMMAND, MNEMONIC } = constants
export default class Code {
logger: Logger
constructor() {
this.logger = new Logger(Code)
}
compile(parsed: IParser) {
if (parsed.command !== COMMAND.C.type) {
return null
}
const r: ICode = {
dest: this.dest(parsed.dest),
comp: this.comp(parsed.comp),
jump: this.jump(parsed.jump),
}
return `111${this.getAbit(parsed.comp)}${r.comp}${r.dest}${r.jump}`
}
getAbit(comp: string) {
return comp.indexOf('M') !== -1 ? 1 : 0
}
dest(str: string) {
return !str ? MNEMONIC.DEST.NONE : MNEMONIC.DEST[str]
}
comp(str: string) {
return MNEMONIC.COMP[str]
}
jump(str: string) {
return !str ? MNEMONIC.JUMP.NONE : MNEMONIC.JUMP[str]
}
}