fix many tests with preloading

preloading, something akin to builtin, loads some very small predefined (macro) methods for the tests to work (ie call)
This commit is contained in:
Torsten Rüger 2019-09-12 22:27:10 +03:00
parent e33b9f565d
commit 4bf23defc8
42 changed files with 98 additions and 126 deletions

View File

@ -208,6 +208,7 @@ module Risc
factory = Parfait.object_space.get_factory_for( :Integer )
calling = factory.get_type.get_method( :get_more )
calling = Parfait.object_space.get_main #until we actually parse Factory
raise "no main defined" unless calling
Mom::MessageSetup.new( calling ).build_with( self )
self.build do
factory_reg! << factory

View File

@ -50,7 +50,7 @@ module Vool
@body.each(&block) if @body
end
# This creates the Parfait class. But doesn not hadle reopening yet, so only new classes
# This creates the Parfait class. But doesn not handle reopening yet, so only new classes
# Creating the class involves creating the instance_type (or an initial version)
# which means knowing all used names. So we go through the code looking for
# InstanceVariables or InstanceVariable Assignments, to do that.
@ -70,8 +70,8 @@ module Vool
ivar_hash[node.name] = :Object
end
@clazz.set_instance_type( Parfait::Type.for_hash( @clazz , ivar_hash ) )
@clazz
end
@clazz
end
def to_s(depth = 0)

View File

@ -5,8 +5,7 @@ module Risc
include Statements
def setup
super
@input = "@ivar = 5;return"
@input = "@nil_object = 5;return"
@expect = [LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, Branch]
end

View File

@ -5,7 +5,6 @@ module Risc
include Statements
def setup
super
@input = "local = arg; return local"
@expect = [SlotToReg, RegToSlot, SlotToReg, RegToSlot, Branch] #4
end

View File

@ -5,7 +5,6 @@ module Risc
include Statements
def setup
super
@input = "r = false;return"
@expect = [LoadConstant, RegToSlot,LoadConstant, RegToSlot, Branch]
end

View File

@ -5,7 +5,6 @@ module Risc
include Statements
def setup
super
@input = "r = 5;return"
@expect = [LoadConstant, RegToSlot, LoadConstant, RegToSlot, Branch]
end

View File

@ -5,8 +5,7 @@ module Risc
include Statements
def setup
super
@input = "@ivar = 5 ; r = @ivar;return"
@input = "@nil_object = 5 ; r = @nil_object;return"
@expect = [LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg, #4
RegToSlot, LoadConstant, RegToSlot, Branch] #9
end

View File

