2014-04-29 15:57:36 +02:00
|
|
|
require_relative "helper"
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-29 15:57:36 +02:00
|
|
|
class TestFunctionDefinition < MiniTest::Test
|
|
|
|
# include the magic (setup and parse -> test method translation), see there
|
|
|
|
include ParserHelper
|
2014-04-27 20:12:42 +02:00
|
|
|
|
2014-04-29 15:57:36 +02:00
|
|
|
def test_simplest_function
|
|
|
|
@string_input = <<HERE
|
|
|
|
def foo(x)
|
|
|
|
5
|
|
|
|
end
|
|
|
|
HERE
|
2014-05-12 12:47:47 +02:00
|
|
|
@parse_output ={:function_name=>{:name=>"foo"},
|
2014-05-11 17:38:02 +02:00
|
|
|
:parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:integer=>"5"}], :end=>"end"}
|
2014-05-05 08:51:16 +02:00
|
|
|
@transform_output = Ast::FunctionExpression.new('foo',
|
|
|
|
[Ast::NameExpression.new('x')],
|
|
|
|
[Ast::IntegerExpression.new(5)])
|
2014-05-10 10:18:39 +02:00
|
|
|
@parser = @parser.function_definition
|
2014-04-28 21:07:35 +02:00
|
|
|
end
|
|
|
|
|
2014-05-12 12:47:47 +02:00
|
|
|
def test_function_ops
|
2014-04-29 15:57:36 +02:00
|
|
|
@string_input = <<HERE
|
|
|
|
def foo(x)
|
|
|
|
abba = 5
|
2014-05-12 12:47:47 +02:00
|
|
|
2 + 5
|
2014-04-29 15:57:36 +02:00
|
|
|
end
|
|
|
|
HERE
|
2014-05-12 12:47:47 +02:00
|
|
|
@parse_output = {:function_name=>{:name=>"foo"},
|
2014-05-11 17:38:02 +02:00
|
|
|
:parmeter_list=>[{:parmeter=>{:name=>"x"}}],
|
2014-05-12 12:47:47 +02:00
|
|
|
:expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}},
|
|
|
|
{:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}
|
|
|
|
@transform_output = Ast::FunctionExpression.new(:foo,
|
|
|
|
[Ast::NameExpression.new("x")] ,
|
|
|
|
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("abba"),Ast::IntegerExpression.new(5)),
|
|
|
|
Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] )
|
2014-05-10 10:18:39 +02:00
|
|
|
@parser = @parser.function_definition
|
2014-04-28 20:21:12 +02:00
|
|
|
end
|
|
|
|
|
2014-05-12 11:14:04 +02:00
|
|
|
def test_function_if
|
2014-05-10 18:02:51 +02:00
|
|
|
@string_input = <<HERE
|
2014-05-12 11:14:04 +02:00
|
|
|
def ofthen(n)
|
2014-05-12 12:47:47 +02:00
|
|
|
if(0)
|
|
|
|
isit = 42
|
2014-05-12 11:14:04 +02:00
|
|
|
else
|
2014-05-12 12:47:47 +02:00
|
|
|
maybenot = 667
|
2014-05-10 18:02:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2014-05-12 12:47:47 +02:00
|
|
|
@parse_output = {:function_name=>{:name=>"ofthen"},
|
|
|
|
:parmeter_list=>[{:parmeter=>{:name=>"n"}}],
|
|
|
|
:expressions=>[{:if=>"if", :conditional=>{:integer=>"0"},
|
|
|
|
:if_true=>{:expressions=>[{:l=>{:name=>"isit"}, :o=>"= ", :r=>{:integer=>"42"}}], :else=>"else"},
|
|
|
|
:if_false=>{:expressions=>[{:l=>{:name=>"maybenot"}, :o=>"= ", :r=>{:integer=>"667"}}], :end=>"end"}}],
|
|
|
|
:end=>"end"}
|
2014-05-12 11:14:04 +02:00
|
|
|
@transform_output = Ast::FunctionExpression.new(:ofthen,
|
|
|
|
[Ast::NameExpression.new("n")] ,
|
|
|
|
[Ast::ConditionalExpression.new(Ast::IntegerExpression.new(0),
|
2014-05-12 12:47:47 +02:00
|
|
|
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("isit"),
|
|
|
|
Ast::IntegerExpression.new(42))],
|
|
|
|
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("maybenot"),Ast::IntegerExpression.new(667))] )] )
|
2014-05-12 11:14:04 +02:00
|
|
|
@parser = @parser.function_definition
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_function_while
|
|
|
|
@string_input = <<HERE
|
|
|
|
def fibonaccit(n)
|
|
|
|
a = 0
|
2014-05-12 12:47:47 +02:00
|
|
|
while (n) do
|
|
|
|
some = 43
|
|
|
|
other = some * 4
|
2014-05-12 11:14:04 +02:00
|
|
|
end
|
2014-05-12 12:47:47 +02:00
|
|
|
end
|
2014-05-12 11:14:04 +02:00
|
|
|
HERE
|
2014-05-12 12:47:47 +02:00
|
|
|
@parse_output ={:function_name=>{:name=>"fibonaccit"},
|
|
|
|
:parmeter_list=>[{:parmeter=>{:name=>"n"}}],
|
|
|
|
:expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}},
|
|
|
|
{:while=>"while", :while_cond=>{:name=>"n"}, :do=>"do",
|
|
|
|
:body=>{:expressions=>[{:l=>{:name=>"some"}, :o=>"= ", :r=>{:integer=>"43"}},
|
|
|
|
{:l=>{:name=>"other"}, :o=>"= ", :r=>{:l=>{:name=>"some"}, :o=>"* ", :r=>{:integer=>"4"}}}],
|
|
|
|
:end=>"end"}}],
|
|
|
|
:end=>"end"}
|
|
|
|
@transform_output = Ast::FunctionExpression.new(:fibonaccit,
|
|
|
|
[Ast::NameExpression.new("n")] ,
|
|
|
|
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(0)),
|
|
|
|
Ast::WhileExpression.new(Ast::NameExpression.new("n"),
|
|
|
|
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("some"),Ast::IntegerExpression.new(43)),
|
|
|
|
Ast::OperatorExpression.new("=", Ast::NameExpression.new("other"),Ast::OperatorExpression.new("*", Ast::NameExpression.new("some"),Ast::IntegerExpression.new(4)))] )] )
|
2014-05-10 18:02:51 +02:00
|
|
|
@parser = @parser.function_definition
|
2014-05-12 13:24:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_function_big_while
|
|
|
|
@string_input = <<HERE
|
|
|
|
def fibonaccit(n)
|
|
|
|
a = 0
|
|
|
|
b = 1
|
|
|
|
while( n > 1 ) do
|
|
|
|
tmp = a
|
|
|
|
a = b
|
|
|
|
b = tmp + b
|
|
|
|
puts(b)
|
|
|
|
n = n - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2014-05-13 20:15:02 +02:00
|
|
|
@parse_output = {:function_name=>{:name=>"fibonaccit"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:integer=>"1"}}, {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}], :end=>"end"}
|
|
|
|
@transform_output = Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new("n")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(0)),Ast::OperatorExpression.new("=", Ast::NameExpression.new("b"),Ast::IntegerExpression.new(1)),Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(1)), [Ast::OperatorExpression.new("=", Ast::NameExpression.new("tmp"),Ast::NameExpression.new("a")), Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::NameExpression.new("b")), Ast::OperatorExpression.new("=", Ast::NameExpression.new("b"),Ast::OperatorExpression.new("+", Ast::NameExpression.new("tmp"),Ast::NameExpression.new("b"))), Ast::CallSiteExpression.new("puts", [Ast::NameExpression.new("b")] ), Ast::OperatorExpression.new("=", Ast::NameExpression.new("n"),Ast::OperatorExpression.new("-", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(1)))] )] )
|
2014-05-12 13:24:36 +02:00
|
|
|
@parser = @parser.function_definition
|
|
|
|
end
|
2014-04-29 15:57:36 +02:00
|
|
|
end
|