change in register_names protocol

move to returning the attribute names
getting and setting can then be automated in the base class
This commit is contained in:
2020-03-20 13:58:40 +02:00
parent 0137056b89
commit c890e8402b
15 changed files with 72 additions and 24 deletions

View File

@ -12,7 +12,7 @@ module Risc
attr_reader :label
# return an array of names of registers that is used by the instruction
def register_names
def register_attributes
[]
end

View File

@ -13,8 +13,8 @@ module Risc
attr_reader :register
# return an array of names of registers that is used by the instruction
def register_names
[register.symbol ]
def register_attributes
[:register ]
end
def to_s

View File

@ -11,7 +11,7 @@ module Risc
attr_reader :method
# return an array of names of registers that is used by the instruction
def register_names
def register_attributes
[]
end

View File

@ -12,8 +12,8 @@ module Risc
attr_reader :register
# return an array of names of registers that is used by the instruction
def register_names
[register.symbol]
def register_attributes
[:register]
end
def to_s

View File

@ -30,9 +30,9 @@ module Risc
attr_accessor :array , :index , :register
# return an array of names of registers that is used by the instruction
def register_names
names = [array.symbol , register.symbol]
names << index.symbol if index.is_a?(RegisterValue)
def register_attributes
names = [:array , :register]
names << :index if index.is_a?(RegisterValue)
names
end

View File

@ -42,10 +42,33 @@ module Risc
end
# return an array of names of registers that is used by the instruction
def register_names
def register_attributes
raise "Not implemented in #{self.class}"
end
# return an array of names of registers that is used by the instruction
def register_names
register_attributes.collect {|attr| get_register(attr)}
end
# get the register value that has the name (ie attribute by that name)
# return the registers name ie RegisterValue's symbol
def get_register(name)
instance_variable_get("@#{name}".to_sym).symbol
end
# set all registers that has the name "name"
# going through the register_names and setting all where the
# get_register would return name
# Set to the value given as second arg.
def set_registers(name , value)
register_attributes.each do |attr|
reg = instance_variable_get("@#{attr}".to_sym)
next unless reg.symbol == name
reg.set_name(value)
end
end
def to_cpu( translator )
translator.translate( self )
end

View File

@ -24,7 +24,7 @@ module Risc
attr_reader :name , :address
# return an array of names of registers that is used by the instruction
def register_names
def register_attributes
[]
end

View File

@ -15,8 +15,8 @@ module Risc
attr_accessor :register , :constant
# return an array of names of registers that is used by the instruction
def register_names
[register.symbol]
def register_attributes
[:register]
end
def to_s

View File

@ -18,8 +18,8 @@ module Risc
attr_accessor :register , :constant
# return an array of names of registers that is used by the instruction
def register_names
[register.symbol]
def register_attributes
[:register]
end
def to_s

View File

@ -33,8 +33,8 @@ module Risc
attr_reader :operator, :left , :right , :result
# return an array of names of registers that is used by the instruction
def register_names
[left.symbol , right.symbol, result.symbol]
def register_attributes
[:left , :right, :result]
end
def to_s

View File

@ -28,9 +28,9 @@ module Risc
attr_accessor :register , :array , :index
# return an array of names of registers that is used by the instruction
def register_names
names = [array.symbol , register.symbol]
names << index.symbol if index.is_a?(RegisterValue)
def register_attributes
names = [:array , :register]
names << :index if index.is_a?(RegisterValue)
names
end

View File

@ -17,7 +17,7 @@ module Risc
attr_reader :name
# return an array of names of registers that is used by the instruction
def register_names
def register_attributes
[]
end

View File

@ -26,8 +26,8 @@ module Risc
attr_reader :from, :to
# return an array of names of registers that is used by the instruction
def register_names
[from.symbol , to.symbol]
def register_attributes
[:from , :to]
end
def to_s