refactor a bit for upcoming delegations

must get method to be private
more tell, not ask
This commit is contained in:
Torsten Ruger 2018-07-09 18:16:51 +03:00
parent 06e78a7326
commit 63b55f2aa4
3 changed files with 27 additions and 2 deletions

View File

@ -171,7 +171,11 @@ module Risc
end
end
# The first arg is a class name (possibly lowercase) and the second an instance variable name.
# resolve a symbol to a type. In the simplest case the sybbol is the class name
# But in building sometimes variations are needed, so next_message or caller work
# too (and return Message)
# Also objects work, in which case the instance_type of their class is returned
# An error is raised if the symbol/object can not be resolved
def self.resolve_type( object , compiler )
object = object.type if object.is_a?(RegisterValue)
case object
@ -205,7 +209,8 @@ module Risc
# Third arg, compiler, is only needed to resolve receiver/arguments/frame
def self.resolve_to_index(object , variable_name ,compiler = nil)
return variable_name if variable_name.is_a?(Integer) or variable_name.is_a?(RegisterValue)
type = resolve_type(object , compiler)
type = compiler.resolve_type( object) if compiler
type = resolve_type(object , compiler) unless type
index = type.variable_index(variable_name)
raise "Index not found for #{variable_name} in #{object} of type #{type}" unless index
return index

View File

@ -69,6 +69,21 @@ module Risc
ret
end
# resolve a symbol to a type. Allowed symbols are :frame , :receiver and arguments
# which return the respective types, otherwise nil
def resolve_type( name )
case name
when :frame
return @method.frame_type
when :arguments
return @method.arguments_type
when :receiver
return @method.self_type
else
return nil
end
end
# convert the given mom instruction to_risc and then add it (see add_code)
# continue down the instruction chain unti depleted
# (adding moves the insertion point so the whole mom chain is added as a risc chain)

View File

@ -2,6 +2,11 @@ require_relative '../helper'
module Mom
class CompilerMock
# resolve a symbol to a type. Allowed symbols are :frame , :receiver and arguments
# which return the respective types, otherwise nil
def resolve_type( name )
return nil
end
def use_reg( type )
Risc.tmp_reg(type , nil)
end