From cfc2c474b253e7dd3b859150af57b14e04d1eae1 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 18 May 2015 10:47:29 +0300 Subject: [PATCH] test compatibility layer test from and to std/parfait objects for list and word for now moved some of that code to virtual, out of parfait --- lib/parfait/list.rb | 12 +++++ lib/parfait/space.rb | 2 +- lib/parfait/word.rb | 13 ++++++ lib/virtual/compile_parfait.rb | 52 +++++++++++++-------- lib/virtual/compiler/basic_expressions.rb | 2 +- lib/virtual/compiler/callsite_expression.rb | 2 +- lib/virtual/machine.rb | 5 +- test/virtual/test_compat.rb | 19 ++++++++ test/virtual/test_conversion.rb | 4 +- 9 files changed, 86 insertions(+), 25 deletions(-) create mode 100644 test/virtual/test_compat.rb diff --git a/lib/parfait/list.rb b/lib/parfait/list.rb index 6c2afa0f..9a9bb9ad 100644 --- a/lib/parfait/list.rb +++ b/lib/parfait/list.rb @@ -84,6 +84,18 @@ module Parfait return if old_length >= len internal_object_grow(len + 1) end + + def ==(other) + # this should call parfait get_class, alas that is not implemented yet + return false if other.class != self.class + return false if other.get_length != self.get_length + index = self.get_length + while(index > 0) + return false if other.get(index) != self.get(index) + index = index - 1 + end + return true + end #many basic List functions can not be defined in ruby, such as # get/set/length/add/delete # so they must be defined as CompiledMethods in Builtin::Kernel diff --git a/lib/parfait/space.rb b/lib/parfait/space.rb index 8619b7f8..f0754a7a 100644 --- a/lib/parfait/space.rb +++ b/lib/parfait/space.rb @@ -60,7 +60,7 @@ module Parfait def create_class name , variable_names c = Class.new_object(name) - c.set_instance_names Parfait.new_list(variable_names) + c.set_instance_names Virtual.new_list(variable_names) @classes[name] = c end diff --git a/lib/parfait/word.rb b/lib/parfait/word.rb index 42843be2..68555180 100644 --- a/lib/parfait/word.rb +++ b/lib/parfait/word.rb @@ -80,6 +80,19 @@ module Parfait def to_sof "Parfait::Word('#{to_s}')" end + + def ==(other) + # this should call parfait get_class, alas that is not implemented yet + return false if other.class != self.class + return false if other.length != self.length + index = self.length + while(index > 0) + return false if other.get_char(index) != self.get_char(index) + index = index - 1 + end + return true + end + def result= value raise "called" class_for(MoveInstruction).new(value , self , :opcode => :mov) diff --git a/lib/virtual/compile_parfait.rb b/lib/virtual/compile_parfait.rb index 65bb4296..1675fe80 100644 --- a/lib/virtual/compile_parfait.rb +++ b/lib/virtual/compile_parfait.rb @@ -55,25 +55,6 @@ module Parfait class Parfait::List end - # Functions to generate parfait objects - def self.new_word( string ) - string = string.to_s if string.is_a? Symbol - word = Parfait::Word.new( string.length ) - string.codepoints.each_with_index do |code , index | - word.set_char(index + 1 , code) - end - word - end - def self.new_list array - list = List.new_object - list.set_length array.length - index = 1 - while index < array.length do - list.set(index , array[index - 1]) - end - list - end - Word.class_eval do def to_s string = "" @@ -85,4 +66,37 @@ module Parfait string end end + List.class_eval do + def to_a + array = [] + index = 1 + while( index <= self.get_length) + array[index - 1] = get(index) + index = index + 1 + end + array + end + end +end + +module Virtual + # Functions to generate parfait objects + def self.new_word( string ) + string = string.to_s if string.is_a? Symbol + word = Parfait::Word.new( string.length ) + string.codepoints.each_with_index do |code , index | + word.set_char(index + 1 , code) + end + word + end + def self.new_list array + list = Parfait::List.new_object + list.set_length array.length + index = 1 + while index <= array.length do + list.set(index , array[index - 1]) + index = index + 1 + end + list + end end diff --git a/lib/virtual/compiler/basic_expressions.rb b/lib/virtual/compiler/basic_expressions.rb index d2492922..f3244d21 100644 --- a/lib/virtual/compiler/basic_expressions.rb +++ b/lib/virtual/compiler/basic_expressions.rb @@ -71,7 +71,7 @@ module Virtual # attr_reader :string def self.compile_string expression , method - value = Parfait.new_word(expression.string) + value = Virtual.new_word(expression.string) to = Return.new(Reference , value) Machine.instance.space.add_object value method.add_code Set.new( to , value ) diff --git a/lib/virtual/compiler/callsite_expression.rb b/lib/virtual/compiler/callsite_expression.rb index 1650f423..1d05be49 100644 --- a/lib/virtual/compiler/callsite_expression.rb +++ b/lib/virtual/compiler/callsite_expression.rb @@ -8,7 +8,7 @@ module Virtual me = Compiler.compile( expession.receiver , method ) method.add_code NewMessage.new method.add_code Set.new(NewSelf.new(me.type), me) - method.add_code Set.new(NewName.new(), Parfait.new_word(expession.name)) + method.add_code Set.new(NewName.new(), Virtual.new_word(expession.name)) compiled_args = [] expession.args.each_with_index do |arg , i| #compile in the running method, ie before passing control diff --git a/lib/virtual/machine.rb b/lib/virtual/machine.rb index deb02056..811c3983 100644 --- a/lib/virtual/machine.rb +++ b/lib/virtual/machine.rb @@ -143,7 +143,10 @@ module Virtual cl = @space.get_class_by_name( name ) cl.set_super_class(object_class) end - #boot_layouts + boot_layouts! + end + def boot_layouts! + end def boot # read all the files needed for a minimal system at compile diff --git a/test/virtual/test_compat.rb b/test/virtual/test_compat.rb new file mode 100644 index 00000000..406f9dc5 --- /dev/null +++ b/test/virtual/test_compat.rb @@ -0,0 +1,19 @@ +require_relative "../helper" + + +class TestCompat < MiniTest::Test + + def test_list_create_from_array + array = [1,2,3] + list = Virtual.new_list(array) + assert_equal list , Virtual.new_list(array) + assert_equal array , list.to_a + end + + def test_word_create_from_string + string = "something" + word = Virtual.new_word(string) + assert_equal word , Virtual.new_word(string) + assert_equal string , word.to_s + end +end diff --git a/test/virtual/test_conversion.rb b/test/virtual/test_conversion.rb index 7fd200bb..ff415eb0 100644 --- a/test/virtual/test_conversion.rb +++ b/test/virtual/test_conversion.rb @@ -50,8 +50,8 @@ class TestConversion < MiniTest::Test def test_string string = "hello" - word = Parfait.new_word string - assert_equal word , Parfait.new_word(string) + word = Virtual.new_word string + assert_equal word , Virtual.new_word(string) assert_equal string , word.to_s end