Rename Vool to Sol

Simple is really the descriptive name for the layer
Sure, it is "virtual" but that is not as important as the fact that it is simple (or simplified)
Also objct (based really) is better, since orientated implies it is a little like that, but only orientated, not really it. Sol only has objects, nothing else
Just cause i was renaming anyway
This commit is contained in:
2019-10-04 00:36:49 +03:00
parent aa9fc8bc81
commit d1f8733623
135 changed files with 636 additions and 636 deletions

View File

@ -0,0 +1 @@
require_relative "../helper"

View File

@ -0,0 +1,42 @@
require_relative "helper"
module Sol
class TestClassDef < MiniTest::Test
include SlotMachine
include SolCompile
def class_main
<<-eos
class Space
def self.one_plus()
return 1 + 1
end
def main(arg)
return Space.one_plus
end
end
eos
end
def setup
source = "class Integer<Data4;def +(other);X.int_operator(:+);end;end;" + class_main
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_slot(source)
@ins = ret.compilers.find_compiler_name(:main).slot_instructions.next
end
def test_array
check_array [MessageSetup,ArgumentTransfer,SimpleCall,SlotLoad,
ReturnJump,Label, ReturnSequence , Label] , @ins
end
def test_any
assert_equal SlotMachine::MessageSetup , @ins.class
end
def test_no_arg
assert_equal SlotMachine::ArgumentTransfer, @ins.next(1).class
assert_equal 0, @ins.next(1).arguments.length
end
def test_call_two
assert_equal SimpleCall, @ins.next(2).class
end
end
end

View File

@ -0,0 +1,64 @@
require_relative "helper"
module Sol
class TestClassInstance < MiniTest::Test
include SlotMachine
include SolCompile
def class_main
<<-eos
class Space
def self.some_inst
return @inst
end
def main(arg)
return Space.some_inst
end
end
eos
end
def setup
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_slot(class_main)
@compiler = ret.compilers.find_compiler_name(:some_inst)
@main = ret.compilers.find_compiler_name(:main)
@ins = @compiler.slot_instructions.next
end
def test_class_inst
space_class = Parfait.object_space.get_class
assert_equal :Space , space_class.name
names = space_class.single_class.instance_type.names
assert names.index_of(:inst) , names
end
def test_compiler
assert_equal SlotMachine::MethodCompiler, @compiler.class
assert_equal Parfait::Type, @compiler.callable.self_type.class
assert_equal 6, @compiler.callable.self_type.names.index_of(:inst) , @compiler.callable.self_type.names
end
def test_array
check_array [SlotLoad, ReturnJump, Label, ReturnSequence, Label] , @ins
end
def test_main_array
check_array [MessageSetup, ArgumentTransfer, SimpleCall, SlotLoad, ReturnJump ,
Label, ReturnSequence, Label] , @main.slot_instructions.next
end
def test_main_args
args = @main.slot_instructions.next(2)
assert_equal Parfait::Class , args.receiver.known_object.class
assert_equal :Space , args.receiver.known_object.name
assert_equal :some_inst , args.receiver.known_object.type.method_names.first
assert_equal :inst , args.receiver.known_object.type.names.last
end
def test_load_inst
assert_equal SlotLoad, @ins.class
end
def test_left
assert_equal SlotDefinition , @ins.left.class
assert_equal [:return_value] , @ins.left.slots
end
def test_right
assert_equal SlotDefinition , @ins.right.class
assert_equal [:receiver , :inst] , @ins.right.slots
end
end
end

View File

@ -0,0 +1,45 @@
require_relative "helper"
module Sol
class TestClassSendInherited < MiniTest::Test
include SlotMachine
include SolCompile
def class_main
<<-eos
class Object
def self.one_plus()
return 1
end
end
class Space < Object
def main(arg)
return Space.one_plus
end
end
eos
end
def setup
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_slot(class_main)
@ins = ret.compilers.find_compiler_name(:main).slot_instructions.next
end
def test_array
check_array [MessageSetup,ArgumentTransfer,SimpleCall,SlotLoad,
ReturnJump,Label, ReturnSequence , Label] , @ins
end
def test_receiver
assert_equal SlotMachine::ArgumentTransfer, @ins.next(1).class
assert_equal 0, @ins.next(1).arguments.length
assert_equal SlotDefinition, @ins.next(1).receiver.class
assert_equal Parfait::Class, @ins.next(1).receiver.known_object.class
assert_equal :Space, @ins.next(1).receiver.known_object.name
end
def test_call
assert_equal SimpleCall, @ins.next(2).class
assert_equal :one_plus, @ins.next(2).method.name
assert_equal Parfait::Type, @ins.next(2).method.self_type.class
assert_equal :"Space.Single", @ins.next(2).method.self_type.object_class.name
end
end
end

View File

