From 398516a596f2fa97e19ae92ab4bfa69f946daa01 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 12 May 2015 18:52:01 +0300 Subject: [PATCH] fixes parfaits hash by implementing array basic concept holds up as parfaits hash is now working on parfaits own array --- lib/parfait.rb | 15 +++++-- lib/parfait/array.rb | 37 +++++++++++++++++ lib/parfait/hash.rb | 6 +-- lib/parfait/module.rb | 2 +- lib/virtual/machine.rb | 10 ++--- test/parfait/test_all.rb | 2 + test/parfait/{hash_test.rb => test_array.rb} | 0 test/parfait/test_hash.rb | 42 ++++++++++++++++++++ test/test_all.rb | 2 +- 9 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 test/parfait/test_all.rb rename test/parfait/{hash_test.rb => test_array.rb} (100%) create mode 100644 test/parfait/test_hash.rb diff --git a/lib/parfait.rb b/lib/parfait.rb index d51e9692..675ecf11 100644 --- a/lib/parfait.rb +++ b/lib/parfait.rb @@ -23,19 +23,28 @@ end class Parfait::Object include FakeMem - def self.new_object &args + def self.new_object *args puts "I am #{self}" object = self.new(*args) object end - def object_length + def internal_object_length @memory.length end + def internal_object_get(index) + @memory[index] + end + def internal_object_set(index , value) + @memory[index] = value + end + def internal_object_grow(index) + @memory[index] = niL + end end class Parfait::Class end class Parfait::Array def length - object_length + internal_object_length end end diff --git a/lib/parfait/array.rb b/lib/parfait/array.rb index 10007937..5e55f98c 100644 --- a/lib/parfait/array.rb +++ b/lib/parfait/array.rb @@ -2,6 +2,43 @@ module Parfait class Array < Object + + # push means add to the end + # this automatically grows the array + def push value + self.set( length , value) + end + + def set( index , value) + raise "negative index for set #{len}" if index < 0 + if index >= self.length + grow_to(index) + end + internal_object_set( index , value) + end + + def get(index) + raise "negative index for get #{len}" if index < 0 + if index >= self.length + return nil + else + return internal_object_get(index) + end + end + + def empty? + self.length == 0 + end + + def grow_to(len) + raise "negative length for grow #{len}" if len < 0 + return unless len > self.length + index = self.length + internal_object_grow(length) + while( index < self.length ) + internal_object_set( index , nil) + end + end #many basic array 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/hash.rb b/lib/parfait/hash.rb index dfe69c43..da012110 100644 --- a/lib/parfait/hash.rb +++ b/lib/parfait/hash.rb @@ -23,7 +23,7 @@ module Parfait def get(key) index = key_index(key) if( index ) - @values[index] + @values.get(index) else nil end @@ -37,7 +37,7 @@ module Parfait index = 0 found = nil while(index < len) - if( @keys[index] == key) + if( @keys.get(index) == key) found = index break end @@ -49,7 +49,7 @@ module Parfait def set(key , value) index = key_index(key) if( index ) - @keys[index] = value + @keys.set(index , value) else @keys.push(key) @values.push(value) diff --git a/lib/parfait/module.rb b/lib/parfait/module.rb index d6d15695..fab74e05 100644 --- a/lib/parfait/module.rb +++ b/lib/parfait/module.rb @@ -12,7 +12,7 @@ # may be added to any object at run-time module Parfait - class Module + class Module < Object # :<, :<=, :>, :>=, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, # :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, # :const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, diff --git a/lib/virtual/machine.rb b/lib/virtual/machine.rb index 9b11ecae..40ff5dce 100644 --- a/lib/virtual/machine.rb +++ b/lib/virtual/machine.rb @@ -92,11 +92,11 @@ module Virtual def boot_classes! # very fiddly chicken 'n egg problem. Functions need to be in the right order, and in fact we have to define some # dummies, just for the other to compile - obj = get_or_create_class :Object + obj = @space.get_or_create_class :Object [:index_of , :_get_instance_variable , :_set_instance_variable].each do |f| obj.add_instance_method Builtin::Object.send(f , nil) end - obj = get_or_create_class :Kernel + obj = @space.get_or_create_class :Kernel # create main first, __init__ calls it @main = Builtin::Kernel.send(:main , @context) obj.add_instance_method @main @@ -107,13 +107,13 @@ module Virtual end # and the @init block in turn _jumps_ to __init__ # the point of which is that by the time main executes, all is "normal" - @init = Virtual::Block.new(:_init_ , nil ) + @init = Block.new(:_init_ , nil ) @init.add_code(Register::RegisterMain.new(underscore_init)) - obj = get_or_create_class :Integer + obj = @space.get_or_create_class :Integer [:putint,:fibo].each do |f| obj.add_instance_method Builtin::Integer.send(f , nil) end - obj = get_or_create_class :String + obj = @space.get_or_create_class :String [:get , :set , :puts].each do |f| obj.add_instance_method Builtin::String.send(f , nil) end diff --git a/test/parfait/test_all.rb b/test/parfait/test_all.rb new file mode 100644 index 00000000..bd0451aa --- /dev/null +++ b/test/parfait/test_all.rb @@ -0,0 +1,2 @@ +require_relative "test_array" +require_relative "test_hash" diff --git a/test/parfait/hash_test.rb b/test/parfait/test_array.rb similarity index 100% rename from test/parfait/hash_test.rb rename to test/parfait/test_array.rb diff --git a/test/parfait/test_hash.rb b/test/parfait/test_hash.rb new file mode 100644 index 00000000..757d4937 --- /dev/null +++ b/test/parfait/test_hash.rb @@ -0,0 +1,42 @@ +require_relative "../helper" + +class TestLists < MiniTest::Test + + def setup + @list = ::Parfait::Hash.new + end + def test_list_create + assert @list.empty? + end + def test_empty_list_doesnt_return + assert_equal nil , @list.get(3) + assert_equal nil , @list.get(:any) + end + def test_one_set1 + assert_equal 1 , @list.set(1,1) + end + def test_one_set2 + assert_equal :some , @list.set(1,:some) + end + def test_two_sets + assert_equal 1 , @list.set(1,1) + assert_equal :some , @list.set(1,:some) + end + def test_one_get1 + test_one_set1 + assert_equal 1 , @list.get(1) + end + def test_one_get2 + test_one_set2 + assert_equal :some , @list.get(1) + end + def test_many_get + shouldda = { :one => 1 , :two => 2 , :three => 3} + shouldda.each do |k,v| + @list.set(k,v) + end + shouldda.each do |k,v| + assert_equal v , @list.get(k) + end + end +end diff --git a/test/test_all.rb b/test/test_all.rb index bde765c7..5d012b05 100644 --- a/test/test_all.rb +++ b/test/test_all.rb @@ -1,5 +1,5 @@ # All working tests (ahm), still working on the others, so no use to be constantly reminded require_relative "arm/test_all" -require "parfait/hash_test" +require "parfait/test_all" require_relative "virtual/test_all"