diff --git a/lib/parfait/behaviour.rb b/lib/parfait/behaviour.rb index 99f1f370..133321c9 100644 --- a/lib/parfait/behaviour.rb +++ b/lib/parfait/behaviour.rb @@ -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 diff --git a/lib/parfait/class.rb b/lib/parfait/class.rb index b82015b2..be29dc4b 100644 --- a/lib/parfait/class.rb +++ b/lib/parfait/class.rb @@ -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 diff --git a/lib/risc/parfait_boot.rb b/lib/risc/parfait_boot.rb index 1d9f6217..05841fc2 100644 --- a/lib/risc/parfait_boot.rb +++ b/lib/risc/parfait_boot.rb @@ -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: {}, diff --git a/lib/ruby/class_statement.rb b/lib/ruby/class_statement.rb index 32a8334f..254b5fd9 100644 --- a/lib/ruby/class_statement.rb +++ b/lib/ruby/class_statement.rb @@ -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 diff --git a/lib/ruby/method_statement.rb b/lib/ruby/method_statement.rb index 122b5ddb..900bd585 100644 --- a/lib/ruby/method_statement.rb +++ b/lib/ruby/method_statement.rb @@ -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 diff --git a/lib/vool/class_expression.rb b/lib/vool/class_expression.rb index 38daee92..e5522573 100644 --- a/lib/vool/class_expression.rb +++ b/lib/vool/class_expression.rb @@ -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 diff --git a/lib/vool/class_method_expression.rb b/lib/vool/class_method_expression.rb index 9eb5d430..93fbf2e4 100644 --- a/lib/vool/class_method_expression.rb +++ b/lib/vool/class_method_expression.rb @@ -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 diff --git a/lib/vool/variables.rb b/lib/vool/variables.rb index 2acaa180..2d4dcfa5 100644 --- a/lib/vool/variables.rb +++ b/lib/vool/variables.rb @@ -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, []) diff --git a/test/elf/test_somethingelse.rb b/test/elf/test_somethingelse.rb index f0eb94e8..f1c116de 100644 --- a/test/elf/test_somethingelse.rb +++ b/test/elf/test_somethingelse.rb @@ -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" diff --git a/test/mains/source/classname_Space_5.rb b/test/mains/source/classname_Space_5.rb index 00393964..f3bfe11b 100644 --- a/test/mains/source/classname_Space_5.rb +++ b/test/mains/source/classname_Space_5.rb @@ -1,4 +1,4 @@ -class Class +class Class < Behaviour def name @name end diff --git a/test/mains/test_new.rb b/test/mains/test_new.rb index 33e3f899..accbd525 100644 --- a/test/mains/test_new.rb +++ b/test/mains/test_new.rb @@ -16,7 +16,7 @@ module Mains def whole_input <<-eos - class Class + class Class < Behaviour def get_name @name end diff --git a/test/parfait/test_singleton_class.rb b/test/parfait/test_singleton_class.rb index e7195687..8e4ce436 100644 --- a/test/parfait/test_singleton_class.rb +++ b/test/parfait/test_singleton_class.rb @@ -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 diff --git a/test/parfait/test_space.rb b/test/parfait/test_space.rb index 4256a4a1..4be62b87 100644 --- a/test/parfait/test_space.rb +++ b/test/parfait/test_space.rb @@ -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 diff --git a/test/risc/interpreter/class/test_class_inst.rb b/test/risc/interpreter/class/test_class_inst.rb index c046c6e7..baa7cf64 100644 --- a/test/risc/interpreter/class/test_class_inst.rb +++ b/test/risc/interpreter/class/test_class_inst.rb @@ -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 diff --git a/test/rubyx/helper.rb b/test/rubyx/helper.rb index bab6af15..bda9d5ba 100644 --- a/test/rubyx/helper.rb +++ b/test/rubyx/helper.rb @@ -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 @@ -23,7 +24,7 @@ module RubyX end module ParfaitHelper include Preloader - + def load_parfait(file) File.read File.expand_path("../../../lib/parfait/#{file}.rb",__FILE__) end diff --git a/test/rubyx/macro/test_integer_comparison.rb b/test/rubyx/macro/test_integer_comparison.rb index c7583307..da9cac0a 100644 --- a/test/rubyx/macro/test_integer_comparison.rb +++ b/test/rubyx/macro/test_integer_comparison.rb @@ -8,7 +8,7 @@ module RubyX def len ; 25 ; end def source < err + rescue => err assert err.message.include?("Blocks") , err.message end 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 diff --git a/test/vool/test_class_expression.rb b/test/vool/test_class_expression.rb index 04876d4b..6d80f3a4 100644 --- a/test/vool/test_class_expression.rb +++ b/test/vool/test_class_expression.rb @@ -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) diff --git a/test/vool/test_class_method_expression.rb b/test/vool/test_class_method_expression.rb new file mode 100644 index 00000000..bad63df4 --- /dev/null +++ b/test/vool/test_class_method_expression.rb @@ -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 diff --git a/test/vool/test_method_expression.rb b/test/vool/test_method_expression.rb index 6d4d8dbb..53bec976 100644 --- a/test/vool/test_method_expression.rb +++ b/test/vool/test_method_expression.rb @@ -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