@ -0,0 +1,62 @@
require_relative "helper"
module Sol
class TestSendClassSlotMachine < MiniTest::Test
include SolCompile
def class_main
<<-eos
class Space
def self.one_plus(one)
return 1 + 1
end
end
class Space
def main(arg)
return Space.one_plus(1)
end
end
eos
end
def setup
source = "class Integer < Data4;def +(other);X.int_operator(:+);end;end;" + class_main
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_slot(source)
@ins = ret.compilers.find_compiler_name(:main).slot_instructions.next
end
def test_array
check_array [MessageSetup,ArgumentTransfer,SimpleCall,SlotLoad,
ReturnJump,Label, ReturnSequence , Label] , @ins
end
def test_class_compiles
assert_equal MessageSetup , @ins.class , @ins
end
def test_receiver
assert_equal SlotDefinition, @ins.next.receiver.class
assert_equal Parfait::Class, @ins.next.receiver.known_object.class
assert_equal :Object , @ins.next.receiver.known_object.name
end
def test_receiver_move
assert_equal SlotDefinition, @ins.next.receiver.class
end
def test_receiver
assert_equal Parfait::Class, @ins.next.receiver.known_object.class
end
def test_arg_one
assert_equal SlotLoad, @ins.next(1).arguments[0].class
end
def test_receiver_move_class
assert_equal ArgumentTransfer, @ins.next(1).class
end
def test_call_is
assert_equal SimpleCall, @ins.next(2).class
assert_equal Parfait::CallableMethod, @ins.next(2).method.class
assert_equal :one_plus, @ins.next(2).method.name
end
def test_call_has_right_receiver
assert_equal "Space.Single_Type", @ins.next(2).method.self_type.name
end
end
end

1
test/sol/helper.rb Normal file
View File

@ -0,0 +1 @@
require_relative "../helper"

View File

@ -0,0 +1 @@
require_relative "../helper"

View File

@ -0,0 +1,80 @@
require_relative "../helper"
module SolBlocks
class TestAssignSlotMachine < MiniTest::Test
include SolCompile
def setup
@ins = compile_main_block( "local = 5" )
end
def test_block_compiles
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
end
def test_slot_is_set
assert @ins.left
end
def test_slot_starts_at_message
assert_equal :message , @ins.left.known_object
end
def test_slots_left
assert_equal [:local1] , @ins.left.slots
end
def test_slot_assigns_something
assert @ins.right
end
def test_slot_assigns_int
assert_equal SlotMachine::IntegerConstant , @ins.right.known_object.class
end
end
class TestAssignSlotMachineInstanceToLocal < MiniTest::Test
include SolCompile
def setup
@ins = compile_main_block( "local = @a" , "@a = 5") #second arg in method scope
end
def test_class_compiles
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
end
def test_slots_left
assert_equal [:local1] , @ins.left.slots
end
def test_slots_right
assert_equal [:receiver, :a] , @ins.right.slots
end
end
class TestAssignToArg < MiniTest::Test
include SolCompile
def setup
@ins = compile_main_block( "arg = 5")
end
def test_class_compiles
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
end
def test_slot_is_set
assert @ins.left
end
def test_slots_left
assert_equal [:caller,:caller, :arg1] , @ins.left.slots
end
end
class TestAssignSlotMachineToInstance < MiniTest::Test
include SolCompile
def setup
end
def test_assigns_const
@ins = compile_main_block( "@a = 5")
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
assert_equal SlotMachine::IntegerConstant , @ins.right.known_object.class , @ins
end
def test_assigns_move
@ins = compile_main_block( "@a = arg")
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
assert_equal SlotMachine::SlotDefinition , @ins.right.class , @ins
end
end
end

View File

@ -0,0 +1,28 @@
require_relative "../helper"
module SolBlocks
class TestClassAssignSlotMachine < MiniTest::Test
def setup
Parfait.boot!(Parfait.default_test_options)
end
def as_class_method(source)
"class Space;def self.main();#{source};end;end"
end
def test_block_not_compiles
source = "main{|val| val = 0}"
sol = Ruby::RubyCompiler.compile( as_class_method(source) ).to_sol
sol.to_parfait
begin
sol.to_slot(nil)
rescue => err
assert err.message.include?("Blocks") , err.message
end
end
def test_assign_compiles
sol = Ruby::RubyCompiler.compile( as_class_method("val = 0") ).to_sol
sol.to_parfait
assert_equal SlotMachine::SlotCollection , sol.to_slot(nil).class
end
end
end

View File

@ -0,0 +1,28 @@
require_relative "helper"
module SolBlocks
class TestConditionIfSlotMachine < MiniTest::Test
include SolCompile
def setup
@ins = compile_main_block( "if(5.div4) ; @a = 6 ; else; @a = 5 ; end" , "local=5", "Integer.div4")
end
def test_condition
assert_equal TruthCheck , @ins.next(3).class
end
def test_condition_is_slot
assert_equal SlotDefinition , @ins.next(3).condition.class , @ins
end
def test_simple_call
assert_equal SimpleCall , @ins.next(2).class
assert_equal :div4 , @ins.next(2).method.name
end
def test_array
check_array [MessageSetup, ArgumentTransfer, SimpleCall, TruthCheck, Label ,
SlotLoad, Jump, Label, SlotLoad, Label ,
Label, ReturnSequence, Label] , @ins
end
end
end

