diff --git a/test/parser/test_all.rb b/test/parser/test_all.rb index 1777def3..62d51d48 100644 --- a/test/parser/test_all.rb +++ b/test/parser/test_all.rb @@ -4,5 +4,6 @@ require_relative "test_arguments" require_relative "test_expressions" require_relative "test_function_call" require_relative "test_conditional" +require_relative "test_operators" require_relative "test_function_definition" require_relative "test_root" \ No newline at end of file diff --git a/test/parser/test_basic.rb b/test/parser/test_basic.rb index 04d69610..f673dbac 100644 --- a/test/parser/test_basic.rb +++ b/test/parser/test_basic.rb @@ -43,11 +43,4 @@ class TestBasic < MiniTest::Test @parser = @parser.string end - def test_assignment - @string_input = "a = 5" - @parse_output = { :asignee => { :name=>"a" } , :asigned => { :integer => "5" } } - @transform_output = Ast::AssignmentExpression.new(Ast::NameExpression.new("a"), Ast::IntegerExpression.new(5)) - @parser = @parser.assignment - end - end \ No newline at end of file diff --git a/test/parser/test_conditional.rb b/test/parser/test_conditional.rb index f08151a4..5e3977e1 100644 --- a/test/parser/test_conditional.rb +++ b/test/parser/test_conditional.rb @@ -12,9 +12,9 @@ else 667 end HERE - @parse_output = { :conditional => { :integer => "0"}, - :if_true => { :expressions => [ { :integer => "42" } ] } , - :if_false => { :expressions => [ { :integer => "667" } ] } } + @parse_output = {:if=>"if", :conditional=>{:integer=>"0"}, + :if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"}, + :if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}} @transform_output = Ast::ConditionalExpression.new( Ast::IntegerExpression.new(0), [Ast::IntegerExpression.new(42)], [Ast::IntegerExpression.new(667)]) @@ -33,12 +33,15 @@ HERE # puts(b) # n = n - 1 - @parse_output = {:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:asignee=>{:name=>"tmp"}, :asigned=>{:name=>"a"}}, {:asignee=>{:name=>"a"}, :asigned=>{:name=>"b"}}]}} + @parse_output = {:while=>"while", + :while_cond=>{:integer=>"1"}, + :do=>"do", + :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}], + :end=>"end"}} @transform_output = Ast::WhileExpression.new( - Ast::IntegerExpression.new(1), - [Ast::AssignmentExpression.new(Ast::NameExpression.new("tmp"), - Ast::NameExpression.new("a")), - Ast::AssignmentExpression.new(Ast::NameExpression.new("a"), Ast::NameExpression.new("b"))] ) + Ast::IntegerExpression.new(1), + [Ast::OperatorExpression.new("=", Ast::NameExpression.new("tmp"),Ast::NameExpression.new("a")), + Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::NameExpression.new("b"))] ) @parser = @parser.while end end \ No newline at end of file diff --git a/test/parser/test_expressions.rb b/test/parser/test_expressions.rb index 6666c428..750fff85 100644 --- a/test/parser/test_expressions.rb +++ b/test/parser/test_expressions.rb @@ -10,8 +10,8 @@ class TestExpressions < MiniTest::Test 5 else HERE - @parse_output = {:expressions=>[{:integer=>"4"}, {:integer=>"5"}]} - @transform_output = {:expressions=>[ Ast::IntegerExpression.new(4), Ast::IntegerExpression.new(5)]} + @parse_output = {:expressions=>[{:integer=>"4"}, {:integer=>"5"}], :else=>"else"} + @transform_output = {:expressions=>[ Ast::IntegerExpression.new(4), Ast::IntegerExpression.new(5)], :else=>"else"} @parser = @parser.expressions_else end @@ -22,72 +22,16 @@ name call(4,6) end HERE - @parse_output = {:expressions => [ { :integer => "5" }, - { :name => "name" }, - { :function_call => { :name => "call" } , - :argument_list => [ {:argument => { :integer => "4" } } , - {:argument => { :integer => "6" } } ] } ]} + @parse_output = {:expressions=>[{:integer=>"5"}, {:name=>"name"}, + {:function_call=>{:name=>"call"}, :argument_list=>[{:argument=>{:integer=>"4"}}, + {:argument=>{:integer=>"6"}}]}], :end=>"end"} args = [ Ast::IntegerExpression.new(4) , Ast::IntegerExpression.new(6) ] @transform_output = {:expressions=>[ Ast::IntegerExpression.new(5), Ast::NameExpression.new("name") , - Ast::FuncallExpression.new("call", args ) ] } + Ast::FuncallExpression.new("call", args ) ] , :end=>"end"} @parser = @parser.expressions_end end - def simple_op op - @string_input = "5 #{op} 3" - @parse_output = {:l=>{:integer=>"5"}, :o=>"#{op} ", :r=>{:integer=>"3"}} - @transform_output = Ast::OperatorExpression.new(op, Ast::IntegerExpression.new(5),Ast::IntegerExpression.new(3)) - @parser = @parser.operator_expression - end - def test_simple_multiply - simple_op "*" - end - def test_simple_devide - simple_op "/" - end - def test_simple_plus - simple_op "+" - end - def test_simple_minus - simple_op "-" - end - def test_op_variable - @string_input = "a + 35" - @parse_output = {:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}} - @transform_output = Ast::OperatorExpression.new("+", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(35)) - @parser = @parser.operator_expression - end - def test_op_two_variable - @string_input = "a - b" - @parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:name=>"b"}} - @transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::NameExpression.new("b")) - @parser = @parser.operator_expression - end - def test_op_variable_string - @string_input = 'a - "st"' - @parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}} - @transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::StringExpression.new("st")) - @parser = @parser.operator_expression - end - def test_two_same_ops - @string_input = '2 + 3 + 4' - @parse_output = {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"4"}}} - @transform_output = Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4))) - @parser = @parser.operator_expression - end - def test_two_different_ops - @string_input = '2 + 3 * 4' - @parse_output = {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:l=>{:integer=>"3"}, :o=>"* ", :r=>{:integer=>"4"}}} - @transform_output = Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4))) - @parser = @parser.operator_expression - end - def test_two_different_ops_order - @string_input = '2 * 3 + 4' - @parse_output = {:l=>{:l=>{:integer=>"2"}, :o=>"* ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}} - @transform_output = Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4)) - @parser = @parser.operator_expression - end end \ No newline at end of file diff --git a/test/parser/test_function_definition.rb b/test/parser/test_function_definition.rb index 755f3c9e..56b200a0 100644 --- a/test/parser/test_function_definition.rb +++ b/test/parser/test_function_definition.rb @@ -10,9 +10,8 @@ def foo(x) 5 end HERE - @parse_output = {:function_definition => {:name => 'foo'}, - :parmeter_list => [{:parmeter => {:name => 'x'}}], - :expressions => [{:integer => '5'}]} + @parse_output = {:function_definition=>{: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)]) @@ -25,12 +24,10 @@ def foo(x) abba = 5 end HERE - @parse_output = { :function_definition => { :name => "foo" } , - :parmeter_list => [{ :parmeter => { :name => "x" } }], - :expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ] - } - @transform_output = Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , - [Ast::AssignmentExpression.new(Ast::NameExpression.new("abba"), Ast::IntegerExpression.new(5))] ) + @parse_output = {:function_definition=>{:name=>"foo"}, + :parmeter_list=>[{:parmeter=>{:name=>"x"}}], + :expressions=>[{:l=>{:name=>"abba"}, :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))] ) @parser = @parser.function_definition end @@ -52,8 +49,7 @@ HERE :parmeter_list => [{ :parmeter => { :name => "x" } }], :expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ] } - @transform_output = Ast::FunctionExpression.new( "foo", [Ast::NameExpression.new("x")], - [Ast::AssignmentExpression.new( "abba", Ast::IntegerExpression.new(5) ) ]) + @transform_output = nil @parser = @parser.function_definition end end \ No newline at end of file diff --git a/test/parser/test_operators.rb b/test/parser/test_operators.rb new file mode 100644 index 00000000..fcb35e2e --- /dev/null +++ b/test/parser/test_operators.rb @@ -0,0 +1,68 @@ +require_relative "helper" + +class TestExpressions < MiniTest::Test + # include the magic (setup and parse -> test method translation), see there + include ParserHelper + + def simple_op op + @string_input = "5 #{op} 3" + @parse_output = {:l=>{:integer=>"5"}, :o=>"#{op} ", :r=>{:integer=>"3"}} + @transform_output = Ast::OperatorExpression.new(op, Ast::IntegerExpression.new(5),Ast::IntegerExpression.new(3)) + @parser = @parser.operator_expression + end + def test_simple_multiply + simple_op "*" + end + def test_simple_devide + simple_op "/" + end + def test_simple_plus + simple_op "+" + end + def test_simple_minus + simple_op "-" + end + def test_op_variable + @string_input = "a + 35" + @parse_output = {:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}} + @transform_output = Ast::OperatorExpression.new("+", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(35)) + @parser = @parser.operator_expression + end + def test_op_two_variable + @string_input = "a - b" + @parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:name=>"b"}} + @transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::NameExpression.new("b")) + @parser = @parser.operator_expression + end + def test_op_variable_string + @string_input = 'a - "st"' + @parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}} + @transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::StringExpression.new("st")) + @parser = @parser.operator_expression + end + def test_two_same_ops + @string_input = '2 + 3 + 4' + @parse_output = {:l=>{:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}} + @transform_output = Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4)) + @parser = @parser.operator_expression + end + def test_two_different_ops + @string_input = '2 + 3 * 4' + @parse_output = {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:l=>{:integer=>"3"}, :o=>"* ", :r=>{:integer=>"4"}}} + @transform_output = Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4))) + @parser = @parser.operator_expression + end + def test_two_different_ops_order + @string_input = '2 * 3 + 4' + @parse_output = {:l=>{:l=>{:integer=>"2"}, :o=>"* ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}} + @transform_output = Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4)) + @parser = @parser.operator_expression + end + def test_assignment + @string_input = "a = 5" + @parse_output = {:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}} + @transform_output = Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(5)) + @parser = @parser.operator_expression + end + +end \ No newline at end of file diff --git a/test/parser/test_root.rb b/test/parser/test_root.rb index d3823e4d..8648ff06 100644 --- a/test/parser/test_root.rb +++ b/test/parser/test_root.rb @@ -12,13 +12,14 @@ end foo( 3 ) HERE - @parse_output = [ - {:function_definition=> { :name=>"foo"}, :parmeter_list => [ {:parmeter=>{:name=>"x"}} ] , - :expressions=>[{:asignee=>{:name=>"a"}, :asigned=>{:integer=>"5"} } ] } , - {:function_call => { :name => "foo" }, :argument_list => [ { :argument => { :integer => "3" } } ] }] - @transform_output = [Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , - [Ast::AssignmentExpression.new(Ast::NameExpression.new("a"), Ast::IntegerExpression.new(5))] ), - Ast::FuncallExpression.new("foo", [Ast::IntegerExpression.new(3)] )] + @parse_output = [{:function_definition=>{:name=>"foo"}, + :parmeter_list=>[{:parmeter=>{:name=>"x"}}], + :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}, + {:function_call=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}] + @transform_output = [Ast::FunctionExpression.new(:foo, + [Ast::NameExpression.new("x")] , + [Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(5))] ), + Ast::FuncallExpression.new("foo", [Ast::IntegerExpression.new(3)] )] end