fixed fibonacci example
This commit is contained in:
parent
1170b0798b
commit
25a7c3ea68
@ -4,12 +4,12 @@ module Parser
|
|||||||
rule(:conditional) do
|
rule(:conditional) do
|
||||||
keyword_if >>
|
keyword_if >>
|
||||||
(( (simple_expression|operator_expression).as(:conditional) ) |
|
(( (simple_expression|operator_expression).as(:conditional) ) |
|
||||||
left_parenthesis >> (simple_expression|operator_expression).as(:conditional) >> right_parenthesis) >>
|
left_parenthesis >> (operator_expression|simple_expression).as(:conditional) >> right_parenthesis) >>
|
||||||
newline >> expressions_else.as(:if_true) >> newline >> expressions_end.as(:if_false)
|
newline >> expressions_else.as(:if_true) >> newline >> expressions_end.as(:if_false)
|
||||||
end
|
end
|
||||||
|
|
||||||
rule(:while_do) do
|
rule(:while_do) do
|
||||||
keyword_while >> left_parenthesis >> (simple_expression|operator_expression).as(:while_cond) >>
|
keyword_while >> left_parenthesis >> (operator_expression|simple_expression).as(:while_cond) >>
|
||||||
right_parenthesis >> keyword_do >> newline >>
|
right_parenthesis >> keyword_do >> newline >>
|
||||||
expressions_end.as(:body)
|
expressions_end.as(:body)
|
||||||
end
|
end
|
||||||
|
@ -87,4 +87,23 @@ HERE
|
|||||||
Ast::OperatorExpression.new("=", Ast::NameExpression.new("other"),Ast::OperatorExpression.new("*", Ast::NameExpression.new("some"),Ast::IntegerExpression.new(4)))] )] )
|
Ast::OperatorExpression.new("=", Ast::NameExpression.new("other"),Ast::OperatorExpression.new("*", Ast::NameExpression.new("some"),Ast::IntegerExpression.new(4)))] )] )
|
||||||
@parser = @parser.function_definition
|
@parser = @parser.function_definition
|
||||||
end
|
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
|
||||||
|
@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"}}}, {:function_call=>{: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::FuncallExpression.new("puts", [Ast::NameExpression.new("b")] ), Ast::OperatorExpression.new("=", Ast::NameExpression.new("n"),Ast::OperatorExpression.new("-", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(1)))] )] )
|
||||||
|
@parser = @parser.function_definition
|
||||||
|
end
|
||||||
end
|
end
|
@ -22,6 +22,12 @@ class TestExpressions < MiniTest::Test
|
|||||||
def test_simple_minus
|
def test_simple_minus
|
||||||
simple_op "-"
|
simple_op "-"
|
||||||
end
|
end
|
||||||
|
def test_simple_greater
|
||||||
|
simple_op ">"
|
||||||
|
end
|
||||||
|
def test_simple_smaller
|
||||||
|
simple_op "<"
|
||||||
|
end
|
||||||
def test_op_variable
|
def test_op_variable
|
||||||
@string_input = "a + 35"
|
@string_input = "a + 35"
|
||||||
@parse_output = {:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}}
|
@parse_output = {:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}}
|
||||||
|
@ -23,6 +23,26 @@ HERE
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_fibo1
|
||||||
|
@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
|
||||||
|
|
||||||
|
fibonaccit( 10 )
|
||||||
|
HERE
|
||||||
|
@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"}}}, {:function_call=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}], :end=>"end"}, {:function_call=>{:name=>"fibonaccit"}, :argument_list=>[{:argument=>{:integer=>"10"}}]}]
|
||||||
|
@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::FuncallExpression.new("puts", [Ast::NameExpression.new("b")] ), Ast::OperatorExpression.new("=", Ast::NameExpression.new("n"),Ast::OperatorExpression.new("-", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(1)))] )] ), Ast::FuncallExpression.new("fibonaccit", [Ast::IntegerExpression.new(10)] )]
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,4 +23,35 @@ HERE
|
|||||||
Ast::FuncallExpression.new("puts", [Ast::NameExpression.new("b")] )] )
|
Ast::FuncallExpression.new("puts", [Ast::NameExpression.new("b")] )] )
|
||||||
@parser = @parser.while_do
|
@parser = @parser.while_do
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_big_while
|
||||||
|
@string_input = <<HERE
|
||||||
|
while( n > 1) do
|
||||||
|
tmp = a
|
||||||
|
a = b
|
||||||
|
b = tmp + b
|
||||||
|
puts(b)
|
||||||
|
n = n - 1
|
||||||
|
end
|
||||||
|
HERE
|
||||||
|
@string_input.chop!
|
||||||
|
@parse_output = {: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"}}},
|
||||||
|
{:function_call=>{:name=>"puts"},
|
||||||
|
:argument_list=>[{:argument=>{:name=>"b"}}]},
|
||||||
|
{:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}],
|
||||||
|
:end=>"end"}}
|
||||||
|
@transform_output = 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::FuncallExpression.new("puts", [Ast::NameExpression.new("b")] ),
|
||||||
|
Ast::OperatorExpression.new("=", Ast::NameExpression.new("n"),Ast::OperatorExpression.new("-", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(1)))] )
|
||||||
|
@parser = @parser.while_do
|
||||||
|
end
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user