View File

@ -0,0 +1,25 @@
require_relative "helper"
module SolBlocks
class TestSimpleWhileSlotMachine < MiniTest::Test
include SolCompile
def setup
@ins = compile_main_block( "while(@a) ; @a = 5 ; end")
end
def test_compiles_as_while
assert_equal Label , @ins.class , @ins
end
def test_condition_compiles_to_check
assert_equal TruthCheck , @ins.next.class , @ins
end
def test_condition_is_slot
assert_equal SlotDefinition , @ins.next.condition.class , @ins
end
def test_array
check_array [Label, TruthCheck, SlotLoad, Jump, Label,
Label, ReturnSequence, Label], @ins
end
end
end

46
test/sol/send/helper.rb Normal file
View File

@ -0,0 +1,46 @@
require_relative "../helper"
module Sol
# relies on @ins and receiver_type method
module SimpleSendHarness
include SolCompile
include SlotMachine
def setup
@compiler = compile_main( send_method , "Integer.div4;Object.get")
@ins = @compiler.slot_instructions.next
end
def test_first_not_array
assert Array != @ins.class , @ins
end
def test_class_compiles
assert_equal MessageSetup , @ins.class , @ins
end
def test_two_instructions_are_returned
assert_equal 8 , @ins.length , @ins
end
def test_receiver_move_class
assert_equal ArgumentTransfer, @ins.next(1).class
end
def test_receiver_move
assert_equal SlotDefinition, @ins.next.receiver.class
end
def test_receiver
type , value = receiver
assert_equal type, @ins.next.receiver.known_object.class
assert_equal value, @ins.next.receiver.known_object.value
end
def test_call_is
assert_equal SimpleCall, @ins.next(2).class
end
def test_call_has_method
assert_equal Parfait::CallableMethod, @ins.next(2).method.class
end
def test_array
check_array [MessageSetup,ArgumentTransfer,SimpleCall,
SlotLoad, ReturnJump, Label, ReturnSequence ,
Label] , @ins
end
end
end

View File

@ -0,0 +1,28 @@
require_relative "../helper"
module Sol
class TestSendCachedSimpleSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "5.div8")
@ins = @compiler.slot_instructions.next
end
def test_check_type
assert_equal NotSameCheck , @ins.class , @ins
end
def test_check_resolve_call
assert_equal ResolveMethod , @ins.next(2).class , @ins
end
def test_dynamic_call_last
assert_equal DynamicCall , @ins.next(6).class , @ins
end
def test_array
check_array [NotSameCheck, SlotLoad, ResolveMethod, Label, MessageSetup ,
ArgumentTransfer, DynamicCall, SlotLoad, ReturnJump,
Label, ReturnSequence, Label] , @ins
end
end
end

View File

@ -0,0 +1,27 @@
require_relative "helper"
module Sol
class TestSendArgsSendSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "a = main(4.div4);return a" , "Integer.div4")
@ins = @compiler.slot_instructions.next
end
def test_array
check_array [MessageSetup, ArgumentTransfer, SimpleCall, SlotLoad, MessageSetup ,
ArgumentTransfer, SimpleCall, SlotLoad ,SlotLoad, ReturnJump,
Label, ReturnSequence , Label] , @ins
end
def test_one_call
assert_equal SimpleCall, @ins.next(2).class
assert_equal :div4 , @ins.next(2).method.name
end
def test_two_call
assert_equal SimpleCall, @ins.next(6).class
assert_equal :main, @ins.next(6).method.name
end
end
end

View File

@ -0,0 +1,35 @@
require_relative "../helper"
module Sol
class TestSendCachedSimpleSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "a = 5; a.div4;return ")
@ins = @compiler.slot_instructions.next
end
def test_check_type
assert_equal NotSameCheck , @ins.next(1).class , @ins
end
def test_type_update
load = @ins.next(2)
assert_equal :message , load.right.known_object , load
assert_equal :local1 , load.right.slots[0] , load
assert_equal :type , load.right.slots[1] , load
end
def test_check_resolve_call
assert_equal ResolveMethod , @ins.next(3).class , @ins
end
def test_dynamic_call_last
assert_equal DynamicCall , @ins.next(7).class , @ins
end
def test_array
check_array [SlotLoad, NotSameCheck, SlotLoad, ResolveMethod ,
Label, MessageSetup, ArgumentTransfer, DynamicCall,
SlotLoad, ReturnJump, Label ,
ReturnSequence, Label] , @ins
end
end
end

View File

