From 48a6dfabff0772de3eec237cc02d8ebebe943736 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 9 Oct 2015 17:28:13 +0300 Subject: [PATCH] fix while and if statements --- lib/parser/control.rb | 23 ++++---- test/cases/if_statement/if_else.tst | 23 ++++---- .../if_statement/if_else_expressions.tst | 38 ++++++------- test/cases/small_conditional/if_end.tst | 17 +++--- .../small_conditional/if_end_expressions.tst | 27 +++++----- test/cases/while_statement/big_while.tst | 53 +++++++++---------- test/cases/while_statement/while.tst | 23 ++++---- test/cases/while_statement/while_method.tst | 35 ++++++------ 8 files changed, 120 insertions(+), 119 deletions(-) diff --git a/lib/parser/control.rb b/lib/parser/control.rb index d6938c4..e498dcc 100644 --- a/lib/parser/control.rb +++ b/lib/parser/control.rb @@ -1,24 +1,25 @@ module Parser module Control include Parslet - rule(:conditional) do + rule(:while_statement) do + keyword_while >> left_parenthesis >> r_value.as(:condition) >> + right_parenthesis >> statements_end.as(:body) + end + + rule(:if_statement) do keyword_if >> - left_parenthesis >> (operator_expression|r_value).as(:conditional) >> right_parenthesis >> - expressions_else.as(:if_true) >> expressions_end.as(:if_false) + left_parenthesis >> r_value.as(:conditional) >> right_parenthesis >> + statements_else.as(:true_statements) >> statements_end.as(:false_statements) end rule(:small_conditional) do keyword_if >> - left_parenthesis >> (operator_expression|r_value).as(:conditional) >> right_parenthesis >> - expressions_end.as(:if_true) + left_parenthesis >> r_value.as(:conditional) >> right_parenthesis >> + statements_end.as(:true_statements) end - rule(:while_do) do - keyword_while >> left_parenthesis >> (operator_expression|r_value).as(:while_cond) >> - right_parenthesis >> expressions_end.as(:body) - end - rule(:simple_return) do - keyword_return >> (operator_expression|r_value).as(:return_expression) + rule(:return_statement) do + keyword_return >> r_value.as(:return_statement) end end end diff --git a/test/cases/if_statement/if_else.tst b/test/cases/if_statement/if_else.tst index e9ec1fe..aef7ded 100644 --- a/test/cases/if_statement/if_else.tst +++ b/test/cases/if_statement/if_else.tst @@ -1,14 +1,17 @@ if(0) - 42 + fourty = 10 else - 667 + twenty = 5 end -- -- -- -s(:expressions, - s(:if, - s(:condition, - s(:int, 0)), - s(:if_true, - s(:int, 42)), - s(:if_false, - s(:int, 667)))) +s(:if_statement, + s(:condition, + s(:int, 0)), + s(:true_statements, + s(:assignment, + s(:name, :fourty), + s(:int, 10))), + s(:false_statements, + s(:assignment, + s(:name, :twenty), + s(:int, 5)))) diff --git a/test/cases/if_statement/if_else_expressions.tst b/test/cases/if_statement/if_else_expressions.tst index b3d6b0f..2790ec5 100644 --- a/test/cases/if_statement/if_else_expressions.tst +++ b/test/cases/if_statement/if_else_expressions.tst @@ -4,22 +4,22 @@ else var.new(33) end -- -- -- -s(:expressions, - s(:if, - s(:condition, - s(:operator, ">", - s(:int, 3), - s(:name, :var))), - s(:if_true, - s(:call, - s(:name, :initialize), - s(:arguments, - s(:int, 3)), - s(:receiver, - s(:module, "Object")))), - s(:if_false, s(:call, - s(:name, :new), - s(:arguments, - s(:int, 33)), - s(:receiver, - s(:name, :var)))))) +s(:if_statement, + s(:condition, + s(:operator_value, :>, + s(:int, 3), + s(:name, :var))), + s(:true_statements, + s(:call, + s(:name, :initialize), + s(:arguments, + s(:int, 3)), + s(:receiver, + s(:class_name, :Object)))), + s(:false_statements, + s(:call, + s(:name, :new), + s(:arguments, + s(:int, 33)), + s(:receiver, + s(:name, :var))))) diff --git a/test/cases/small_conditional/if_end.tst b/test/cases/small_conditional/if_end.tst index f847f4b..bcecc17 100644 --- a/test/cases/small_conditional/if_end.tst +++ b/test/cases/small_conditional/if_end.tst @@ -1,11 +1,12 @@ if(0) - 42 + four = 42 end -- -- -- -s(:expressions, - s(:if, - s(:condition, - s(:int, 0)), - s(:if_true, - s(:int, 42)), - s(:if_false, nil))) +s(:if_statement, + s(:condition, + s(:int, 0)), + s(:true_statements, + s(:assignment, + s(:name, :four), + s(:int, 42))), + s(:false_statements, nil)) diff --git a/test/cases/small_conditional/if_end_expressions.tst b/test/cases/small_conditional/if_end_expressions.tst index c72321d..3d0d001 100644 --- a/test/cases/small_conditional/if_end_expressions.tst +++ b/test/cases/small_conditional/if_end_expressions.tst @@ -2,17 +2,16 @@ if(3 > var) Object.initialize(3) end -- -- -- -s(:expressions, - s(:if, - s(:condition, - s(:operator, ">", - s(:int, 3), - s(:name, :var))), - s(:if_true, - s(:call, - s(:name, :initialize), - s(:arguments, - s(:int, 3)), - s(:receiver, - s(:module, "Object")))), - s(:if_false, nil))) +s(:if_statement, + s(:condition, + s(:operator_value, :>, + s(:int, 3), + s(:name, :var))), + s(:true_statements, + s(:call, + s(:name, :initialize), + s(:arguments, + s(:int, 3)), + s(:receiver, + s(:class_name, :Object)))), + s(:false_statements, nil)) diff --git a/test/cases/while_statement/big_while.tst b/test/cases/while_statement/big_while.tst index 331421a..e339769 100644 --- a/test/cases/while_statement/big_while.tst +++ b/test/cases/while_statement/big_while.tst @@ -6,30 +6,29 @@ while( n > 1) n = n - 1 end -- -- -- -s(:expressions, - s(:while, - s(:condition, - s(:operator, ">", - s(:name, :n), - s(:int, 1))), - s(:expressions, - s(:assign, - s(:name, :tmp), - s(:name, :a)), - s(:assign, - s(:name, :a), - s(:name, :b)), - s(:assign, - s(:name, :b), - s(:operator, "+", - s(:name, :tmp), - s(:name, :b))), - s(:call, - s(:name, :puts), - s(:arguments, - s(:name, :b))), - s(:assign, - s(:name, :n), - s(:operator, "-", - s(:name, :n), - s(:int, 1)))))) +s(:while_statement, + s(:condition, + s(:operator_value, :>, + s(:name, :n), + s(:int, 1))), + s(:statements, + s(:assignment, + s(:name, :tmp), + s(:name, :a)), + s(:assignment, + s(:name, :a), + s(:name, :b)), + s(:assignment, + s(:name, :b), + s(:operator_value, :+, + s(:name, :tmp), + s(:name, :b))), + s(:call, + s(:name, :puts), + s(:arguments, + s(:name, :b))), + s(:assignment, + s(:name, :n), + s(:operator_value, :-, + s(:name, :n), + s(:int, 1))))) diff --git a/test/cases/while_statement/while.tst b/test/cases/while_statement/while.tst index 7532b74..4b1be18 100644 --- a/test/cases/while_statement/while.tst +++ b/test/cases/while_statement/while.tst @@ -3,15 +3,14 @@ while(1) puts(b) end -- -- -- -s(:expressions, - s(:while, - s(:condition, - s(:int, 1)), - s(:expressions, - s(:assign, - s(:name, :tmp), - s(:name, :a)), - s(:call, - s(:name, :puts), - s(:arguments, - s(:name, :b)))))) +s(:while_statement, + s(:condition, + s(:int, 1)), + s(:statements, + s(:assignment, + s(:name, :tmp), + s(:name, :a)), + s(:call, + s(:name, :puts), + s(:arguments, + s(:name, :b))))) diff --git a/test/cases/while_statement/while_method.tst b/test/cases/while_statement/while_method.tst index a29d56d..3fd2348 100644 --- a/test/cases/while_statement/while_method.tst +++ b/test/cases/while_statement/while_method.tst @@ -3,21 +3,20 @@ while(1) tmp.puts(i) end -- -- -- -s(:expressions, - s(:while, - s(:condition, - s(:int, 1)), - s(:expressions, - s(:assign, - s(:name, :tmp), - s(:call, - s(:name, :new), - s(:arguments), - s(:receiver, - s(:module, "String")))), - s(:call, - s(:name, :puts), - s(:arguments, - s(:name, :i)), - s(:receiver, - s(:name, :tmp)))))) +s(:while_statement, + s(:condition, + s(:int, 1)), + s(:statements, + s(:assignment, + s(:name, :tmp), + s(:call, + s(:name, :new), + s(:arguments), + s(:receiver, + s(:class_name, :String)))), + s(:call, + s(:name, :puts), + s(:arguments, + s(:name, :i)), + s(:receiver, + s(:name, :tmp)))))