rltk 2.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +12 -12
  3. data/README.md +458 -285
  4. data/Rakefile +99 -92
  5. data/lib/rltk/ast.rb +221 -126
  6. data/lib/rltk/cfg.rb +218 -239
  7. data/lib/rltk/cg/basic_block.rb +1 -1
  8. data/lib/rltk/cg/bindings.rb +9 -26
  9. data/lib/rltk/cg/builder.rb +40 -8
  10. data/lib/rltk/cg/context.rb +1 -1
  11. data/lib/rltk/cg/contractor.rb +51 -0
  12. data/lib/rltk/cg/execution_engine.rb +45 -8
  13. data/lib/rltk/cg/function.rb +12 -2
  14. data/lib/rltk/cg/generated_bindings.rb +2541 -575
  15. data/lib/rltk/cg/generic_value.rb +2 -2
  16. data/lib/rltk/cg/instruction.rb +104 -83
  17. data/lib/rltk/cg/llvm.rb +44 -3
  18. data/lib/rltk/cg/memory_buffer.rb +22 -5
  19. data/lib/rltk/cg/module.rb +85 -36
  20. data/lib/rltk/cg/old_generated_bindings.rb +6152 -0
  21. data/lib/rltk/cg/pass_manager.rb +87 -43
  22. data/lib/rltk/cg/support.rb +2 -4
  23. data/lib/rltk/cg/target.rb +158 -28
  24. data/lib/rltk/cg/triple.rb +8 -8
  25. data/lib/rltk/cg/type.rb +69 -25
  26. data/lib/rltk/cg/value.rb +107 -66
  27. data/lib/rltk/cg.rb +16 -17
  28. data/lib/rltk/lexer.rb +21 -11
  29. data/lib/rltk/lexers/calculator.rb +1 -1
  30. data/lib/rltk/lexers/ebnf.rb +8 -7
  31. data/lib/rltk/parser.rb +300 -247
  32. data/lib/rltk/parsers/infix_calc.rb +1 -1
  33. data/lib/rltk/parsers/postfix_calc.rb +2 -2
  34. data/lib/rltk/parsers/prefix_calc.rb +2 -2
  35. data/lib/rltk/token.rb +1 -2
  36. data/lib/rltk/version.rb +3 -3
  37. data/lib/rltk.rb +6 -6
  38. data/test/cg/tc_basic_block.rb +83 -0
  39. data/test/cg/tc_control_flow.rb +191 -0
  40. data/test/cg/tc_function.rb +54 -0
  41. data/test/cg/tc_generic_value.rb +33 -0
  42. data/test/cg/tc_instruction.rb +256 -0
  43. data/test/cg/tc_llvm.rb +25 -0
  44. data/test/cg/tc_math.rb +88 -0
  45. data/test/cg/tc_module.rb +89 -0
  46. data/test/cg/tc_transforms.rb +68 -0
  47. data/test/cg/tc_type.rb +69 -0
  48. data/test/cg/tc_value.rb +151 -0
  49. data/test/cg/ts_cg.rb +23 -0
  50. data/test/tc_ast.rb +105 -8
  51. data/test/tc_cfg.rb +63 -48
  52. data/test/tc_lexer.rb +84 -96
  53. data/test/tc_parser.rb +224 -52
  54. data/test/tc_token.rb +6 -6
  55. data/test/ts_rltk.rb +12 -15
  56. metadata +149 -75
  57. data/lib/rltk/cg/generated_extended_bindings.rb +0 -287
  58. data/lib/rltk/util/abstract_class.rb +0 -25
  59. data/lib/rltk/util/monkeys.rb +0 -129