@ -0,0 +1,32 @@
require_relative "helper"
module Sol
class TestSendSelfSlotMachine < MiniTest::Test
include SimpleSendHarness
def send_method
"self.get_internal_word(0);return"
end
def test_receiver
assert_equal SlotDefinition, @ins.next.receiver.class
end
def test_arg_one
assert_equal SlotLoad, @ins.next(1).arguments[0].class
end
def test_call_two
assert_equal SimpleCall, @ins.next(2).class
end
def test_call_has_method
assert_equal Parfait::CallableMethod, @ins.next(2).method.class
end
def test_call_has_right_method
assert_equal :get_internal_word, @ins.next(2).method.name
end
end
class TestSendSelfImplicitSlotMachine < TestSendSelfSlotMachine
def send_method
"get_internal_word(0)"
end
end
end

View File

@ -0,0 +1,18 @@
require_relative "helper"
module Sol
class TestSendSimpleSlotMachine < MiniTest::Test
include SimpleSendHarness
def send_method
"5.div4;return"
end
def receiver
[SlotMachine::IntegerConstant , 5]
end
def test_call_has_right_method
assert_equal :div4, @ins.next(2).method.name
end
end
end

View File

@ -0,0 +1,27 @@
require_relative "helper"
module Sol
class TestSendSimpleArgsSlotMachine < MiniTest::Test
include SimpleSendHarness
def send_method
"5.div4(1,2);return"
end
def receiver
[SlotMachine::IntegerConstant , 5]
end
def test_args_two_move
assert_equal :next_message, @ins.next(1).arguments[1].left.slots[0]
assert_equal :arg2, @ins.next(1).arguments[1].left.slots[1]
end
def test_args_two_str
assert_equal SlotMachine::IntegerConstant, @ins.next(1).arguments[1].right.known_object.class
assert_equal 2, @ins.next(1).arguments[1].right.known_object.value
end
def test_array
check_array [MessageSetup,ArgumentTransfer,SimpleCall, SlotLoad, ReturnJump,
Label, ReturnSequence , Label] , @ins
end
end
end

View File

@ -0,0 +1,86 @@
require_relative "helper"
module Sol
class TestAssignSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "local = 5;return")
@ins = @compiler.slot_instructions.next
end
def test_class_compiles
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
end
def test_slot_is_set
assert @ins.left
end
def test_slot_starts_at_message
assert_equal :message , @ins.left.known_object
end
def test_slot_gets_local
assert_equal :local1 , @ins.left.slots[0]
end
def test_slot_assigns_something
assert @ins.right
end
def test_slot_assigns_int
assert_equal SlotMachine::IntegerConstant , @ins.right.known_object.class
end
end
#otherwise as above, but assigning instance, so should get a SlotLoad
class TestAssignSlotMachineInstanceToLocal < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "@a = 5 ; local = @a;return")
@ins = @compiler.slot_instructions.next
end
def test_class_compiles
assert_equal SlotMachine::SlotLoad , @ins.next.class , @ins
end
end
#compiling to an argument should result in different second parameter in the slot array
class TestAssignToArg < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "arg = 5;return")
@ins = @compiler.slot_instructions.next
end
def test_class_compiles
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
end
def test_slot_is_set
assert @ins.left
end
def test_slot_starts_at_message
assert_equal :message , @ins.left.known_object
end
def test_slot_gets_arg
assert_equal :arg1 , @ins.left.slots[0]
end
end
class TestAssignSlotMachineToInstance < MiniTest::Test
include SolCompile
def setup
Parfait.boot!(Parfait.default_test_options)
end
def test_assigns_const
@compiler = compile_main( "@a = 5;return")
@ins = @compiler.slot_instructions.next
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
assert_equal SlotMachine::IntegerConstant , @ins.right.known_object.class , @ins
end
def test_assigns_move
@compiler = compile_main( "@a = arg;return")
@ins = @compiler.slot_instructions.next
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
assert_equal SlotMachine::SlotDefinition , @ins.right.class , @ins
end
end
end

52
test/sol/test_builtin.rb Normal file
View File

