diff --git a/test/cases/basic/module_name.tst b/test/cases/basic/module_name.tst new file mode 100644 index 0000000..dee3471 --- /dev/null +++ b/test/cases/basic/module_name.tst @@ -0,0 +1,3 @@ +FooBar +-- -- -- +s(:list, [s(:module, "FooBar")]) diff --git a/test/cases/basic/name.tst b/test/cases/basic/name.tst new file mode 100644 index 0000000..4c15a7c --- /dev/null +++ b/test/cases/basic/name.tst @@ -0,0 +1,3 @@ +foo +-- -- -- +s(:list, [s(:name, "foo")]) diff --git a/test/cases/basic/name_underscode_middle.tst b/test/cases/basic/name_underscode_middle.tst new file mode 100644 index 0000000..29135b8 --- /dev/null +++ b/test/cases/basic/name_underscode_middle.tst @@ -0,0 +1,3 @@ +foo_bar +-- -- -- +s(:list, [s(:name, "foo_bar")]) diff --git a/test/cases/basic/name_underscode_start.tst b/test/cases/basic/name_underscode_start.tst new file mode 100644 index 0000000..2913c37 --- /dev/null +++ b/test/cases/basic/name_underscode_start.tst @@ -0,0 +1,3 @@ +_bar +-- -- -- +s(:list, [s(:name, "_bar")]) diff --git a/test/cases/basic/number.tst b/test/cases/basic/number.tst new file mode 100644 index 0000000..cc9ba29 --- /dev/null +++ b/test/cases/basic/number.tst @@ -0,0 +1,3 @@ +42 +-- -- -- +s(:list, [s(:int, 42)]) diff --git a/test/cases/basic/string.tst b/test/cases/basic/string.tst new file mode 100644 index 0000000..4436dd0 --- /dev/null +++ b/test/cases/basic/string.tst @@ -0,0 +1,3 @@ +"hello" +-- -- -- +s(:list, [s(:string, "hello")]) diff --git a/test/cases/basic/string_escapes.tst b/test/cases/basic/string_escapes.tst new file mode 100644 index 0000000..33bab13 --- /dev/null +++ b/test/cases/basic/string_escapes.tst @@ -0,0 +1,3 @@ +"hello \nyou" +-- -- -- +s(:list, [s(:string, "hello \\nyou")]) diff --git a/test/cases/call_site/call_puts_two.tst b/test/cases/call_site/call_puts_two.tst new file mode 100644 index 0000000..3efca82 --- /dev/null +++ b/test/cases/call_site/call_puts_two.tst @@ -0,0 +1,7 @@ +puts(3 , a ) +-- -- -- +s(:list, [s(:call, + s(:name, "puts"), + s(:arguments, + s(:int, 3), + s(:name, "a")))]) diff --git a/test/cases/call_site/call_site_multi.tst b/test/cases/call_site/call_site_multi.tst new file mode 100644 index 0000000..74c7320 --- /dev/null +++ b/test/cases/call_site/call_site_multi.tst @@ -0,0 +1,7 @@ +baz(42, foo) +-- -- -- +s(:list, [s(:call, + s(:name, "baz"), + s(:arguments, + s(:int, 42), + s(:name, "foo")))]) diff --git a/test/cases/call_site/call_site_string.tst b/test/cases/call_site/call_site_string.tst new file mode 100644 index 0000000..de214a2 --- /dev/null +++ b/test/cases/call_site/call_site_string.tst @@ -0,0 +1,6 @@ +puts( "hello") +-- -- -- +s(:list, [s(:call, + s(:name, "puts"), + s(:arguments, + s(:string, "hello")))]) diff --git a/test/cases/call_site/int_receiver.tst b/test/cases/call_site/int_receiver.tst new file mode 100644 index 0000000..2790ad6 --- /dev/null +++ b/test/cases/call_site/int_receiver.tst @@ -0,0 +1,7 @@ +42.put() +-- -- -- +s(:list, [s(:call, + s(:name, "put"), + s(:arguments), + s(:receiver, + s(:int, 42)))]) diff --git a/test/cases/call_site/puts_call.tst b/test/cases/call_site/puts_call.tst new file mode 100644 index 0000000..be2d260 --- /dev/null +++ b/test/cases/call_site/puts_call.tst @@ -0,0 +1,6 @@ +puts( 5) +-- -- -- +s(:list, [s(:call, + s(:name, "puts"), + s(:arguments, + s(:int, 5)))]) diff --git a/test/cases/call_site/single_argument.tst b/test/cases/call_site/single_argument.tst new file mode 100644 index 0000000..64f411c --- /dev/null +++ b/test/cases/call_site/single_argument.tst @@ -0,0 +1,6 @@ +foo(42) +-- -- -- +s(:list, [s(:call, + s(:name, "foo"), + s(:arguments, + s(:int, 42)))]) diff --git a/test/cases/call_site/single_class.tst b/test/cases/call_site/single_class.tst new file mode 100644 index 0000000..6fa105e --- /dev/null +++ b/test/cases/call_site/single_class.tst @@ -0,0 +1,8 @@ +Object.foo(42) +-- -- -- +s(:list, [s(:call, + s(:name, "foo"), + s(:arguments, + s(:int, 42)), + s(:receiver, + s(:module, "Object")))]) diff --git a/test/cases/call_site/single_name.tst b/test/cases/call_site/single_name.tst new file mode 100644 index 0000000..3a7589e --- /dev/null +++ b/test/cases/call_site/single_name.tst @@ -0,0 +1,8 @@ +my_my.foo(42) +-- -- -- +s(:list, [s(:call, + s(:name, "foo"), + s(:arguments, + s(:int, 42)), + s(:receiver, + s(:name, "my_my")))]) diff --git a/test/cases/call_site/single_self.tst b/test/cases/call_site/single_self.tst new file mode 100644 index 0000000..9eea1d1 --- /dev/null +++ b/test/cases/call_site/single_self.tst @@ -0,0 +1,8 @@ +self.foo(42) +-- -- -- +s(:list, [s(:call, + s(:name, "foo"), + s(:arguments, + s(:int, 42)), + s(:receiver, + s(:name, "self")))]) diff --git a/test/cases/call_site/string_receiver.tst b/test/cases/call_site/string_receiver.tst new file mode 100644 index 0000000..0bd4ae2 --- /dev/null +++ b/test/cases/call_site/string_receiver.tst @@ -0,0 +1,7 @@ +"hello".puts() +-- -- -- +s(:list, [s(:call, + s(:name, "puts"), + s(:arguments), + s(:receiver, + s(:string, "hello")))]) diff --git a/test/cases/class_def/class_derived.tst b/test/cases/class_def/class_derived.tst new file mode 100644 index 0000000..778f96f --- /dev/null +++ b/test/cases/class_def/class_derived.tst @@ -0,0 +1,10 @@ +class Foo < Object + ofthen(3 , var) +end +-- -- -- +s(:list, [s(:class, "Foo", + s(:module, "Object"), [s(:call, + s(:name, "ofthen"), + s(:arguments, + s(:int, 3), + s(:name, "var")))])]) diff --git a/test/cases/class_def/class_function.tst b/test/cases/class_def/class_function.tst new file mode 100644 index 0000000..b2a1808 --- /dev/null +++ b/test/cases/class_def/class_function.tst @@ -0,0 +1,18 @@ +class Pifi + ofthen(3 , var) + int ofthen(int n , ref m) + 44 + end +end +-- -- -- +s(:list, [s(:class, "Pifi", nil, [s(:call, + s(:name, "ofthen"), + s(:arguments, + s(:int, 3), + s(:name, "var"))), s(:function, :int, + s(:name, "ofthen"), + s(:parameters, + s(:field, :int, :n), + s(:field, :ref, :m)), + s(:expressions, + s(:int, 44)))])]) diff --git a/test/cases/class_def/class_if.tst b/test/cases/class_def/class_if.tst new file mode 100644 index 0000000..ad88c5e --- /dev/null +++ b/test/cases/class_def/class_if.tst @@ -0,0 +1,25 @@ +class Ifi + int ofthen(int n) + if(0) + isit = 42 + else + maybenot = 667 + end + end +end +-- -- -- +s(:list, [s(:class, "Ifi", nil, [s(:function, :int, + s(:name, "ofthen"), + s(:parameters, + s(:field, :int, :n)), + s(:expressions, + s(:if, + s(:condition, + s(:int, 0)), + s(:if_true, + s(:assign, + s(:name, "isit"), + s(:int, 42))), + s(:if_false, [s(:assign, + s(:name, "maybenot"), + s(:int, 667))]))))])]) diff --git a/test/cases/class_def/class_method.tst b/test/cases/class_def/class_method.tst new file mode 100644 index 0000000..6b73e92 --- /dev/null +++ b/test/cases/class_def/class_method.tst @@ -0,0 +1,7 @@ +class Foo < Object + int Foo.test() + 43 + end +end +-- -- -- +{:expression_list=>[{:module_name=>"Foo", :derived_name=>s(:module, "Object"), :class_expressions=>[{:type=>"int", :receiver=>s(:module, "Foo"), :function_name=>s(:name, "test"), :parameter_list=>[], :expressions=>[s(:int, 43)], :end=>"end"}], :end=>"end"}]} diff --git a/test/cases/class_def/class_module.tst b/test/cases/class_def/class_module.tst new file mode 100644 index 0000000..bdebcad --- /dev/null +++ b/test/cases/class_def/class_module.tst @@ -0,0 +1,11 @@ +class Foo + module Boo + funcall(3 , var) + end +end +-- -- -- +s(:list, [s(:class, "Foo", nil, [s(:module, "Boo", [s(:call, + s(:name, "funcall"), + s(:arguments, + s(:int, 3), + s(:name, "var")))])])]) diff --git a/test/cases/class_def/class_ops.tst b/test/cases/class_def/class_ops.tst new file mode 100644 index 0000000..c2bbdb8 --- /dev/null +++ b/test/cases/class_def/class_ops.tst @@ -0,0 +1,19 @@ +class Opers + int foo(int x) + int abba = 5 + abba + 5 + end +end +-- -- -- +s(:list, [s(:class, "Opers", nil, [s(:function, :int, + s(:name, "foo"), + s(:parameters, + s(:field, :int, :x)), + s(:expressions, + s(:name, "int"), + s(:assign, + s(:name, "abba"), + s(:int, 5)), + s(:operator, "+", + s(:name, "abba"), + s(:int, 5))))])]) diff --git a/test/cases/class_def/simplest_class.tst b/test/cases/class_def/simplest_class.tst new file mode 100644 index 0000000..c8dd9e2 --- /dev/null +++ b/test/cases/class_def/simplest_class.tst @@ -0,0 +1,5 @@ +class Foo + 5 +end +-- -- -- +s(:list, [s(:class, "Foo", nil, [s(:int, 5)])]) diff --git a/test/cases/compound/array_list.tst b/test/cases/compound/array_list.tst new file mode 100644 index 0000000..4ce4a9e --- /dev/null +++ b/test/cases/compound/array_list.tst @@ -0,0 +1,3 @@ +[42, foo] +-- -- -- +s(:list, [s(:array, [s(:int, 42), s(:name, "foo")])]) diff --git a/test/cases/compound/array_ops.tst b/test/cases/compound/array_ops.tst new file mode 100644 index 0000000..546b1c6 --- /dev/null +++ b/test/cases/compound/array_ops.tst @@ -0,0 +1,8 @@ +[ 3 + 4 , foo(22) ] +-- -- -- +s(:list, [s(:array, [s(:operator, "+", + s(:int, 3), + s(:int, 4)), s(:call, + s(:name, "foo"), + s(:arguments, + s(:int, 22)))])]) diff --git a/test/cases/compound/hash.tst b/test/cases/compound/hash.tst new file mode 100644 index 0000000..264f417 --- /dev/null +++ b/test/cases/compound/hash.tst @@ -0,0 +1,5 @@ +{ foo => 33 } +-- -- -- +s(:list, [s(:hash, [s(:assoc, + s(:name, "foo"), + s(:int, 33))])]) diff --git a/test/cases/compound/hash2.tst b/test/cases/compound/hash2.tst new file mode 100644 index 0000000..9e1246c --- /dev/null +++ b/test/cases/compound/hash2.tst @@ -0,0 +1,5 @@ +{ foo => true } +-- -- -- +s(:list, [s(:hash, [s(:assoc, + s(:name, "foo"), + s(:true))])]) diff --git a/test/cases/compound/hash_list.tst b/test/cases/compound/hash_list.tst new file mode 100644 index 0000000..22d820b --- /dev/null +++ b/test/cases/compound/hash_list.tst @@ -0,0 +1,7 @@ +{foo => 33 , bar => 42} +-- -- -- +s(:list, [s(:hash, [s(:assoc, + s(:name, "foo"), + s(:int, 33)), s(:assoc, + s(:name, "bar"), + s(:int, 42))])]) diff --git a/test/cases/compound/one_array.tst b/test/cases/compound/one_array.tst new file mode 100644 index 0000000..79f7884 --- /dev/null +++ b/test/cases/compound/one_array.tst @@ -0,0 +1,3 @@ +[42] +-- -- -- +s(:list, [s(:array, [s(:int, 42)])]) diff --git a/test/cases/conditional/if_else.tst b/test/cases/conditional/if_else.tst new file mode 100644 index 0000000..0a90dc4 --- /dev/null +++ b/test/cases/conditional/if_else.tst @@ -0,0 +1,12 @@ +if(0) + 42 +else + 667 +end +-- -- -- +s(:list, [s(:if, + s(:condition, + s(:int, 0)), + s(:if_true, + s(:int, 42)), + s(:if_false, [s(:int, 667)]))]) diff --git a/test/cases/conditional/if_else_expressions.tst b/test/cases/conditional/if_else_expressions.tst new file mode 100644 index 0000000..bac2eaa --- /dev/null +++ b/test/cases/conditional/if_else_expressions.tst @@ -0,0 +1,24 @@ +if(3 > var) + Object.initialize(3) +else + var.new(33) +end +-- -- -- +s(:list, [s(:if, + s(:condition, + s(:operator, ">", + s(:int, 3), + s(:name, "var"))), + s(:if_true, + s(:call, + s(:name, "initialize"), + s(:arguments, + s(:int, 3)), + s(:receiver, + s(:module, "Object")))), + s(:if_false, [s(:call, + s(:name, "new"), + s(:arguments, + s(:int, 33)), + s(:receiver, + s(:name, "var")))]))]) diff --git a/test/cases/conditional/if_end.tst b/test/cases/conditional/if_end.tst new file mode 100644 index 0000000..c813e70 --- /dev/null +++ b/test/cases/conditional/if_end.tst @@ -0,0 +1,10 @@ +if(0) + 42 +end +-- -- -- +s(:list, [s(:if, + s(:condition, + s(:int, 0)), + s(:if_true, + s(:int, 42)), + s(:if_false, nil))]) diff --git a/test/cases/conditional/if_end_expressions.tst b/test/cases/conditional/if_end_expressions.tst new file mode 100644 index 0000000..6ef7d40 --- /dev/null +++ b/test/cases/conditional/if_end_expressions.tst @@ -0,0 +1,17 @@ +if(3 > var) + Object.initialize(3) +end +-- -- -- +s(:list, [s(:if, + s(:condition, + s(:operator, ">", + s(:int, 3), + s(:name, "var"))), + s(:if_true, + s(:call, + s(:name, "initialize"), + s(:arguments, + s(:int, 3)), + s(:receiver, + s(:module, "Object")))), + s(:if_false, nil))]) diff --git a/test/cases/expressions/assignment.tst b/test/cases/expressions/assignment.tst new file mode 100644 index 0000000..0063a8e --- /dev/null +++ b/test/cases/expressions/assignment.tst @@ -0,0 +1,5 @@ +a = 5 +-- -- -- +s(:list, [s(:assign, + s(:name, "a"), + s(:int, 5))]) diff --git a/test/cases/expressions/assignment_instance.tst b/test/cases/expressions/assignment_instance.tst new file mode 100644 index 0000000..0063a8e --- /dev/null +++ b/test/cases/expressions/assignment_instance.tst @@ -0,0 +1,5 @@ +a = 5 +-- -- -- +s(:list, [s(:assign, + s(:name, "a"), + s(:int, 5))]) diff --git a/test/cases/expressions/op_two_variable.tst b/test/cases/expressions/op_two_variable.tst new file mode 100644 index 0000000..8b88b09 --- /dev/null +++ b/test/cases/expressions/op_two_variable.tst @@ -0,0 +1,5 @@ +a - b +-- -- -- +s(:list, [s(:operator, "-", + s(:name, "a"), + s(:name, "b"))]) diff --git a/test/cases/expressions/op_variable.tst b/test/cases/expressions/op_variable.tst new file mode 100644 index 0000000..55ca48a --- /dev/null +++ b/test/cases/expressions/op_variable.tst @@ -0,0 +1,5 @@ +a - 5 +-- -- -- +s(:list, [s(:operator, "-", + s(:name, "a"), + s(:int, 5))]) diff --git a/test/cases/expressions/op_variable_string.tst b/test/cases/expressions/op_variable_string.tst new file mode 100644 index 0000000..d4c5afc --- /dev/null +++ b/test/cases/expressions/op_variable_string.tst @@ -0,0 +1,5 @@ +a - "st" +-- -- -- +s(:list, [s(:operator, "-", + s(:name, "a"), + s(:string, "st"))]) diff --git a/test/cases/expressions/op_variable_true.tst b/test/cases/expressions/op_variable_true.tst new file mode 100644 index 0000000..51a2d24 --- /dev/null +++ b/test/cases/expressions/op_variable_true.tst @@ -0,0 +1,5 @@ +a == true +-- -- -- +s(:list, [s(:operator, "==", + s(:name, "a"), + s(:true))]) diff --git a/test/cases/expressions/simple_devide.tst b/test/cases/expressions/simple_devide.tst new file mode 100644 index 0000000..7837959 --- /dev/null +++ b/test/cases/expressions/simple_devide.tst @@ -0,0 +1,5 @@ +5 / 3 +-- -- -- +s(:list, [s(:operator, "/", + s(:int, 5), + s(:int, 3))]) diff --git a/test/cases/expressions/simple_greater.tst b/test/cases/expressions/simple_greater.tst new file mode 100644 index 0000000..9877ea8 --- /dev/null +++ b/test/cases/expressions/simple_greater.tst @@ -0,0 +1,5 @@ +5 > 3 +-- -- -- +s(:list, [s(:operator, ">", + s(:int, 5), + s(:int, 3))]) diff --git a/test/cases/expressions/simple_minus.tst b/test/cases/expressions/simple_minus.tst new file mode 100644 index 0000000..bcbe077 --- /dev/null +++ b/test/cases/expressions/simple_minus.tst @@ -0,0 +1,5 @@ +5 - 3 +-- -- -- +s(:list, [s(:operator, "-", + s(:int, 5), + s(:int, 3))]) diff --git a/test/cases/expressions/simple_multiply.tst b/test/cases/expressions/simple_multiply.tst new file mode 100644 index 0000000..b04262b --- /dev/null +++ b/test/cases/expressions/simple_multiply.tst @@ -0,0 +1,5 @@ +5 * 3 +-- -- -- +s(:list, [s(:operator, "*", + s(:int, 5), + s(:int, 3))]) diff --git a/test/cases/expressions/simple_plus.tst b/test/cases/expressions/simple_plus.tst new file mode 100644 index 0000000..cb5efd0 --- /dev/null +++ b/test/cases/expressions/simple_plus.tst @@ -0,0 +1,5 @@ +5 + 3 +-- -- -- +s(:list, [s(:operator, "+", + s(:int, 5), + s(:int, 3))]) diff --git a/test/cases/expressions/simple_smaller.tst b/test/cases/expressions/simple_smaller.tst new file mode 100644 index 0000000..8b20ae7 --- /dev/null +++ b/test/cases/expressions/simple_smaller.tst @@ -0,0 +1,5 @@ +5 < 3 +-- -- -- +s(:list, [s(:operator, "<", + s(:int, 5), + s(:int, 3))]) diff --git a/test/cases/expressions/two_different_ops.tst b/test/cases/expressions/two_different_ops.tst new file mode 100644 index 0000000..dd1b4dd --- /dev/null +++ b/test/cases/expressions/two_different_ops.tst @@ -0,0 +1,7 @@ +2 + 3 * 4 +-- -- -- +s(:list, [s(:operator, "+", + s(:int, 2), + s(:operator, "*", + s(:int, 3), + s(:int, 4)))]) diff --git a/test/cases/expressions/two_different_ops_order.tst b/test/cases/expressions/two_different_ops_order.tst new file mode 100644 index 0000000..c2274b9 --- /dev/null +++ b/test/cases/expressions/two_different_ops_order.tst @@ -0,0 +1,7 @@ +2 * 3 + 4 +-- -- -- +s(:list, [s(:operator, "+", + s(:operator, "*", + s(:int, 2), + s(:int, 3)), + s(:int, 4))]) diff --git a/test/cases/expressions/two_same_ops.tst b/test/cases/expressions/two_same_ops.tst new file mode 100644 index 0000000..0851802 --- /dev/null +++ b/test/cases/expressions/two_same_ops.tst @@ -0,0 +1,7 @@ +2 + 3 + 4 +-- -- -- +s(:list, [s(:operator, "+", + s(:operator, "+", + s(:int, 2), + s(:int, 3)), + s(:int, 4))]) diff --git a/test/cases/function_definition/class_function.tst b/test/cases/function_definition/class_function.tst new file mode 100644 index 0000000..f696c99 --- /dev/null +++ b/test/cases/function_definition/class_function.tst @@ -0,0 +1,5 @@ +int String.length( ref x ) + length +end +-- -- -- +{:expression_list=>[{:type=>"int", :receiver=>s(:module, "String"), :function_name=>s(:name, "length"), :parameter_list=>[s(:field, :ref, :x)], :expressions=>[s(:name, "length")], :end=>"end"}]} diff --git a/test/cases/function_definition/function_big_while.tst b/test/cases/function_definition/function_big_while.tst new file mode 100644 index 0000000..a501dbd --- /dev/null +++ b/test/cases/function_definition/function_big_while.tst @@ -0,0 +1,49 @@ +int fibonaccit(int n) + a = 0 + b = 1 + while( n > 1 ) + tmp = a + a = b + b = tmp + b + puts(b) + n = n - 1 + end +end +-- -- -- +s(:list, [s(:function, :int, + s(:name, "fibonaccit"), + s(:parameters, + s(:field, :int, :n)), + s(:expressions, + s(:assign, + s(:name, "a"), + s(:int, 0)), + s(:assign, + s(:name, "b"), + s(:int, 1)), + s(:while, + s(:condition, + s(:operator, ">", + s(:name, "n"), + s(:int, 1))), + s(:expressions, + s(:assign, + s(:name, "tmp"), + s(:name, "a")), + s(:assign, + s(:name, "a"), + s(:name, "b")), + s(:assign, + s(:name, "b"), + s(:operator, "+", + s(:name, "tmp"), + s(:name, "b"))), + s(:call, + s(:name, "puts"), + s(:arguments, + s(:name, "b"))), + s(:assign, + s(:name, "n"), + s(:operator, "-", + s(:name, "n"), + s(:int, 1)))))))]) diff --git a/test/cases/function_definition/function_if.tst b/test/cases/function_definition/function_if.tst new file mode 100644 index 0000000..6c96cc2 --- /dev/null +++ b/test/cases/function_definition/function_if.tst @@ -0,0 +1,23 @@ +ref ofthen(int n) + if(0) + isit = 42 + else + maybenot = 667 + end +end +-- -- -- +s(:list, [s(:function, :ref, + s(:name, "ofthen"), + s(:parameters, + s(:field, :int, :n)), + s(:expressions, + s(:if, + s(:condition, + s(:int, 0)), + s(:if_true, + s(:assign, + s(:name, "isit"), + s(:int, 42))), + s(:if_false, [s(:assign, + s(:name, "maybenot"), + s(:int, 667))]))))]) diff --git a/test/cases/function_definition/function_no_arg.tst b/test/cases/function_definition/function_no_arg.tst new file mode 100644 index 0000000..a855b70 --- /dev/null +++ b/test/cases/function_definition/function_no_arg.tst @@ -0,0 +1,9 @@ +int foo() + 5 +end +-- -- -- +s(:list, [s(:function, :int, + s(:name, "foo"), + s(:parameters), + s(:expressions, + s(:int, 5)))]) diff --git a/test/cases/function_definition/function_ops.tst b/test/cases/function_definition/function_ops.tst new file mode 100644 index 0000000..c9e8fca --- /dev/null +++ b/test/cases/function_definition/function_ops.tst @@ -0,0 +1,17 @@ +int foo(int x) + int abba = 5 + abba + 5 +end +-- -- -- +s(:list, [s(:function, :int, + s(:name, "foo"), + s(:parameters, + s(:field, :int, :x)), + s(:expressions, + s(:name, "int"), + s(:assign, + s(:name, "abba"), + s(:int, 5)), + s(:operator, "+", + s(:name, "abba"), + s(:int, 5))))]) diff --git a/test/cases/function_definition/function_return.tst b/test/cases/function_definition/function_return.tst new file mode 100644 index 0000000..7494de8 --- /dev/null +++ b/test/cases/function_definition/function_return.tst @@ -0,0 +1,16 @@ +int retvar(ref n) + int i = 5 + return i +end +-- -- -- +s(:list, [s(:function, :int, + s(:name, "retvar"), + s(:parameters, + s(:field, :ref, :n)), + s(:expressions, + s(:name, "int"), + s(:assign, + s(:name, "i"), + s(:int, 5)), + s(:return, + s(:name, "i"))))]) diff --git a/test/cases/function_definition/function_return_if.tst b/test/cases/function_definition/function_return_if.tst new file mode 100644 index 0000000..a6b7642 --- /dev/null +++ b/test/cases/function_definition/function_return_if.tst @@ -0,0 +1,23 @@ +int retvar(int n) + if( n > 5) + return 10 + else + return 20 + end +end +-- -- -- +s(:list, [s(:function, :int, + s(:name, "retvar"), + s(:parameters, + s(:field, :int, :n)), + s(:expressions, + s(:if, + s(:condition, + s(:operator, ">", + s(:name, "n"), + s(:int, 5))), + s(:if_true, + s(:return, + s(:int, 10))), + s(:if_false, [s(:return, + s(:int, 20))]))))]) diff --git a/test/cases/function_definition/function_return_while.tst b/test/cases/function_definition/function_return_while.tst new file mode 100644 index 0000000..bb56065 --- /dev/null +++ b/test/cases/function_definition/function_return_while.tst @@ -0,0 +1,25 @@ +int retvar(int n ) + while( n > 5) + n = n + 1 + return n + end +end +-- -- -- +s(:list, [s(:function, :int, + s(:name, "retvar"), + s(:parameters, + s(:field, :int, :n)), + s(:expressions, + s(:while, + s(:condition, + s(:operator, ">", + s(:name, "n"), + s(:int, 5))), + s(:expressions, + s(:assign, + s(:name, "n"), + s(:operator, "+", + s(:name, "n"), + s(:int, 1))), + s(:return, + s(:name, "n"))))))]) diff --git a/test/cases/function_definition/function_two_args.tst b/test/cases/function_definition/function_two_args.tst new file mode 100644 index 0000000..9447225 --- /dev/null +++ b/test/cases/function_definition/function_two_args.tst @@ -0,0 +1,11 @@ +int foo( int n ,ref m) + n +end +-- -- -- +s(:list, [s(:function, :int, + s(:name, "foo"), + s(:parameters, + s(:field, :int, :n), + s(:field, :ref, :m)), + s(:expressions, + s(:name, "n")))]) diff --git a/test/cases/function_definition/function_while.tst b/test/cases/function_definition/function_while.tst new file mode 100644 index 0000000..6876c51 --- /dev/null +++ b/test/cases/function_definition/function_while.tst @@ -0,0 +1,28 @@ +ref fibonaccit(int n) + a = 0 + while(n) + some = 43 + other = some * 4 + end +end +-- -- -- +s(:list, [s(:function, :ref, + s(:name, "fibonaccit"), + s(:parameters, + s(:field, :int, :n)), + s(:expressions, + s(:assign, + s(:name, "a"), + s(:int, 0)), + s(:while, + s(:condition, + s(:name, "n")), + s(:expressions, + s(:assign, + s(:name, "some"), + s(:int, 43)), + s(:assign, + s(:name, "other"), + s(:operator, "*", + s(:name, "some"), + s(:int, 4)))))))]) diff --git a/test/cases/function_definition/simplest_function.tst b/test/cases/function_definition/simplest_function.tst new file mode 100644 index 0000000..56ca1c5 --- /dev/null +++ b/test/cases/function_definition/simplest_function.tst @@ -0,0 +1,10 @@ +int foo(ref x) + 5 +end +-- -- -- +s(:list, [s(:function, :int, + s(:name, "foo"), + s(:parameters, + s(:field, :ref, :x)), + s(:expressions, + s(:int, 5)))]) diff --git a/test/cases/module_def/module_assign_var.tst b/test/cases/module_def/module_assign_var.tst new file mode 100644 index 0000000..dabc319 --- /dev/null +++ b/test/cases/module_def/module_assign_var.tst @@ -0,0 +1,7 @@ +module Opers + abba = 5 +end +-- -- -- +s(:list, [s(:module, "Opers", [s(:assign, + s(:name, "abba"), + s(:int, 5))])]) diff --git a/test/cases/module_def/module_class.tst b/test/cases/module_def/module_class.tst new file mode 100644 index 0000000..431e457 --- /dev/null +++ b/test/cases/module_def/module_class.tst @@ -0,0 +1,11 @@ +module Foo + class Bar + funcall(3 , var) + end +end +-- -- -- +s(:list, [s(:module, "Foo", [s(:class, "Bar", nil, [s(:call, + s(:name, "funcall"), + s(:arguments, + s(:int, 3), + s(:name, "var")))])])]) diff --git a/test/cases/module_def/module_function.tst b/test/cases/module_def/module_function.tst new file mode 100644 index 0000000..e47cc6e --- /dev/null +++ b/test/cases/module_def/module_function.tst @@ -0,0 +1,19 @@ +module Soho + ofthen(3 , var) + int ofthen(int n,ref m ) + return 44 + end +end +-- -- -- +s(:list, [s(:module, "Soho", [s(:call, + s(:name, "ofthen"), + s(:arguments, + s(:int, 3), + s(:name, "var"))), s(:function, :int, + s(:name, "ofthen"), + s(:parameters, + s(:field, :int, :n), + s(:field, :ref, :m)), + s(:expressions, + s(:return, + s(:int, 44))))])]) diff --git a/test/cases/module_def/module_if.tst b/test/cases/module_def/module_if.tst new file mode 100644 index 0000000..555a4e5 --- /dev/null +++ b/test/cases/module_def/module_if.tst @@ -0,0 +1,25 @@ +module Foo + ref ofthen(int n) + if(0) + isit = 42 + else + maybenot = 667 + end + end +end +-- -- -- +s(:list, [s(:module, "Foo", [s(:function, :ref, + s(:name, "ofthen"), + s(:parameters, + s(:field, :int, :n)), + s(:expressions, + s(:if, + s(:condition, + s(:int, 0)), + s(:if_true, + s(:assign, + s(:name, "isit"), + s(:int, 42))), + s(:if_false, [s(:assign, + s(:name, "maybenot"), + s(:int, 667))]))))])]) diff --git a/test/cases/module_def/module_ops.tst b/test/cases/module_def/module_ops.tst new file mode 100644 index 0000000..d406e20 --- /dev/null +++ b/test/cases/module_def/module_ops.tst @@ -0,0 +1,20 @@ +module Opers + int foo(int x) + int abba = 5 + return abba + 5 + end +end +-- -- -- +s(:list, [s(:module, "Opers", [s(:function, :int, + s(:name, "foo"), + s(:parameters, + s(:field, :int, :x)), + s(:expressions, + s(:name, "int"), + s(:assign, + s(:name, "abba"), + s(:int, 5)), + s(:return, + s(:operator, "+", + s(:name, "abba"), + s(:int, 5)))))])]) diff --git a/test/cases/module_def/simplest_module.tst b/test/cases/module_def/simplest_module.tst new file mode 100644 index 0000000..99f9375 --- /dev/null +++ b/test/cases/module_def/simplest_module.tst @@ -0,0 +1,5 @@ +module Simple + 5 +end +-- -- -- +s(:list, [s(:module, "Simple", [s(:int, 5)])]) diff --git a/test/cases/return/return_int.tst b/test/cases/return/return_int.tst new file mode 100644 index 0000000..c79a659 --- /dev/null +++ b/test/cases/return/return_int.tst @@ -0,0 +1,4 @@ +return 42 +-- -- -- +s(:list, [s(:return, + s(:int, 42))]) diff --git a/test/cases/return/return_string.tst b/test/cases/return/return_string.tst new file mode 100644 index 0000000..a584017 --- /dev/null +++ b/test/cases/return/return_string.tst @@ -0,0 +1,4 @@ +return "hello" +-- -- -- +s(:list, [s(:return, + s(:string, "hello"))]) diff --git a/test/cases/return/return_variable.tst b/test/cases/return/return_variable.tst new file mode 100644 index 0000000..61467b8 --- /dev/null +++ b/test/cases/return/return_variable.tst @@ -0,0 +1,4 @@ +return foo +-- -- -- +s(:list, [s(:return, + s(:name, "foo"))]) diff --git a/test/cases/root/class_method.tst b/test/cases/root/class_method.tst new file mode 100644 index 0000000..9bbf6af --- /dev/null +++ b/test/cases/root/class_method.tst @@ -0,0 +1,11 @@ +class FooBo + Bar.call(35) +end + +-- -- -- +s(:list, [s(:class, "FooBo", nil, [s(:call, + s(:name, "call"), + s(:arguments, + s(:int, 35)), + s(:receiver, + s(:module, "Bar")))])]) diff --git a/test/cases/root/double_root.tst b/test/cases/root/double_root.tst new file mode 100644 index 0000000..4f7d984 --- /dev/null +++ b/test/cases/root/double_root.tst @@ -0,0 +1,17 @@ +int foo(ref x) + a = 5 +end + +foo( 3 ) +-- -- -- +s(:list, [s(:function, :int, + s(:name, "foo"), + s(:parameters, + s(:field, :ref, :x)), + s(:expressions, + s(:assign, + s(:name, "a"), + s(:int, 5)))), s(:call, + s(:name, "foo"), + s(:arguments, + s(:int, 3)))]) diff --git a/test/cases/root/fibo1.tst b/test/cases/root/fibo1.tst new file mode 100644 index 0000000..872afd7 --- /dev/null +++ b/test/cases/root/fibo1.tst @@ -0,0 +1,54 @@ +int fibonaccit(int n) + a = 0 + b = 1 + while( n > 1 ) + tmp = a + a = b + b = tmp + b + puts(b) + n = n - 1 + end +end + +fibonaccit( 10 ) +-- -- -- +s(:list, [s(:function, :int, + s(:name, "fibonaccit"), + s(:parameters, + s(:field, :int, :n)), + s(:expressions, + s(:assign, + s(:name, "a"), + s(:int, 0)), + s(:assign, + s(:name, "b"), + s(:int, 1)), + s(:while, + s(:condition, + s(:operator, ">", + s(:name, "n"), + s(:int, 1))), + s(:expressions, + s(:assign, + s(:name, "tmp"), + s(:name, "a")), + s(:assign, + s(:name, "a"), + s(:name, "b")), + s(:assign, + s(:name, "b"), + s(:operator, "+", + s(:name, "tmp"), + s(:name, "b"))), + s(:call, + s(:name, "puts"), + s(:arguments, + s(:name, "b"))), + s(:assign, + s(:name, "n"), + s(:operator, "-", + s(:name, "n"), + s(:int, 1))))))), s(:call, + s(:name, "fibonaccit"), + s(:arguments, + s(:int, 10)))]) diff --git a/test/cases/root/module_assignment.tst b/test/cases/root/module_assignment.tst new file mode 100644 index 0000000..856cf8d --- /dev/null +++ b/test/cases/root/module_assignment.tst @@ -0,0 +1,16 @@ +module Fibo + a = 5 + foo + bar( b , a , r) +end + +-- -- -- +s(:list, [s(:module, "Fibo", [s(:assign, + s(:name, "a"), + s(:operator, "+", + s(:int, 5), + s(:name, "foo"))), s(:call, + s(:name, "bar"), + s(:arguments, + s(:name, "b"), + s(:name, "a"), + s(:name, "r")))])]) diff --git a/test/cases/root/module_method.tst b/test/cases/root/module_method.tst new file mode 100644 index 0000000..f3b8ff9 --- /dev/null +++ b/test/cases/root/module_method.tst @@ -0,0 +1,24 @@ +module Fibo + int fibonaccit(int n) + int a = 0 + return a + end + + fibonaccit( 10 ) +end + +-- -- -- +s(:list, [s(:module, "Fibo", [s(:function, :int, + s(:name, "fibonaccit"), + s(:parameters, + s(:field, :int, :n)), + s(:expressions, + s(:name, "int"), + s(:assign, + s(:name, "a"), + s(:int, 0)), + s(:return, + s(:name, "a")))), s(:call, + s(:name, "fibonaccit"), + s(:arguments, + s(:int, 10)))])]) diff --git a/test/cases/root/root_module_class.tst b/test/cases/root/root_module_class.tst new file mode 100644 index 0000000..b770f6b --- /dev/null +++ b/test/cases/root/root_module_class.tst @@ -0,0 +1,12 @@ +module FooBo + class Bar + a = 5 + foo + end +end + +-- -- -- +s(:list, [s(:module, "FooBo", [s(:class, "Bar", nil, [s(:assign, + s(:name, "a"), + s(:operator, "+", + s(:int, 5), + s(:name, "foo")))])])]) diff --git a/test/cases/while/big_while.tst b/test/cases/while/big_while.tst new file mode 100644 index 0000000..d1a1c5a --- /dev/null +++ b/test/cases/while/big_while.tst @@ -0,0 +1,34 @@ +while( n > 1) + tmp = a + a = b + b = tmp + b + puts(b) + n = n - 1 +end +-- -- -- +s(:list, [s(:while, + s(:condition, + s(:operator, ">", + s(:name, "n"), + s(:int, 1))), + s(:expressions, + s(:assign, + s(:name, "tmp"), + s(:name, "a")), + s(:assign, + s(:name, "a"), + s(:name, "b")), + s(:assign, + s(:name, "b"), + s(:operator, "+", + s(:name, "tmp"), + s(:name, "b"))), + s(:call, + s(:name, "puts"), + s(:arguments, + s(:name, "b"))), + s(:assign, + s(:name, "n"), + s(:operator, "-", + s(:name, "n"), + s(:int, 1)))))]) diff --git a/test/cases/while/while.tst b/test/cases/while/while.tst new file mode 100644 index 0000000..9976191 --- /dev/null +++ b/test/cases/while/while.tst @@ -0,0 +1,16 @@ +while(1) + tmp = a + puts(b) +end +-- -- -- +s(:list, [s(:while, + s(:condition, + s(:int, 1)), + s(:expressions, + s(:assign, + s(:name, "tmp"), + s(:name, "a")), + s(:call, + s(:name, "puts"), + s(:arguments, + s(:name, "b")))))]) diff --git a/test/cases/while/while_method.tst b/test/cases/while/while_method.tst new file mode 100644 index 0000000..aae755b --- /dev/null +++ b/test/cases/while/while_method.tst @@ -0,0 +1,22 @@ +while(1) + tmp = String.new() + tmp.puts(i) +end +-- -- -- +s(:list, [s(:while, + s(:condition, + s(:int, 1)), + s(:expressions, + s(:assign, + s(:name, "tmp"), + s(:call, + s(:name, "new"), + s(:arguments), + s(:receiver, + s(:module, "String")))), + s(:call, + s(:name, "puts"), + s(:arguments, + s(:name, "i")), + s(:receiver, + s(:name, "tmp")))))]) diff --git a/test/parser_helper.rb b/test/parser_helper.rb index 17effdb..0ed0287 100644 --- a/test/parser_helper.rb +++ b/test/parser_helper.rb @@ -1,18 +1,14 @@ require_relative "setup" require "parslet/convenience" -# remove the line numbers on assert fails, so it's easy to copy paste the result as the expected result -Parslet::Slice.class_eval do - def inspect - '"' + to_s + '"' - end -end -# Included in parser test will create tests methods + + +# Older test harness module ParserHelper def self.included(base) base.send :include, InstanceMethods #provides helpers and setup - base.send :include, AST::Sexp + base.send :include, AST::if_true base.send :extend, ClassMethods #gets the method creation going end @@ -39,11 +35,36 @@ module ParserHelper def check_ast syntax = @parser.parse(@string_input) is = @transform.apply(syntax) - #puts is.inspect + puts is.inspect assert_equal @transform_output , is end + + def check_write test + dirname = decamelize(self.class.name)[10 .. -1] + test = test[5 .. -1] + syntax = @parser.parse_with_debug(@string_input) + out = Parser::Transform.new.apply(syntax).inspect + dir = File.dirname(__FILE__) + "/" + dirname + FileUtils.mkdir_p(dir) unless File.exists?(dir) + out_file = File.new(dir + "/" + test + ".tst", "w") + out_file.puts @string_input + out_file.puts "-- -- --" + out_file.puts out + out_file.close + end + + def decamelize str + str.gsub(/(^|[a-z])([A-Z])/) do + ($1.empty?)? $2 : "#{$1}_#{$2}" + end.downcase + end + + def camelize str + str.gsub(/(^|_)([a-z])/) { $2.upcase } + end end + module ClassMethods # this creates test methods dynamically. For each test_* method we create # three test_*[ast/parse/transf] methods that in turn check the three phases. @@ -51,12 +72,12 @@ module ParserHelper def runnable_methods tests = [] public_instance_methods(true).grep(/^test_/).map(&:to_s).each do |test| - ["ast" , "transform" , "parse"].each do |what| + ["write"].each do |what| name = "#{test}_#{what}" tests << name self.send(:define_method, name ) do send(test) - send("check_#{what}") + send("check_#{what}" , test) end end end diff --git a/test/test_all.rb b/test/test_all.rb index b295712..f3b954b 100644 --- a/test/test_all.rb +++ b/test/test_all.rb @@ -1,2 +1,32 @@ -require_relative "unit/test_all" -require_relative "roots/test_all" +require_relative "setup" +require "parslet/convenience" +require "ast/sexp" + +class TestAll < MiniTest::Test + include AST::Sexp + + def check_file file + inn , out = File.new(file).read.split("-- -- --") + sexp = eval(out) + syntax = Parser::Salama.new.parse_with_debug(inn) + result = Parser::Transform.new.apply(syntax) + assert_equal sexp , result + end + # this creates test methods dynamically. For each test_* method we create + # three test_*[ast/parse/transf] methods that in turn check the three phases. + # runnable_methods is called by minitest to determine which tests to run + def self.runnable_methods + puts "called" + all = Dir["test/cases/*/*.tst"] + puts "case #{all.length}" + tests =[] + all.each do |file| + name = file.sub("test/cases/","").sub("/","_").sub(".tst","") + tests << name + self.send(:define_method, name ) do + send("check_file" , file) + end + end + tests + end +end