From 6bfe6430324b269b76d72cdd1106221f003bddcb Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 27 May 2014 16:51:37 +0300 Subject: [PATCH] add some return test to functions --- lib/parser/control.rb | 2 +- lib/parser/expression.rb | 2 +- test/parser/test_all.rb | 1 + test/parser/test_function_definition.rb | 41 +++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/parser/control.rb b/lib/parser/control.rb index bce7b4ef..25424383 100644 --- a/lib/parser/control.rb +++ b/lib/parser/control.rb @@ -14,7 +14,7 @@ module Parser expressions_end.as(:body) end rule(:simple_return) do - keyword_return >> (operator_expression|value_expression).as(:return_expression) >> eol + keyword_return >> (operator_expression|value_expression).as(:return_expression) end end end diff --git a/lib/parser/expression.rb b/lib/parser/expression.rb index dafc4ed4..77e315f1 100644 --- a/lib/parser/expression.rb +++ b/lib/parser/expression.rb @@ -4,7 +4,7 @@ module Parser rule(:value_expression) { call_site | basic_type } - rule(:expression) { (while_do | conditional | operator_expression | call_site ) >> newline } + rule(:expression) { (simple_return | while_do | conditional | operator_expression | call_site ) >> newline } def delimited_expressions( delimit ) ( (delimit.absent? >> expression).repeat(1)).as(:expressions) >> delimit diff --git a/test/parser/test_all.rb b/test/parser/test_all.rb index 9a092f6c..2e1f0522 100644 --- a/test/parser/test_all.rb +++ b/test/parser/test_all.rb @@ -6,6 +6,7 @@ require_relative "test_expressions" require_relative "test_call_site" require_relative "test_conditional" require_relative "test_while" +require_relative "test_return" require_relative "test_operators" require_relative "test_function_definition" require_relative "test_root" \ No newline at end of file diff --git a/test/parser/test_function_definition.rb b/test/parser/test_function_definition.rb index 2cc132f3..3c243fa1 100644 --- a/test/parser/test_function_definition.rb +++ b/test/parser/test_function_definition.rb @@ -61,6 +61,47 @@ HERE @parser = @parser.function_definition end + def test_function_return + @string_input = <{:name=>"retvar"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"i"}, :o=>"= ", :r=>{:integer=>"5"}}, {:return=>"return", :return_expression=>{:name=>"i"}}], :end=>"end"} + @transform_output = Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new("n")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("i"),Ast::IntegerExpression.new(5)),Ast::ReturnExpression.new(Ast::NameExpression.new("i") )] ) + @parser = @parser.function_definition + end + + def test_function_return_if + @string_input = < 5) + return 10 + else + return 20 + end +end +HERE + @parse_output = {:function_name=>{:name=>"retvar"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}], :expressions=>[{:if=>"if", :conditional=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"5"}}, :if_true=>{:expressions=>[{:return=>"return", :return_expression=>{:integer=>"10"}}], :else=>"else"}, :if_false=>{:expressions=>[{:return=>"return", :return_expression=>{:integer=>"20"}}], :end=>"end"}}], :end=>"end"} + @transform_output = Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new("n")] , [Ast::IfExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(5)), [Ast::ReturnExpression.new(Ast::IntegerExpression.new(10) )],[Ast::ReturnExpression.new(Ast::IntegerExpression.new(20) )] )] ) + @parser = @parser.function_definition + end + + def test_function_return_while + @string_input = < 5) do + n = n + 1 + return n + end +end +HERE + @parse_output = {:function_name=>{:name=>"retvar"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}], :expressions=>[{:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"5"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"+ ", :r=>{:integer=>"1"}}}, {:return=>"return", :return_expression=>{:name=>"n"}}], :end=>"end"}}], :end=>"end"} + @transform_output = Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new("n")] , [Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(5)), [Ast::OperatorExpression.new("=", Ast::NameExpression.new("n"),Ast::OperatorExpression.new("+", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(1))), Ast::ReturnExpression.new(Ast::NameExpression.new("n") )] )] ) + @parser = @parser.function_definition + end + def test_function_while @string_input = <