-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_interpreter.py
More file actions
58 lines (49 loc) · 1.17 KB
/
simple_interpreter.py
File metadata and controls
58 lines (49 loc) · 1.17 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
#!/usr/bin/env python
# https://www.codewars.com/kata/58e24788e24ddee28e000053
def simple_assembler(program):
pc = 0
pl = len(program)
regs = {}
def mov(reg, value):
if value.isalpha():
regs[reg] = regs[value]
else:
regs[reg] = int(value)
def inc(reg):
regs[reg] = regs[reg]+1
def dec(reg):
regs[reg] = regs[reg]-1
def jnz(v1, v2):
nonlocal pc
if v1.isalpha():
if regs[v1] != 0:
pc = pc+int(v2)-1
else:
if v1 != 0:
pc = pc+int(v2)-1
cmds = {
'mov':mov,
'inc':inc,
'dec':dec,
'jnz':jnz
}
while pc < pl:
curr_cmd = program[pc].split()
pc = pc+1
cmds[curr_cmd[0]](*curr_cmd[1:])
# return a dictionary with the registers
return regs
if __name__=="__main__":
code = '''\
mov c 12
mov b 0
mov a 200
dec a
inc b
jnz a -2
dec c
mov a b
jnz c -5
jnz 0 1
mov c a'''
print(simple_assembler(code.splitlines()))