Решение на Четвърта задача от Петър Мазълов

Обратно към всички решения

Към профила на Петър Мазълов

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 4 успешни тест(а)
  • 4 неуспешни тест(а)

Код

module Asm
def Asm.asm(&block)
assembler = Evaluator.new
assembler.instance_eval &block
[assembler.ax.x , assembler.bx.x, assembler.cx.x, assembler.dx.x]
end
class Register
attr_accessor :x
def initialize
@x = 0
end
end
class Evaluator
attr_accessor :ax, :bx, :cx, :dx, :compare, :operations
def initialize
@ax = Register.new
@bx = Register.new
@cx = Register.new
@dx = Register.new
@compare = Register.new
@operations = []
end
def mov(destination_register, source)
if source.is_a? Register
destination_register.x = source.x
else
destination_register.x = source
end
@operations << [:mov, destination_register, source]
end
def inc(destination_register, value = 1)
if value.is_a? Register
destination_register.x += value.x
else
destination_register.x += value
end
@operations << [:inc, destination_register, value]
end
def dec(destination_register, value = 1)
if value.is_a? Register
destination_register.x -= value.x
else
destination_register.x -= value
end
@operations << [:dec, destination_register, value]
end
def cmp(register, value)
if value.is_a? Register
compare.x = (register.x <=> value.x)
else
compare.x = (register.x <=> value)
end
@operations << [:cmp, register, value]
end
end
end

Лог от изпълнението

....FFFF

Failures:

  1) Asm.asm implements CMP
     Failure/Error: je 5
     NoMethodError:
       undefined method `je' for #<Asm::Evaluator:0xb8552ec8>
     # /tmp/d20140115-8451-1jg7jig/spec.rb:38:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-1jg7jig/solution.rb:5:in `instance_eval'
     # /tmp/d20140115-8451-1jg7jig/solution.rb:5:in `asm'
     # /tmp/d20140115-8451-1jg7jig/spec.rb:34:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Asm.asm implements JMP
     Failure/Error: jmp l1
     NameError:
       undefined local variable or method `l1' for #<Asm::Evaluator:0xb8551460>
     # /tmp/d20140115-8451-1jg7jig/spec.rb:70:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-1jg7jig/solution.rb:5:in `instance_eval'
     # /tmp/d20140115-8451-1jg7jig/solution.rb:5:in `asm'
     # /tmp/d20140115-8451-1jg7jig/spec.rb:68:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Asm.asm implements LABEL
     Failure/Error: je l1
     NameError:
       undefined local variable or method `l1' for #<Asm::Evaluator:0xb854f73c>
     # /tmp/d20140115-8451-1jg7jig/spec.rb:81:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-1jg7jig/solution.rb:5:in `instance_eval'
     # /tmp/d20140115-8451-1jg7jig/solution.rb:5:in `asm'
     # /tmp/d20140115-8451-1jg7jig/spec.rb:78:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Asm.asm can be used to find GCD of two numbers
     Failure/Error: label cycle
     NameError:
       undefined local variable or method `cycle' for #<Asm::Evaluator:0xb854dbbc>
     # /tmp/d20140115-8451-1jg7jig/spec.rb:92:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-1jg7jig/solution.rb:5:in `instance_eval'
     # /tmp/d20140115-8451-1jg7jig/solution.rb:5:in `asm'
     # /tmp/d20140115-8451-1jg7jig/spec.rb:89:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.00689 seconds
8 examples, 4 failures

Failed examples:

rspec /tmp/d20140115-8451-1jg7jig/spec.rb:33 # Asm.asm implements CMP
rspec /tmp/d20140115-8451-1jg7jig/spec.rb:67 # Asm.asm implements JMP
rspec /tmp/d20140115-8451-1jg7jig/spec.rb:77 # Asm.asm implements LABEL
rspec /tmp/d20140115-8451-1jg7jig/spec.rb:88 # Asm.asm can be used to find GCD of two numbers

История (1 версия и 1 коментар)

Петър обнови решението на 14.01.2014 19:12 (преди почти 11 години)

+module Asm
+
+ def Asm.asm(&block)
+ assembler = Evaluator.new
+ assembler.instance_eval &block
+ [assembler.ax.x , assembler.bx.x, assembler.cx.x, assembler.dx.x]
+ end
+
+ class Register
+ attr_accessor :x
+ def initialize
+ @x = 0
+ end
+ end
+
+ class Evaluator
+
+ attr_accessor :ax, :bx, :cx, :dx, :compare, :operations
+ def initialize
+ @ax = Register.new
+ @bx = Register.new
+ @cx = Register.new
+ @dx = Register.new
+ @compare = Register.new
+ @operations = []
+ end
+
+ def mov(destination_register, source)
+ if source.is_a? Register
+ destination_register.x = source.x
+ else
+ destination_register.x = source
+ end
+ @operations << [:mov, destination_register, source]
+ end
+
+ def inc(destination_register, value = 1)
+ if value.is_a? Register
+ destination_register.x += value.x
+ else
+ destination_register.x += value
+ end
+ @operations << [:inc, destination_register, value]
+ end
+
+ def dec(destination_register, value = 1)
+ if value.is_a? Register
+ destination_register.x -= value.x
+ else
+ destination_register.x -= value
+ end
+ @operations << [:dec, destination_register, value]
+ end
+
+ def cmp(register, value)
+ if value.is_a? Register
+ compare.x = (register.x <=> value.x)
+ else
+ compare.x = (register.x <=> value)
+ end
+ @operations << [:cmp, register, value]
+ end
+ end
+end

Добро начало.

  • Добре е, че има клас Register, но защо атрибута му се казва x, а не value, или content, например?
  • Повторението на if-ове в методите може да е проблемно по-нататък. Можеш да опиташ да ги абстрахираш в помощен метод, който взима регистър, стойност и операция и прави каквото трябва. Така това ще става само на едно място и кодът ще спазва DRY принципа.