first steps to defining specialised slot classes

getting rid of the mess in SlotDefinition (wip)
This commit is contained in:
2020-02-10 18:12:39 +07:00
parent df4fd409c1
commit 24d7fe25da
35 changed files with 104 additions and 62 deletions

View File

@ -18,7 +18,7 @@ module SlotLanguage
end
def slot_def(compiler)
SlotMachine::SlotDefinition.new(:message , leaps)
SlotMachine::SlotDefinition.for(:message , leaps)
end
def to_s

View File

@ -0,0 +1,29 @@
module SlotMachine
class MessageDefinition < SlotDefinition
def initialize(slots)
super(:message , slots)
end
def known_name
known_object
end
# load the slots into a register
# the code is added to compiler
# the register returned
def to_register(compiler, source)
type = :Message
right = compiler.use_reg( type )
slots = @slots.dup
left = Risc.message_reg
left = left.resolve_and_add( slots.shift , compiler)
reg = compiler.current.register
while( !slots.empty? )
left = left.resolve_and_add( slots.shift , compiler)
end
return reg
end
end
end

View File

@ -1,7 +1,7 @@
module SlotMachine
# A SlotDefinition defines a slot. A bit like a variable name but for objects.
#
# PS: for the interested: A "developement" of Smalltalk was the
# PS: for the interested: A "development" of Smalltalk was the
# prototype based language (read: JavaScript equivalent)
# called Self https://en.wikipedia.org/wiki/Self_(programming_language)
#
@ -15,6 +15,16 @@ module SlotMachine
# Instructions. Or in the case of constants to ConstantLoad
#
class SlotDefinition
# get the right definition, depending on the object
def self.for(object , slots)
case object
when :message
MessageDefinition.new(slots)
else
SlotDefinition.new(object,slots)
end
end
attr_reader :known_object , :slots
# is an array of symbols, that specifies the first the object, and then the Slot.
# The first element is either a known type name (Capitalized symbol of the class name) ,
@ -39,8 +49,6 @@ module SlotMachine
known_object.class.short_name
when Risc::Label
known_object.to_s
when Symbol
known_object
else
"unknown"
end
@ -69,14 +77,12 @@ module SlotMachine
end
raise "Can't have slots into Constants #{slots}" if slots.length > 1
when Parfait::Object , Risc::Label
const = const = Risc.load_constant(source, known_object , right)
const = Risc.load_constant(source, known_object , right)
compiler.add_code const
if slots.length > 0
# desctructively replace the existing value to be loaded if more slots
compiler.add_code Risc.slot_to_reg( source , right ,slots[0], right)
end
when Symbol
return sym_to_risc(compiler , source)
else
raise "We have a #{self} #{known_object}"
end
@ -92,19 +98,5 @@ module SlotMachine
return const.register
end
# resolve the slots one by one to slot_to_reg instructions using the
# type information inferred from their names / type hierachy
def sym_to_risc(compiler , source)
slots = @slots.dup
raise "Not Message #{@known_object}" unless @known_object == :message
left = Risc.message_reg
left = left.resolve_and_add( slots.shift , compiler)
reg = compiler.current.register
while( !slots.empty? )
left = left.resolve_and_add( slots.shift , compiler)
end
return reg
end
end
end

View File

@ -31,8 +31,8 @@ module SlotMachine
def initialize(source , left , right, original_source = nil)
super(source)
@left , @right = left , right
@left = SlotDefinition.new(@left.shift , @left) if @left.is_a? Array
@right = SlotDefinition.new(@right.shift , @right) if @right.is_a? Array
@left = SlotDefinition.for(@left.shift , @left) if @left.is_a? Array
@right = SlotDefinition.for(@right.shift , @right) if @right.is_a? Array
raise "right not SlotMachine, #{@right.to_s}" unless @right.is_a?( SlotDefinition )
@original_source = original_source || self
end
@ -81,3 +81,4 @@ module SlotMachine
end
require_relative "slot_definition"
require_relative "message_definition"

View File

@ -32,7 +32,7 @@ module Sol
#
# Derived classes do not implement to_slot, only slot_position
def to_slot(compiler)
to = SlotMachine::SlotDefinition.new(:message , self.slot_position(compiler))
to = SlotMachine::SlotDefinition.for(:message , self.slot_position(compiler))
from = @value.to_slot_definition(compiler)
assign = SlotMachine::SlotLoad.new(self,to,from)
return assign unless @value.is_a?(CallStatement)

View File

