fixing module and class rules to actually use module names. fix all tests
This commit is contained in:
parent
f03d445f3d
commit
806960a4d6
@ -2,12 +2,12 @@ module Parser
|
||||
module ModuleDef
|
||||
include Parslet
|
||||
rule(:module_definition) do
|
||||
keyword_module >> name >> eol >>
|
||||
keyword_module >> module_name >> eol >>
|
||||
( (keyword_end.absent? >> root).repeat(1)).as(:module_expressions) >> keyword_end >> newline
|
||||
end
|
||||
|
||||
rule(:class_definition) do
|
||||
keyword_class >> name >> eol >>
|
||||
keyword_class >> module_name >> eol >>
|
||||
( (keyword_end.absent? >> root).repeat(1)).as(:class_expressions) >> keyword_end >> newline
|
||||
end
|
||||
|
||||
|
@ -56,11 +56,11 @@ module Parser
|
||||
end
|
||||
|
||||
#modules and classes are undertsndibly quite similar Class < Module
|
||||
rule( :name => simple(:name) , :module_expressions => sequence(:module_expressions) , :end=>"end") do
|
||||
Ast::ModuleExpression.new(name , module_expressions)
|
||||
rule( :module_name => simple(:module_name) , :module_expressions => sequence(:module_expressions) , :end=>"end") do
|
||||
Ast::ModuleExpression.new(module_name , module_expressions)
|
||||
end
|
||||
rule( :name => simple(:name) , :class_expressions => sequence(:class_expressions) , :end=>"end") do
|
||||
Ast::ClassExpression.new(name , class_expressions)
|
||||
rule( :module_name => simple(:module_name) , :class_expressions => sequence(:class_expressions) , :end=>"end") do
|
||||
Ast::ClassExpression.new(module_name , class_expressions)
|
||||
end
|
||||
|
||||
#shortcut to get the ast tree for a given string
|
||||
|
@ -6,32 +6,32 @@ class TestModuleDef < MiniTest::Test
|
||||
|
||||
def test_simplest_module
|
||||
@string_input = <<HERE
|
||||
module foo
|
||||
module Simple
|
||||
5
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:name=>"foo", :module_expressions=>[{:integer=>"5"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new("foo" ,[Ast::IntegerExpression.new(5)] )
|
||||
@parse_output = {:module_name=>"Simple", :module_expressions=>[{:integer=>"5"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:Simple ,[Ast::IntegerExpression.new(5)] )
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
def test_module_ops
|
||||
@string_input = <<HERE
|
||||
module ops
|
||||
module Opers
|
||||
def foo(x)
|
||||
abba = 5
|
||||
2 + 5
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:name=>"ops", :module_expressions=>[{: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"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new("ops" ,[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 = {:module_name=>"Opers", :module_expressions=>[{: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"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:Opers ,[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))] )] )
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
def test_module_if
|
||||
@string_input = <<HERE
|
||||
module sif
|
||||
module Foo
|
||||
def ofthen(n)
|
||||
if(0)
|
||||
isit = 42
|
||||
@ -41,34 +41,34 @@ module sif
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:name=>"sif", :module_expressions=>[{: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"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new("sif" ,[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 = {:module_name=>"Foo", :module_expressions=>[{: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"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:Foo ,[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))] )] )] )
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
def test_module_function
|
||||
@string_input = <<HERE
|
||||
module sif
|
||||
module Soho
|
||||
ofthen(3+4 , var)
|
||||
def ofthen(n,m)
|
||||
44
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:name=>"sif", :module_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}, {:function_name=>{:name=>"ofthen"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}, {:parmeter=>{:name=>"m"}}], :expressions=>[{:integer=>"44"}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:sif ,[Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new("var")] ), Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new("n"),Ast::NameExpression.new("m")] , [Ast::IntegerExpression.new(44)] )] )
|
||||
@parse_output = {:module_name=>"Soho", :module_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}, {:function_name=>{:name=>"ofthen"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}, {:parmeter=>{:name=>"m"}}], :expressions=>[{:integer=>"44"}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:Soho ,[Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new("var")] ), Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new("n"),Ast::NameExpression.new("m")] , [Ast::IntegerExpression.new(44)] )] )
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
def test_module_class
|
||||
@string_input = <<HERE
|
||||
module foo
|
||||
class bar
|
||||
module Foo
|
||||
class Bar
|
||||
funcall(3+4 , var)
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:name=>"foo", :module_expressions=>[{:name=>"bar", :class_expressions=>[{:call_site=>{:name=>"funcall"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:foo ,[Ast::ClassExpression.new(:bar ,[Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new("var")] )] )] )
|
||||
@parse_output = {:module_name=>"Foo", :module_expressions=>[{:module_name=>"Bar", :class_expressions=>[{:call_site=>{:name=>"funcall"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:Foo ,[Ast::ClassExpression.new(:Bar ,[Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new("var")] )] )] )
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
end
|
@ -64,7 +64,7 @@ HERE
|
||||
|
||||
def test_module_method
|
||||
@string_input = <<HERE
|
||||
module fibo
|
||||
module Fibo
|
||||
def fibonaccit(n)
|
||||
a = 0
|
||||
end
|
||||
@ -73,33 +73,33 @@ module fibo
|
||||
end
|
||||
|
||||
HERE
|
||||
@parse_output = [{:name=>"fibo", :module_expressions=>[{:function_name=>{:name=>"fibonaccit"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}], :end=>"end"}, {:call_site=>{:name=>"fibonaccit"}, :argument_list=>[{:argument=>{:integer=>"10"}}]}], :end=>"end"}]
|
||||
@transform_output = [Ast::ModuleExpression.new(:fibo ,[Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new("n")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(0))] ), Ast::CallSiteExpression.new(:fibonaccit, [Ast::IntegerExpression.new(10)] )] )]
|
||||
@parse_output = [{:module_name=>"Fibo", :module_expressions=>[{:function_name=>{:name=>"fibonaccit"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}], :end=>"end"}, {:call_site=>{:name=>"fibonaccit"}, :argument_list=>[{:argument=>{:integer=>"10"}}]}], :end=>"end"}]
|
||||
@transform_output = [Ast::ModuleExpression.new(:Fibo ,[Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new("n")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(0))] ), Ast::CallSiteExpression.new(:fibonaccit, [Ast::IntegerExpression.new(10)] )] )]
|
||||
end
|
||||
|
||||
def test_module_assignment
|
||||
@string_input = <<HERE
|
||||
module fibo
|
||||
module Fibo
|
||||
a = 5 + foo
|
||||
bar( b , a , r)
|
||||
end
|
||||
|
||||
HERE
|
||||
@parse_output = [{:name=>"fibo", :module_expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:l=>{:integer=>"5"}, :o=>"+ ", :r=>{:name=>"foo"}}}, {:call_site=>{:name=>"bar"}, :argument_list=>[{:argument=>{:name=>"b"}}, {:argument=>{:name=>"a"}}, {:argument=>{:name=>"r"}}]}], :end=>"end"}]
|
||||
@transform_output = [Ast::ModuleExpression.new(:fibo ,[Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(5),Ast::NameExpression.new("foo"))), Ast::CallSiteExpression.new(:bar, [Ast::NameExpression.new("b"),Ast::NameExpression.new("a"),Ast::NameExpression.new("r")] )] )]
|
||||
@parse_output = [{:module_name=>"Fibo", :module_expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:l=>{:integer=>"5"}, :o=>"+ ", :r=>{:name=>"foo"}}}, {:call_site=>{:name=>"bar"}, :argument_list=>[{:argument=>{:name=>"b"}}, {:argument=>{:name=>"a"}}, {:argument=>{:name=>"r"}}]}], :end=>"end"}]
|
||||
@transform_output = [Ast::ModuleExpression.new(:Fibo ,[Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(5),Ast::NameExpression.new("foo"))), Ast::CallSiteExpression.new(:bar, [Ast::NameExpression.new("b"),Ast::NameExpression.new("a"),Ast::NameExpression.new("r")] )] )]
|
||||
end
|
||||
|
||||
def test_module_class
|
||||
@string_input = <<HERE
|
||||
module fibo
|
||||
class bar
|
||||
module FooBo
|
||||
class Bar
|
||||
a = 5 + foo
|
||||
end
|
||||
end
|
||||
|
||||
HERE
|
||||
@parse_output = [{:name=>"fibo", :module_expressions=>[{:name=>"bar", :class_expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:l=>{:integer=>"5"}, :o=>"+ ", :r=>{:name=>"foo"}}}], :end=>"end"}], :end=>"end"}]
|
||||
@transform_output = [Ast::ModuleExpression.new(:fibo ,[Ast::ClassExpression.new(:bar ,[Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(5),Ast::NameExpression.new("foo")))] )] )]
|
||||
@parse_output = [{:module_name=>"FooBo", :module_expressions=>[{:module_name=>"Bar", :class_expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:l=>{:integer=>"5"}, :o=>"+ ", :r=>{:name=>"foo"}}}], :end=>"end"}], :end=>"end"}]
|
||||
@transform_output = [Ast::ModuleExpression.new(:FooBo ,[Ast::ClassExpression.new(:Bar ,[Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(5),Ast::NameExpression.new("foo")))] )] )]
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user