use factory to generte intergers in space

start with just integer factory in space
change all the hand-out code
still #14
This commit is contained in:
Torsten Ruger 2018-08-24 18:49:21 +03:00
parent d396da16e3
commit 71ab369c71
16 changed files with 59 additions and 61 deletions

View File

@ -21,7 +21,7 @@ module Mom
@value = value @value = value
end end
def to_parfait(compiler) def to_parfait(compiler)
value = Parfait.object_space.get_integer value = Parfait.object_space.get_factory_for(:Integer).get_next_object
value.set_value(@value) value.set_value(@value)
compiler.add_constant(value) compiler.add_constant(value)
value value

View File

@ -68,11 +68,6 @@ module Parfait
type type
end end
# return the metaclass
def meta
MetaClass.new self
end
def get_instance_variables def get_instance_variables
type.names type.names
end end

View File

@ -31,8 +31,9 @@ module Parfait
class Space < Object class Space < Object
attr :type, :classes , :types , :next_message , :next_integer , :next_address attr :type, :classes , :types , :factories
attr :messages, :integers , :addresses attr :next_message , :next_address
attr :messages, :addresses
attr :true_object , :false_object , :nil_object attr :true_object , :false_object , :nil_object
def initialize( classes ) def initialize( classes )
@ -41,7 +42,8 @@ module Parfait
classes.each do |name , cl| classes.each do |name , cl|
add_type(cl.instance_type) add_type(cl.instance_type)
end end
101.times { self.integers = Integer.new(0,self.integers) } self.factories = Dictionary.new
factories[ :Integer ] = Factory.new( classes[:Integer].instance_type)
400.times { self.addresses = ReturnAddress.new(0,self.addresses) } 400.times { self.addresses = ReturnAddress.new(0,self.addresses) }
message = Message.new(nil) message = Message.new(nil)
50.times do 50.times do
@ -50,7 +52,6 @@ module Parfait
message = self.messages message = self.messages
end end
self.next_message = self.messages self.next_message = self.messages
self.next_integer = self.integers
self.next_address = self.addresses self.next_address = self.addresses
self.true_object = Parfait::TrueClass.new self.true_object = Parfait::TrueClass.new
self.false_object = Parfait::FalseClass.new self.false_object = Parfait::FalseClass.new
@ -64,14 +65,16 @@ module Parfait
16 16
end end
# hand out one of the preallocated ints for use as constant # return the factory for the given type
# the same code is hardcoded as risc instructions for "normal" use, to # or more exactly the type that has a class_name "name"
# avoid the method call at runtime. But at compile time we want to keep def get_factory_for(name)
# the number of integers known (fixed). factories[name]
def get_integer end
int = self.next_integer
self.next_integer = next_integer.next_integer # use the factory of given name to generate next_object
int # just a shortcut basically
def get_next_for(name)
factories[name].get_next_object
end end
# hand out a return address for use as constant the address is added # hand out a return address for use as constant the address is added
@ -81,12 +84,14 @@ module Parfait
addr addr
end end
# yield each type in the space
def each_type def each_type
types.values.each do |type| types.values.each do |type|
yield(type) yield(type)
end end
end end
# add a type, meaning the instance given must be a valid type
def add_type( type ) def add_type( type )
hash = type.hash hash = type.hash
raise "upps #{hash} #{hash.class}" unless hash.is_a?(Fixnum) raise "upps #{hash} #{hash.class}" unless hash.is_a?(Fixnum)
@ -95,6 +100,7 @@ module Parfait
types[hash] = type types[hash] = type
end end
# get a type by the type hash (the hash is what uniquely identifies the type)
def get_type_for( hash ) def get_type_for( hash )
types[hash] types[hash]
end end
@ -110,11 +116,13 @@ module Parfait
methods methods
end end
# shortcut to get the main method. main is defined on Space
def get_main def get_main
space = get_class_by_name :Space space = get_class_by_name :Space
space.instance_type.get_method :main space.instance_type.get_method :main
end end
# shortcut to get the __init__ method, which is defined on Object
def get_init def get_init
object = get_class_by_name :Object object = get_class_by_name :Object
object.instance_type.get_method :__init__ object.instance_type.get_method :__init__

