Class: Urm::Coder
- Inherits:
-
Object
- Object
- Urm::Coder
- Defined in:
- lib/urm/coder.rb
Overview
The Coder class provides methods for encoding and decoding URM instructions and entire machines using Godel numbering. This class is useful for converting instructions and machines into unique numerical representations and back. The primary methods available in this class are:
-
self.code_single_instruction: Encodes a single URM instruction into a Godel number. -
self.code_machine: Encodes an entire URM machine into an array of Godel numbers. -
self.godel_code: Encodes an array of integers into a single Godel number. -
self.decode_single_instruction: Decodes a Godel number back into a single URM instruction. -
self.decode_machine: Decodes an array of Godel numbers back into a URM machine. -
self.extract_exponents: Extracts the exponents from a Godel number.
The class supports the following instruction types:
-
:set => [1, label, register, value]
-
:inc => [2, label, register]
-
:dec => [3, label, register]
-
:if => [4, label, register, label_true, label_false]
-
:stop => [5, label]
Class Method Summary collapse
- .code_machine(machine) ⇒ Object
- .code_single_instruction(instruction) ⇒ Object
- .decode_dec(label, exponents) ⇒ Object
- .decode_if(label, exponents) ⇒ Object
- .decode_inc(label, exponents) ⇒ Object
- .decode_machine(godel_numbers) ⇒ Object
- .decode_set(label, exponents) ⇒ Object
- .decode_single_instruction(godel_number) ⇒ Object
- .decode_stop(label) ⇒ Object
- .extract_exponents(godel_number) ⇒ Object
- .godel_code(numbers) ⇒ Object
Class Method Details
.code_machine(machine) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/urm/coder.rb', line 44 def self.code_machine(machine) machine.validate_instructions machine.instructions.compact.map do |instruction| code_single_instruction(instruction) end end |
.code_single_instruction(instruction) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/urm/coder.rb', line 26 def self.code_single_instruction(instruction) code = case instruction.type when :set [1, instruction.label, instruction.register, instruction.value] when :inc [2, instruction.label, instruction.register] when :dec [3, instruction.label, instruction.register] when :if [4, instruction.label, instruction.register, instruction.label_true, instruction.label_false] when :stop [5, instruction.label] else raise "Unknown instruction type: #{instruction.type}" end godel_code(code) end |
.decode_dec(label, exponents) ⇒ Object
119 120 121 |
# File 'lib/urm/coder.rb', line 119 def self.decode_dec(label, exponents) Instruction.dec(label, exponents[2]) end |
.decode_if(label, exponents) ⇒ Object
123 124 125 |
# File 'lib/urm/coder.rb', line 123 def self.decode_if(label, exponents) Instruction.if(label, exponents[2], exponents[3], exponents[4]) end |
.decode_inc(label, exponents) ⇒ Object
115 116 117 |
# File 'lib/urm/coder.rb', line 115 def self.decode_inc(label, exponents) Instruction.inc(label, exponents[2]) end |
.decode_machine(godel_numbers) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/urm/coder.rb', line 84 def self.decode_machine(godel_numbers) instructions = godel_numbers.map { |number| decode_single_instruction(number) } machine = Machine.new(0) # Создаем машину с 0 входными регистрами instructions.each { |instruction| machine.add(instruction) } machine end |
.decode_set(label, exponents) ⇒ Object
111 112 113 |
# File 'lib/urm/coder.rb', line 111 def self.decode_set(label, exponents) Instruction.set(label, exponents[2], exponents[3]) end |
.decode_single_instruction(godel_number) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/urm/coder.rb', line 62 def self.decode_single_instruction(godel_number) exponents = extract_exponents(godel_number) type = exponents[0] label = exponents[1] case type when 1 decode_set(label, exponents) when 2 decode_inc(label, exponents) when 3 decode_dec(label, exponents) when 4 decode_if(label, exponents) when 5 decode_stop(label) else raise "Unknown instruction type: #{type}" end end |
.decode_stop(label) ⇒ Object
127 128 129 |
# File 'lib/urm/coder.rb', line 127 def self.decode_stop(label) Instruction.stop(label) end |
.extract_exponents(godel_number) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/urm/coder.rb', line 91 def self.extract_exponents(godel_number) exponents = [] prime_enum = Prime.each loop do prime = prime_enum.next exponent = 0 while (godel_number % prime).zero? godel_number /= prime exponent += 1 end exponents << (exponent - 1) break if godel_number == 1 end exponents end |
.godel_code(numbers) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/urm/coder.rb', line 51 def self.godel_code(numbers) primes = Prime.first(numbers.size) godel_number = 1 numbers.each_with_index do |num, index| godel_number *= primes[index]**(num + 1) end godel_number end |