From dff0e8fab4899b8e4487c8ae52f6c6ccaa0eb3e9 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 9 Oct 2015 17:28:47 +0300 Subject: [PATCH] fix function definition --- lib/parser/function_definition.rb | 4 +-- lib/parser/keywords.rb | 13 +++------- .../function_definition/class_function.tst | 9 ++++--- .../function_big_while.tst | 26 +++++++++---------- .../function_definition/function_foo2.tst | 13 +++------- .../cases/function_definition/function_if.tst | 14 +++++----- .../function_definition/function_no_arg.tst | 15 ++++++----- .../function_definition/function_ops.tst | 12 +++++---- .../function_definition/function_return.tst | 4 +-- .../function_return_if.tst | 12 ++++----- .../function_return_while.tst | 14 +++++----- .../function_definition/function_two_args.tst | 9 ++++--- .../function_definition/function_while.tst | 16 ++++++------ .../function_definition/simplest_function.tst | 10 ++++--- 14 files changed, 83 insertions(+), 88 deletions(-) diff --git a/lib/parser/function_definition.rb b/lib/parser/function_definition.rb index ff6fb35..0202f13 100644 --- a/lib/parser/function_definition.rb +++ b/lib/parser/function_definition.rb @@ -5,11 +5,11 @@ module Parser rule(:function_definition) { type >> ((class_name|name).as(:receiver) >> str(".")).maybe >> #possibly qualified name.as(:function_name) >> left_parenthesis >> - parameter_list.maybe >> right_parenthesis >> expressions_end >> space? + parameter_list.maybe >> right_parenthesis >> statements_end >> space? } rule(:parameter_list) { - ((field.as(:parameter) >> (comma >> field.as(:parameter)).repeat(0)).repeat(0,1)).as(:parameter_list) + ((field_def.as(:parameter) >> (comma >> field_def.as(:parameter)).repeat(0)).repeat(0,1)).as(:parameter_list) } end diff --git a/lib/parser/keywords.rb b/lib/parser/keywords.rb index 89b6a6c..1a64956 100644 --- a/lib/parser/keywords.rb +++ b/lib/parser/keywords.rb @@ -4,25 +4,20 @@ module Parser rule(:keyword_begin) { str('begin').as(:begin) >> space?} rule(:keyword_class) { str('class') >> space? } - rule(:keyword_do) { str('do').as(:do) >> space?} rule(:keyword_else) { str('else').as(:else) >> space? } rule(:keyword_end) { str('end').as(:end) >> space? } rule(:keyword_false) { str('false').as(:false) } rule(:keyword_field) { str('field').as(:field) >> space? } - rule(:keyword_if) { str('if').as(:if) } - rule(:keyword_rescue) { str('rescue').as(:rescue) >> space?} + rule(:keyword_if) { str('if').as(:if_statement) } rule(:keyword_return) { str('return').as(:return) >> space?} rule(:keyword_true) { str('true').as(:true) } - rule(:keyword_module) { str('module') >> space? } rule(:keyword_nil) { str('nil').as(:nil) } - rule(:keyword_unless) { str('unless').as(:unless) >> space?} - rule(:keyword_until) { str('until').as(:until) >> space?} rule(:keyword_while) { str('while').as(:while) } # this rule is just to make sure identifiers can't be keywords. Kind of duplication here, but we need the # space in above rules, so just make sure to add any here too. - rule(:keyword){ str('begin') | str('def') | str('do') | str('else') | str('end') | - str('false')| str('if')| str('rescue')| str('true')| str('nil') | - str('unless')| str('until')| str('while') | str('field')} + rule(:keyword){ str('if') | str('else') | str('end') | str('while') | + str('false') | str('true')| str('nil') | str("class") | + str('return')| str('int')| str('field')} end end diff --git a/test/cases/function_definition/class_function.tst b/test/cases/function_definition/class_function.tst index ddaf24a..7d7c0fc 100644 --- a/test/cases/function_definition/class_function.tst +++ b/test/cases/function_definition/class_function.tst @@ -1,12 +1,13 @@ int self.length( ref x ) - length + return 5 end -- -- -- -s(:expressions, +s(:statements, s(:function, :int, s(:name, :length), s(:parameters, s(:parameter, :ref, :x)), - s(:expressions, - s(:name, :length)), + s(:statements, + s(:return, + s(:int, 5))), s(:receiver, :self))) diff --git a/test/cases/function_definition/function_big_while.tst b/test/cases/function_definition/function_big_while.tst index 3a252fe..de2118f 100644 --- a/test/cases/function_definition/function_big_while.tst +++ b/test/cases/function_definition/function_big_while.tst @@ -10,41 +10,41 @@ int fibonaccit(int n) end end -- -- -- -s(:expressions, +s(:statements, s(:function, :int, s(:name, :fibonaccit), s(:parameters, s(:parameter, :int, :n)), - s(:expressions, - s(:assign, + s(:statements, + s(:assignment, s(:name, :a), s(:int, 0)), - s(:assign, + s(:assignment, s(:name, :b), s(:int, 1)), - s(:while, + s(:while_statement, s(:condition, - s(:operator, ">", + s(:operator_value, :>, s(:name, :n), s(:int, 1))), - s(:expressions, - s(:assign, + s(:statements, + s(:assignment, s(:name, :tmp), s(:name, :a)), - s(:assign, + s(:assignment, s(:name, :a), s(:name, :b)), - s(:assign, + s(:assignment, s(:name, :b), - s(:operator, "+", + s(:operator_value, :+, s(:name, :tmp), s(:name, :b))), s(:call, s(:name, :puts), s(:arguments, s(:name, :b))), - s(:assign, + s(:assignment, s(:name, :n), - s(:operator, "-", + s(:operator_value, :-, s(:name, :n), s(:int, 1)))))))) diff --git a/test/cases/function_definition/function_foo2.tst b/test/cases/function_definition/function_foo2.tst index 5f286f9..287a16c 100644 --- a/test/cases/function_definition/function_foo2.tst +++ b/test/cases/function_definition/function_foo2.tst @@ -2,21 +2,14 @@ int foo(int x) int a = 5 return a end -3.foo( 4 ) -- -- -- -s(:expressions, +s(:statements, s(:function, :int, s(:name, :foo), s(:parameters, s(:parameter, :int, :x)), - s(:expressions, + s(:statements, s(:field_def, :int, :a, s(:int, 5)), s(:return, - s(:name, :a)))), - s(:call, - s(:name, :foo), - s(:arguments, - s(:int, 4)), - s(:receiver, - s(:int, 3)))) + s(:name, :a))))) diff --git a/test/cases/function_definition/function_if.tst b/test/cases/function_definition/function_if.tst index d12f1c7..35ed295 100644 --- a/test/cases/function_definition/function_if.tst +++ b/test/cases/function_definition/function_if.tst @@ -6,20 +6,20 @@ ref ofthen(int n) end end -- -- -- -s(:expressions, +s(:statements, s(:function, :ref, s(:name, :ofthen), s(:parameters, s(:parameter, :int, :n)), - s(:expressions, - s(:if, + s(:statements, + s(:if_statement, s(:condition, s(:int, 0)), - s(:if_true, - s(:assign, + s(:true_statements, + s(:assignment, s(:name, :isit), s(:int, 42))), - s(:if_false, - s(:assign, + s(:false_statements, + s(:assignment, s(:name, :maybenot), s(:int, 667))))))) diff --git a/test/cases/function_definition/function_no_arg.tst b/test/cases/function_definition/function_no_arg.tst index 898fd62..09b5b12 100644 --- a/test/cases/function_definition/function_no_arg.tst +++ b/test/cases/function_definition/function_no_arg.tst @@ -1,10 +1,11 @@ int foo() - 5 + return 5 end -- -- -- -s(:expressions, - s(:function, :int, - s(:name, :foo), - s(:parameters), - s(:expressions, - s(:int, 5)))) +s(:statements, + s(:function, :int, + s(:name, :foo), + s(:parameters), + s(:statements, + s(:return, + s(:int, 5))))) diff --git a/test/cases/function_definition/function_ops.tst b/test/cases/function_definition/function_ops.tst index 15d6b78..84da084 100644 --- a/test/cases/function_definition/function_ops.tst +++ b/test/cases/function_definition/function_ops.tst @@ -1,16 +1,18 @@ int foo(int x) int abba = 5 - abba + 5 + abba = abba + 5 end -- -- -- -s(:expressions, +s(:statements, s(:function, :int, s(:name, :foo), s(:parameters, s(:parameter, :int, :x)), - s(:expressions, + s(:statements, s(:field_def, :int, :abba, s(:int, 5)), - s(:operator, "+", + s(:assignment, s(:name, :abba), - s(:int, 5))))) + s(:operator_value, :+, + s(:name, :abba), + s(:int, 5)))))) diff --git a/test/cases/function_definition/function_return.tst b/test/cases/function_definition/function_return.tst index 6481d19..7fbb76e 100644 --- a/test/cases/function_definition/function_return.tst +++ b/test/cases/function_definition/function_return.tst @@ -3,12 +3,12 @@ int retvar(ref n) return i end -- -- -- -s(:expressions, +s(:statements, s(:function, :int, s(:name, :retvar), s(:parameters, s(:parameter, :ref, :n)), - s(:expressions, + s(:statements, s(:field_def, :int, :i, s(:field_access, s(:receiver, diff --git a/test/cases/function_definition/function_return_if.tst b/test/cases/function_definition/function_return_if.tst index ce3d8a0..d518a5c 100644 --- a/test/cases/function_definition/function_return_if.tst +++ b/test/cases/function_definition/function_return_if.tst @@ -6,20 +6,20 @@ int retvar(int n) end end -- -- -- -s(:expressions, +s(:statements, s(:function, :int, s(:name, :retvar), s(:parameters, s(:parameter, :int, :n)), - s(:expressions, - s(:if, + s(:statements, + s(:if_statement, s(:condition, - s(:operator, ">", + s(:operator_value, :>, s(:name, :n), s(:int, 5))), - s(:if_true, + s(:true_statements, s(:return, s(:int, 10))), - s(:if_false, + s(:false_statements, s(:return, s(:int, 20))))))) diff --git a/test/cases/function_definition/function_return_while.tst b/test/cases/function_definition/function_return_while.tst index 9ca63b0..941d05a 100644 --- a/test/cases/function_definition/function_return_while.tst +++ b/test/cases/function_definition/function_return_while.tst @@ -5,21 +5,21 @@ int retvar(int n ) end end -- -- -- -s(:expressions, +s(:statements, s(:function, :int, s(:name, :retvar), s(:parameters, s(:parameter, :int, :n)), - s(:expressions, - s(:while, + s(:statements, + s(:while_statement, s(:condition, - s(:operator, ">", + s(:operator_value, :>, s(:name, :n), s(:int, 5))), - s(:expressions, - s(:assign, + s(:statements, + s(:assignment, s(:name, :n), - s(:operator, "+", + s(:operator_value, :+, s(:name, :n), s(:int, 1))), s(:return, diff --git a/test/cases/function_definition/function_two_args.tst b/test/cases/function_definition/function_two_args.tst index 59ec5a6..778ed94 100644 --- a/test/cases/function_definition/function_two_args.tst +++ b/test/cases/function_definition/function_two_args.tst @@ -1,12 +1,13 @@ int foo( int n ,ref m) - n + return n end -- -- -- -s(:expressions, +s(:statements, s(:function, :int, s(:name, :foo), s(:parameters, s(:parameter, :int, :n), s(:parameter, :ref, :m)), - s(:expressions, - s(:name, :n)))) + s(:statements, + s(:return, + s(:name, :n))))) diff --git a/test/cases/function_definition/function_while.tst b/test/cases/function_definition/function_while.tst index 05fe506..c90f3fc 100644 --- a/test/cases/function_definition/function_while.tst +++ b/test/cases/function_definition/function_while.tst @@ -6,24 +6,24 @@ ref fibonaccit(int n) end end -- -- -- -s(:expressions, +s(:statements, s(:function, :ref, s(:name, :fibonaccit), s(:parameters, s(:parameter, :int, :n)), - s(:expressions, - s(:assign, + s(:statements, + s(:assignment, s(:name, :a), s(:int, 0)), - s(:while, + s(:while_statement, s(:condition, s(:name, :n)), - s(:expressions, - s(:assign, + s(:statements, + s(:assignment, s(:name, :some), s(:int, 43)), - s(:assign, + s(:assignment, s(:name, :other), - s(:operator, "*", + s(:operator_value, :*, s(:name, :some), s(:int, 4)))))))) diff --git a/test/cases/function_definition/simplest_function.tst b/test/cases/function_definition/simplest_function.tst index da3fb22..4408c2b 100644 --- a/test/cases/function_definition/simplest_function.tst +++ b/test/cases/function_definition/simplest_function.tst @@ -1,11 +1,13 @@ int foo(ref x) - 5 + a = 1 end -- -- -- -s(:expressions, +s(:statements, s(:function, :int, s(:name, :foo), s(:parameters, s(:parameter, :ref, :x)), - s(:expressions, - s(:int, 5)))) + s(:statements, + s(:assignment, + s(:name, :a), + s(:int, 1)))))