Introduce singleton types

Just for future, as this gives us a way to know immediately in the type, which represent normal, and which singleton classes
Also instantiate singleton class lazily (with singleton type)
This makes the type of class single, ie unique, immediately when it is used, ie methods or variables defined.
Fixes a design mistake, where all singletonn classes shared the same type, and thus unique methods per class were impossible
(Also some misc in commit)
This commit is contained in:
2019-09-30 17:09:13 +03:00
parent ba83affd8c
commit 2dcb2a9a72
22 changed files with 124 additions and 52 deletions

View File

@ -48,6 +48,20 @@ module Parfait
@space.get_class.add_instance_variable(:counter , :Integer)
assert before != @space.get_class.instance_type.hash
end
def test_has_single
assert_equal SingletonClass , @try.single_class.class
end
def test_before_not_single_type
assert_equal false , @try.type.is_single?
end
def test_single_type_not_class
hash_after = @try.single_class.instance_type.hash
assert_equal @try.type.hash , hash_after
end
def test_single_type_not_class_before
hash_before = @try.type.hash
hash_after = @try.single_class.instance_type.hash
refute_equal hash_before , hash_after
end
end
end

View File

@ -49,5 +49,11 @@ module Parfait
@try.add_instance_variable(:counter , :Integer)
assert_equal @try.clazz.type , @try.instance_type
end
def test_name
assert_equal :"Try.Single" , @try.name
end
def test_type_is_single
assert_equal true , @try.instance_type.is_single?
end
end
end

View File

@ -13,35 +13,29 @@ module Parfait
def test_type_index
assert_equal @mess.get_type , @mess.get_internal_word(Parfait::TYPE_INDEX) , "mess"
end
def test_type_is_first
type = @mess.get_type
assert_equal 0 , type.variable_index(:type)
end
def test_length
assert @mess
assert @mess.get_type
assert_equal 31 , @mess.get_type.instance_length , @mess.get_type.inspect
end
def test_names
assert @type.names
end
def test_types
assert @type.types
end
def test_type_length
assert_equal 31 , @mess.get_type.instance_length , @mess.get_type.inspect
end
def test_type_length_index
type = @mess.get_type.get_type
assert_equal 4 , type.variable_index(:methods)
assert_equal type.object_class , type.get_internal_word(3)
end
def test_no_index_below_0
type = @mess.get_type
names = type.names
@ -50,12 +44,10 @@ module Parfait
assert type.variable_index(n) >= 0
end
end
def test_attribute_set
@mess.set_receiver( 55)
assert_equal 55 , @mess.receiver
end
def test_variable_index
assert_equal 1 , @type.variable_index(:next_message)
end
@ -68,7 +60,6 @@ module Parfait
def test_type_for
assert_equal :Message , @type.type_for(:next_message)
end
def test_remove_me
type = @mess.get_type
assert_equal type , @mess.get_internal_word(0)
@ -83,5 +74,9 @@ module Parfait
int_class = @space.get_type_by_class_name(:Integer)
assert_equal :Integer, int_class.object_class.name
end
def test_create_single
single = Type.for_hash( {} , :Object ,1)
assert_equal true , single.is_single?
end
end
end

View File

@ -38,7 +38,7 @@ module Risc
ret = main_ticks(49)
assert_equal FunctionReturn , ret.class
assert_equal :r3 , ret.register.symbol
assert_equal 38236 , @interpreter.get_register(ret.register)
assert_equal 36540 , @interpreter.get_register(ret.register)
end
end
end

View File

@ -38,7 +38,7 @@ module Risc
end
def len
1479
1426
end
def test_collect_all_types
@ -70,7 +70,7 @@ module Risc
end
def len
2959
2906
end
end
end

View File

@ -54,7 +54,7 @@ module Risc
end
def test_pc
@interpreter.tick
assert_equal t = 37800 , @interpreter.pc
assert_equal t = 36104 , @interpreter.pc
@interpreter.tick
assert_equal t + 4 , @interpreter.pc
end

View File

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

View File

@ -1,7 +1,7 @@
require_relative "../helper"
module RubyX
class TestIntegerCompile# < MiniTest::Test
class TestIntegerCompile < MiniTest::Test
include ParfaitHelper
def setup
@compiler = compiler
@ -26,14 +26,17 @@ module RubyX
assert_equal :Data8 , vool[3].name
end
def test_mom
mom = @compiler.ruby_to_mom source
vool = @compiler.ruby_to_vool source
vool.to_parfait
#puts vool
mom = vool.to_mom(nil)
assert_equal Mom::MomCollection , mom.class
end
def test_risc
def est_risc
risc = compiler.ruby_to_risc source
assert_equal Risc::RiscCollection , risc.class
end
def test_binary
def est_binary
risc = compiler.ruby_to_binary source , :interpreter
assert_equal Risc::Linker , risc.class
end

View File

@ -48,7 +48,7 @@ module RubyX
end
end
end
class TestObjectRtTest #< Minitest::Test
class TestObjectRtTest < Minitest::Test
self.class.include ParfaitHelper
include Risc::Ticker

View File

@ -40,7 +40,7 @@ module Vool
assert_equal SimpleCall, @ins.next(2).class
assert_equal :one_plus, @ins.next(2).method.name
assert_equal Parfait::Type, @ins.next(2).method.self_type.class
assert_equal :Class, @ins.next(2).method.self_type.object_class.name
assert_equal :"Space.Single", @ins.next(2).method.self_type.object_class.name
end
end
end

View File

@ -4,9 +4,25 @@ module Vool
class TestSendClassMom < MiniTest::Test
include VoolCompile
def class_main
<<-eos
class Space
def self.one_plus(one)
return 1 + 1
end
end
class Space
def main(arg)
return Space.one_plus(1)
end
end
eos
end
def setup
@compiler = compile_main( "Object.get_internal_word(0)" , "Object.get" )
@ins = @compiler.mom_instructions.next
source = "class Integer < Data4;def +(other);X.int_operator(:+);end;end;" + class_main
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(source)
@ins = ret.compilers.find_compiler_name(:main).mom_instructions.next
end
def test_array
@ -37,10 +53,10 @@ module Vool
def test_call_is
assert_equal SimpleCall, @ins.next(2).class
assert_equal Parfait::CallableMethod, @ins.next(2).method.class
assert_equal :get_internal_word, @ins.next(2).method.name
assert_equal :one_plus, @ins.next(2).method.name
end
def test_call_has_right_receiver
assert_equal "Class_Type", @ins.next(2).method.self_type.name
assert_equal "Space.Single_Type", @ins.next(2).method.self_type.name
end
end
end

View File

@ -5,7 +5,7 @@ module Vool
include VoolCompile
def class_code
"class Space;def self.meth;return 1 ; end;end"
"class Space;def self.meth; return meth(22 + 22) ; end;end"
end
def setup
Parfait.boot!(Parfait.default_test_options)
@ -33,5 +33,16 @@ module Vool
m = clazz.single_class.instance_type.get_method(:meth)
assert m , "no type method :meth"
end
def as_mom
@clazz.to_parfait
@clazz.to_mom(nil)
end
def test_mom
assert_equal :meth , as_mom.method_compilers.callable.name
end
def test_mom_frame
callable = as_mom.method_compilers.callable
assert callable.frame_type.names.last.to_s.start_with?("tmp_") , "no tmp_ variable #{callable.frame_type.names}"
end
end
end