add parfait block
This commit is contained in:
parent
9005513368
commit
2f07cc34f3
@ -11,6 +11,7 @@ require_relative "parfait/list"
|
|||||||
require_relative "parfait/word"
|
require_relative "parfait/word"
|
||||||
require_relative "parfait/binary_code"
|
require_relative "parfait/binary_code"
|
||||||
require_relative "parfait/callable"
|
require_relative "parfait/callable"
|
||||||
|
require_relative "parfait/block"
|
||||||
require_relative "parfait/callable_method"
|
require_relative "parfait/callable_method"
|
||||||
require_relative "parfait/vool_method"
|
require_relative "parfait/vool_method"
|
||||||
require_relative "parfait/dictionary"
|
require_relative "parfait/dictionary"
|
||||||
|
23
lib/parfait/block.rb
Normal file
23
lib/parfait/block.rb
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
module Parfait
|
||||||
|
|
||||||
|
# A Block is a callable object, much like a CallableMethod.
|
||||||
|
# Surprisingly similar in fact, as the block is really only missing the name.
|
||||||
|
#
|
||||||
|
# The difference lies mostly in the way they are compiled
|
||||||
|
#
|
||||||
|
# Also both have a list of blocks defined in their scope. But this is
|
||||||
|
# notimplemented for blocks yet
|
||||||
|
#
|
||||||
|
class Block < Callable
|
||||||
|
|
||||||
|
def ==(other)
|
||||||
|
return false unless other.is_a?(Block)
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#{@self_type.object_class.name}(#{arguments_type.inspect})"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
@ -36,5 +36,24 @@ module Parfait
|
|||||||
block.call( self )
|
block.call( self )
|
||||||
@next.each_method( &block ) if @next
|
@next.each_method( &block ) if @next
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_block(args , frame)
|
||||||
|
bl = Block.new(self_type , args , frame)
|
||||||
|
block = self.blocks
|
||||||
|
bl.next = block if(block)
|
||||||
|
@blocks = bl
|
||||||
|
end
|
||||||
|
|
||||||
|
def has_block(block)
|
||||||
|
each_block{ |bl| return true if bl == block}
|
||||||
|
false
|
||||||
|
end
|
||||||
|
def each_block(&bl)
|
||||||
|
blo = blocks
|
||||||
|
while( blo )
|
||||||
|
yield(blo)
|
||||||
|
blo = blo.next
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -124,6 +124,7 @@ module Parfait
|
|||||||
Word: :Data8 ,
|
Word: :Data8 ,
|
||||||
List: :Data16 ,
|
List: :Data16 ,
|
||||||
CallableMethod: :Callable,
|
CallableMethod: :Callable,
|
||||||
|
Block: :Callable,
|
||||||
ReturnAddress: :Integer}
|
ReturnAddress: :Integer}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -132,6 +133,9 @@ module Parfait
|
|||||||
# and all instance variable names. Really have to find a better way
|
# and all instance variable names. Really have to find a better way
|
||||||
def self.type_names
|
def self.type_names
|
||||||
{BinaryCode: {next: :BinaryCode} ,
|
{BinaryCode: {next: :BinaryCode} ,
|
||||||
|
Block: {binary: :BinaryCode, next: :CallableMethod,
|
||||||
|
arguments_type: :Type , self_type: :Type, frame_type: :Type } ,
|
||||||
|
|
||||||
CacheEntry: {cached_type: :Type , cached_method: :CallableMethod } ,
|
CacheEntry: {cached_type: :Type , cached_method: :CallableMethod } ,
|
||||||
Callable: {binary: :BinaryCode,next: :Callable ,
|
Callable: {binary: :BinaryCode,next: :Callable ,
|
||||||
arguments_type: :Type , self_type: :Type, frame_type: :Type } ,
|
arguments_type: :Type , self_type: :Type, frame_type: :Type } ,
|
||||||
|
@ -7,5 +7,11 @@ module Parfait
|
|||||||
Parfait.boot!
|
Parfait.boot!
|
||||||
@space = Parfait.object_space
|
@space = Parfait.object_space
|
||||||
end
|
end
|
||||||
|
def make_method
|
||||||
|
@obj = Parfait.object_space.get_class_by_name(:Object).instance_type
|
||||||
|
@args = Parfait::Type.for_hash( @obj.object_class , { bar: :Integer , foo: :Type})
|
||||||
|
@frame = Parfait::Type.for_hash( @obj.object_class , { local_bar: :Integer , local_foo: :Type})
|
||||||
|
@method = Parfait::CallableMethod.new( @obj , :meth , @args , @frame)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
24
test/parfait/test_block.rb
Normal file
24
test/parfait/test_block.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
module Parfait
|
||||||
|
class TestBlock < ParfaitTest
|
||||||
|
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
make_method
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_make_block
|
||||||
|
assert_equal Block , @method.create_block(@args , @frame ).class
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_block_type
|
||||||
|
assert_equal @method.self_type , @method.create_block(@args , @frame ).self_type
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_block_in_method
|
||||||
|
assert @method.has_block( @method.create_block(@args , @frame ))
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
@ -5,10 +5,7 @@ module Parfait
|
|||||||
|
|
||||||
def setup
|
def setup
|
||||||
super
|
super
|
||||||
@obj = Parfait.object_space.get_class_by_name(:Object).instance_type
|
make_method
|
||||||
@args = Parfait::Type.for_hash( @obj.object_class , { bar: :Integer , foo: :Type})
|
|
||||||
@frame = Parfait::Type.for_hash( @obj.object_class , { local_bar: :Integer , local_foo: :Type})
|
|
||||||
@method = Parfait::CallableMethod.new( @obj , :meth , @args , @frame)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_method_name
|
def test_method_name
|
||||||
|
@ -4,7 +4,7 @@ module Parfait
|
|||||||
class TestSpace < ParfaitTest
|
class TestSpace < ParfaitTest
|
||||||
|
|
||||||
def classes
|
def classes
|
||||||
[:BinaryCode,:CacheEntry,:Callable,:CallableMethod,:Class,
|
[:BinaryCode,:Block,:CacheEntry,:Callable,:CallableMethod,:Class,
|
||||||
:DataObject,:Data4,:Data8,:Data16,:Dictionary,:Integer,:FalseClass,
|
:DataObject,:Data4,:Data8,:Data16,:Dictionary,:Integer,:FalseClass,
|
||||||
:List,:Message,:NamedList,:NilClass,:Object,:ReturnAddress,
|
:List,:Message,:NamedList,:NilClass,:Object,:ReturnAddress,
|
||||||
:Space,:TrueClass,:Type,:VoolMethod,:Word]
|
:Space,:TrueClass,:Type,:VoolMethod,:Word]
|
||||||
|
@ -45,7 +45,7 @@ module Risc
|
|||||||
ret = main_ticks(63)
|
ret = main_ticks(63)
|
||||||
assert_equal FunctionReturn , ret.class
|
assert_equal FunctionReturn , ret.class
|
||||||
assert_equal :r1 , ret.register.symbol
|
assert_equal :r1 , ret.register.symbol
|
||||||
assert_equal 21476 , @interpreter.get_register(ret.register)
|
assert_equal 21732 , @interpreter.get_register(ret.register)
|
||||||
end
|
end
|
||||||
def test_sys
|
def test_sys
|
||||||
sys = main_ticks(68)
|
sys = main_ticks(68)
|
||||||
|
@ -54,7 +54,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
def test_pc1
|
def test_pc1
|
||||||
@interpreter.tick
|
@interpreter.tick
|
||||||
assert_equal 21048 , @interpreter.pc
|
assert_equal 21304 , @interpreter.pc
|
||||||
end
|
end
|
||||||
def test_tick2
|
def test_tick2
|
||||||
@interpreter.tick
|
@interpreter.tick
|
||||||
@ -68,7 +68,7 @@ module Risc
|
|||||||
def test_pc2
|
def test_pc2
|
||||||
@interpreter.tick
|
@interpreter.tick
|
||||||
@interpreter.tick
|
@interpreter.tick
|
||||||
assert_equal 21052 , @interpreter.pc
|
assert_equal 21308 , @interpreter.pc
|
||||||
end
|
end
|
||||||
def test_tick_14_jump
|
def test_tick_14_jump
|
||||||
14.times {@interpreter.tick}
|
14.times {@interpreter.tick}
|
||||||
|
@ -25,7 +25,7 @@ module Risc
|
|||||||
assert_equal 0 , Position.get(@linker.cpu_init).at
|
assert_equal 0 , Position.get(@linker.cpu_init).at
|
||||||
end
|
end
|
||||||
def test_cpu_at
|
def test_cpu_at
|
||||||
assert_equal "0x60ec" , Position.get(@linker.cpu_init.first).to_s
|
assert_equal "0x61ec" , Position.get(@linker.cpu_init.first).to_s
|
||||||
end
|
end
|
||||||
def test_cpu_label
|
def test_cpu_label
|
||||||
assert_equal Position , Position.get(@linker.cpu_init.first).class
|
assert_equal Position , Position.get(@linker.cpu_init.first).class
|
||||||
|
Loading…
Reference in New Issue
Block a user