Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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