fixes parfaits hash by implementing array
basic concept holds up as parfaits hash is now working on parfaits own array
This commit is contained in:
parent
b980def84e
commit
398516a596
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
2
test/parfait/test_all.rb
Normal file
2
test/parfait/test_all.rb
Normal file
@ -0,0 +1,2 @@
|
||||
require_relative "test_array"
|
||||
require_relative "test_hash"
|
42
test/parfait/test_hash.rb
Normal file
42
test/parfait/test_hash.rb
Normal file
@ -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
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user