2014-04-24 17:38:06 +03:00
|
|
|
require_relative "helper"
|
2014-04-24 15:43:20 +03:00
|
|
|
|
2014-04-28 22:22:37 +03:00
|
|
|
# Please read note in test_parser
|
2014-04-24 17:17:17 +03:00
|
|
|
|
2014-04-28 16:26:19 +03:00
|
|
|
require_relative 'helper'
|
|
|
|
|
2014-04-28 22:22:37 +03:00
|
|
|
class TestAst < MiniTest::Test
|
2014-04-24 17:38:06 +03:00
|
|
|
|
2014-04-24 19:45:22 +03:00
|
|
|
def setup
|
2014-04-27 16:30:32 +03:00
|
|
|
@parser = Parser::Composed.new
|
2014-04-27 15:44:34 +03:00
|
|
|
@transform = Parser::Transform.new
|
2014-04-24 17:17:17 +03:00
|
|
|
end
|
2014-04-28 16:26:19 +03:00
|
|
|
|
|
|
|
def check
|
2014-04-29 16:22:39 +03:00
|
|
|
syntax = @parser.parse(@string_input)
|
2014-04-24 19:45:22 +03:00
|
|
|
tree = @transform.apply(syntax)
|
2014-04-28 22:07:35 +03:00
|
|
|
# puts tree.inspect
|
2014-04-29 11:37:43 +03:00
|
|
|
assert_equal @transform_output , tree
|
2014-04-24 17:17:17 +03:00
|
|
|
end
|
2014-04-24 19:45:22 +03:00
|
|
|
|
2014-04-28 16:26:19 +03:00
|
|
|
def test_expression_else
|
2014-04-29 16:22:39 +03:00
|
|
|
@string_input = <<HERE
|
2014-04-28 16:26:19 +03:00
|
|
|
4
|
|
|
|
5
|
|
|
|
else
|
|
|
|
HERE
|
2014-04-29 11:37:43 +03:00
|
|
|
@transform_output = {:expressions=>[ Parser::IntegerExpression.new(4), Parser::IntegerExpression.new(5)]}
|
2014-04-28 16:26:19 +03:00
|
|
|
|
|
|
|
@parser = @parser.expressions_else
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expression_end
|
2014-04-29 16:22:39 +03:00
|
|
|
@string_input = <<HERE
|
2014-04-28 16:26:19 +03:00
|
|
|
5
|
|
|
|
name
|
|
|
|
call(4,6)
|
|
|
|
end
|
|
|
|
HERE
|
2014-04-29 11:37:43 +03:00
|
|
|
@transform_output = {:expressions=> [Parser::IntegerExpression.new(5), Parser::NameExpression.new("name"),
|
2014-04-28 21:21:12 +03:00
|
|
|
Parser::FuncallExpression.new("call", [Parser::IntegerExpression.new(4), Parser::IntegerExpression.new(6) ]) ] }
|
2014-04-28 16:26:19 +03:00
|
|
|
@parser = @parser.expressions_end
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_conditional
|
2014-04-29 16:22:39 +03:00
|
|
|
@string_input = <<HERE
|
2014-04-28 16:26:19 +03:00
|
|
|
if (0)
|
|
|
|
42
|
|
|
|
else
|
|
|
|
667
|
|
|
|
end
|
|
|
|
HERE
|
2014-04-29 11:37:43 +03:00
|
|
|
@transform_output = Parser::ConditionalExpression.new( Parser::IntegerExpression.new(0),
|
2014-04-28 21:21:12 +03:00
|
|
|
[Parser::IntegerExpression.new(42)],
|
|
|
|
[Parser::IntegerExpression.new(667)])
|
2014-04-28 16:26:19 +03:00
|
|
|
@parser = @parser.conditional
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_function_definition
|
2014-04-29 16:22:39 +03:00
|
|
|
@string_input = <<HERE
|
2014-04-28 16:26:19 +03:00
|
|
|
def foo(x)
|
2014-04-27 21:12:42 +03:00
|
|
|
5
|
2014-04-28 16:26:19 +03:00
|
|
|
end
|
2014-04-27 21:12:42 +03:00
|
|
|
HERE
|
2014-04-29 11:37:43 +03:00
|
|
|
@transform_output = Parser::FunctionExpression.new('foo',
|
2014-04-28 21:21:12 +03:00
|
|
|
[Parser::NameExpression.new('x')],
|
|
|
|
[Parser::IntegerExpression.new(5)])
|
2014-04-27 21:12:42 +03:00
|
|
|
@parser = @parser.function_definition
|
2014-04-28 16:26:19 +03:00
|
|
|
check
|
2014-04-27 21:12:42 +03:00
|
|
|
end
|
2014-04-28 22:07:35 +03:00
|
|
|
def test_function_assignment
|
2014-04-29 16:22:39 +03:00
|
|
|
@string_input = <<HERE
|
2014-04-28 22:07:35 +03:00
|
|
|
def foo(x)
|
|
|
|
abba = 5
|
2014-04-24 17:17:17 +03:00
|
|
|
end
|
2014-04-28 22:07:35 +03:00
|
|
|
HERE
|
2014-04-29 11:37:43 +03:00
|
|
|
@transform_output = Parser::FunctionExpression.new( "foo", [Parser::NameExpression.new("x")],
|
2014-04-28 22:07:35 +03:00
|
|
|
[Parser::AssignmentExpression.new( "abba", Parser::IntegerExpression.new(5) ) ])
|
|
|
|
check
|
|
|
|
end
|
|
|
|
end
|