Browse Source

Tidy a little

Treat PC as a register, which avoids using the global kw.
master
Chris Smith 7 years ago
parent
commit
4c242d8a99
1 changed files with 6 additions and 9 deletions
  1. 6
    9
      12.py

+ 6
- 9
12.py View File

@@ -12,22 +12,19 @@ def value(x):
12 12
 def cpy(args): registers[args[1]] = value(args[0])
13 13
 def inc(args): registers[args[0]] += 1
14 14
 def dec(args): registers[args[0]] -= 1
15
-def jnz(args): global pc; pc += (0 if value(args[0]) == 0 else value(args[1]) - 1)
15
+def jnz(args): registers['pc'] += 0 if value(args[0]) == 0 else value(args[1]) - 1
16 16
 
17 17
 def run():
18
-    global pc
19
-    while pc < len(instr):
20
-        globals()[instr[pc][0]](instr[pc][1:])
21
-        pc += 1
18
+    while registers['pc'] < len(instr):
19
+        globals()[instr[registers['pc']][0]](instr[registers['pc']][1:])
20
+        registers['pc'] += 1
22 21
 
23
-registers = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
24
-pc = 0
22
+registers = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'pc': 0}
25 23
 run()
26 24
 
27 25
 print("Stage 1: %s" % registers['a'])
28 26
 
29
-registers = {'a': 0, 'b': 0, 'c': 1, 'd': 0}
30
-pc = 0
27
+registers = {'a': 0, 'b': 0, 'c': 1, 'd': 0, 'pc': 0}
31 28
 run()
32 29
 
33 30
 print("Stage 2: %s" % registers['a'])

Loading…
Cancel
Save