-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
62 lines (56 loc) · 1.42 KB
/
index.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
function executeCommands(commands) {
let records = Array.from({ length: 8 }).fill(0)
const getIndexFromRef = ref => Number(ref.at(-1))
const checkOverflow = val => {
if (val > 255) {
return val - 256
}
if (val < 0) {
return 254 - val
}
return val
}
const functions = {
MOV: (value, ref) => {
let nValue = Number(value)
if (value.startsWith('V')) {
nValue = records[getIndexFromRef(value)]
}
records[getIndexFromRef(ref)] = checkOverflow(nValue)
},
ADD: (refx, refy) => {
return (records[getIndexFromRef(refx)] += checkOverflow(
records[getIndexFromRef(refy)]
))
},
DEC: ref => {
return (records[getIndexFromRef(ref)] = checkOverflow(
records[getIndexFromRef(ref)] - 1
))
},
INC: ref => {
return (records[getIndexFromRef(ref)] = checkOverflow(
records[getIndexFromRef(ref)] + 1
))
},
JMP: (s, t) => {
const z = commands.slice(s, t)
while (records[0] !== 0) {
z.forEach(zz => {
const x = zz.split(' ')
const fun = x[0]
const [value, ref] = x[1].split(',')
functions[fun](value, ref)
})
}
return
}
}
commands.forEach((cmd, i) => {
const x = cmd.split(' ')
const command = x[0]
const [value, ref] = x[1].split(',')
return functions[command](value, ref || i)
})
return records
}