introduces compile time type (ct_type)

to determine whether we can call directly
This commit is contained in:
Torsten Ruger 2017-04-19 20:59:13 +03:00
parent d2fba19b95
commit 3e282c083d
8 changed files with 130 additions and 12 deletions

View File

@ -30,6 +30,9 @@ module Vool
raise "Not implemented for #{self}"
end
def ct_type
nil
end
# create corresponding parfait objects, ie classes, types, methods
# mainly implemented by class/method statement
def create_objects

View File

@ -4,18 +4,33 @@ module Vool
def initialize(value)
@value = value
end
def ct_type
Parfait.object_space.get_class_by_name(:Integer).instance_type
end
end
class FloatStatement < Statement
attr_reader :value
def initialize(value)
@value = value
end
def ct_type
true
end
end
class TrueStatement < Statement
def ct_type
Parfait.object_space.get_class_by_name(:True).instance_type
end
end
class FalseStatement < Statement
def ct_type
Parfait.object_space.get_class_by_name(:False).instance_type
end
end
class NilStatement < Statement
def ct_type
Parfait.object_space.get_class_by_name(:Nil).instance_type
end
end
class SelfStatement < Statement
end
@ -26,7 +41,13 @@ module Vool
def initialize(value)
@value = value
end
def ct_type
Parfait.object_space.get_class_by_name(:Word).instance_type
end
end
class SymbolStatement < StringStatement
def ct_type
Parfait.object_space.get_class_by_name(:Word).instance_type
end
end
end

View File

@ -41,24 +41,21 @@ module Vool
pops = [Mom::SlotConstant.new([:message , :next_message , :receiver] , @receiver) ]
@arguments.each_with_index do |arg , index|
arg_target = [:message , :next_message , :arguments]
pops << Mom::SlotConstant.new( arg_target << index , arg)
pops << Mom::SlotConstant.new( arg_target + [index] , arg)
end
pops
end
def call_instruction
if(receiver_type)
if(@receiver.ct_type)
simple_call
else
cached_call
end
end
def receiver_type
Parfait.object_space.get_class_by_name(:Integer).instance_type
end
def simple_call
type = receiver_type
type = @receiver.ct_type
method = type.get_method(@name)
[Mom::SimpleCall.new( method) ]
end

View File

@ -46,4 +46,32 @@ module Vool
assert_equal 1 , lst.statements.first.value
end
end
class TestBasicTypes < MiniTest::Test
def setup
Risc.machine.boot
end
def compile( input )
lst = RubyCompiler.compile( input )
lst.ct_type
end
def test_integer
assert_equal "Integer_Type" , compile( "123").name
end
def test_string
assert_equal "Word_Type" , compile( "'string'").name
end
def test_sym
assert_equal "Word_Type" , compile( ":symbol").name
end
# classes fot these are not implemented in parfait yet
# def pest_nil
# assert_equal "Nil_Type" , compile( "nil").name
# end
# def pest_false
# assert_equal "False_Type" , compile( "false").name
# end
# def pest_true
# assert_equal "True_Type" , compile( "true").name
# end
end
end

View File

@ -58,6 +58,22 @@ module Vool
lst = RubyCompiler.compile( "super(1)")
assert_nil lst.name
end
end
class TestSendReceiverType < MiniTest::Test
def setup
Risc.machine.boot
end
def test_int_receiver
sent = RubyCompiler.compile( "5.mod4")
assert_equal Parfait::Type , sent.receiver.ct_type.class
assert_equal "Integer_Type" , sent.receiver.ct_type.name
end
def test_string_receiver
sent = RubyCompiler.compile( "'5'.putstring")
assert_equal Parfait::Type , sent.receiver.ct_type.class
assert_equal "Word_Type" , sent.receiver.ct_type.name
end
end
end

View File

@ -18,12 +18,16 @@ module Vool
def test_two_instructions_are_returned
assert_equal 2 , @stats.length
end
def test_receiver_class
def test_receiver_move_class
assert_equal Mom::SlotConstant, @stats.first.class
end
def test_receiver_move
assert_equal :receiver, @stats.first.left[2]
end
def test_receiver
assert_equal IntegerStatement, @stats.first.right.class
assert_equal 5, @stats.first.right.value
end
def test_call_is
assert_equal Mom::SimpleCall, @stats[1].class
end

View File

@ -15,22 +15,27 @@ module Vool
def test_four_instructions_are_returned
assert_equal 4 , @stats.length
end
def test_receiver_class
assert_equal Mom::SlotConstant, @stats.first.class
end
def test_receiver_move
assert_equal Mom::SlotConstant, @stats[0].class
assert_equal :receiver, @stats[0].left[2]
end
def test_receiver
assert_equal IntegerStatement, @stats[0].right.class
assert_equal 5, @stats[0].right.value
end
def test_args_one_move
assert_equal :next_message, @stats[1].left[1]
assert_equal :arguments, @stats[1].left[2]
assert_equal 0 , @stats[1].left[3]
end
def test_args_one_int
assert_equal IntegerStatement, @stats[1].right.class
assert_equal 1, @stats[1].right.value
end
def test_args_two_move
assert_equal :next_message, @stats[2].left[1]
assert_equal :arguments, @stats[2].left[2]
assert_equal 1 , @stats[2].left[3]
end
def test_args_two_int
assert_equal IntegerStatement, @stats[2].right.class

View File

@ -0,0 +1,44 @@
require_relative "../helper"
module Vool
class TestSendSimpleStringArgsMom < MiniTest::Test
include MomCompile
def setup
Risc.machine.boot
@stats = compile_first_method( "'5'.get_internal_byte(1)").first
end
def test_class_compiles
assert_equal Mom::SlotConstant , @stats.first.class , @stats
end
def test_four_instructions_are_returned
assert_equal 3 , @stats.length
end
def test_receiver_move
assert_equal Mom::SlotConstant, @stats.first.class
assert_equal :receiver, @stats[0].left[2]
end
def test_receiver
assert_equal StringStatement, @stats[0].right.class
assert_equal "5", @stats[0].right.value
end
def test_args_one_move
assert_equal :next_message, @stats[1].left[1]
assert_equal :arguments, @stats[1].left[2]
end
def test_args_one_int
assert_equal IntegerStatement, @stats[1].right.class
assert_equal 1, @stats[1].right.value
end
def test_call_is
assert_equal Mom::SimpleCall, @stats[2].class
end
def test_call_has_method
assert_equal Parfait::TypedMethod , @stats[2].method.class
end
def test_call_has_right_method
assert_equal :get_internal_byte, @stats[2].method.name
end
end
end