@@ -0,0 +1,25 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Ruby Language Toolkit
3
+ # Date: 2012/05/04
4
+ # Description: This file contains unit tests for rltk/cg/llvm.rb file.
5
+
6
+ ############
7
+ # Requires #
8
+ ############
9
+
10
+ # Gems
11
+ require 'minitest/autorun'
12
+
13
+ # Ruby Language Toolkit
14
+ require 'rltk/version'
15
+ require 'rltk/cg/llvm'
16
+
17
+ #######################
18
+ # Classes and Modules #
19
+ #######################
20
+
21
+ class LLVMTester < Minitest::Test
22
+ def test_init
23
+ assert_raises(ArgumentError) { RLTK::CG::LLVM.init(:foo) }
24
+ end
25
+ end
@@ -0,0 +1,88 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Ruby Language Toolkit
3
+ # Date: 2012/05/09
4
+ # Description: This file contains unit tests for various math instructions.
5
+
6
+ ############
7
+ # Requires #
8
+ ############
9
+
10
+ # Gems
11
+ require 'minitest/autorun'
12
+
13
+ # Ruby Language Toolkit
14
+ require 'rltk/cg/llvm'
15
+ require 'rltk/cg/module'
16
+ require 'rltk/cg/execution_engine'
17
+ require 'rltk/cg/type'
18
+ require 'rltk/cg/value'
19
+
20
+ #######################
21
+ # Classes and Modules #
22
+ #######################
23
+
24
+ class MathTester < Minitest::Test
25
+ def setup
26
+ RLTK::CG::LLVM.init(:X86)
27
+
28
+ @mod = RLTK::CG::Module.new('Testing Module')
29
+ @jit = RLTK::CG::JITCompiler.new(@mod)
30
+ end
31
+
32
+ def test_integer_binary_operations
33
+ int_binop_assert(:add, 3, 2, 3 + 2)
34
+ int_binop_assert(:sub, 3, 2, 3 - 2)
35
+ int_binop_assert(:mul, 3, 2, 3 * 2)
36
+ int_binop_assert(:udiv, 10, 2, 10 / 2)
37
+ int_binop_assert(:sdiv, 10, 2, 10 / 2)
38
+ int_binop_assert(:urem, 10, 3, 10 % 3)
39
+ int_binop_assert(:srem, 10, 3, 10 % 3)
40
+ end
41
+
42
+ def test_integer_bitwise_binary_operations
43
+ int_binop_assert(:shl, 2, 3, 2 << 3)
44
+ int_binop_assert(:lshr, 16, 3, 16 >> 3)
45
+ int_binop_assert(:ashr, 16, 3, 16 >> 3)
46
+ int_binop_assert(:and, 2, 1, 2 & 1)
47
+ int_binop_assert(:or, 2, 1, 2 | 1)
48
+ int_binop_assert(:xor, 3, 2, 3 ^ 2)
49
+ end
50
+
51
+ def test_float_binary_operations
52
+ float_binop_assert(:fadd, 3.1, 2.2, 3.1 + 2.2)
53
+ float_binop_assert(:fsub, 3.1, 2.2, 3.1 - 2.2)
54
+ float_binop_assert(:fmul, 3.1, 2.2, 3.1 * 2.2)
55
+ float_binop_assert(:fdiv, 3.1, 2.2, 3.1 / 2.2)
56
+ float_binop_assert(:frem, 3.1, 2.2, 3.1 % 2.2)
57
+ end
58
+
59
+ def test_simple_math_fun
60
+ fun = @mod.functions.add('simple_math_tester', RLTK::CG::FloatType, [RLTK::CG::FloatType]) do |fun|
61
+ blocks.append do
62
+ ret(fadd(fun.params[0], RLTK::CG::Float.new(1.0)))
63
+ end
64
+ end
65
+
66
+ assert_equal(6.0, @jit.run_function(fun, 5.0).to_f)
67
+ end
68
+
69
+ ##################
70
+ # Helper Methods #
71
+ ##################
72
+
73
+ def float_binop_assert(op, operand0, operand1, expected)
74
+ assert_in_delta(expected, run_binop(op, RLTK::CG::Float.new(operand0), RLTK::CG::Float.new(operand1), RLTK::CG::FloatType).to_f, 0.001)
75
+ end
76
+
77
+ def int_binop_assert(op, operand0, operand1, expected)
78
+ assert_equal(expected, run_binop(op, RLTK::CG::NativeInt.new(operand0), RLTK::CG::NativeInt.new(operand1), RLTK::CG::NativeIntType).to_i)
79
+ end
80
+
81
+ def run_binop(op, operand0, operand1, ret_type)
82
+ fun = @mod.functions.add(op.to_s + '_tester', ret_type, []) do
83
+ blocks.append { ret(self.send(op, operand0, operand1)) }
84
+ end
85
+
86
+ @jit.run_function(fun)
87
+ end
88
+ end
@@ -0,0 +1,89 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Ruby Language Toolkit
3
+ # Date: 2012/05/04
4
+ # Description: This file contains unit tests for the RLTK::CG::Module class.
5
+
6
+ ############
7
+ # Requires #
8
+ ############
9
+
10
+ # Standard Library
11
+ require 'tempfile'
12
+
13
+ # Gems
14
+ require 'minitest/autorun'
15
+
16
+ # Ruby Language Toolkit
17
+ require 'rltk/cg/llvm'
18
+ require 'rltk/cg/module'
19
+ require 'rltk/cg/execution_engine'
20
+ require 'rltk/cg/type'
21
+ require 'rltk/cg/value'
22
+
23
+ #######################
24
+ # Classes and Modules #
25
+ #######################
26
+
27
+ class ModuleTester < Minitest::Test
28
+ def setup
29
+ RLTK::CG::LLVM.init(:X86)
30
+
31
+ @mod = RLTK::CG::Module.new('Testing Module')
32
+ @jit = RLTK::CG::JITCompiler.new(@mod)
33
+
34
+ @mod.functions.add('int_function_tester', RLTK::CG::NativeIntType, []) do
35
+ blocks.append { ret RLTK::CG::NativeInt.new(1) }
36
+ end
37
+ end
38
+
39
+ def test_bitcode
40
+ Tempfile.open('bitcode') do |tmp|
41
+ assert(@mod.write_bitcode(tmp))
42
+
43
+ new_mod = RLTK::CG::Module.read_bitcode(tmp.path)
44
+ new_jit = RLTK::CG::JITCompiler.new(new_mod)
45
+
46
+ assert_equal(1, new_jit.run_function(new_mod.functions['int_function_tester']).to_i)
47
+ end
48
+ end
49
+
50
+ def test_equality
51
+ mod0 = RLTK::CG::Module.new('foo')
52
+ mod1 = RLTK::CG::Module.new('bar')
53
+ mod2 = RLTK::CG::Module.new(mod0.ptr)
54
+
55
+ assert_equal(mod0, mod2)
56
+ refute_equal(mod0, mod1)
57
+ end
58
+
59
+ def test_external_fun
60
+ fun = @mod.functions.add(:sin, RLTK::CG::DoubleType, [RLTK::CG::DoubleType])
61
+ res = @jit.run_function(fun, RLTK::CG::GenericValue.new(1.0, RLTK::CG::DoubleType)).to_f(RLTK::CG::DoubleType)
62
+
63
+ assert_in_delta(Math.sin(1.0), res, 1e-10)
64
+ end
65
+
66
+ def test_simple_int_fun
67
+ assert_equal(1, @jit.run_function(@mod.functions['int_function_tester']).to_i)
68
+ end
69
+
70
+ def test_simple_float_fun
71
+ fun = @mod.functions.add('float_function_tester', RLTK::CG::FloatType, []) do
72
+ blocks.append do
73
+ ret RLTK::CG::Float.new(1.5)
74
+ end
75
+ end
76
+
77
+ assert_equal(1.5, @jit.run_function(fun).to_f(RLTK::CG::FloatType))
78
+ end
79
+
80
+ def test_simple_double_fun
81
+ fun = @mod.functions.add('double_function_tester', RLTK::CG::DoubleType, []) do
82
+ blocks.append do
83
+ ret RLTK::CG::Double.new(1.6)
84
+ end
85
+ end
86
+
87
+ assert_equal(1.6, @jit.run_function(fun).to_f(RLTK::CG::DoubleType))
88
+ end
89
+ end
@@ -0,0 +1,68 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Ruby Language Toolkit
3
+ # Date: 2012/05/11
4
+ # Description: This file contains unit tests for the mechanics beind
5
+ # transformation passes.
6
+
7
+ ############
8
+ # Requires #
9
+ ############
10
+
11
+ # Gems
12
+ require 'minitest/autorun'
13
+
14
+ # Ruby Language Toolkit
15
+ require 'rltk/cg/llvm'
16
+ require 'rltk/cg/module'
17
+ require 'rltk/cg/execution_engine'
18
+ require 'rltk/cg/type'
19
+ require 'rltk/cg/value'
20
+
21
+ class TransformTester < Minitest::Test
22
+ def setup
23
+ RLTK::CG::LLVM.init(:X86)
24
+
25
+ @mod = RLTK::CG::Module.new('Testing Module')
26
+ @jit = RLTK::CG::JITCompiler.new(@mod)
27
+ end
28
+
29
+ def test_gdce
30
+ fn0 = @mod.functions.add('fn0', RLTK::CG::VoidType, []) do |fun|
31
+ fun.linkage = :internal
32
+
33
+ blocks.append do
34
+ ret_void
35
+ end
36
+ end
37
+
38
+ fn1 = @mod.functions.add('fn1', RLTK::CG::VoidType, []) do |fun|
39
+ fun.linkage = :internal
40
+
41
+ blocks.append do
42
+ ret_void
43
+ end
44
+ end
45
+
46
+ main = @mod.functions.add('main', RLTK::CG::VoidType, []) do
47
+ blocks.append do
48
+ call(fn0)
49
+ ret_void
50
+ end
51
+ end
52
+
53
+ funs = @mod.functions.to_a
54
+
55
+ assert(funs.include?(fn0))
56
+ assert(funs.include?(fn1))
57
+ assert(funs.include?(main))
58
+
59
+ @mod.pass_manager << :GDCE
60
+ assert(@mod.pass_manager.run)
61
+
62
+ funs = @mod.functions.to_a
63
+
64
+ assert( funs.include?(fn0))
65
+ assert(!funs.include?(fn1))
66
+ assert( funs.include?(main))
67
+ end
68
+ end
@@ -0,0 +1,69 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Ruby Language Toolkit
3
+ # Date: 2012/05/09
4
+ # Description: This file contains unit tests for the RLTK::CG::Type class and
5
+ # its subclasses.
6
+
7
+ ############
8
+ # Requires #
9
+ ############
10
+
11
+ # Gems
12
+ require 'minitest/autorun'
13
+
14
+ # Ruby Language Toolkit
15
+ require 'rltk/cg/type'
16
+
17
+ class TypeTester < Minitest::Test
18
+ def setup
19
+ @pointee = RLTK::CG::NativeIntType.instance
20
+ @pointer = RLTK::CG::PointerType.new(@pointee)
21
+ end
22
+
23
+ def test_deferrent_element_type_stuct_type
24
+ type = RLTK::CG::StructType.new([], 'test_struct')
25
+ type.element_types = [RLTK::CG::NativeIntType, RLTK::CG::FloatType]
26
+
27
+ assert_equal(2, type.element_types.size)
28
+ assert_equal(RLTK::CG::NativeIntType.instance, type.element_types[0])
29
+ assert_equal(RLTK::CG::FloatType.instance, type.element_types[1])
30
+
31
+ end
32
+
33
+ def test_element_type
34
+ assert_equal(@pointee, @pointer.element_type)
35
+ end
36
+
37
+ def test_equality
38
+ assert_equal(RLTK::CG::NativeIntType, RLTK::CG::NativeIntType)
39
+ refute_equal(RLTK::CG::NativeIntType, RLTK::CG::FloatType)
40
+
41
+ at0 = RLTK::CG::ArrayType.new(RLTK::CG::NativeIntType, 2)
42
+ at1 = RLTK::CG::ArrayType.new(RLTK::CG::NativeIntType, 2)
43
+ at2 = RLTK::CG::ArrayType.new(RLTK::CG::FloatType, 2)
44
+
45
+ assert_equal(at0, at1)
46
+ refute_equal(at0, at2)
47
+ end
48
+
49
+ def test_kind
50
+ assert_equal(:pointer, @pointer.kind)
51
+ assert_equal(:integer, @pointee.kind)
52
+ end
53
+
54
+ def test_named_struct_type
55
+ type = RLTK::CG::StructType.new([RLTK::CG::NativeIntType, RLTK::CG::FloatType], 'test_struct')
56
+
57
+ assert_instance_of(RLTK::CG::StructType, type)
58
+ assert_equal('test_struct', type.name)
59
+ end
60
+
61
+ def test_simple_struct_type
62
+ type = RLTK::CG::StructType.new([RLTK::CG::NativeIntType, RLTK::CG::FloatType])
63
+
64
+ assert_instance_of(RLTK::CG::StructType, type)
65
+ assert_equal(2, type.element_types.size)
66
+ assert_equal(RLTK::CG::NativeIntType.instance, type.element_types[0])
67
+ assert_equal(RLTK::CG::FloatType.instance, type.element_types[1])
68
+ end
69
+ end
@@ -0,0 +1,151 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Ruby Language Toolkit
3
+ # Date: 2012/05/09
4
+ # Description: This file contains unit tests for the RLTK::CG::Value class and
5
+ # its subclasses.
6
+
7
+ ############
8
+ # Requires #
9
+ ############
10
+
11
+ # Gems
12
+ require 'minitest/autorun'
13
+
14
+ # Ruby Language Toolkit
15
+ require 'rltk/cg/llvm'
16
+ require 'rltk/cg/module'
17
+ require 'rltk/cg/execution_engine'
18
+ require 'rltk/cg/type'
19
+ require 'rltk/cg/value'
20
+
21
+ class ValueTester < Minitest::Test
22
+ def setup
23
+ RLTK::CG::LLVM.init(:X86)
24
+
25
+ @mod = RLTK::CG::Module.new('Testing Module')
26
+ @jit = RLTK::CG::JITCompiler.new(@mod)
27
+ end
28
+
29
+ def test_array_values
30
+ fun = @mod.functions.add('array_function_tester', RLTK::CG::NativeIntType,
31
+ [RLTK::CG::NativeIntType, RLTK::CG::NativeIntType]) do |fun|
32
+
33
+ blocks.append do
34
+ ptr = alloca(RLTK::CG::ArrayType.new(RLTK::CG::NativeIntType, 2))
35
+
36
+ array = load(ptr)
37
+ array = insert_value(array, fun.params[0], 0)
38
+ array = insert_value(array, fun.params[1], 1)
39
+
40
+ ret(add(extract_value(array, 0), extract_value(array, 1)))
41
+ end
42
+ end
43
+
44
+ assert_equal(5, @jit.run_function(fun, 2, 3).to_i)
45
+ end
46
+
47
+ def test_constant_array_from_array
48
+ array = RLTK::CG::ConstantArray.new(RLTK::CG::NativeIntType, [RLTK::CG::NativeInt.new(0), RLTK::CG::NativeInt.new(1)])
49
+
50
+ assert_instance_of(RLTK::CG::ConstantArray, array)
51
+ assert_equal(2, array.length)
52
+ end
53
+
54
+ def test_constant_array_from_size
55
+ array = RLTK::CG::ConstantArray.new(RLTK::CG::NativeIntType, 2) { |i| RLTK::CG::NativeInt.new(i) }
56
+
57
+ assert_instance_of(RLTK::CG::ConstantArray, array)
58
+ assert_equal(2, array.length)
59
+ end
60
+
61
+ def test_constant_vector_elements
62
+ fun = @mod.functions.add('constant_vector_elements_tester', RLTK::CG::NativeIntType,
63
+ [RLTK::CG::NativeIntType, RLTK::CG::NativeIntType]) do |fun|
64
+
65
+ blocks.append do
66
+ ptr = alloca(RLTK::CG::VectorType.new(RLTK::CG::NativeIntType, 2))
67
+
68
+ vector = load(ptr)
69
+ vector = insert_element(vector, fun.params[0], RLTK::CG::NativeInt.new(0))
70
+ vector = insert_element(vector, fun.params[1], RLTK::CG::NativeInt.new(1))
71
+
72
+ ret(add(extract_element(vector, RLTK::CG::NativeInt.new(0)), extract_element(vector, RLTK::CG::NativeInt.new(1))))
73
+ end
74
+ end
75
+
76
+ assert_equal(5, @jit.run_function(fun, 2, 3).to_i)
77
+ end
78
+
79
+ def test_constant_vector_from_array
80
+ vector = RLTK::CG::ConstantVector.new([RLTK::CG::NativeInt.new(0), RLTK::CG::NativeInt.new(1)])
81
+
82
+ assert_instance_of(RLTK::CG::ConstantVector, vector)
83
+ assert_equal(2, vector.size)
84
+ end
85
+
86
+ def test_constant_vector_from_size
87
+ vector = RLTK::CG::ConstantVector.new(2) { |i| RLTK::CG::NativeInt.new(i) }
88
+
89
+ assert_instance_of(RLTK::CG::ConstantVector, vector)
90
+ assert_equal(2, vector.size)
91
+ end
92
+
93
+ def test_constant_vector_shuffle
94
+ fun = @mod.functions.add('constant_vector_shuffle_tester', RLTK::CG::NativeIntType, Array.new(4, RLTK::CG::NativeIntType)) do |fun|
95
+ blocks.append do
96
+ vec_type = RLTK::CG::VectorType.new(RLTK::CG::NativeIntType, 2)
97
+
98
+ v0 = load(alloca(vec_type))
99
+ v0 = insert_element(v0, fun.params[0], RLTK::CG::NativeInt.new(0))
100
+ v0 = insert_element(v0, fun.params[1], RLTK::CG::NativeInt.new(1))
101
+
102
+ v1 = load(alloca(vec_type))
103
+ v1 = insert_element(v1, fun.params[2], RLTK::CG::NativeInt.new(0))
104
+ v1 = insert_element(v1, fun.params[3], RLTK::CG::NativeInt.new(1))
105
+
106
+ v2 = shuffle_vector(v0, v1, RLTK::CG::ConstantVector.new([RLTK::CG::NativeInt.new(0), RLTK::CG::NativeInt.new(3)]))
107
+
108
+ ret(add(extract_element(v2, RLTK::CG::NativeInt.new(0)), extract_element(v2, RLTK::CG::NativeInt.new(1))))
109
+ end
110
+ end
111
+
112
+ assert_equal(5, @jit.run_function(fun, 1, 2, 3, 4).to_i)
113
+ end
114
+
115
+ def test_constant_struct_from_size_packed
116
+ struct = RLTK::CG::ConstantStruct.new(2, true) { |i| RLTK::CG::NativeInt.new(i) }
117
+
118
+ assert_instance_of(RLTK::CG::ConstantStruct, struct)
119
+ assert_equal(2, struct.operands.size)
120
+ end
121
+
122
+ def test_constant_struct_from_size_unpacked
123
+ struct = RLTK::CG::ConstantStruct.new(2, false) { |i| RLTK::CG::NativeInt.new(i) }
124
+
125
+ assert_instance_of(RLTK::CG::ConstantStruct, struct)
126
+ assert_equal(2, struct.operands.size)
127
+ end
128
+
129
+ def test_constant_struct_from_values_packed
130
+ struct = RLTK::CG::ConstantStruct.new([RLTK::CG::NativeInt.new(0), RLTK::CG::NativeInt.new(1)], true)
131
+
132
+ assert_instance_of(RLTK::CG::ConstantStruct, struct)
133
+ assert_equal(2, struct.operands.size)
134
+ end
135
+
136
+ def test_constant_struct_from_values_unpacked
137
+ struct = RLTK::CG::ConstantStruct.new([RLTK::CG::NativeInt.new(0), RLTK::CG::NativeInt.new(1)], false)
138
+
139
+ assert_instance_of(RLTK::CG::ConstantStruct, struct)
140
+ assert_equal(2, struct.operands.size)
141
+ end
142
+
143
+ def test_equality
144
+ v0 = RLTK::CG::NativeInt.new(0)
145
+ v1 = RLTK::CG::NativeInt.new(1)
146
+ v2 = RLTK::CG::NativeInt.new(v0.ptr)
147
+
148
+ assert_equal(v0, v2)
149
+ refute_equal(v0, v1)
150
+ end
151
+ end
data/test/cg/ts_cg.rb ADDED
@@ -0,0 +1,23 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Ruby Language Toolkit
3
+ # Date: 2012/05/04
4
+ # Description: This file contains test suit for the RLTK LLVM bindigns. It
5
+ # requires the individual tests from their respective files.
6
+
7
+ ############
8
+ # Requires #
9
+ ############
10
+
11
+ # Ruby Language Toolkit
12
+ require 'cg/tc_llvm'
13
+ require 'cg/tc_module'
14
+
15
+ require 'cg/tc_basic_block'
16
+ require 'cg/tc_control_flow'
17
+ require 'cg/tc_function'
18
+ require 'cg/tc_generic_value'
19
+ require 'cg/tc_instruction'
20
+ require 'cg/tc_math'
21
+ require 'cg/tc_transforms'
22
+ require 'cg/tc_type'
23
+ require 'cg/tc_value'
data/test/tc_ast.rb CHANGED
@@ -7,9 +7,8 @@
7
7
  # Requires #