View File

@ -131,10 +131,10 @@ module Risc
to.set_builder( self ) # esecially div10 comes in without having used builder to.set_builder( self ) # esecially div10 comes in without having used builder
from.set_builder( self ) # not named regs, different regs ==> silent errors from.set_builder( self ) # not named regs, different regs ==> silent errors
build do build do
space? << Parfait.object_space factory! << Parfait.object_space.get_factory_for(:Integer)
to << space[:next_integer] to << factory[:next_object]
integer_2! << to[:next_integer] integer_2! << to[:next_integer]
space[:next_integer] << integer_2 factory[:next_object] << integer_2
to[Parfait::Integer.integer_index] << from to[Parfait::Integer.integer_index] << from
end end
end end

View File

@ -163,9 +163,8 @@ module Parfait
Factory: { for_type: :Type , next_object: :Object , last_object: :Object , Factory: { for_type: :Type , next_object: :Object , last_object: :Object ,
reserve: :Object , attribute_name: :Word }, reserve: :Object , attribute_name: :Word },
ReturnAddress: {next_integer: :ReturnAddress}, ReturnAddress: {next_integer: :ReturnAddress},
Space: {classes: :Dictionary , types: :Dictionary , Space: {classes: :Dictionary , types: :Dictionary , factories: :Dictionary,
next_message: :Message , messages: :Message , next_message: :Message , messages: :Message ,
next_integer: :Integer, integers: :Integer ,
next_address: :ReturnAddress ,addresses: :ReturnAddress , next_address: :ReturnAddress ,addresses: :ReturnAddress ,
true_object: :TrueClass, false_object: :FalseClass , nil_object: :NilClass}, true_object: :TrueClass, false_object: :FalseClass , nil_object: :NilClass},
TrueClass: {}, TrueClass: {},

View File

@ -28,7 +28,7 @@ module Risc
end end
def test_load_first_message #from space (ie r2) def test_load_first_message #from space (ie r2)
sl = @produced.next( 2 ) sl = @produced.next( 2 )
assert_slot_to_reg( sl , :r2 , 3 , :r3 ) assert_slot_to_reg( sl , :r2 , 4 , :r3 )
end end
def test_get_next_next #reduce onto itself def test_get_next_next #reduce onto itself
sl = @produced.next( 3 ) sl = @produced.next( 3 )
@ -36,7 +36,7 @@ module Risc
end end
def test_store_next_next_in_space def test_store_next_next_in_space
sl = @produced.next( 4 ) sl = @produced.next( 4 )
assert_reg_to_slot( sl , :r4 , :r2 , 3 ) assert_reg_to_slot( sl , :r4 , :r2 , 4 )
end end
def test_store_message_in_current def test_store_message_in_current
sl = @produced.next( 5 ) sl = @produced.next( 5 )

View File

@ -36,13 +36,13 @@ module Risc
assert_load( instruction(4) , Parfait::Space ) assert_load( instruction(4) , Parfait::Space )
end end
def test_get_next def test_get_next
assert_slot_to_reg( instruction( 5 ) , :r3 , 3 , :r4 ) assert_slot_to_reg( instruction( 5 ) , :r3 , 4 , :r4 )
end end
def test_save_next def test_save_next
assert_reg_to_slot( instruction( 6 ) , :r4 , :r0 , 1 ) assert_reg_to_slot( instruction( 6 ) , :r4 , :r0 , 1 )
end end
def test_save_this def test_save_this
assert_reg_to_slot( instruction( 7 ) , :r0 , :r3 , 3 ) assert_reg_to_slot( instruction( 7 ) , :r0 , :r3 , 4 )
end end
def test_save_addr def test_save_addr

View File