@ -0,0 +1,52 @@
require_relative "helper"
module Sol
class TestBuiltin < MiniTest::Test
include SolCompile
def setup
Parfait.boot!(Parfait.default_test_options)
@code = Builtin.boot_methods({})
end
def as_ruby
@ruby = Ruby::RubyCompiler.compile(@code)
end
def as_slot
sol = as_ruby.to_sol
sol.to_parfait
sol.to_slot(nil)
end
def test_boot
assert_equal String , @code.class
assert @code.include?("Integer")
end
def test_compile_ruby
assert_equal Ruby::ClassStatement , as_ruby.class
assert_equal Ruby::MethodStatement , @ruby.body.first.class
assert_equal :+ , @ruby.body.first.name
end
def test_compile_sol
sol = as_ruby.to_sol
assert_equal Sol::ClassExpression , sol.class
assert_equal Sol::MethodExpression , sol.body.first.class
end
def test_sol_method
sol = as_ruby.to_sol
assert_equal :+ , sol.body.first.name
assert_equal Sol::ReturnStatement , sol.body.first.body.class
assert_equal Sol::MacroExpression , sol.body.first.body.return_value.class
end
def test_slot_basic
slot = as_slot
assert_equal SlotMachine::SlotCollection , slot.class
assert_equal SlotMachine::MethodCompiler , slot.method_compilers.class
end
def test_slot_instructions
slot_compiler = as_slot.method_compilers
assert_equal SlotMachine::Label , slot_compiler.slot_instructions.class
assert_equal SlotMachine::IntOperator , slot_compiler.slot_instructions.next.class
assert_equal SlotMachine::SlotLoad , slot_compiler.slot_instructions.next(2).class
assert_equal SlotMachine::ReturnJump , slot_compiler.slot_instructions.next(3).class
end
end
end

View File

@ -0,0 +1,83 @@
require_relative "helper"
module Sol
class TestClassStatement < MiniTest::Test
include ScopeHelper
def setup
Parfait.boot!(Parfait.default_test_options)
ruby_tree = Ruby::RubyCompiler.compile( as_test_main("@a = 5") )
@sol = ruby_tree.to_sol
end
def test_class
assert_equal ClassExpression , @sol.class
assert_equal :Test , @sol.name
end
def test_method
assert_equal MethodExpression , @sol.body.first.class
end
def test_create_class
assert_equal Parfait::Class , @sol.create_class_object.class
end
def test_create_class
assert_equal :Test , @sol.to_parfait.name
end
def test_class_instance
assert_equal :a , @sol.to_parfait.instance_type.names[1]
end
def test_to_s
assert_tos "class Test < Object;def main(arg);@a = 5;return @a;end;end" , @sol
end
end
class TestClassStatementTypeCreation < MiniTest::Test
include ScopeHelper
def setup
Parfait.boot!(Parfait.default_test_options)
end
def check_type_for(input)
ruby_tree = Ruby::RubyCompiler.compile( as_test_main(input) )
sol = ruby_tree.to_sol
assert_equal ClassExpression , sol.class
clazz = sol.to_parfait
assert_equal Parfait::Class , clazz.class
assert_equal :a , clazz.instance_type.names[1]
end
def test_while_cond
check_type_for("while(@a) ; 1 == 1 ; end")
end
def test_while_cond_eq
check_type_for("while(@a==1); 1 == 1 ; end")
end
def test_if_cond
check_type_for("if(@a); 1 == 1 ; end")
end
def test_send_1
check_type_for("@a.call")
end
def test_send_arg
check_type_for("call(@a)")
end
def test_return
check_type_for("return @a")
end
def test_return_call
check_type_for("return call(@a)")
end
def test_return_rec
check_type_for("return @a.call()")
end
end
class TestClassSuperMismatch < MiniTest::Test
include ScopeHelper
def setup
Parfait.boot!(Parfait.default_test_options)
end
def space_test
as_test_main("return 1") + ";class Test < Space ; def main();return 1;end;end"
end
def test_mismatch
sol_tree = Ruby::RubyCompiler.compile( space_test).to_sol
assert_raises {sol_tree.to_parfait}
end
end
end

View File

@ -0,0 +1,35 @@
require_relative "helper"
module Sol
class TestClassStatementCompile < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "if(@a) ; @a = 5 ; else; @a = 6 ; end; return")
@ins = @compiler.slot_instructions
end
def test_label
assert_equal Label , @ins.class , @ins
assert_equal "Space_Type.main" , @ins.name , @ins
end
def test_condition_compiles_to_check
assert_equal TruthCheck , @ins.next.class , @ins
end
def test_condition_is_slot
assert_equal SlotDefinition , @ins.next.condition.class , @ins
end
def test_label_after_check
assert_equal Label , @ins.next(2).class , @ins
end
def test_label_last
assert_equal Label , @ins.last.class , @ins
end
def test_array
check_array [Label, TruthCheck, Label, SlotLoad, Jump ,
Label, SlotLoad, Label, SlotLoad, ReturnJump ,
Label, ReturnSequence, Label] , @ins
end
end
end

View File

@ -0,0 +1,48 @@
require_relative "helper"
module Sol
class TestClassMethodExpression < MiniTest::Test
include SolCompile
def class_code
"class Space;def self.meth; return meth(22 + 22) ; end;end"
end
def setup
Parfait.boot!(Parfait.default_test_options)
ruby_tree = Ruby::RubyCompiler.compile( class_code )
@clazz = ruby_tree.to_sol
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_creates_class_method
clazz = @clazz.to_parfait
m = clazz.single_class.get_instance_method(:meth)
assert m , "no method :meth"
end
def test_creates_type_method
clazz = @clazz.to_parfait
m = clazz.single_class.instance_type.get_method(:meth)
assert m , "no type method :meth"
end
def as_slot
@clazz.to_parfait
@clazz.to_slot(nil)
end
def test_slot
assert_equal :meth , as_slot.method_compilers.callable.name
end
def test_slot_frame
callable = as_slot.method_compilers.callable
assert callable.frame_type.names.last.to_s.start_with?("tmp_") , "no tmp_ variable #{callable.frame_type.names}"
end
end
end

