add basic compiler expression tests (back)
This commit is contained in:
parent
5c2f545f8e
commit
de5d87cde7
@ -15,24 +15,24 @@ end
|
|||||||
|
|
||||||
AST::Node.class_eval do
|
AST::Node.class_eval do
|
||||||
|
|
||||||
def [](name)
|
# def [](name)
|
||||||
#puts self.inspect
|
# #puts self.inspect
|
||||||
children.each do |child|
|
# children.each do |child|
|
||||||
if child.is_a?(AST::Node)
|
# if child.is_a?(AST::Node)
|
||||||
#puts child.type
|
# #puts child.type
|
||||||
if (child.type == name)
|
# if (child.type == name)
|
||||||
return child.children
|
# return child.children
|
||||||
end
|
# end
|
||||||
else
|
# else
|
||||||
#puts child.class
|
# #puts child.class
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
nil
|
# nil
|
||||||
end
|
# end
|
||||||
|
#
|
||||||
def first_from( node_name )
|
# def first_from( node_name )
|
||||||
from = self[node_name]
|
# from = self[node_name]
|
||||||
return nil unless from
|
# return nil unless from
|
||||||
from.first
|
# from.first
|
||||||
end
|
# end
|
||||||
end
|
end
|
||||||
|
@ -1,52 +0,0 @@
|
|||||||
require_relative "helper"
|
|
||||||
|
|
||||||
|
|
||||||
class TestBasic < MiniTest::Test
|
|
||||||
include ExpressionHelper
|
|
||||||
|
|
||||||
def setup
|
|
||||||
@root = :basic_type
|
|
||||||
@output = Register::RegisterValue
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_number
|
|
||||||
@string_input = '42 '
|
|
||||||
assert_equal 42 , check.value
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_true
|
|
||||||
@string_input = 'true'
|
|
||||||
check
|
|
||||||
end
|
|
||||||
def test_false
|
|
||||||
@string_input = 'false '
|
|
||||||
check
|
|
||||||
end
|
|
||||||
def test_nil
|
|
||||||
@string_input = 'nil '
|
|
||||||
check
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_var
|
|
||||||
@string_input = 'int foo '
|
|
||||||
@root = :field_def
|
|
||||||
@output = NilClass
|
|
||||||
check
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_self
|
|
||||||
@string_input = 'self '
|
|
||||||
check
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_space
|
|
||||||
@string_input = 'self '
|
|
||||||
check
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_string
|
|
||||||
@string_input = "\"hello\""
|
|
||||||
check
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
@ -1,5 +1,4 @@
|
|||||||
require_relative '../../helper'
|
require_relative '../helper'
|
||||||
require 'parslet/convenience'
|
|
||||||
|
|
||||||
Typed::Compiler.class_eval do
|
Typed::Compiler.class_eval do
|
||||||
def set_main main
|
def set_main main
|
||||||
@ -17,17 +16,12 @@ module ExpressionHelper
|
|||||||
def check
|
def check
|
||||||
machine = Register.machine
|
machine = Register.machine
|
||||||
machine.boot unless machine.booted
|
machine.boot unless machine.booted
|
||||||
parser = Parser::Salama.new
|
|
||||||
parser = parser.send @root
|
|
||||||
syntax = parser.parse_with_debug(@string_input, reporter: Parslet::ErrorReporter::Deepest.new)
|
|
||||||
parts = Parser::Transform.new.apply(syntax)
|
|
||||||
codes = Soml.ast_to_code parts
|
|
||||||
#puts parts.inspect
|
|
||||||
compiler = Typed::Compiler.new
|
compiler = Typed::Compiler.new
|
||||||
set_main(compiler)
|
set_main(compiler)
|
||||||
produced = compiler.process( codes )
|
code = Typed.ast_to_code @input
|
||||||
|
produced = compiler.process( code )
|
||||||
assert @output , "No output given"
|
assert @output , "No output given"
|
||||||
assert_equal produced.class, @output , "Wrong class"
|
assert_equal produced.class , @output , "Wrong class"
|
||||||
produced
|
produced
|
||||||
end
|
end
|
||||||
|
|
38
test/typed/expressions/test_basic.rb
Normal file
38
test/typed/expressions/test_basic.rb
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
|
||||||
|
class TestBasic < MiniTest::Test
|
||||||
|
include ExpressionHelper
|
||||||
|
include AST::Sexp
|
||||||
|
|
||||||
|
def setup
|
||||||
|
@output = Register::RegisterValue
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_number
|
||||||
|
@input = s(:int , 42)
|
||||||
|
assert_equal 42 , check.value
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_true
|
||||||
|
@input = s(:true)
|
||||||
|
check
|
||||||
|
end
|
||||||
|
def test_false
|
||||||
|
@input = s(:false)
|
||||||
|
check
|
||||||
|
end
|
||||||
|
def test_nil
|
||||||
|
@input = s(:nil)
|
||||||
|
check
|
||||||
|
end
|
||||||
|
def test_self
|
||||||
|
@input = s(:name, :self)
|
||||||
|
check
|
||||||
|
end
|
||||||
|
def test_string
|
||||||
|
@input = s(:string , "hello")
|
||||||
|
check
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -11,28 +11,28 @@ module Register
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_call_main_plain
|
def test_call_main_plain
|
||||||
@string_input = 'main()'
|
@input = 'main()'
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_call_main_int
|
def test_call_main_int
|
||||||
@string_input = 'main(1)'
|
@input = 'main(1)'
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_call_main_string
|
def test_call_main_string
|
||||||
@string_input = 'main("1")'
|
@input = 'main("1")'
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_call_main_op
|
def test_call_main_op
|
||||||
Register.machine.space.get_main.ensure_local(:bar , :Integer)
|
Register.machine.space.get_main.ensure_local(:bar , :Integer)
|
||||||
@string_input = 'main( bar )'
|
@input = 'main( bar )'
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_call_string_put
|
def test_call_string_put
|
||||||
@string_input = '"Hello Raisa, I am salama".putstring()'
|
@input = '"Hello Raisa, I am salama".putstring()'
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
@ -10,7 +10,7 @@ module Register
|
|||||||
|
|
||||||
def test_field_not_defined
|
def test_field_not_defined
|
||||||
@root = :field_access
|
@root = :field_access
|
||||||
@string_input = <<HERE
|
@input = <<HERE
|
||||||
self.a
|
self.a
|
||||||
HERE
|
HERE
|
||||||
assert_raises(RuntimeError) { check }
|
assert_raises(RuntimeError) { check }
|
||||||
@ -18,7 +18,7 @@ HERE
|
|||||||
|
|
||||||
def test_field_not_space
|
def test_field_not_space
|
||||||
@root = :field_access
|
@root = :field_access
|
||||||
@string_input = <<HERE
|
@input = <<HERE
|
||||||
self.space
|
self.space
|
||||||
HERE
|
HERE
|
||||||
assert_raises(RuntimeError) { check }
|
assert_raises(RuntimeError) { check }
|
||||||
@ -27,7 +27,7 @@ HERE
|
|||||||
def test_field
|
def test_field
|
||||||
Register.machine.space.get_class_by_name(:Object).instance_type.add_instance_variable(:bro,:Object)
|
Register.machine.space.get_class_by_name(:Object).instance_type.add_instance_variable(:bro,:Object)
|
||||||
@root = :field_access
|
@root = :field_access
|
||||||
@string_input = <<HERE
|
@input = <<HERE
|
||||||
self.bro
|
self.bro
|
||||||
HERE
|
HERE
|
||||||
@output = Register::RegisterValue
|
@output = Register::RegisterValue
|
||||||
@ -37,14 +37,14 @@ HERE
|
|||||||
def test_local
|
def test_local
|
||||||
Register.machine.space.get_main.ensure_local(:bar , :Integer)
|
Register.machine.space.get_main.ensure_local(:bar , :Integer)
|
||||||
@root = :name
|
@root = :name
|
||||||
@string_input = 'bar '
|
@input = 'bar '
|
||||||
@output = Register::RegisterValue
|
@output = Register::RegisterValue
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_space
|
def test_space
|
||||||
@root = :name
|
@root = :name
|
||||||
@string_input = 'space '
|
@input = 'space '
|
||||||
@output = Register::RegisterValue
|
@output = Register::RegisterValue
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
@ -52,7 +52,7 @@ HERE
|
|||||||
def test_args
|
def test_args
|
||||||
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :bar)
|
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :bar)
|
||||||
@root = :name
|
@root = :name
|
||||||
@string_input = 'bar '
|
@input = 'bar '
|
||||||
@output = Register::RegisterValue
|
@output = Register::RegisterValue
|
||||||
check
|
check
|
||||||
end
|
end
|
@ -15,30 +15,30 @@ module Register
|
|||||||
end
|
end
|
||||||
def test_ints
|
def test_ints
|
||||||
operators.each do |op|
|
operators.each do |op|
|
||||||
@string_input = '2 + 3'.sub("+" , op)
|
@input = '2 + 3'.sub("+" , op)
|
||||||
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)
|
||||||
@string_input = 'bar + 3'
|
@input = 'bar + 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)
|
||||||
@string_input = '3 + bar'
|
@input = '3 + 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)
|
Register.machine.space.get_class_by_name(:Object).instance_type.add_instance_variable(:bro,:int)
|
||||||
@string_input = "self.bro + 3"
|
@input = "self.bro + 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)
|
Register.machine.space.get_class_by_name(:Object).instance_type.add_instance_variable(:bro,:int)
|
||||||
@string_input = "3 + self.bro"
|
@input = "3 + self.bro"
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
x
Reference in New Issue
Block a user