8
8
  ############
9
9
 
10
- # Standard Library
11
- require 'test/unit'
12
- require 'pp'
10
+ # Gems
11
+ require 'minitest/autorun'
13
12
 
14
13
  # Ruby Language Toolkit
15
14
  require 'rltk/ast'
@@ -18,7 +17,7 @@ require 'rltk/ast'
18
17
  # Classes and Modules #
19
18
  #######################
20
19
 
21
- class ASTNodeTester < Test::Unit::TestCase
20
+ class ASTNodeTester < Minitest::Test
22
21
  class ANode < RLTK::ASTNode
23
22
  child :left, ANode
24
23
  child :right, ANode
@@ -29,6 +28,14 @@ class ASTNodeTester < Test::Unit::TestCase
29
28
 
30
29
  class DNode < RLTK::ASTNode; end
31
30
 
31
+ class ENode < RLTK::ASTNode
32
+ value :str, String
33
+ end
34
+
35
+ class FNode < ENode
36
+ child :c, [ENode]
37
+ end
38
+
32
39
  class SNode < RLTK::ASTNode
33
40
  value :string, String
34
41
 
@@ -36,6 +43,11 @@ class ASTNodeTester < Test::Unit::TestCase
36
43
  child :right, SNode
37
44
  end
38
45
 