@ -10,7 +10,7 @@ module Sol
@value = value
end
def to_slot_definition(_)
return SlotMachine::SlotDefinition.new(SlotMachine::IntegerConstant.new(@value) , [])
return SlotMachine::SlotDefinition.for(SlotMachine::IntegerConstant.new(@value) , [])
end
def ct_type
Parfait.object_space.get_type_by_class_name(:Integer)
@ -38,7 +38,7 @@ module Sol
Parfait.object_space.get_type_by_class_name(:True)
end
def to_slot_definition(_)
return SlotMachine::SlotDefinition.new(Parfait.object_space.true_object , [])
return SlotMachine::SlotDefinition.for(Parfait.object_space.true_object , [])
end
def to_s(depth = 0)
"true"
@ -50,7 +50,7 @@ module Sol
Parfait.object_space.get_type_by_class_name(:False)
end
def to_slot_definition(_)
return SlotMachine::SlotDefinition.new(Parfait.object_space.false_object , [])
return SlotMachine::SlotDefinition.for(Parfait.object_space.false_object , [])
end
def to_s(depth = 0)
"false"
@ -62,7 +62,7 @@ module Sol
Parfait.object_space.get_type_by_class_name(:Nil)
end
def to_slot_definition(_)
return SlotMachine::SlotDefinition.new(Parfait.object_space.nil_object , [])
return SlotMachine::SlotDefinition.for(Parfait.object_space.nil_object , [])
end
def to_s(depth = 0)
"nil"
@ -77,7 +77,7 @@ module Sol
end
def to_slot_definition(compiler)
@my_type = compiler.receiver_type
SlotMachine::SlotDefinition.new(:message , [:receiver])
SlotMachine::SlotDefinition.for(:message , [:receiver])
end
def ct_type
@my_type
@ -92,7 +92,7 @@ module Sol
@value = value
end
def to_slot_definition(_)
return SlotMachine::SlotDefinition.new(SlotMachine::StringConstant.new(@value),[])
return SlotMachine::SlotDefinition.for(SlotMachine::StringConstant.new(@value),[])
end
def ct_type
Parfait.object_space.get_type_by_class_name(:Word)

View File

@ -11,7 +11,7 @@ module Sol
# When used as right hand side, this tells what data to move to get the result into
# a varaible. It is (off course) the return value of the message
def to_slot_definition(_)
SlotMachine::SlotDefinition.new(:message ,[ :return_value])
SlotMachine::SlotDefinition.for(:message ,[ :return_value])
end
def to_s(depth = 0)

View File

@ -15,7 +15,7 @@ module Sol
# fact never called)
def to_slot_definition(compiler)
compile(compiler) unless @parfait_block
return SlotMachine::SlotDefinition.new(SlotMachine::LambdaConstant.new(parfait_block(compiler)) , [])
return SlotMachine::SlotDefinition.for(SlotMachine::LambdaConstant.new(parfait_block(compiler)) , [])
end
# create a block, a compiler for it, compile the block and add the compiler(code)

View File

@ -15,7 +15,7 @@ module Sol
# When used as right hand side, this tells what data to move to get the result into
# a varaible. It is (off course) the return value of the message
def to_slot_definition(_)
SlotMachine::SlotDefinition.new(:message ,[ :return_value])
SlotMachine::SlotDefinition.for(:message ,[ :return_value])
end
def to_s(depth = 0)

View File

@ -116,7 +116,7 @@ module Sol
defi
end
def build_condition(ok_label, compiler)
cached_type = SlotMachine::SlotDefinition.new(dynamic_call.cache_entry , [:cached_type])
cached_type = SlotMachine::SlotDefinition.for(dynamic_call.cache_entry , [:cached_type])
current_type = receiver_type_definition(compiler)
SlotMachine::NotSameCheck.new(cached_type , current_type, ok_label)
end

View File

@ -12,7 +12,7 @@ module Sol
include Named
def to_slot_definition(compiler)
slot_def = compiler.slot_type_for(@name)
SlotMachine::SlotDefinition.new(:message , slot_def)
SlotMachine::SlotDefinition.for(:message , slot_def)
end
def to_s(depth = 0)
name.to_s
@ -25,7 +25,7 @@ module Sol
class InstanceVariable < Expression
include Named
def to_slot_definition(_)
SlotMachine::SlotDefinition.new(:message , [ :receiver , @name] )
SlotMachine::SlotDefinition.for(:message , [ :receiver , @name] )
end
# used to collect type information
def add_ivar( array )
@ -52,7 +52,7 @@ module Sol
get_named_class.single_class.instance_type
end
def to_slot_definition(_)
return SlotMachine::SlotDefinition.new( get_named_class, [])
return SlotMachine::SlotDefinition.for( get_named_class, [])
end
def get_named_class
Parfait.object_space.get_class_by_name(self.name)

View File

@ -34,8 +34,8 @@ module Sol
# we brace ourselves with the check, and exit (later raise) if . . .
def method_check(compiler)
ok_label = SlotMachine::Label.new(self,"method_ok_#{self.object_id}")
compile_method = SlotMachine::SlotDefinition.new( compiler.get_method , [])
runtime_method = SlotMachine::SlotDefinition.new( :message , [ :method] )
compile_method = SlotMachine::SlotDefinition.for( compiler.get_method , [])
runtime_method = SlotMachine::SlotDefinition.for( :message , [ :method] )
check = SlotMachine::NotSameCheck.new(compile_method , runtime_method, ok_label)
# TODO? Maybe create slot instructions for this
#builder = compiler.builder("yield")