@ -31,18 +31,6 @@ module Parfait
assert_equal 20 , @int.set_internal_word( Integer.integer_index , 20 ) assert_equal 20 , @int.set_internal_word( Integer.integer_index , 20 )
assert_equal 20 , @int.get_internal_word( Integer.integer_index ) assert_equal 20 , @int.get_internal_word( Integer.integer_index )
end end
def test_integer_first
assert Parfait.object_space.next_integer
end
def test_integer_20
int = Parfait.object_space.next_integer
20.times do
assert int
assert_equal Parfait::Integer , int.class
assert int.get_internal_word(1)
int = int.next_integer
end
end
def test_set def test_set
@int.set_value(1) @int.set_value(1)
assert_equal 1 , @int.value assert_equal 1 , @int.value

View File

@ -11,7 +11,7 @@ module Parfait
end end
def test_space_length def test_space_length
assert_equal 12 , @space.get_type.instance_length , @space.get_type.inspect assert_equal 11 , @space.get_type.instance_length , @space.get_type.inspect
end end
def test_singletons def test_singletons
assert @space.true_object , "No truth" assert @space.true_object , "No truth"
@ -21,7 +21,6 @@ module Parfait
def test_global_space def test_global_space
assert_equal Parfait::Space , Parfait.object_space.class assert_equal Parfait::Space , Parfait.object_space.class
end end
def test_get_class_by_name def test_get_class_by_name
assert_equal Parfait::Class , Parfait.object_space.get_class_by_name(:Space).class assert_equal Parfait::Class , Parfait.object_space.get_class_by_name(:Space).class
end end
@ -31,10 +30,6 @@ module Parfait
def test_get_type_by_class_name_nil def test_get_type_by_class_name_nil
assert_nil Parfait.object_space.get_type_by_class_name(:Spac) assert_nil Parfait.object_space.get_type_by_class_name(:Spac)
end end
def test_get_integer_instance
int = @space.get_integer
assert_equal Integer , int.class
end
def test_classes_class def test_classes_class
classes.each do |name| classes.each do |name|
assert_equal :Class , @space.classes[name].get_class.name assert_equal :Class , @space.classes[name].get_class.name
@ -93,12 +88,26 @@ module Parfait
end end
end end
end end
def test_has_factory
assert_equal Dictionary , @space.factories.class
end
def test_factory_length
assert_equal 1 , @space.factories.length
end
def test_has_integer_factory
ints = @space.get_factory_for(:Integer)
assert_equal Factory , ints.class
assert_equal :Integer , ints.for_type.class_name
end
def test_has_integers def test_has_integers
assert_equal Parfait::Integer , @space.next_integer.class nekst = @space.get_next_for(:Integer)
assert_equal 0 , @space.next_integer.value assert_equal Parfait::Integer , nekst.class
assert_nil nekst.value
end end
def test_has_next_integer def test_has_next_integer
assert_equal Parfait::Integer , @space.next_integer.next_integer.class nekst = @space.get_next_for(:Integer)
nekst = @space.get_next_for(:Integer)
assert_equal Parfait::Integer , nekst.class
end end
def test_has_addresses def test_has_addresses
assert_equal Parfait::ReturnAddress , @space.next_address.class assert_equal Parfait::ReturnAddress , @space.next_address.class
@ -116,7 +125,6 @@ module Parfait
end end
assert_equal 400, count assert_equal 400, count
end end
def test_messages def test_messages
mess = @space.messages mess = @space.messages
all = [] all = []

View File

@ -27,7 +27,7 @@ module Parfait
assert_equal Parfait::Space , space.class assert_equal Parfait::Space , space.class
type = space.get_type type = space.get_type
assert_equal Parfait::Type , type.class assert_equal Parfait::Type , type.class
assert_equal 12 , type.names.get_length assert_equal 11 , type.names.get_length
assert_equal type.object_class.class , Parfait::Class assert_equal type.object_class.class , Parfait::Class
assert_equal type.object_class.name , :Space assert_equal type.object_class.name , :Space
end end

View File

