|
@@ -1,7 +1,7 @@
|
1
|
1
|
package intcode
|
2
|
2
|
|
3
|
3
|
// opcodeFunc is a function that describes an opcode implemented in the VM.
|
4
|
|
-type opcodeFunc = func(vm *VirtualMachine)
|
|
4
|
+type opcodeFunc = func(vm *VirtualMachine, arg1, arg2, arg3 *int)
|
5
|
5
|
|
6
|
6
|
var opcodes = [100]opcodeFunc{
|
7
|
7
|
1: addOpcode,
|
|
@@ -16,45 +16,58 @@ var opcodes = [100]opcodeFunc{
|
16
|
16
|
99: haltOpcode,
|
17
|
17
|
}
|
18
|
18
|
|
|
19
|
+var opcodeArity = [100]int{
|
|
20
|
+ 1: 3,
|
|
21
|
+ 2: 3,
|
|
22
|
+ 3: 1,
|
|
23
|
+ 4: 1,
|
|
24
|
+ 5: 2,
|
|
25
|
+ 6: 2,
|
|
26
|
+ 7: 3,
|
|
27
|
+ 8: 3,
|
|
28
|
+ 9: 1,
|
|
29
|
+ 99: 0,
|
|
30
|
+}
|
|
31
|
+
|
19
|
32
|
// addOpcode takes the values specified by args 1 and 2, adds them together, and stores at the memory address given
|
20
|
33
|
// by arg 3.
|
21
|
|
-func addOpcode(vm *VirtualMachine) {
|
22
|
|
- *vm.arg(2) = *vm.arg(0) + *vm.arg(1)
|
|
34
|
+func addOpcode(vm *VirtualMachine, arg1, arg2, arg3 *int) {
|
|
35
|
+ *arg3 = *arg1 + *arg2
|
23
|
36
|
vm.ip += 4
|
24
|
37
|
}
|
25
|
38
|
|
26
|
39
|
// mulOpcode takes the values specified by args 1 and 2, multiplies them together, and stores at the memory address
|
27
|
40
|
// given by arg 3.
|
28
|
|
-func mulOpcode(vm *VirtualMachine) {
|
29
|
|
- *vm.arg(2) = *vm.arg(0) * *vm.arg(1)
|
|
41
|
+func mulOpcode(vm *VirtualMachine, arg1, arg2, arg3 *int) {
|
|
42
|
+ *arg3 = *arg1 * *arg2
|
30
|
43
|
vm.ip += 4
|
31
|
44
|
}
|
32
|
45
|
|
33
|
46
|
// readOpCode reads a value from the input stream and stores it at the memory address given by arg 1.
|
34
|
|
-func readOpCode(vm *VirtualMachine) {
|
35
|
|
- *vm.arg(0) = <-vm.Input
|
|
47
|
+func readOpCode(vm *VirtualMachine, arg1, _, _ *int) {
|
|
48
|
+ *arg1 = <-vm.Input
|
36
|
49
|
vm.ip += 2
|
37
|
50
|
}
|
38
|
51
|
|
39
|
52
|
// writeOpCode writes the value specified by the first argument to the output stream.
|
40
|
|
-func writeOpCode(vm *VirtualMachine) {
|
41
|
|
- vm.Output <- *vm.arg(0)
|
|
53
|
+func writeOpCode(vm *VirtualMachine, arg1, _, _ *int) {
|
|
54
|
+ vm.Output <- *arg1
|
42
|
55
|
vm.ip += 2
|
43
|
56
|
}
|
44
|
57
|
|
45
|
58
|
// jumpIfTrueOpCode checks if the first argument is not zero, and if so jumps to the second argument.
|
46
|
|
-func jumpIfTrueOpCode(vm *VirtualMachine) {
|
47
|
|
- if *vm.arg(0) != 0 {
|
48
|
|
- vm.ip = *vm.arg(1)
|
|
59
|
+func jumpIfTrueOpCode(vm *VirtualMachine, arg1, arg2, _ *int) {
|
|
60
|
+ if *arg1 != 0 {
|
|
61
|
+ vm.ip = *arg2
|
49
|
62
|
} else {
|
50
|
63
|
vm.ip += 3
|
51
|
64
|
}
|
52
|
65
|
}
|
53
|
66
|
|
54
|
67
|
// jumpIfFalseOpCode checks if the first argument is zero, and if so jumps to the second argument.
|
55
|
|
-func jumpIfFalseOpCode(vm *VirtualMachine) {
|
56
|
|
- if *vm.arg(0) == 0 {
|
57
|
|
- vm.ip = *vm.arg(1)
|
|
68
|
+func jumpIfFalseOpCode(vm *VirtualMachine, arg1, arg2, _ *int) {
|
|
69
|
+ if *arg1 == 0 {
|
|
70
|
+ vm.ip = *arg2
|
58
|
71
|
} else {
|
59
|
72
|
vm.ip += 3
|
60
|
73
|
}
|
|
@@ -62,33 +75,33 @@ func jumpIfFalseOpCode(vm *VirtualMachine) {
|
62
|
75
|
|
63
|
76
|
// lessThanOpCode checks if the first argument is less than the second, and stores the result at the address given
|
64
|
77
|
// by the third argument.
|
65
|
|
-func lessThanOpCode(vm *VirtualMachine) {
|
66
|
|
- if *vm.arg(0) < *vm.arg(1) {
|
67
|
|
- *vm.arg(2) = 1
|
|
78
|
+func lessThanOpCode(vm *VirtualMachine, arg1, arg2, arg3 *int) {
|
|
79
|
+ if *arg1 < *arg2 {
|
|
80
|
+ *arg3 = 1
|
68
|
81
|
} else {
|
69
|
|
- *vm.arg(2) = 0
|
|
82
|
+ *arg3 = 0
|
70
|
83
|
}
|
71
|
84
|
vm.ip += 4
|
72
|
85
|
}
|
73
|
86
|
|
74
|
87
|
// equalsOpCode checks if the first argument is equal to the second, and stores the result at the address given
|
75
|
88
|
// by the third argument.
|
76
|
|
-func equalsOpCode(vm *VirtualMachine) {
|
77
|
|
- if *vm.arg(0) == *vm.arg(1) {
|
78
|
|
- *vm.arg(2) = 1
|
|
89
|
+func equalsOpCode(vm *VirtualMachine, arg1, arg2, arg3 *int) {
|
|
90
|
+ if *arg1 == *arg2 {
|
|
91
|
+ *arg3 = 1
|
79
|
92
|
} else {
|
80
|
|
- *vm.arg(2) = 0
|
|
93
|
+ *arg3 = 0
|
81
|
94
|
}
|
82
|
95
|
vm.ip += 4
|
83
|
96
|
}
|
84
|
97
|
|
85
|
98
|
// relativeBaseOffsetOpCode increases the relative base by the given argument.
|
86
|
|
-func relativeBaseOffsetOpCode(vm *VirtualMachine) {
|
87
|
|
- vm.rb += *vm.arg(0)
|
|
99
|
+func relativeBaseOffsetOpCode(vm *VirtualMachine, arg1, _, _ *int) {
|
|
100
|
+ vm.rb += *arg1
|
88
|
101
|
vm.ip += 2
|
89
|
102
|
}
|
90
|
103
|
|
91
|
104
|
// haltOpcode halts the VM and takes no arguments.
|
92
|
|
-func haltOpcode(vm *VirtualMachine) {
|
|
105
|
+func haltOpcode(vm *VirtualMachine, _, _, _ *int) {
|
93
|
106
|
vm.Halted = true
|
94
|
107
|
}
|