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
end
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)
compiler.add_constant(value)
value

View File

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

View File

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

View File

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