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