View File

@ -0,0 +1,30 @@
require_relative "helper"
module Sol
class TestIfNoElse < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "if(@a) ; @a = 5 ; end;return")
@ins = @compiler.slot_instructions.next
end
def test_condition_compiles_to_check
assert_equal TruthCheck , @ins.class , @ins
end
def test_condition_is_slot
assert_equal SlotDefinition , @ins.condition.class , @ins
end
def test_label_after_check
assert_equal Label , @ins.next.class , @ins
end
def test_label_last
assert_equal Label , @ins.last.class , @ins
end
def test_array
check_array [TruthCheck, Label, SlotLoad, Label, SlotLoad, ReturnJump,
Label ,ReturnSequence, Label], @ins
end
end
end

30
test/sol/test_if_no_if.rb Normal file
View File

@ -0,0 +1,30 @@
require_relative "helper"
module Sol
class TestIfNoIf < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "unless(@a) ; @a = 5 ; end;return")
@ins = @compiler.slot_instructions.next
end
def test_condition_compiles_to_check
assert_equal TruthCheck , @ins.class , @ins
end
def test_condition_is_slot
assert_equal SlotDefinition , @ins.condition.class , @ins
end
def test_label_after_check
assert_equal Label , @ins.next.class , @ins
end
def test_label_last
assert_equal Label , @ins.last.class , @ins
end
def test_array
check_array [TruthCheck, Label, Jump, Label, SlotLoad ,
Label, SlotLoad, ReturnJump,Label, ReturnSequence, Label] , @ins
end
end
end

View File

@ -0,0 +1,30 @@
require_relative "helper"
module Sol
class TestSimpleIfSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "if(@a) ; @a = 5 ; else; @a = 6 ; end;return")
@ins = @compiler.slot_instructions.next
end
def test_condition_compiles_to_check
assert_equal TruthCheck , @ins.class , @ins
end
def test_condition_is_slot
assert_equal SlotDefinition , @ins.condition.class , @ins
end
def test_label_after_check
assert_equal Label , @ins.next.class , @ins
end
def test_label_last
assert_equal Label , @ins.last.class , @ins
end
def test_array
check_array [TruthCheck, Label, SlotLoad, Jump, Label ,
SlotLoad, Label, SlotLoad, ReturnJump,Label, ReturnSequence, Label], @ins
end
end
end

View File

@ -0,0 +1,29 @@
require_relative "helper"
module Sol
class TestConditionIfSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "if(5.div4) ; @a = 6 ; else; @a = 5 ; end;return" , "Integer.div4")
@ins = @compiler.slot_instructions.next
end
def test_condition
assert_equal TruthCheck , @ins.next(3).class
end
def test_condition_is_slot
assert_equal SlotDefinition , @ins.next(3).condition.class , @ins
end
def test_hoisted_call
assert_equal SimpleCall , @ins.next(2).class
assert_equal :div4 , @ins.next(2).method.name
end
def test_array
check_array [MessageSetup, ArgumentTransfer, SimpleCall, TruthCheck, Label ,
SlotLoad, Jump, Label, SlotLoad, Label ,
SlotLoad, ReturnJump,Label, ReturnSequence, Label] , @ins
end
end
end

32
test/sol/test_ivar.rb Normal file
View File

@ -0,0 +1,32 @@
require_relative "helper"
module Sol
class TestIvarSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "@a = 5")
@ins = @compiler.slot_instructions.next
end
def test_array
check_array [SlotLoad, SlotLoad, ReturnJump, Label, ReturnSequence ,
Label] , @ins
end
def test_class_compiles
assert_equal SlotLoad , @ins.class , @ins
assert @ins.left
assert_equal :message , @ins.left.known_object
end
def test_slot_gets_self
assert_equal :receiver , @ins.left.slots[0]
end
def test_slot_assigns_to_local
assert_equal :a , @ins.left.slots[-1]
end
def test_slot_assigns_something
assert @ins.right
assert_equal SlotMachine::IntegerConstant , @ins.right.known_object.class
end
end
end

View File