46
+ class VNode < RLTK::ASTNode
47
+ value :a, Integer
48
+ value :b, Integer
49
+ end
50
+
39
51
  def setup
40
52
  @leaf0 = CNode.new
41
53
  @tree0 = ANode.new(BNode.new(@leaf0), BNode.new)
@@ -60,6 +72,21 @@ class ASTNodeTester < Test::Unit::TestCase
60
72
  )
61
73
  )
62
74
 
75
+ @tree5 = FNode.new('one',
76
+ [FNode.new('two',
77
+ [ENode.new('three')]),
78
+ ENode.new('four')])
79
+
80
+ @tree6 = FNode.new('one',
81
+ [FNode.new('two',
82
+ [ENode.new('three')]),
83
+ ENode.new('four')])
84
+
85
+ @tree7 = FNode.new('one!',
86
+ [FNode.new('two!',
87
+ [ENode.new('three!')]),
88
+ ENode.new('four!')])
89
+
63
90
  @bc_proc = Proc.new do |n|
64
91
  case n
65
92
  when BNode then CNode.new(n.left, n.right)
@@ -77,6 +104,16 @@ class ASTNodeTester < Test::Unit::TestCase
77
104
  node.children = (expected_children = [BNode.new, CNode.new])
78
105
 
79
106
  assert_equal(node.children, expected_children)