@ -35,11 +35,11 @@ module Risc
def test_load_space def test_load_space
load_ins = main_ticks 53 load_ins = main_ticks 53
assert_load load_ins, Parfait::Space assert_load load_ins, Parfait::Factory
end end
def test_load_to def test_load_to
to = main_ticks 54 to = main_ticks 54
assert_slot_to_reg to , :r5 , 5 ,:r2 assert_slot_to_reg to , :r5 , 2 ,:r2
end end
def test_load_25 def test_load_25
load_ins = main_ticks 9 load_ins = main_ticks 9

View File

@ -45,7 +45,7 @@ module Risc
ret = main_ticks(64) ret = main_ticks(64)
assert_equal FunctionReturn , ret.class assert_equal FunctionReturn , ret.class
assert_equal :r1 , ret.register.symbol assert_equal :r1 , ret.register.symbol
assert_equal 27080 , @interpreter.get_register(ret.register) assert_equal 41992 , @interpreter.get_register(ret.register)
end end
end end
end end

View File

@ -68,11 +68,11 @@ module Risc
end end
def test_load_int_space def test_load_int_space
cons = main_ticks(base + 6) cons = main_ticks(base + 6)
assert_load( cons , Parfait::Space , :r3) assert_load( cons , Parfait::Factory , :r3)
end end
def test_load_int_next_space def test_load_int_next_space
sl = main_ticks(base + 7) sl = main_ticks(base + 7)
assert_slot_to_reg( sl , :r3 , 5 , :r2) assert_slot_to_reg( sl , :r3 , 2 , :r2)
assert_equal Parfait::Integer , @interpreter.get_register(:r2).class assert_equal Parfait::Integer , @interpreter.get_register(:r2).class
end end
def test_load_int_next_int def test_load_int_next_int
@ -82,7 +82,7 @@ module Risc
end end
def test_load_int_next_int2 def test_load_int_next_int2
sl = main_ticks(base + 9) sl = main_ticks(base + 9)
assert_reg_to_slot( sl , :r4 , :r3 , 5) assert_reg_to_slot( sl , :r4 , :r3 , 2)
end end
end end
end end

View File

@ -54,7 +54,7 @@ module Risc
end end
def test_pc1 def test_pc1
@interpreter.tick @interpreter.tick
assert_equal 26680 , @interpreter.pc assert_equal 41592 , @interpreter.pc
end end
def test_tick2 def test_tick2
@interpreter.tick @interpreter.tick
@ -68,7 +68,7 @@ module Risc
def test_pc2 def test_pc2
@interpreter.tick @interpreter.tick
@interpreter.tick @interpreter.tick
assert_equal 26684 , @interpreter.pc assert_equal 41596 , @interpreter.pc
end end
def test_tick_14_jump def test_tick_14_jump
14.times {@interpreter.tick} 14.times {@interpreter.tick}

View File

@ -25,7 +25,7 @@ module Risc
assert_equal 0 , Position.get(@linker.cpu_init).at assert_equal 0 , Position.get(@linker.cpu_init).at
end end
def test_cpu_at def test_cpu_at
assert_equal "0x76ec" , Position.get(@linker.cpu_init.first).to_s assert_equal "0xb12c" , Position.get(@linker.cpu_init.first).to_s
end end
def test_cpu_label def test_cpu_label
assert_equal Position , Position.get(@linker.cpu_init.first).class assert_equal Position , Position.get(@linker.cpu_init.first).class

View File

@ -81,7 +81,7 @@ module Risc
assert_equal SlotToReg , instr.class assert_equal SlotToReg , instr.class
assert_equal @r1 , instr.array assert_equal @r1 , instr.array
assert_equal @r0 , instr.register assert_equal @r0 , instr.register
assert_equal 3 , instr.index assert_equal 4 , instr.index
end end
def test_reg_to_byte def test_reg_to_byte
instr = @r1[1] <= @r0 instr = @r1[1] <= @r0
@ -95,7 +95,7 @@ module Risc
assert_equal RegToSlot , instr.class assert_equal RegToSlot , instr.class
assert_equal @r1 , instr.array assert_equal @r1 , instr.array
assert_equal @r0 , instr.register assert_equal @r0 , instr.register
assert_equal 3 , instr.index assert_equal 4 , instr.index
end end
end end
end end