fix function definition

This commit is contained in:
Torsten Ruger 2015-10-09 17:28:47 +03:00
parent 48a6dfabff
commit dff0e8fab4
14 changed files with 83 additions and 88 deletions

View File

@ -5,11 +5,11 @@ module Parser
rule(:function_definition) { rule(:function_definition) {
type >> ((class_name|name).as(:receiver) >> str(".")).maybe >> #possibly qualified type >> ((class_name|name).as(:receiver) >> str(".")).maybe >> #possibly qualified
name.as(:function_name) >> left_parenthesis >> 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) { 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 end

View File

@ -4,25 +4,20 @@ module Parser
rule(:keyword_begin) { str('begin').as(:begin) >> space?} rule(:keyword_begin) { str('begin').as(:begin) >> space?}
rule(:keyword_class) { str('class') >> 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_else) { str('else').as(:else) >> space? }
rule(:keyword_end) { str('end').as(:end) >> space? } rule(:keyword_end) { str('end').as(:end) >> space? }
rule(:keyword_false) { str('false').as(:false) } rule(:keyword_false) { str('false').as(:false) }
rule(:keyword_field) { str('field').as(:field) >> space? } rule(:keyword_field) { str('field').as(:field) >> space? }
rule(:keyword_if) { str('if').as(:if) } rule(:keyword_if) { str('if').as(:if_statement) }
rule(:keyword_rescue) { str('rescue').as(:rescue) >> space?}
rule(:keyword_return) { str('return').as(:return) >> space?} rule(:keyword_return) { str('return').as(:return) >> space?}
rule(:keyword_true) { str('true').as(:true) } rule(:keyword_true) { str('true').as(:true) }
rule(:keyword_module) { str('module') >> space? }
rule(:keyword_nil) { str('nil').as(:nil) } 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) } 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 # 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. # 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') | rule(:keyword){ str('if') | str('else') | str('end') | str('while') |
str('false')| str('if')| str('rescue')| str('true')| str('nil') | str('false') | str('true')| str('nil') | str("class") |
str('unless')| str('until')| str('while') | str('field')} str('return')| str('int')| str('field')}
end end
end end

View File

@ -1,12 +1,13 @@
int self.length( ref x ) int self.length( ref x )
length return 5
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :int, s(:function, :int,
s(:name, :length), s(:name, :length),
s(:parameters, s(:parameters,
s(:parameter, :ref, :x)), s(:parameter, :ref, :x)),
s(:expressions, s(:statements,
s(:name, :length)), s(:return,
s(:int, 5))),
s(:receiver, :self))) s(:receiver, :self)))

View File

@ -10,41 +10,41 @@ int fibonaccit(int n)
end end
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :int, s(:function, :int,
s(:name, :fibonaccit), s(:name, :fibonaccit),
s(:parameters, s(:parameters,
s(:parameter, :int, :n)), s(:parameter, :int, :n)),
s(:expressions, s(:statements,
s(:assign, s(:assignment,
s(:name, :a), s(:name, :a),
s(:int, 0)), s(:int, 0)),
s(:assign, s(:assignment,
s(:name, :b), s(:name, :b),
s(:int, 1)), s(:int, 1)),
s(:while, s(:while_statement,
s(:condition, s(:condition,
s(:operator, ">", s(:operator_value, :>,
s(:name, :n), s(:name, :n),
s(:int, 1))), s(:int, 1))),
s(:expressions, s(:statements,
s(:assign, s(:assignment,
s(:name, :tmp), s(:name, :tmp),
s(:name, :a)), s(:name, :a)),
s(:assign, s(:assignment,
s(:name, :a), s(:name, :a),
s(:name, :b)), s(:name, :b)),
s(:assign, s(:assignment,
s(:name, :b), s(:name, :b),
s(:operator, "+", s(:operator_value, :+,
s(:name, :tmp), s(:name, :tmp),
s(:name, :b))), s(:name, :b))),
s(:call, s(:call,
s(:name, :puts), s(:name, :puts),
s(:arguments, s(:arguments,
s(:name, :b))), s(:name, :b))),
s(:assign, s(:assignment,
s(:name, :n), s(:name, :n),
s(:operator, "-", s(:operator_value, :-,
s(:name, :n), s(:name, :n),
s(:int, 1)))))))) s(:int, 1))))))))

View File

@ -2,21 +2,14 @@ int foo(int x)
int a = 5 int a = 5
return a return a
end end
3.foo( 4 )
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :int, s(:function, :int,
s(:name, :foo), s(:name, :foo),
s(:parameters, s(:parameters,
s(:parameter, :int, :x)), s(:parameter, :int, :x)),
s(:expressions, s(:statements,
s(:field_def, :int, :a, s(:field_def, :int, :a,
s(:int, 5)), s(:int, 5)),
s(:return, s(:return,
s(:name, :a)))), s(:name, :a)))))
s(:call,
s(:name, :foo),
s(:arguments,
s(:int, 4)),
s(:receiver,
s(:int, 3))))

View File

