adds class definition with tests

This commit is contained in:
Torsten Ruger 2014-05-30 10:59:49 +03:00
parent e7a823bf3e
commit 3e93517986
7 changed files with 114 additions and 3 deletions

View File

@ -30,5 +30,7 @@ module Ast
end
end
class ClassExpression < ModuleExpression
end
end

View File

@ -31,6 +31,6 @@ module Parser
include Operators
include ModuleDef
rule(:root){ (module_definition | function_definition | expression | call_site ).repeat }
rule(:root){ (module_definition | class_definition | function_definition | expression | call_site ).repeat }
end
end

View File

@ -5,5 +5,11 @@ module Parser
keyword_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_end.absent? >> root).repeat(1)).as(:class_expressions) >> keyword_end >> newline
end
end
end

View File

@ -53,8 +53,12 @@ module Parser
Ast::OperatorExpression.new( o.to_s.strip , l ,r)
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)
Ast::ModuleExpression.new(name , module_expressions)
end
rule( :name => simple(:name) , :class_expressions => sequence(:class_expressions) , :end=>"end") do
Ast::ClassExpression.new(name , class_expressions)
end
#shortcut to get the ast tree for a given string

74
test/parser/test_class.rb Normal file
View File

@ -0,0 +1,74 @@
require_relative "helper"
class TestClassDef < MiniTest::Test
# include the magic (setup and parse -> test method translation), see there
include ParserHelper
def test_simplest_class
@string_input = <<HERE
class foo
5
end
HERE
@parse_output = {:name=>"foo", :class_expressions=>[{:integer=>"5"}], :end=>"end"}
@transform_output = Ast::ClassExpression.new("foo" ,[Ast::IntegerExpression.new(5)] )
@parser = @parser.class_definition
end
def test_class_ops
@string_input = <<HERE
class ops
def foo(x)
abba = 5
2 + 5
end
end
HERE
@parse_output = {:name=>"ops", :class_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::ClassExpression.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))] )] )
@parser = @parser.class_definition
end
def test_class_if
@string_input = <<HERE
class sif
def ofthen(n)
if(0)
isit = 42
else
maybenot = 667
end
end
end
HERE
@parse_output = {:name=>"sif", :class_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::ClassExpression.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))] )] )] )
@parser = @parser.class_definition
end
def test_class_function
@string_input = <<HERE
class sif
ofthen(3+4 , var)
def ofthen(n,m)
44
end
end
HERE
@parse_output = {:name=>"sif", :class_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::ClassExpression.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)] )] )
@parser = @parser.class_definition
end
def test_class_module
@string_input = <<HERE
class foo
module bar
funcall(3+4 , var)
end
end
HERE
@parse_output = {:name=>"foo", :class_expressions=>[{:name=>"bar", :module_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::ClassExpression.new(:foo ,[Ast::ModuleExpression.new(:bar ,[Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new("var")] )] )] )
@parser = @parser.class_definition
end
end

View File

@ -59,4 +59,16 @@ HERE
@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)] )] )
@parser = @parser.module_definition
end
def test_module_class
@string_input = <<HERE
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")] )] )] )
@parser = @parser.module_definition
end
end

View File

@ -89,6 +89,19 @@ HERE
@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
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")))] )] )]
end
end