@ -0,0 +1,64 @@
require_relative "helper"
module Sol
class TestLocalSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "a = 5")
@ins = @compiler.slot_instructions.next
end
def test_compiles_not_array
assert Array != @ins.class , @ins
end
def test_class_compiles
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
end
def test_slot_is_set
assert @ins.left
end
def test_slot_starts_at_message
assert_equal :message , @ins.left.known_object
end
def test_slot_gets_local
assert_equal :local1 , @ins.left.slots[0]
end
def test_slot_assigns_something
assert @ins.right
end
def test_slot_assigns_int
assert_equal SlotMachine::IntegerConstant , @ins.right.known_object.class
end
end
class TestArgSlotMachine < MiniTest::Test
include SolCompile
def setup
Parfait.boot!(Parfait.default_test_options)
@compiler = compile_main( "arg = 5")
@ins = @compiler.slot_instructions.next
end
def test_class_compiles
assert_equal SlotMachine::SlotLoad , @ins.class , @ins
end
def test_slot_is_set
assert @ins.left
end
def test_slot_starts_at_message
assert_equal :message , @ins.left.known_object
end
def test_slot_gets_arg
assert_equal :arg1 , @ins.left.slots[0]
end
def test_slot_assigns_something
assert @ins.right
end
def test_slot_assigns_int
assert_equal SlotMachine::IntegerConstant , @ins.right.known_object.class
end
end
end

View File

@ -0,0 +1,42 @@
require_relative "helper"
module SlotMachine
class PlusEquals < Instruction
attr_reader :a , :b
def initialize(source , arg , b)
super(source)
@a = arg
@b = b
end
def to_risc(compiler)
Risc.label("some" , "thing")
end
end
end
module Sol
class TestMacroSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "X.plus_equals(arg,1)")
@ins = @compiler.slot_instructions.next
end
def test_class_compiles
assert_equal SlotMachine::PlusEquals , @ins.class , @ins
end
def test_arg1
assert_equal Sol::LocalVariable , @ins.a.class
assert_equal :arg , @ins.a.name
end
def test_arg2
assert_equal Sol::IntegerConstant , @ins.b.class
assert_equal 1 , @ins.b.value
end
def test_to_risc
comp = @compiler.to_risc
assert_equal Risc::MethodCompiler , comp.class
assert_equal :main , comp.callable.name
end
end
end

View File

@ -0,0 +1,53 @@
require_relative "helper"
module Sol
class TestMethodExpression < MiniTest::Test
include SolCompile
def setup
Parfait.boot!(Parfait.default_test_options)
ruby_tree = Ruby::RubyCompiler.compile( as_main("a = 5") )
@clazz = ruby_tree.to_sol
end
def method
@clazz.body.first
end
def test_setup
assert_equal ClassExpression , @clazz.class
assert_equal Statements , @clazz.body.class
assert_equal MethodExpression , method.class
end
def test_fail
assert_raises{ method.to_parfait }
end
def test_method
assert_equal Parfait::Class , @clazz.to_parfait.class
end
def test_creates_instance_method
main = @clazz.to_parfait.get_instance_method(:main)
assert_equal Parfait::SolMethod , main.class
assert_equal :main , main.name
end
def test_creates_type_method
clazz = @clazz.to_parfait
m = clazz.instance_type.get_method(:main)
assert m , "no type method :main"
end
end
class TestMethodExpressionDoubleDef < MiniTest::Test
include SolCompile
def setup
Parfait.boot!(Parfait.default_test_options)
ruby_tree = Ruby::RubyCompiler.compile( as_main("a = 5") + ";" + as_main("a = 5") )
@clazz = ruby_tree.to_sol
end
def method
@clazz.body.first
end
def test_no_double
assert_raises {@clazz.to_parfait}
end
end
end

56
test/sol/test_return.rb Normal file
View File

@ -0,0 +1,56 @@
require_relative "helper"
module Sol
class TestReturnSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "return 5")
@ins = @compiler.slot_instructions.next
end
def test_class_compiles
assert_equal SlotLoad , @ins.class , @ins
end
def test_slot_is_set
assert @ins.left
end
def test_two_instructions_are_returned
assert_equal 5 , @ins.length
end
def test_slot_starts_at_message
assert_equal :message , @ins.left.known_object
end
def test_slot_gets_return
assert_equal :return_value , @ins.left.slots[0]
end
def test_slot_assigns_something
assert @ins.right
end
def test_slot_assigns_int
assert_equal SlotMachine::IntegerConstant , @ins.right.known_object.class
end
def test_second_is_return
assert_equal ReturnJump, @ins.next(1).class
end
def test_array
check_array [SlotLoad, ReturnJump, Label, ReturnSequence, Label], @ins
end
end
class TestReturnSendSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "return 5.div4" , "Integer.div4" )
@ins = @compiler.slot_instructions.next
end
def test_return_is_last
assert_equal ReturnJump , @ins.next(4).class
end
def test_array
check_array [MessageSetup, ArgumentTransfer, SimpleCall, SlotLoad, ReturnJump ,
Label, ReturnSequence, Label] , @ins
end
end
end

View File

