seperate different Position classes into own files

also tests
and have Position module keep all positions
(singletons should be at module, not class level)
This commit is contained in:
Torsten Ruger
2018-05-10 20:56:12 +03:00
parent 1169fa7220
commit bc1e29e4f6
9 changed files with 192 additions and 145 deletions

View File

@ -0,0 +1,22 @@
module Risc
module Position
class CodePosition < ObjectPosition
attr_reader :code , :method
def initialize(code, pos , method)
super(pos)
@code = code
@method = method
end
def init(at)
return unless code.next
Position.set(code.next , at + code.padded_length, method)
end
def reset_to(pos)
super(pos)
#puts "Reset (#{changed}) #{instruction}"
init(pos)
end
end
end
end

View File

@ -0,0 +1,31 @@
module Risc
module Position
class InstructionPosition < ObjectPosition
attr_reader :instruction , :binary
def initialize(instruction, pos , binary)
raise "not set " unless binary
super(pos)
@instruction = instruction
@binary = binary
end
def init(at)
return unless instruction.next
at += instruction.byte_length
bin = binary
if( 12 == at % 60)
at = 12
bin = binary.next
end
Position.set(instruction.next, at , binary)
end
def reset_to(pos)
super(pos)
#puts "Reset (#{changed}) #{instruction}"
init(pos)
end
end
end
end

View File

@ -0,0 +1,36 @@
module Risc
module Position
class ObjectPosition
attr_reader :at
def initialize( at )
@at = at
raise "not int #{self}-#{at}" unless @at.is_a?(Integer)
end
def +(offset)
offset = offset.at if offset.is_a?(ObjectPosition)
@at + offset
end
def -(offset)
offset = offset.at if offset.is_a?(ObjectPosition)
@at - offset
end
def to_s
"0x#{@at.to_s(16)}"
end
# just a callback after creation AND insertion
def init(pos)
end
def reset_to(pos)
return false if pos == at
if((at - pos).abs > 1000)
raise "position set too far off #{pos}!=#{at} for #{object}:#{object.class}"
end
@at = pos
true
end
end
end
end