107
+
108
+ node.children = (expected_children = {:left => CNode.new, :right => BNode.new})
109
+
110
+ assert_equal(node.children(Hash), expected_children)
111
+ end
112
+
113
+ def test_copy
114
+ new_tree = @tree5.copy
115
+
116
+ assert_equal(@tree5, new_tree)
80
117
  end
81
118
 
82
119
  def test_dump
@@ -108,16 +145,32 @@ class ASTNodeTester < Test::Unit::TestCase
108
145
  @tree4.each(:level) { |n| nodes << n.string }
109
146
 
110
147
  assert_equal(expected, nodes)
148
+
149
+ # Test iteration with array children.
150
+
151
+ res = ''
152
+ @tree5.each(:pre) { |node| res += ' ' + node.str }
153
+ assert_equal(res, ' one two three four')
154
+
155
+ res = ''
156
+ @tree5.each(:post) { |node| res += ' ' + node.str }
157
+ assert_equal(res, ' three two four one')
158
+
159
+ res = ''
160
+ @tree5.each(:level) { |node| res += ' ' + node.str }
161
+ assert_equal(res, ' one two four three')
111
162
  end
112
163
 
113
164
  def test_equal
114
165
  assert_equal(@tree0, @tree1)
115
- assert_not_equal(@tree0, @tree2)
166
+ refute_equal(@tree0, @tree2)
116
167
  end
