|
@@ -1,50 +1,47 @@
|
1
|
1
|
package intcode
|
2
|
2
|
|
3
|
|
-// ArgFunc provides the value of an argument for an opcode
|
4
|
|
-type ArgFunc = func(pos int) int
|
5
|
|
-
|
6
|
3
|
// OpcodeFunc is a function that describes an opcode implemented in the VM.
|
7
|
|
-type OpcodeFunc = func(vm *VirtualMachine, param ArgFunc)
|
|
4
|
+type OpcodeFunc = func(vm *VirtualMachine)
|
8
|
5
|
|
9
|
6
|
// AddOpcode takes the values specified by args 1 and 2, adds them together, and stores at the memory address given
|
10
|
7
|
// by arg 3.
|
11
|
|
-func AddOpcode(vm *VirtualMachine, arg ArgFunc) {
|
12
|
|
- vm.Memory[vm.Memory[vm.ip+3]] = arg(0) + arg(1)
|
|
8
|
+func AddOpcode(vm *VirtualMachine) {
|
|
9
|
+ vm.Memory[vm.Memory[vm.ip+3]] = vm.arg(0) + vm.arg(1)
|
13
|
10
|
vm.ip += 4
|
14
|
11
|
}
|
15
|
12
|
|
16
|
13
|
// MulOpcode takes the values specified by args 1 and 2, multiplies them together, and stores at the memory address
|
17
|
14
|
// given by arg 3.
|
18
|
|
-func MulOpcode(vm *VirtualMachine, arg ArgFunc) {
|
19
|
|
- vm.Memory[vm.Memory[vm.ip+3]] = arg(0) * arg(1)
|
|
15
|
+func MulOpcode(vm *VirtualMachine) {
|
|
16
|
+ vm.Memory[vm.Memory[vm.ip+3]] = vm.arg(0) * vm.arg(1)
|
20
|
17
|
vm.ip += 4
|
21
|
18
|
}
|
22
|
19
|
|
23
|
20
|
// ReadOpCode reads a value from the input stream and stores it at the memory address given by arg 1.
|
24
|
|
-func ReadOpCode(vm *VirtualMachine, arg ArgFunc) {
|
|
21
|
+func ReadOpCode(vm *VirtualMachine) {
|
25
|
22
|
vm.Memory[vm.Memory[vm.ip+1]] = <-vm.Input
|
26
|
23
|
vm.ip += 2
|
27
|
24
|
}
|
28
|
25
|
|
29
|
26
|
// WriteOpCode writes the value specified by the first argument to the output stream.
|
30
|
|
-func WriteOpCode(vm *VirtualMachine, arg ArgFunc) {
|
31
|
|
- vm.Output <- arg(0)
|
|
27
|
+func WriteOpCode(vm *VirtualMachine) {
|
|
28
|
+ vm.Output <- vm.arg(0)
|
32
|
29
|
vm.ip += 2
|
33
|
30
|
}
|
34
|
31
|
|
35
|
32
|
// JumpIfTrueOpCode checks if the first argument is not zero, and if so jumps to the second argument.
|
36
|
|
-func JumpIfTrueOpCode(vm *VirtualMachine, arg ArgFunc) {
|
37
|
|
- if arg(0) != 0 {
|
38
|
|
- vm.ip = arg(1)
|
|
33
|
+func JumpIfTrueOpCode(vm *VirtualMachine) {
|
|
34
|
+ if vm.arg(0) != 0 {
|
|
35
|
+ vm.ip = vm.arg(1)
|
39
|
36
|
} else {
|
40
|
37
|
vm.ip += 3
|
41
|
38
|
}
|
42
|
39
|
}
|
43
|
40
|
|
44
|
41
|
// JumpIfFalseOpCode checks if the first argument is zero, and if so jumps to the second argument.
|
45
|
|
-func JumpIfFalseOpCode(vm *VirtualMachine, arg ArgFunc) {
|
46
|
|
- if arg(0) == 0 {
|
47
|
|
- vm.ip = arg(1)
|
|
42
|
+func JumpIfFalseOpCode(vm *VirtualMachine) {
|
|
43
|
+ if vm.arg(0) == 0 {
|
|
44
|
+ vm.ip = vm.arg(1)
|
48
|
45
|
} else {
|
49
|
46
|
vm.ip += 3
|
50
|
47
|
}
|
|
@@ -52,8 +49,8 @@ func JumpIfFalseOpCode(vm *VirtualMachine, arg ArgFunc) {
|
52
|
49
|
|
53
|
50
|
// LessThanOpCode checks if the first argument is less than the second, and stores the result at the address given
|
54
|
51
|
// by the third argument.
|
55
|
|
-func LessThanOpCode(vm *VirtualMachine, arg ArgFunc) {
|
56
|
|
- if arg(0) < arg(1) {
|
|
52
|
+func LessThanOpCode(vm *VirtualMachine) {
|
|
53
|
+ if vm.arg(0) < vm.arg(1) {
|
57
|
54
|
vm.Memory[vm.Memory[vm.ip+3]] = 1
|
58
|
55
|
} else {
|
59
|
56
|
vm.Memory[vm.Memory[vm.ip+3]] = 0
|
|
@@ -63,8 +60,8 @@ func LessThanOpCode(vm *VirtualMachine, arg ArgFunc) {
|
63
|
60
|
|
64
|
61
|
// EqualsOpCode checks if the first argument is equal to the second, and stores the result at the address given
|
65
|
62
|
// by the third argument.
|
66
|
|
-func EqualsOpCode(vm *VirtualMachine, arg ArgFunc) {
|
67
|
|
- if arg(0) == arg(1) {
|
|
63
|
+func EqualsOpCode(vm *VirtualMachine) {
|
|
64
|
+ if vm.arg(0) == vm.arg(1) {
|
68
|
65
|
vm.Memory[vm.Memory[vm.ip+3]] = 1
|
69
|
66
|
} else {
|
70
|
67
|
vm.Memory[vm.Memory[vm.ip+3]] = 0
|
|
@@ -73,6 +70,6 @@ func EqualsOpCode(vm *VirtualMachine, arg ArgFunc) {
|
73
|
70
|
}
|
74
|
71
|
|
75
|
72
|
// HaltOpcode halts the VM and takes no arguments.
|
76
|
|
-func HaltOpcode(vm *VirtualMachine, arg ArgFunc) {
|
|
73
|
+func HaltOpcode(vm *VirtualMachine) {
|
77
|
74
|
vm.Halted = true
|
78
|
75
|
}
|