@ -6,20 +6,20 @@ ref ofthen(int n)
end end
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :ref, s(:function, :ref,
s(:name, :ofthen), s(:name, :ofthen),
s(:parameters, s(:parameters,
s(:parameter, :int, :n)), s(:parameter, :int, :n)),
s(:expressions, s(:statements,
s(:if, s(:if_statement,
s(:condition, s(:condition,
s(:int, 0)), s(:int, 0)),
s(:if_true, s(:true_statements,
s(:assign, s(:assignment,
s(:name, :isit), s(:name, :isit),
s(:int, 42))), s(:int, 42))),
s(:if_false, s(:false_statements,
s(:assign, s(:assignment,
s(:name, :maybenot), s(:name, :maybenot),
s(:int, 667))))))) s(:int, 667)))))))

View File

@ -1,10 +1,11 @@
int foo() int foo()
5 return 5
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :int, s(:function, :int,
s(:name, :foo), s(:name, :foo),
s(:parameters), s(:parameters),
s(:expressions, s(:statements,
s(:int, 5)))) s(:return,
s(:int, 5)))))

View File

@ -1,16 +1,18 @@
int foo(int x) int foo(int x)
int abba = 5 int abba = 5
abba + 5 abba = abba + 5
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :int, s(:function, :int,
s(:name, :foo), s(:name, :foo),
s(:parameters, s(:parameters,
s(:parameter, :int, :x)), s(:parameter, :int, :x)),
s(:expressions, s(:statements,
s(:field_def, :int, :abba, s(:field_def, :int, :abba,
s(:int, 5)), s(:int, 5)),
s(:operator, "+", s(:assignment,
s(:name, :abba), s(:name, :abba),
s(:int, 5))))) s(:operator_value, :+,
s(:name, :abba),
s(:int, 5))))))

View File

@ -3,12 +3,12 @@ int retvar(ref n)
return i return i
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :int, s(:function, :int,
s(:name, :retvar), s(:name, :retvar),
s(:parameters, s(:parameters,
s(:parameter, :ref, :n)), s(:parameter, :ref, :n)),
s(:expressions, s(:statements,
s(:field_def, :int, :i, s(:field_def, :int, :i,
s(:field_access, s(:field_access,
s(:receiver, s(:receiver,

View File

@ -6,20 +6,20 @@ int retvar(int n)
end end
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :int, s(:function, :int,
s(:name, :retvar), s(:name, :retvar),
s(:parameters, s(:parameters,
s(:parameter, :int, :n)), s(:parameter, :int, :n)),
s(:expressions, s(:statements,
s(:if, s(:if_statement,
s(:condition, s(:condition,
s(:operator, ">", s(:operator_value, :>,
s(:name, :n), s(:name, :n),
s(:int, 5))), s(:int, 5))),
s(:if_true, s(:true_statements,
s(:return, s(:return,
s(:int, 10))), s(:int, 10))),
s(:if_false, s(:false_statements,
s(:return, s(:return,
s(:int, 20))))))) s(:int, 20)))))))

View File

@ -5,21 +5,21 @@ int retvar(int n )
end end
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :int, s(:function, :int,
s(:name, :retvar), s(:name, :retvar),
s(:parameters, s(:parameters,
s(:parameter, :int, :n)), s(:parameter, :int, :n)),
s(:expressions, s(:statements,
s(:while, s(:while_statement,
s(:condition, s(:condition,
s(:operator, ">", s(:operator_value, :>,
s(:name, :n), s(:name, :n),
s(:int, 5))), s(:int, 5))),
s(:expressions, s(:statements,
s(:assign, s(:assignment,
s(:name, :n), s(:name, :n),
s(:operator, "+", s(:operator_value, :+,
s(:name, :n), s(:name, :n),
s(:int, 1))), s(:int, 1))),
s(:return, s(:return,

View File

@ -1,12 +1,13 @@
int foo( int n ,ref m) int foo( int n ,ref m)
n return n
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :int, s(:function, :int,
s(:name, :foo), s(:name, :foo),
s(:parameters, s(:parameters,
s(:parameter, :int, :n), s(:parameter, :int, :n),
s(:parameter, :ref, :m)), s(:parameter, :ref, :m)),
s(:expressions, s(:statements,
s(:name, :n)))) s(:return,
s(:name, :n)))))

View File

@ -6,24 +6,24 @@ ref fibonaccit(int n)
end end
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :ref, s(:function, :ref,
s(:name, :fibonaccit), s(:name, :fibonaccit),
s(:parameters, s(:parameters,
s(:parameter, :int, :n)), s(:parameter, :int, :n)),
s(:expressions, s(:statements,
s(:assign, s(:assignment,
s(:name, :a), s(:name, :a),
s(:int, 0)), s(:int, 0)),
s(:while, s(:while_statement,
s(:condition, s(:condition,
s(:name, :n)), s(:name, :n)),
s(:expressions, s(:statements,
s(:assign, s(:assignment,
s(:name, :some), s(:name, :some),
s(:int, 43)), s(:int, 43)),
s(:assign, s(:assignment,
s(:name, :other), s(:name, :other),
s(:operator, "*", s(:operator_value, :*,
s(:name, :some), s(:name, :some),
s(:int, 4)))))))) s(:int, 4))))))))

View File

@ -1,11 +1,13 @@
int foo(ref x) int foo(ref x)
5 a = 1
end end
-- -- -- -- -- --
s(:expressions, s(:statements,
s(:function, :int, s(:function, :int,
s(:name, :foo), s(:name, :foo),
s(:parameters, s(:parameters,
s(:parameter, :ref, :x)), s(:parameter, :ref, :x)),
s(:expressions, s(:statements,
s(:int, 5)))) s(:assignment,
s(:name, :a),
s(:int, 1)))))