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

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

Към профила на Методи Димитров

Резултати

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

Код

class Object
def ax()
'ax'
end
def bx()
'bx'
end
def cx()
'cx'
end
def dx()
'dx'
end
end
module Jumps
def jmp(label)
if(labels_lookup_table[label] == nil)
@@operations_log.each(&:eval)
else
@@operations_log.each(&:eval)
end
end
def je(label)
end
def jne(label)
end
def jl(label)
end
def jle(label)
end
def jg(label)
end
def jge(label)
end
end
module Asm
include Jumps
def Asm.asm(&block)
@@registers = {'ax' => 0, 'bx' => 0, 'cx' => 0, 'dx' => 0}
@@flag = 0
@@operations_log = []
@@labels_lookup_table = {}
block.call
@@registers.values
end
def mov(destination_register, source)
if (@@registers[source] == nil)
@@registers[destination_register] = source
else
@@registers[destination_register] = @@registers[source]
end
@@operations_log << "mov #{destination_register}, #{source}"
end
def inc(destination_register, source = 1)
if (@@registers[source] == nil)
@@registers[destination_register] += source
else
@@registers[destination_register] += @@registers[source]
end
@@operations_log << "inc #{destination_register}, #{source}"
end
def dec(destination_register, source = 1)
if (@@registers[source] == nil)
@@registers[destination_register] -= source
else
@@registers[destination_register] -= @@registers[source]
end
@@operations_log << "dec #{destination_register}, #{source}"
end
def cmp(destination_register, source)
if (@@registers[source] == nil)
@@flag = (@@registers[destination_register] <=> source)
else
@@flag = (@@registers[destination_register] <=> @@registers[source])
end
@@operations_log << "cmp #{destination_register}, #{source}"
end
def label(label)
@@labels_lookup_table[label] = @@operations_log.size
end
end
include Asm

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

....FFFF

Failures:

  1) Asm.asm implements CMP
     Failure/Error: Asm.asm do
       
       expected: [2, 2, 0, 3]
            got: [2, 2, 3, 3]
       
       (compared using ==)
     # /tmp/d20140115-8451-h17vft/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 #<RSpec::Core::ExampleGroup::Nested_1:0xb863217c>
     # /tmp/d20140115-8451-h17vft/spec.rb:70:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-h17vft/solution.rb:58:in `call'
     # /tmp/d20140115-8451-h17vft/solution.rb:58:in `asm'
     # /tmp/d20140115-8451-h17vft/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 #<RSpec::Core::ExampleGroup::Nested_1:0xb863055c>
     # /tmp/d20140115-8451-h17vft/spec.rb:81:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-h17vft/solution.rb:58:in `call'
     # /tmp/d20140115-8451-h17vft/solution.rb:58:in `asm'
     # /tmp/d20140115-8451-h17vft/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 #<RSpec::Core::ExampleGroup::Nested_1:0xb862ea18>
     # /tmp/d20140115-8451-h17vft/spec.rb:92:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-h17vft/solution.rb:58:in `call'
     # /tmp/d20140115-8451-h17vft/solution.rb:58:in `asm'
     # /tmp/d20140115-8451-h17vft/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.00919 seconds
8 examples, 4 failures

Failed examples:

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

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

Методи обнови решението на 15.01.2014 14:52 (преди почти 11 години)

+class Object
+
+ def ax()
+ 'ax'
+ end
+
+ def bx()
+ 'bx'
+ end
+
+ def cx()
+ 'cx'
+ end
+
+ def dx()
+ 'dx'
+ end
+
+end
+module Jumps
+
+ def jmp(label)
+ if(labels_lookup_table[label] == nil)
+ @@operations_log.each(&:eval)
+ else
+ @@operations_log.each(&:eval)
+ end
+ end
+
+ def je(label)
+ end
+
+ def jne(label)
+ end
+
+ def jl(label)
+ end
+
+ def jle(label)
+ end
+
+ def jg(label)
+ end
+
+ def jge(label)
+ end
+
+end
+module Asm
+
+ include Jumps
+
+ def Asm.asm(&block)
+ @@registers = {'ax' => 0, 'bx' => 0, 'cx' => 0, 'dx' => 0}
+ @@flag = 0
+ @@operations_log = []
+ @@labels_lookup_table = {}
+ block.call
+ @@registers.values
+ end
+
+ def mov(destination_register, source)
+ if (@@registers[source] == nil)
+ @@registers[destination_register] = source
+ else
+ @@registers[destination_register] = @@registers[source]
+ end
+ @@operations_log << "mov #{destination_register}, #{source}"
+ end
+
+ def inc(destination_register, source = 1)
+ if (@@registers[source] == nil)
+ @@registers[destination_register] += source
+ else
+ @@registers[destination_register] += @@registers[source]
+ end
+ @@operations_log << "inc #{destination_register}, #{source}"
+ end
+
+ def dec(destination_register, source = 1)
+ if (@@registers[source] == nil)
+ @@registers[destination_register] -= source
+ else
+ @@registers[destination_register] -= @@registers[source]
+ end
+ @@operations_log << "dec #{destination_register}, #{source}"
+ end
+
+ def cmp(destination_register, source)
+ if (@@registers[source] == nil)
+ @@flag = (@@registers[destination_register] <=> source)
+ else
+ @@flag = (@@registers[destination_register] <=> @@registers[source])
+ end
+ @@operations_log << "cmp #{destination_register}, #{source}"
+ end
+
+ def label(label)
+ @@labels_lookup_table[label] = @@operations_log.size
+ end
+
+end
+
+include Asm

Използвал си клас променливи, което е лошо. Казвали сме го безброй пъти на лекции, включително и на последната сбирка. Това ще ти струва наказателни точки.

Наказателни точки ще има и за този include Asm в края на програмата, защото това е една от анти-практиките, които показахме на лекции, свързана с решението на последното предизвикателство. Това го има и в слайдовете, от което следва, че не си прочел тези слайдове. Това също ще ти струва наказателни точки.

Освен тези неща, имаш ред други възможнисти за подобрения в кода и дизайна. Тъй като срокът на задачата току-що изтече, може да разгледаш решенията на колегите си, както и нашето (в GitHub е).