rename singleton_class to single_class and misc

was clashing with real ruby method name
also many superclass mismatch fixes
some misc
This commit is contained in:
Torsten Rüger 2019-09-24 17:25:19 +03:00
parent dd810cfc49
commit 3df54910cc
29 changed files with 87 additions and 35 deletions

View File

@ -52,7 +52,6 @@ module Parfait
def get_instance_method( fname )
raise "get_instance_method #{fname}.#{fname.class}" unless fname.is_a?(Symbol)
#if we had a hash this would be easier. Detect or find would help too
@instance_methods.find {|m| m.name == fname }
end

View File

@ -17,7 +17,7 @@
module Parfait
class Class < Behaviour
attr_reader :name , :super_class_name , :singleton_class
attr_reader :name , :super_class_name , :single_class
def self.type_length
6
@ -30,7 +30,7 @@ module Parfait
super(instance_type)
@name = name
@super_class_name = superclass
@singleton_class = SingletonClass.new( self , self.type || @name)
@single_class = SingletonClass.new( self , self.type || @name)
end
def rxf_reference_name

View File

@ -51,7 +51,7 @@ module Parfait
classes.each do |name , cl|
object_type = Parfait.object_space.get_type_by_class_name(name)
raise "nil type" unless object_type
cl.singleton_class.instance_eval{ @instance_type = class_type}
cl.single_class.instance_eval{ @instance_type = class_type}
cl.instance_eval{ @instance_type = object_type}
cl.instance_eval{ @super_class_name = super_names[name] || :Object}
object_type.instance_eval{ @object_class = cl }
@ -79,6 +79,9 @@ module Parfait
Data16: :DataObject ,
Data32: :DataObject ,
BinaryCode: :Data32 ,
TrueClass: :Data4 ,
FalseClass: :Data4 ,
NilClass: :Data4 ,
Integer: :Data4 ,
Word: :Data8 ,
List: :Data16 ,
@ -106,7 +109,7 @@ module Parfait
arguments_type: :Type , self_type: :Type, frame_type: :Type ,
name: :Word , blocks: :Block} ,
Class: {instance_methods: :List, instance_type: :Type,
name: :Word, super_class_name: :Word , singleton_class: :SingletonClass},
name: :Word, super_class_name: :Word , single_class: :SingletonClass},
DataObject: {},
Data4: {},
Data8: {},

View File

@ -71,7 +71,11 @@ module Ruby
end
def to_s(depth = 0)
at_depth(depth , "class #{name}" , @body.to_s(depth + 1) , "end")
at_depth(depth , "class #{name} #{super_s}\n#{@body.to_s(depth + 1)}\nend")
end
# deriviation if apropriate
def super_s
@super_class_name ? " < #{@super_class_name}" : ""
end
end
end

View File

@ -39,7 +39,7 @@ module Ruby
def to_s(depth = 0)
arg_str = @args.collect{|a| a.to_s}.join(', ')
at_depth(depth , "def #{name}(#{arg_str})" , @body.to_s(depth + 1) , "end")
at_depth(depth , "def #{name}(#{arg_str})\n #{@body.to_s(1)}\nend")
end
end

View File

@ -59,7 +59,7 @@ module Vool
when MethodExpression
node.to_mom(@clazz)
when ClassMethodExpression
node.to_mom(@clazz.singleton_class)
node.to_mom(@clazz.single_class)
else
raise "Only methods for now #{node.class}:#{node}"
end
@ -78,7 +78,7 @@ module Vool
when MethodExpression
target = @clazz
when ClassMethodExpression
target = @clazz.singleton_class
target = @clazz.single_class
else
raise "Only methods for now #{node.class}:#{node}"
end

View File

@ -12,11 +12,11 @@ module Vool
# Must pass in the actual Parfait class (default nil is just to conform to api)
def to_parfait( clazz = nil )
raise "No class given to class method #{name}" unless clazz
clazz.add_instance_method_for(name , make_arg_type , make_frame , body )
clazz.single_class.add_instance_method_for(name , make_arg_type , make_frame , body )
end
def to_mom(clazz)
raise "not singleton" unless clazz.class == Parfait::SingletonClass
raise "not singleton #{clazz.class}" unless clazz.class == Parfait::SingletonClass
raise( "no class in #{self}") unless clazz
method = clazz.get_instance_method(name )
raise( "no class method in #{@name} in #{clazz}") unless method

View File

@ -49,7 +49,7 @@ module Vool
class ModuleName < Expression
include Named
def ct_type
get_named_class.singleton_class.instance_type
get_named_class.single_class.instance_type
end
def to_slot(_)
return Mom::SlotDefinition.new( get_named_class, [])

View File

@ -6,7 +6,7 @@ module Elf
def test_string_put
hello = "Hello World!\n"
input = "return '#{hello}'.putstring"
preload = "class Word;def putstring;X.putstring;end;end;"
preload = "class Word < Data8;def putstring;X.putstring;end;end;"
@stdout = hello
@exit_code = hello.length
check preload + as_main(input), "hello"

View File

@ -1,4 +1,4 @@
class Class
class Class < Behaviour
def name
@name
end

View File

@ -16,7 +16,7 @@ module Mains
def whole_input
<<-eos
class Class
class Class < Behaviour
def get_name
@name
end

View File

@ -5,7 +5,7 @@ module Parfait
def setup
super
@try = @space.create_class( :Try , :Object).singleton_class
@try = @space.create_class( :Try , :Object).single_class
end
def test_type_forclass

View File

