fix more expression tests

This commit is contained in:
Torsten Ruger 2016-12-10 15:18:37 +02:00
parent 98bc0f6e40
commit 4054b61752
7 changed files with 31 additions and 46 deletions

View File

@ -8,7 +8,7 @@ module Typed
clazz = Register.machine.space.get_class_by_name receiver.type clazz = Register.machine.space.get_class_by_name receiver.type
field_name = statement.field.name field_name = statement.field.name
index = clazz.instance_type.variable_index(field_name) index = clazz.instance_type.variable_index(field_name)
raise "field access, but no such field:#{field_name} for class #{clazz.name}" unless index raise "no such field:#{field_name} for class #{clazz.name}:#{clazz.instance_type.inspect}" unless index
value = use_reg(clazz.instance_type.type_at(index)) value = use_reg(clazz.instance_type.type_at(index))
add_code Register.get_slot(statement , receiver , index, value) add_code Register.get_slot(statement , receiver , index, value)

View File

@ -23,7 +23,7 @@ module Typed
private private
def handle_local statement def handle_local statement
index = @method.has_local( name ) index = @method.has_local( statement.name )
raise "must define variable '#{name}' before using it" unless index raise "must define variable '#{name}' before using it" unless index
frame = use_reg :Frame frame = use_reg :Frame
add_code Register.get_slot(statement , :message , :frame , frame ) add_code Register.get_slot(statement , :message , :frame , frame )

View File

@ -25,4 +25,9 @@ module ExpressionHelper
produced produced
end end
# test hack to in place change object type
def add_object_field(name,type)
class_type = Register.machine.space.get_class_by_name(:Object).instance_type
class_type.send(:private_add_instance_variable, name , type)
end
end end

View File

@ -1,4 +1,4 @@
require_relative "test_basic" require_relative "test_basic"
require_relative "test_field_access"
require_relative "test_ops"
#require_relative "test_call" #require_relative "test_call"
#require_relative "test_field_access"
#require_relative "test_ops"

View File

@ -6,7 +6,6 @@ module Register
def setup def setup
Register.machine.boot Register.machine.boot
@root = :call_site
@output = Register::RegisterValue @output = Register::RegisterValue
end end

View File

@ -3,6 +3,7 @@ require_relative "helper"
module Register module Register
class TestFields < MiniTest::Test class TestFields < MiniTest::Test
include ExpressionHelper include ExpressionHelper
include AST::Sexp
def setup def setup
Register.machine.boot Register.machine.boot
@ -10,49 +11,25 @@ module Register
def test_field_not_defined def test_field_not_defined
@root = :field_access @root = :field_access
@input = <<HERE @input = s(:field_access, s(:receiver, s(:name, :self)), s(:field, s(:name, :a)))
self.a
HERE
assert_raises(RuntimeError) { check } assert_raises(RuntimeError) { check }
end end
def test_field_not_space def test_field_not_space
@root = :field_access @root = :field_access
@input = <<HERE @input = s(:field_access,
self.space s(:receiver,
HERE s(:name, :self)),
s(:field,
s(:name, :space)))
assert_raises(RuntimeError) { check } assert_raises(RuntimeError) { check }
end end
def test_field def test_field
Register.machine.space.get_class_by_name(:Object).instance_type.add_instance_variable(:bro,:Object) add_object_field(:bro,:Object)
@root = :field_access @root = :field_access
@input = <<HERE @input = s(:field_access, s(:receiver, s(:name, :self)), s(:field, s(:name, :bro)))
self.bro
HERE
@output = Register::RegisterValue
check
end
def test_local
Register.machine.space.get_main.ensure_local(:bar , :Integer)
@root = :name
@input = 'bar '
@output = Register::RegisterValue
check
end
def test_space
@root = :name
@input = 'space '
@output = Register::RegisterValue
check
end
def test_args
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :bar)
@root = :name
@input = 'bar '
@output = Register::RegisterValue @output = Register::RegisterValue
check check
end end

View File

@ -3,6 +3,7 @@ require_relative "helper"
module Register module Register
class TestOps < MiniTest::Test class TestOps < MiniTest::Test
include ExpressionHelper include ExpressionHelper
include AST::Sexp
def setup def setup
Register.machine.boot Register.machine.boot
@ -11,34 +12,37 @@ module Register
end end
def operators def operators
["+" , "-" , "*" , "/" , "=="] [:+ , :- , :* , :/ , :== ]
end end
def test_ints def test_ints
operators.each do |op| operators.each do |op|
@input = '2 + 3'.sub("+" , op) @input = s(:operator_value, op , s(:int, 2), s(:int, 3))
check check
end end
end end
def test_local_int def test_local_int
Register.machine.space.get_main.ensure_local(:bar , :Integer) Register.machine.space.get_main.ensure_local(:bar , :Integer)
@input = 'bar + 3' @input = s(:operator_value, :+, s(:name, :bar), s(:int, 3))
check check
end end
def test_int_local def test_int_local
Register.machine.space.get_main.ensure_local(:bar , :Integer) Register.machine.space.get_main.ensure_local(:bar , :Integer)
@input = '3 + bar' @input = s(:operator_value, :+, s(:int, 3), s(:name, :bar))
check check
end end
def test_field_int def test_field_int
Register.machine.space.get_class_by_name(:Object).instance_type.add_instance_variable(:bro,:int) add_object_field(:bro,:int)
@input = "self.bro + 3" @input = s(:operator_value, :+, s(:field_access, s(:receiver, s(:name, :self)),
s(:field, s(:name, :bro))),
s(:int, 3))
check check
end end
def test_int_field def test_int_field
Register.machine.space.get_class_by_name(:Object).instance_type.add_instance_variable(:bro,:int) add_object_field(:bro,:int)
@input = "3 + self.bro" @input = s(:operator_value, :+, s(:int, 3), s(:field_access, s(:receiver, s(:name, :self)),
s(:field,s(:name, :bro))))
check check
end end
end end