diff --git a/lib/ast/function_expression.rb b/lib/ast/function_expression.rb index f80202d2..dae94360 100644 --- a/lib/ast/function_expression.rb +++ b/lib/ast/function_expression.rb @@ -1,16 +1,19 @@ module Ast class FunctionExpression < Expression - attr_reader :name, :params, :body - def initialize name, params, body - @name, @params, @body = name.to_sym, params, body + attr_reader :name, :params, :body , :receiver + def initialize name, params, body , receiver = nil + @name = name.to_sym + @params = params + @body = body + @receiver = receiver end def attributes - [:name, :params, :body] + [:name, :params, :body , :receiver] end def inspect self.class.name + ".new(" + name.inspect + ", ["+ params.collect{|m| m.inspect }.join( ",") +"] , [" + - body.collect{|m| m.inspect }.join( ",") +"] )" + body.collect{|m| m.inspect }.join( ",") +"] ,"+ receiver.inspect + " )" end def to_s diff --git a/lib/parser/function_definition.rb b/lib/parser/function_definition.rb index 47a80e76..dd7fdb2b 100644 --- a/lib/parser/function_definition.rb +++ b/lib/parser/function_definition.rb @@ -3,7 +3,8 @@ module Parser include Parslet rule(:function_definition) { - keyword_def >> name.as(:function_name) >> parmeter_list >> newline >> expressions_end >> newline + keyword_def >> ((module_name|instance_variable|name).as(:receiver) >> str(".")).maybe >> #possibly qualified + name.as(:function_name) >> parmeter_list >> newline >> expressions_end >> newline } rule(:parmeter_list) { diff --git a/lib/parser/transform.rb b/lib/parser/transform.rb index 5c6225d3..cddbcefb 100644 --- a/lib/parser/transform.rb +++ b/lib/parser/transform.rb @@ -21,6 +21,7 @@ module Parser rule(:argument => simple(:argument)) { argument } rule(:argument_list => sequence(:argument_list)) { argument_list } + #Two rules for calls, simple and qualified. Keeps the rules simpler rule( :call_site => simple(:call_site), :argument_list => sequence(:argument_list)) do Ast::CallSiteExpression.new(call_site.name, argument_list ) @@ -49,17 +50,25 @@ module Parser rule(:parmeter => simple(:parmeter)) { parmeter } rule(:parmeter_list => sequence(:parmeter_list)) { parmeter_list } + # Also two rules for function definitions, unqualified and qualified rule(:function_name => simple(:function_name), :parmeter_list => sequence(:parmeter_list), :expressions => sequence(:expressions) , :end => simple(:e)) do - Ast::FunctionExpression.new(function_name.name, parmeter_list, expressions) + Ast::FunctionExpression.new(function_name.name, parmeter_list, expressions) end - + + rule(:receiver=> simple(:receiver), + :function_name => simple(:function_name), + :parmeter_list => sequence(:parmeter_list), + :expressions => sequence(:expressions) , :end => simple(:e)) do + Ast::FunctionExpression.new(function_name.name, parmeter_list, expressions , receiver) + end + rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do Ast::OperatorExpression.new( o.to_s.strip , l ,r) end - #modules and classes are undertsndibly quite similar Class < Module + #modules and classes are understandibly quite similar Class < Module rule( :module_name => simple(:module_name) , :module_expressions => sequence(:module_expressions) , :end=>"end") do Ast::ModuleExpression.new(module_name , module_expressions) end diff --git a/test/parser/test_function_definition.rb b/test/parser/test_function_definition.rb index 3c243fa1..84b5911b 100644 --- a/test/parser/test_function_definition.rb +++ b/test/parser/test_function_definition.rb @@ -10,11 +10,19 @@ def foo(x) 5 end HERE - @parse_output ={:function_name=>{:name=>"foo"}, - :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:integer=>"5"}], :end=>"end"} - @transform_output = Ast::FunctionExpression.new('foo', - [Ast::NameExpression.new('x')], - [Ast::IntegerExpression.new(5)]) + @parse_output = {:function_name=>{:name=>"foo"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:integer=>"5"}], :end=>"end"} + @transform_output = Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::IntegerExpression.new(5)] ) + @parser = @parser.function_definition + end + + def test_class_function + @string_input = <{:module_name=>"String"}, :function_name=>{:name=>"length"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:instance_variable=>{:name=>"length"}}], :end=>"end"} + @transform_output = Ast::FunctionExpression.new(:length, [Ast::NameExpression.new("x")] , [Ast::VariableExpression.new("length")] ,Ast::ModuleName.new("String") ) @parser = @parser.function_definition end @@ -25,14 +33,8 @@ def foo(x) 2 + 5 end HERE - @parse_output = {:function_name=>{:name=>"foo"}, - :parmeter_list=>[{:parmeter=>{:name=>"x"}}], - :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}, - {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"} - @transform_output = Ast::FunctionExpression.new(:foo, - [Ast::NameExpression.new("x")] , - [Ast::OperatorExpression.new("=", Ast::NameExpression.new("abba"),Ast::IntegerExpression.new(5)), - Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] ) + @parse_output = {:function_name=>{:name=>"foo"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"} + @transform_output = Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("abba"),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] ,nil ) @parser = @parser.function_definition end @@ -46,18 +48,8 @@ def ofthen(n) end end HERE - @parse_output = {:function_name=>{:name=>"ofthen"}, - :parmeter_list=>[{:parmeter=>{:name=>"n"}}], - :expressions=>[{:if=>"if", :conditional=>{:integer=>"0"}, - :if_true=>{:expressions=>[{:l=>{:name=>"isit"}, :o=>"= ", :r=>{:integer=>"42"}}], :else=>"else"}, - :if_false=>{:expressions=>[{:l=>{:name=>"maybenot"}, :o=>"= ", :r=>{:integer=>"667"}}], :end=>"end"}}], - :end=>"end"} - @transform_output = Ast::FunctionExpression.new(:ofthen, - [Ast::NameExpression.new("n")] , - [Ast::IfExpression.new(Ast::IntegerExpression.new(0), - [Ast::OperatorExpression.new("=", Ast::NameExpression.new("isit"), - Ast::IntegerExpression.new(42))], - [Ast::OperatorExpression.new("=", Ast::NameExpression.new("maybenot"),Ast::IntegerExpression.new(667))] )] ) + @parse_output = {:function_name=>{:name=>"ofthen"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}], :expressions=>[{:if=>"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:l=>{:name=>"isit"}, :o=>"= ", :r=>{:integer=>"42"}}], :else=>"else"}, :if_false=>{:expressions=>[{:l=>{:name=>"maybenot"}, :o=>"= ", :r=>{:integer=>"667"}}], :end=>"end"}}], :end=>"end"} + @transform_output = Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new("n")] , [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::OperatorExpression.new("=", Ast::NameExpression.new("isit"),Ast::IntegerExpression.new(42))],[Ast::OperatorExpression.new("=", Ast::NameExpression.new("maybenot"),Ast::IntegerExpression.new(667))] )] ,nil ) @parser = @parser.function_definition end @@ -112,20 +104,8 @@ def fibonaccit(n) end end HERE - @parse_output ={:function_name=>{:name=>"fibonaccit"}, - :parmeter_list=>[{:parmeter=>{:name=>"n"}}], - :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, - {:while=>"while", :while_cond=>{:name=>"n"}, :do=>"do", - :body=>{:expressions=>[{:l=>{:name=>"some"}, :o=>"= ", :r=>{:integer=>"43"}}, - {:l=>{:name=>"other"}, :o=>"= ", :r=>{:l=>{:name=>"some"}, :o=>"* ", :r=>{:integer=>"4"}}}], - :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::WhileExpression.new(Ast::NameExpression.new("n"), - [Ast::OperatorExpression.new("=", Ast::NameExpression.new("some"),Ast::IntegerExpression.new(43)), - Ast::OperatorExpression.new("=", Ast::NameExpression.new("other"),Ast::OperatorExpression.new("*", Ast::NameExpression.new("some"),Ast::IntegerExpression.new(4)))] )] ) + @parse_output = {:function_name=>{:name=>"fibonaccit"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:while=>"while", :while_cond=>{:name=>"n"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"some"}, :o=>"= ", :r=>{:integer=>"43"}}, {:l=>{:name=>"other"}, :o=>"= ", :r=>{:l=>{:name=>"some"}, :o=>"* ", :r=>{:integer=>"4"}}}], :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::WhileExpression.new(Ast::NameExpression.new("n"), [Ast::OperatorExpression.new("=", Ast::NameExpression.new("some"),Ast::IntegerExpression.new(43)), Ast::OperatorExpression.new("=", Ast::NameExpression.new("other"),Ast::OperatorExpression.new("*", Ast::NameExpression.new("some"),Ast::IntegerExpression.new(4)))] )] ,nil ) @parser = @parser.function_definition end