Browse files

Counting instructions

  • Loading branch information...
1 parent afdc108 commit 177e51abe3c946215b3a91bfcd41b0bc184668cb @jstepien committed Oct 15, 2010
Showing with 10 additions and 1 deletion.
  1. +1 −0 ruram.rb
  2. +9 −1 vm.rb
View
1 ruram.rb
@@ -9,3 +9,4 @@
vm = VM.new instructions
vm.execute
pp vm.registers
+pp vm.instructions_count
View
10 vm.rb
@@ -2,11 +2,12 @@
class VM
KNOWN_INSTRUCTIONS = [:clr, :jmp, :cjmp, :mov, :clr, :del, :cont, :add]
- attr_reader :registers
+ attr_reader :registers, :instructions_count
def initialize(instructions)
@labels = {}
@registers = Registers.new
+ @instructions_count = Counter.new
@instructions = instructions
@current_instr = 0
instructions.each do |instr|
@@ -27,6 +28,7 @@ def step
if unknown_instruction?
Log.error "Unexpected instruction code: #{current_instruction.code}"
end
+ @instructions_count[current_instruction.code] += 1
send current_instruction.code, current_instruction.args
end
@@ -75,3 +77,9 @@ def [](x)
self[x] = []
end
end
+
+class Counter < Hash
+ def [](x)
+ super(x) or self[x] = 0
+ end
+end

0 comments on commit 177e51a

Please sign in to comment.