117
168
 
118
169
  def test_initialize
119
- assert_raise(RuntimeError) { RLTK::ASTNode.new }
120
- assert_nothing_raised(RuntimeError) { ANode.new }
170
+ assert_raises(AbstractClassError) { RLTK::ASTNode.new }
171
+
172
+ node = ENode.new { self.str = 'hello world' }
173
+ assert_equal('hello world', node.str)
121
174
  end
122
175
 
123
176
  def test_map
@@ -125,19 +178,37 @@ class ASTNodeTester < Test::Unit::TestCase
125
178
 
126
179
  assert_equal(@tree0, @tree1)
127
180
  assert_equal(@tree3, mapped_tree)
181
+
182
+ mapped_tree = @tree5.map do |c|
183
+ c.str += '!'
184
+
185
+ c
186
+ end
187
+
188
+ assert_equal(@tree6, @tree5)
189
+ assert_equal(@tree7, mapped_tree)
128
190
  end
129
191
 
130
192
  def test_map!
131
193
  tree1_clone = @tree1.clone
132
194
  tree1_clone.map!(&@bc_proc)
133
195
 
134
- assert_not_equal(@tree1, tree1_clone)
196
+ refute_equal(@tree1, tree1_clone)
135
197
  assert_equal(@tree3, tree1_clone)
