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) {
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

View File

@ -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

View File

@ -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)))

View File

@ -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))))))))

View File

@ -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)))))

View File

@ -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)))))))

View File

@ -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)))))

View File

@ -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))))))

View File

@ -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,

View File

@ -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)))))))

View File

@ -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,

View File

@ -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)))))

View File

@ -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))))))))

View File

@ -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)))))