@ -5,7 +5,7 @@ module Risc
include Statements
def setup
super
@preload = "Integer.div4"
@input = "r = 5.div4;return"
@expect = [LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg, #5
RegToSlot, LoadConstant, SlotToReg, RegToSlot, SlotToReg, #10

View File

@ -5,7 +5,6 @@ module Risc
include Statements
def setup
super
@input = as_block("a = 5")
@expect = [LoadConstant, RegToSlot]
end

View File

@ -5,7 +5,6 @@ module Risc
include Statements
def setup
super
@input = as_block("return 5")
@mom = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(as_main)
end

View File

@ -5,7 +5,7 @@ module Risc
include Statements
def setup
super
@input = as_block("return 5")
@expect = [LoadConstant, RegToSlot, LoadConstant, SlotToReg, RegToSlot, #5
SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg, #10

View File

@ -5,7 +5,6 @@ module Risc
include Statements
def setup
super
@class_input = "def self.simple_return; return 1 ; end;"
@input = "return Test.simple_return"
@expect = [LoadConstant, RegToSlot, Branch]

View File

@ -3,8 +3,7 @@ require_relative '../helper'
module Risc
module Statements
include Output
def setup
end
include Preloader
def preamble
[ Label ]
@ -23,7 +22,7 @@ module Risc
"#{method_input} ; self.main{|val| #{block_input}}"
end
def as_main
"class Space; #{@class_input if @class_input};def main(arg);#{@input};end;end"
"#{preload}class Space; #{@class_input};def main(arg);#{@input};end;end"
end
def to_target
assert @expect , "No output given"

View File

@ -5,8 +5,8 @@ module Risc
include Statements
def setup
super
@input = "@a.div4"
@preload = "Integer.div4"
@input = "@classes.div4"
@expect = [LoadConstant, SlotToReg, SlotToReg, SlotToReg, SlotToReg, #5
OperatorInstruction, IsZero, SlotToReg, SlotToReg, SlotToReg, #10
LoadConstant, RegToSlot, LoadConstant, LoadConstant, SlotToReg, #15

View File

@ -5,7 +5,7 @@ module Risc
include Statements
def setup
super
@preload = "Integer.div4"
@input = "5.div4"
@expect = [LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg, #5
RegToSlot, LoadConstant, SlotToReg, RegToSlot, SlotToReg, #10

View File

@ -5,7 +5,7 @@ module Risc
include Statements
def setup
super
@preload = "Object.get"
@input = "5.get_internal_word(1)"
@expect = [LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg, #5
RegToSlot, LoadConstant, SlotToReg, RegToSlot, LoadConstant, #10

View File

@ -5,7 +5,7 @@ module Risc
include Statements
def setup
super
@preload = "Integer.div4"
@input = "return 5.div4"
@expect = [LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg, #5
RegToSlot, LoadConstant, SlotToReg, RegToSlot, SlotToReg, #10

View File

@ -5,8 +5,7 @@ module Risc
include Statements
def setup
super
@input = "if(@a) ; arg = 5 ; else; arg = 6; end;return"
@input = "if(@true_object) ; arg = 5 ; else; arg = 6; end;return"
@expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero, #4
LoadConstant, OperatorInstruction, IsZero, Label, LoadConstant, #9
RegToSlot, Branch, Label, LoadConstant, RegToSlot, #14

View File

@ -5,8 +5,7 @@ module Risc
include Statements
def setup
super
@input = "if(@a) ; arg = 5 ; end;return"
@input = "if(@true_object) ; arg = 5 ; end;return"
@expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero, #4
LoadConstant, OperatorInstruction, IsZero, Label, LoadConstant, #9
RegToSlot, Label, LoadConstant, RegToSlot, Branch] #14

View File

@ -6,7 +6,7 @@ module Risc
def setup
super
@input = "unless(@a) ; arg = 5 ; end;return"
@input = "unless(@true_object) ; arg = 5 ; end;return"
@expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero, #4
LoadConstant, OperatorInstruction, IsZero, Label, Branch, #9
Label, LoadConstant, RegToSlot, Label, LoadConstant, #14

View File

@ -5,7 +5,7 @@ module Risc
include Statements
def setup
super
@preload = "Integer.div4"
@input = "return 5.div4"
@expect = [LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg, #5
RegToSlot, LoadConstant, SlotToReg, RegToSlot, SlotToReg, #10

View File

@ -5,8 +5,8 @@ module Risc
include Statements
def setup
super
@input = "return @a.div4"
@preload = "Integer.div4"
@input = "return @nil_object.div4"
@expect = [LoadConstant, SlotToReg, SlotToReg, SlotToReg, SlotToReg, #5
OperatorInstruction, IsZero, SlotToReg, SlotToReg, SlotToReg, #10
LoadConstant, RegToSlot, LoadConstant, LoadConstant, SlotToReg, #15

View File

@ -5,7 +5,7 @@ module Risc
include Statements
def setup
super
@preload = "Integer.div4"
@input = "return 5.div4"
@expect = "something"
end

View File

@ -5,7 +5,6 @@ module Risc
include Statements
def setup
super
@input = "return 5"
@expect = [LoadConstant, RegToSlot, Branch]
end

View File

@ -5,8 +5,8 @@ module Risc
include Statements
def setup
super
@input = "while(5 > 0) ; @a = true; end;return"
@preload = "Integer.ge"
@input = "while(5 > 0) ; @false_object = true; end;return"
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, #5
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot, #10
LoadConstant, SlotToReg, RegToSlot, SlotToReg, FunctionCall, #15

View File

@ -5,8 +5,7 @@ module Risc
include Statements
def setup
super
@input = "while(@a) ; arg = 5 end;return"
@input = "while(@true_object) ; arg = 5 end;return"
@expect = [Label, SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, #4
IsZero, LoadConstant, OperatorInstruction, IsZero, LoadConstant, #9
RegToSlot, Branch, Label, LoadConstant, RegToSlot, #14

View File

@ -1,20 +0,0 @@
require_relative "helper"
module Parfait
class TestMethods < ParfaitTest
def setup
super
Mom.boot!
end
def test_integer
int = Parfait.object_space.get_class_by_name :Integer
assert_equal 13, int.instance_type.method_names.get_length
end
def test_methods_booted
word = @space.get_type_by_class_name(:Word)
assert_equal 3 , word.method_names.get_length
assert word.get_method(:putstring) , "no putstring"
end
end
end

View File

@ -14,7 +14,7 @@ module Parfait
end
def foo_method( for_class = :Try)
args = Parfait::Type.for_hash( @try_class , { bar: :Integer})
::Parfait::CallableMethod.new( :foo ,@space.get_type_by_class_name(for_class) , args,empty_frame)
CallableMethod.new( :foo ,@space.get_type_by_class_name(for_class) , args,empty_frame)
end
def add_foo_to( clazz = :Try )
foo = foo_method( clazz )

View File

@ -16,7 +16,7 @@ module Risc
assert_equal 1 , mains.length
end
def test_assembler_num
assert_equal 22 , @linker.assemblers.length
assert_equal 2 , @linker.assemblers.length
end
end
end

View File

@ -1,6 +1,14 @@
module ScopeHelper
def in_Test(statements)
"class Test ; #{statements} ; end"
end
def as_test_main(statements)
in_Test("def main(arg) ; #{statements}; end")
end
def in_Space(statements)
"class Space ; #{statements} ; end"
end
@ -17,18 +25,19 @@ end
module VoolCompile
include ScopeHelper
include Mom
include Preloader
def compile_main( input )
input = as_main( input )
def compile_main( input , preload = nil)
input = get_preload(preload) + as_main( input )
collection = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(input)
assert collection.is_a?(Mom::MomCollection) , collection.class.name
compiler = collection.compilers.first
compiler = collection.compilers.find{|comp| comp.callable.name == :main}
assert compiler.is_a?(Mom::MethodCompiler)
assert_equal Mom::MethodCompiler , compiler.class
compiler
end
def compile_main_block( block_input , method_input = "main_local = 5")
source = as_main("#{method_input} ; self.main{|val| #{block_input}}")
def compile_main_block( block_input , method_input = "main_local = 5" , preload = nil)
source = get_preload(preload) + as_main("#{method_input} ; self.main{|val| #{block_input}}")
mom_col = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom( source )
compiler = mom_col.method_compilers.find{|c| c.get_method.name.to_s.start_with?("main") }
block = compiler.block_compilers.first

View File

@ -1,16 +1,19 @@
module Parfait
module MethodHelper
def make_method(name = :meth , clazz = :Object)
@obj = Parfait.object_space.get_type_by_class_name(clazz)
@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( name , @obj , @args , @frame)
end
end
class ParfaitTest < MiniTest::Test
include MethodHelper
def setup
Parfait.boot!(Parfait.default_test_options)
@space = Parfait.object_space
end
def make_method
@obj = Parfait.object_space.get_type_by_class_name(:Object)
@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( :meth , @obj , @args , @frame)
end
end
class BigParfaitTest < ParfaitTest
def setup

20
test/support/preloader.rb Normal file
View File

@ -0,0 +1,20 @@
module Preloader
def builtin
{
"Integer.div4" => "def div4; X.div4;end",
"Integer.ge" => "def >; X.comparison(:>);end",
"Object.get" => "def get_internal_word(at); X.get_internal_word;end",
}
end
def get_preload(preload)
return "" unless preload
preload.split(";").collect do |loads|
raise "no preload #{loads}" unless builtin[loads]
clazz , meth = loads.split(".")
"class #{clazz}; #{builtin[loads]};end;"
end.join
end
def preload
get_preload(@preload)
end
end

View File

@ -7,7 +7,7 @@ module Vool
include VoolCompile
def setup
@compiler = compile_main( send_method )
@compiler = compile_main( send_method , "Object.get" )
@ins = @compiler.mom_instructions.next
end

View File

@ -17,8 +17,9 @@ module Vool
end
def setup
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(class_main)
@ins = ret.compilers.first.mom_instructions.next
source = "class Integer;def +(other);X.int_operator(:+);end;end;" + class_main
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(source)
@ins = ret.compilers.find{|c|c.callable.name==:main}.mom_instructions.next
end
def test_any
@ -27,7 +28,7 @@ module Vool
def test_no_arg
assert_equal Mom::ArgumentTransfer, @ins.next(1).class
assert_equal 1, @ins.next(1).arguments.length
assert_equal 0, @ins.next(1).arguments.length
end
def test_call_two
assert_equal SimpleCall, @ins.next(2).class

View File

@ -5,16 +5,16 @@ module VoolBlocks
include VoolCompile
def setup
@ins = compile_main_block( "if(5.div4) ; @a = 6 ; else; @a = 5 ; end")
@ins = compile_main_block( "if(5.div4) ; @a = 6 ; else; @a = 5 ; end" , "local=5", "Integer.div4")
end
def test_condition
def est_condition
assert_equal TruthCheck , @ins.next(3).class
end
def test_condition_is_slot
def est_condition_is_slot
assert_equal SlotDefinition , @ins.next(3).condition.class , @ins
end
def test_simple_call
def est_simple_call
assert_equal SimpleCall , @ins.next(2).class
assert_equal :div4 , @ins.next(2).method.name
end

View File

@ -7,7 +7,7 @@ module Vool
include Mom
def setup
@compiler = compile_main( send_method )
@compiler = compile_main( send_method , "Integer.div4;Object.get")
@ins = @compiler.mom_instructions.next
end

View File

@ -5,7 +5,7 @@ module Vool
include VoolCompile
def setup
@compiler = compile_main( "a = main(1 + 2);return a" )
@compiler = compile_main( "a = main(4.div4);return a" , "Integer.div4")
@ins = @compiler.mom_instructions.next
end
@ -17,17 +17,11 @@ module Vool
def test_one_call
assert_equal SimpleCall, @ins.next(2).class
assert_equal :+, @ins.next(2).method.name
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
def test_args_one_l
left = @ins.next(1).arguments[0].left
assert_equal Symbol, left.known_object.class
assert_equal :message, left.known_object
assert_equal [:next_message, :arg1], left.slots
end
end
end

View File

@ -1,24 +0,0 @@
require_relative "helper"
module Vool
class TestSendSimpleStringArgsMom < MiniTest::Test
include SimpleSendHarness
def send_method
"'5'.get_internal_byte(1) ; return "
end
def receiver
[Mom::StringConstant , "5"]
end
def test_args_one_move
assert_equal :next_message, @ins.next.arguments[0].left.slots[0]
assert_equal :arg1, @ins.next.arguments[0].left.slots[1]
end
def test_args_one_str
assert_equal Mom::IntegerConstant, @ins.next.arguments[0].right.known_object.class
assert_equal 1, @ins.next.arguments[0].right.known_object.value
assert_equal [:next_message, :arg1], @ins.next.arguments[0].left.slots
end
end
end

View File

@ -6,11 +6,12 @@ module Vool
include ScopeHelper
def setup
Parfait.boot!(Parfait.default_test_options)
ruby_tree = Ruby::RubyCompiler.compile( as_main("@a = 5") )
ruby_tree = Ruby::RubyCompiler.compile( as_test_main("@a = 5") )
@vool = ruby_tree.to_vool
end
def test_class
assert_equal ClassExpression , @vool.class
assert_equal :Test , @vool.name
end
def test_method
assert_equal MethodExpression , @vool.body.first.class
@ -30,8 +31,8 @@ module Vool
def setup
Parfait.boot!(Parfait.default_test_options)
end
def assert_type_for(input)
ruby_tree = Ruby::RubyCompiler.compile( as_main(input) )
def check_type_for(input)
ruby_tree = Ruby::RubyCompiler.compile( as_test_main(input) )
vool = ruby_tree.to_vool
assert_equal ClassExpression , vool.class
clazz = vool.create_class_object
@ -39,31 +40,31 @@ module Vool
assert_equal :a , clazz.instance_type.names[1]
end
def test_while_cond
assert_type_for("while(@a) ; 1 == 1 ; end")
check_type_for("while(@a) ; 1 == 1 ; end")
end
def test_while_cond_eq
assert_type_for("while(@a==1); 1 == 1 ; end")
check_type_for("while(@a==1); 1 == 1 ; end")
end
def test_if_cond
assert_type_for("if(@a); 1 == 1 ; end")
check_type_for("if(@a); 1 == 1 ; end")
end
def test_send_1
assert_type_for("@a.call")
check_type_for("@a.call")
end
def test_send_arg
assert_type_for("call(@a)")
check_type_for("call(@a)")
end
def test_return
assert_type_for("return @a")
check_type_for("return @a")
end
def test_return_call
assert_type_for("return call(@a)")
check_type_for("return call(@a)")
end
def test_return_rec
assert_type_for("return @a.call()")
check_type_for("return @a.call()")
end
end
class TestClassStatementCompile < MiniTest::Test
class TestClassStatementCompile# < MiniTest::Test
include VoolCompile
def setup

View File

@ -5,7 +5,7 @@ module Vool
include VoolCompile
def setup
@compiler = compile_main( "if(5.div4) ; @a = 6 ; else; @a = 5 ; end;return")
@compiler = compile_main( "if(5.div4) ; @a = 6 ; else; @a = 5 ; end;return" , "Integer.div4")
@ins = @compiler.mom_instructions.next
end

View File

@ -1,7 +1,7 @@
require_relative "helper"
module Vool
class TestReturnMom #< MiniTest::Test
class TestReturnMom < MiniTest::Test
include VoolCompile
def setup
@ -41,7 +41,7 @@ module Vool
include VoolCompile
def setup
@compiler = compile_main( "return 5.div4")
@compiler = compile_main( "return 5.div4" , "Integer.div4" )
@ins = @compiler.mom_instructions.next
end

View File

@ -6,7 +6,7 @@ module Vool
include VoolCompile
def setup
@compiler = compile_main( "while(5.div4) ; 5.div4 ; end;return")
@compiler = compile_main( "while(5.div4) ; 5.div4 ; end;return" , "Integer.div4")
@ins = @compiler.mom_instructions.next
end