136
198
 
137
199
  replace_node = BNode.new
138
200
  replace_node = replace_node.map!(&@bc_proc)
139
201
 
140
202
  assert_equal(CNode.new, replace_node)
203
+
204
+ mapped_tree = @tree5.map! do |c|
205
+ c.str += '!'
206
+
207
+ c
208
+ end
209
+
210
+ refute_equal(@tree6, @tree5)
211
+ assert_equal(@tree7, @tree5)
141
212
  end
142
213
 
143
214
  def test_notes
@@ -151,8 +222,34 @@ class ASTNodeTester < Test::Unit::TestCase
151
222
  assert_nil(node[:a])
152
223
  end
153
224
 
225
+ def test_one_definition_rule
226
+ asserter = self
227
+
228
+ Class.new(ANode) do
229
+ asserter.assert_raises(ArgumentError) { child :left, ANode }
230
+ end
231
+
232
+ Class.new(ENode) do
233
+ asserter.assert_raises(ArgumentError) { value :str, String }
234
+ end
235
+ end
236
+
154
237
  def test_root
155
238
  assert_same(@tree0, @tree0.root)
156
239
  assert_same(@tree0, @leaf0.root)
157
240
  end
241
+
242
+ def test_value
243
+ node = VNode.new
244
+
245
+ assert_equal(node.values, [nil, nil])
246
+
247
+ node.values = (expected_values = [42, 1984])
248
+
249
+ assert_equal(node.values, expected_values)
250
+
251
+ node.values = (expected_values = {:a => 1984, :b => 42})
252
+
253
+ assert_equal(node.values(Hash), expected_values)
254
+ end
158
255
  end