@ -28,7 +28,7 @@ module Parfait
assert_equal Parfait::Class , space_class.class
end
def test_get_singleton_class
assert_equal Parfait::SingletonClass , space_class.singleton_class.class
assert_equal Parfait::SingletonClass , space_class.single_class.class
end
def test_get_type_by_class_name
assert_equal Parfait::Type , Parfait.object_space.get_type_by_class_name(:Space).class
@ -74,7 +74,7 @@ module Parfait
end
def test_all_singletons
@space.classes.each do |name , clazz|
assert clazz.singleton_class , clazz.name
assert clazz.single_class , clazz.name
end
end
def test_has_factory

View File

@ -19,7 +19,6 @@ module Risc
MAIN
super
end
#Space type is wrong, shold be same as singleton_class.instance_type
def test_chain
#show_main_ticks # get output of what is
run_input @string_input

View File

@ -15,6 +15,7 @@ module RubyX
end
def compile_in_test( input , options = {})
vool = ruby_to_vool(in_Test(input) , options)
vool.to_parfait
vool.to_mom(nil)
itest = Parfait.object_space.get_class_by_name(:Test)
assert itest

View File

@ -8,7 +8,7 @@ module RubyX
def len ; 25 ; end
def source
<<GET
class Integer
class Integer < Data4
def #{op}(other)
X.comparison(:"#{op}")
end

View File

@ -6,7 +6,7 @@ module RubyX
include MacroHelper
def source
<<GET
class Integer
class Integer < Data4
def div10
X.div10
end

View File

@ -6,7 +6,7 @@ module RubyX
include MacroHelper
def source
<<GET
class Integer
class Integer < Data4
def div4
X.div4
end

View File

@ -7,7 +7,7 @@ module RubyX
def op ; :+ ; end
def source
<<GET
class Integer
class Integer < Data4
def #{op}(other)
X.int_operator(:"#{op}")
end

View File

@ -6,7 +6,7 @@ module RubyX
include MacroHelper
def source
<<GET
class Word
class Word < Data8
def get_internal_byte(at)
X.get_internal_byte
end

View File

@ -6,7 +6,7 @@ module RubyX
include MacroHelper
def source
<<GET
class Word
class Word < Data8
def putstring
X.putstring
end

View File

@ -6,7 +6,7 @@ module RubyX
include MacroHelper
def source
<<GET
class Word
class Word < Data8
def set_internal_byte( at , value)
X.set_internal_byte
end

View File

@ -19,7 +19,7 @@ module Vool
end
def setup
source = "class Integer;def +(other);X.int_operator(:+);end;end;" + class_main
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{|c|c.callable.name==:main}.mom_instructions.next
end

View File

@ -27,7 +27,7 @@ module Vool
def test_class_inst
space_class = Parfait.object_space.get_class
assert_equal :Space , space_class.name
names = space_class.singleton_class.instance_type.names
names = space_class.single_class.instance_type.names
assert names.index_of(:inst) , names
end
def test_compiler

View File

@ -7,7 +7,7 @@ module Vool
def class_main
<<-eos
class Object
class Space
def self.one_plus()
return 1 + 1
end

View File

@ -12,6 +12,7 @@ module VoolBlocks
def test_block_not_compiles
source = "main{|val| val = 0}"
vool = Ruby::RubyCompiler.compile( as_class_method(source) ).to_vool
vool.to_parfait
begin
vool.to_mom(nil)
rescue => err
@ -20,6 +21,7 @@ module VoolBlocks
end
def test_assign_compiles
vool = Ruby::RubyCompiler.compile( as_class_method("val = 0") ).to_vool
vool.to_parfait
assert_equal Mom::MomCollection , vool.to_mom(nil).class
end
end

View File

@ -2,7 +2,7 @@
require_relative "helper"
module Vool
class TestClassStatement #< MiniTest::Test
class TestClassStatement < MiniTest::Test
include ScopeHelper
def setup
Parfait.boot!(Parfait.default_test_options)
@ -26,7 +26,7 @@ module Vool
assert_equal :a , @vool.to_parfait.instance_type.names[1]
end
end
class TestClassStatementTypeCreation #< MiniTest::Test
class TestClassStatementTypeCreation < MiniTest::Test
include ScopeHelper
def setup
Parfait.boot!(Parfait.default_test_options)

View File

@ -0,0 +1,39 @@
require_relative "helper"
module Vool
class TestClassMethodExpression < MiniTest::Test
include VoolCompile
def class_code
"class Space;def self.meth;return 1 ; end;end"
end
def setup
Parfait.boot!(Parfait.default_test_options)
ruby_tree = Ruby::RubyCompiler.compile( class_code )
@clazz = ruby_tree.to_vool
end
def method
@clazz.body.first
end
def test_setup
assert_equal ClassExpression , @clazz.class
assert_equal Statements , @clazz.body.class
assert_equal ClassMethodExpression , method.class
end
def test_fail
assert_raises{ method.to_parfait }
end
def test_method
clazz = @clazz.to_parfait
assert_equal Parfait::Class , clazz.class
meth = method.to_parfait(clazz)
assert_equal Parfait::VoolMethod , meth.class
assert_equal :meth , meth.name
end
def test_is_class_method
clazz = @clazz.to_parfait
m = clazz.single_class.get_instance_method(:meth)
assert m , "no method :meth"
end
end
end

View File

@ -1,7 +1,7 @@
require_relative "helper"
module Vool
class TestMethodStatement < MiniTest::Test
class TestMethodExpression < MiniTest::Test
include VoolCompile
def setup
@ -23,7 +23,12 @@ module Vool
def test_method
clazz = @clazz.to_parfait
assert_equal Parfait::Class , clazz.class
assert_equal Parfait::VoolMethod , method.to_parfait(clazz).class
meth = method.to_parfait(clazz)
assert_equal Parfait::VoolMethod , meth.class
assert_equal :main , meth.name
end
def test_is_instance_method
assert main = @clazz.to_parfait.get_instance_method(:main)
end
end