@ -0,0 +1,26 @@
require_relative "helper"
module Sol
class TestSimpleWhileSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "while(@a) ; @a = 5 ; end;return")
@ins = @compiler.slot_instructions.next
end
def test_compiles_as_while
assert_equal Label , @ins.class , @ins
end
def test_condition_compiles_to_check
assert_equal TruthCheck , @ins.next.class , @ins
end
def test_condition_is_slot
assert_equal SlotDefinition , @ins.next.condition.class , @ins
end
def test_array
check_array [Label, TruthCheck, SlotLoad, Jump, Label ,
SlotLoad, ReturnJump,Label, ReturnSequence, Label], @ins
end
end
end

View File

@ -0,0 +1,37 @@
require_relative "helper"
module Sol
class TestWhileConditionSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "while(5.div4) ; 5.div4 ; end;return" , "Integer.div4")
@ins = @compiler.slot_instructions.next
end
def test_condition_compiles_to_check
assert_equal TruthCheck , @ins.next(4).class
end
def test_condition_is_slot
assert_equal SlotDefinition , @ins.next(4).condition.class , @ins
end
def test_hoisetd
jump = @ins.next(8)
assert_kind_of Jump , jump
assert jump.label.name.start_with?("cond_label") , jump.label.name
end
def test_label
label = @ins
assert_equal Label , label.class
assert label.name.start_with?("cond_label") , label.name
end
def test_array
check_array [Label, MessageSetup, ArgumentTransfer, SimpleCall, TruthCheck ,
MessageSetup, ArgumentTransfer, SimpleCall, Jump, Label ,
SlotLoad, ReturnJump,Label, ReturnSequence, Label ,Label ,
ReturnSequence, Label] , @ins
end
end
end

View File

@ -0,0 +1,93 @@
require_relative "helper"
module Sol
class TestYieldArgsSendSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "return yield(1)" )
@ins = @compiler.slot_instructions.next
end
def test_array
check_array [NotSameCheck, Label, MessageSetup, ArgumentTransfer, BlockYield ,
SlotLoad, ReturnJump, Label, ReturnSequence , Label] , @ins
end
def test_check_label
assert_equal NotSameCheck, @ins.class
assert @ins.false_jump.name.start_with?("method_ok_") , @ins.false_jump.name
end
def test_transfer
assert_equal ArgumentTransfer, @ins.next(3).class
assert_equal 1, @ins.next(3).arguments.length
end
def test_args_one_l
left = @ins.next(3).arguments[0].left
assert_equal Symbol, left.known_object.class
assert_equal :message, left.known_object
assert_equal [:next_message, :arg1], left.slots
end
def test_check_left
assert_equal SlotDefinition, @ins.left.class
assert_equal Parfait::CallableMethod, @ins.left.known_object.class
assert_equal :main, @ins.left.known_object.name
assert @ins.left.slots.empty?
end
def test_check_right
assert_equal SlotDefinition, @ins.right.class
assert_equal :message, @ins.right.known_object
assert_equal [:method] , @ins.right.slots
end
def test_label
assert_equal Label, @ins.next(1).class
assert @ins.next(1).name.start_with?("method_ok_")
end
def test_setup
assert_equal MessageSetup, @ins.next(2).class
assert_equal 2, @ins.next(2).method_source
end
def test_receiver
assert_equal :message , @ins.next(3).receiver.known_object
assert_equal [:receiver] , @ins.next(3).receiver.slots
end
def test_yield
assert_equal BlockYield, @ins.next(4).class
assert_equal 2, @ins.next(4).arg_index
end
def test_return_load
assert_equal SlotLoad, @ins.next(5).class
assert_equal :message, @ins.next(5).left.known_object
assert_equal :message, @ins.next(5).right.known_object
assert_equal [:return_value], @ins.next(5).left.slots
assert_equal [:return_value], @ins.next(5).right.slots
end
def test_return
assert_equal ReturnJump, @ins.next(6).class
end
end
class TestYieldNoArgsSendSlotMachine < MiniTest::Test
include SolCompile
def setup
@compiler = compile_main( "return yield(some.extra.calls)" )
@ins = @compiler.slot_instructions.next
end
def test_check_label
assert_equal NotSameCheck, @ins.class
assert @ins.false_jump.name.start_with?("cache_ok_") , @ins.false_jump.name
end
def test_array
check_array [NotSameCheck, SlotLoad, ResolveMethod, Label, MessageSetup ,
ArgumentTransfer, DynamicCall, SlotLoad, NotSameCheck, SlotLoad ,
ResolveMethod, Label, MessageSetup, ArgumentTransfer, DynamicCall ,
SlotLoad, NotSameCheck, SlotLoad, ResolveMethod, Label ,
MessageSetup, ArgumentTransfer, DynamicCall, SlotLoad, NotSameCheck ,
Label, MessageSetup, ArgumentTransfer, BlockYield, SlotLoad ,
ReturnJump, Label, ReturnSequence, Label] , @ins
end
def test_transfer
assert_equal ArgumentTransfer, @ins.next(5).class
assert_equal 0, @ins.next(5).arguments.length
end
end
end