koi-reference-compiler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.gitignore +3 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +76 -0
  4. data/Rakefile +42 -0
  5. data/VERSION +1 -0
  6. data/lib/koi-reference-compiler.rb +10 -0
  7. data/lib/koi-reference-compiler/exceptions.rb +2 -0
  8. data/lib/koi-reference-compiler/koi-reference-compiler.rb +23 -0
  9. data/lib/koi-reference-compiler/node_extensions/assignment/assignment.rb +12 -0
  10. data/lib/koi-reference-compiler/node_extensions/blocks/block.rb +13 -0
  11. data/lib/koi-reference-compiler/node_extensions/control_flow/if.rb +14 -0
  12. data/lib/koi-reference-compiler/node_extensions/control_flow/unless.rb +14 -0
  13. data/lib/koi-reference-compiler/node_extensions/expressions/additive_expression.rb +13 -0
  14. data/lib/koi-reference-compiler/node_extensions/expressions/comparative_expression.rb +13 -0
  15. data/lib/koi-reference-compiler/node_extensions/expressions/expression.rb +9 -0
  16. data/lib/koi-reference-compiler/node_extensions/expressions/multitive_expression.rb +13 -0
  17. data/lib/koi-reference-compiler/node_extensions/functions/function_call.rb +54 -0
  18. data/lib/koi-reference-compiler/node_extensions/functions/function_definition.rb +19 -0
  19. data/lib/koi-reference-compiler/node_extensions/hash_literals/hash_access.rb +12 -0
  20. data/lib/koi-reference-compiler/node_extensions/hash_literals/hash_accessor.rb +15 -0
  21. data/lib/koi-reference-compiler/node_extensions/hash_literals/hash_accessor_list.rb +26 -0
  22. data/lib/koi-reference-compiler/node_extensions/hash_literals/hash_assignment.rb +17 -0
  23. data/lib/koi-reference-compiler/node_extensions/hash_literals/hash_literal.rb +11 -0
  24. data/lib/koi-reference-compiler/node_extensions/hash_literals/key_value.rb +13 -0
  25. data/lib/koi-reference-compiler/node_extensions/hash_literals/key_value_list.rb +13 -0
  26. data/lib/koi-reference-compiler/node_extensions/identifiers/identifier.rb +31 -0
  27. data/lib/koi-reference-compiler/node_extensions/literals/false_literal.rb +9 -0
  28. data/lib/koi-reference-compiler/node_extensions/literals/float_literal.rb +9 -0
  29. data/lib/koi-reference-compiler/node_extensions/literals/integer_literal.rb +9 -0
  30. data/lib/koi-reference-compiler/node_extensions/literals/nil_literal.rb +9 -0
  31. data/lib/koi-reference-compiler/node_extensions/literals/string_literal.rb +9 -0
  32. data/lib/koi-reference-compiler/node_extensions/literals/true_literal.rb +9 -0
  33. data/lib/koi-reference-compiler/node_extensions/operators/addition_operator.rb +9 -0
  34. data/lib/koi-reference-compiler/node_extensions/operators/assignment_operator.rb +6 -0
  35. data/lib/koi-reference-compiler/node_extensions/operators/division_operator.rb +9 -0
  36. data/lib/koi-reference-compiler/node_extensions/operators/equality_operator.rb +9 -0
  37. data/lib/koi-reference-compiler/node_extensions/operators/great_than_operator.rb +9 -0
  38. data/lib/koi-reference-compiler/node_extensions/operators/inequality_operator.rb +9 -0
  39. data/lib/koi-reference-compiler/node_extensions/operators/less_than_operator.rb +9 -0
  40. data/lib/koi-reference-compiler/node_extensions/operators/multiplication_operator.rb +9 -0
  41. data/lib/koi-reference-compiler/node_extensions/operators/subtraction_operator.rb +9 -0
  42. data/lib/koi-reference-compiler/node_extensions/statements/statement.rb +9 -0
  43. data/lib/koi-reference-compiler/syntax_node.rb +18 -0
  44. data/lib/koi-reference-compiler/vm_opcodes.rb +70 -0
  45. data/test/compiler/unit/ast_hash_loader/ast_hash_loader_test.rb +46 -0
  46. data/test/compiler/unit/node_extensions/assignment/assignment_test.rb +22 -0
  47. data/test/compiler/unit/node_extensions/blocks/block_test.rb +37 -0
  48. data/test/compiler/unit/node_extensions/control_flow/if_test.rb +39 -0
  49. data/test/compiler/unit/node_extensions/control_flow/unless_test.rb +40 -0
  50. data/test/compiler/unit/node_extensions/expressions/additive_expression_test.rb +21 -0
  51. data/test/compiler/unit/node_extensions/expressions/comparative_expression_test.rb +21 -0
  52. data/test/compiler/unit/node_extensions/expressions/expression_test.rb +17 -0
  53. data/test/compiler/unit/node_extensions/expressions/multitive_expression_test.rb +21 -0
  54. data/test/compiler/unit/node_extensions/functions/function_call_test.rb +296 -0
  55. data/test/compiler/unit/node_extensions/functions/function_definition_test.rb +37 -0
  56. data/test/compiler/unit/node_extensions/hash_literals/hash_accessor_list_test.rb +83 -0
  57. data/test/compiler/unit/node_extensions/hash_literals/hash_accessor_test.rb +33 -0
  58. data/test/compiler/unit/node_extensions/hash_literals/hash_literal_test.rb +29 -0
  59. data/test/compiler/unit/node_extensions/hash_literals/key_value_list_test.rb +37 -0
  60. data/test/compiler/unit/node_extensions/hash_literals/key_value_test.rb +24 -0
  61. data/test/compiler/unit/node_extensions/identifiers/identifier_test.rb +103 -0
  62. data/test/compiler/unit/node_extensions/literals/false_literal_test.rb +13 -0
  63. data/test/compiler/unit/node_extensions/literals/float_literal_test.rb +13 -0
  64. data/test/compiler/unit/node_extensions/literals/integer_literal_test.rb +13 -0
  65. data/test/compiler/unit/node_extensions/literals/nil_literal_test.rb +13 -0
  66. data/test/compiler/unit/node_extensions/literals/string_literal_test.rb +13 -0
  67. data/test/compiler/unit/node_extensions/literals/true_literal_test.rb +13 -0
  68. data/test/compiler/unit/node_extensions/operators/addition_operator_test.rb +13 -0
  69. data/test/compiler/unit/node_extensions/operators/division_operator_test.rb +13 -0
  70. data/test/compiler/unit/node_extensions/operators/equality_operator_test.rb +13 -0
  71. data/test/compiler/unit/node_extensions/operators/greater_than_operator_test.rb +13 -0
  72. data/test/compiler/unit/node_extensions/operators/inequality_operator_test.rb +13 -0
  73. data/test/compiler/unit/node_extensions/operators/less_than_operator_test.rb +13 -0
  74. data/test/compiler/unit/node_extensions/operators/multiplication_operator_test.rb +13 -0
  75. data/test/compiler/unit/node_extensions/operators/subtraction_operator_test.rb +13 -0
  76. data/test/compiler/unit/node_extensions/statements/statement_test.rb +24 -0
  77. data/test/setup/test_unit_extensions.rb +21 -0
  78. data/test/test_helper.rb +10 -0
  79. metadata +177 -0
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class FunctionDefinitionTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile FunctionDefinition" do
8
+ tree = FunctionDefinition.new("function( arg )\n arg = 1\n end", 0, [
9
+ Identifier.new("arg", 10),
10
+ Block.new("arg = 1\n", 17,[
11
+ Statement.new("arg = 1", 17, [
12
+ Assignment.new("arg = 1", 17, [
13
+ Identifier.new("arg", 17),
14
+ AssignmentOperator.new("=", 21),
15
+ Expression.new("1", 23, [
16
+ IntegerLiteral.new("1", 23)
17
+ ])
18
+ ])
19
+ ])
20
+ ])
21
+ ])
22
+ bytecode = tree.compile
23
+ assert_equal [
24
+ PUSH_FUNCTION, 0,
25
+
26
+ SET_LOCAL, :arg,
27
+
28
+ PUSH_INT, 1,
29
+ SET_LOCAL, :arg,
30
+
31
+ RETURN,
32
+ END_FUNCTION, 0,
33
+ END_FUNCTION, 0
34
+ ], bytecode
35
+ end
36
+
37
+ end
@@ -0,0 +1,83 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class HashAccessorListTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile lvalue HashAccessorList to SET_KEY" do
8
+ tree = HashAssignment.new("test[1] = 1", 0, [
9
+ Identifier.new("test", 0),
10
+ HashAccessorList.new("[1]", 4, [
11
+ HashAccessor.new("[1]", 4, [
12
+ Expression.new("1", 5, [
13
+ IntegerLiteral.new("1", 5)
14
+ ])
15
+ ])
16
+ ]),
17
+ AssignmentOperator.new("=", 8),
18
+ Expression.new("1", 10, [
19
+ IntegerLiteral.new("1", 10)
20
+ ])
21
+ ])
22
+ bytecode = tree.compile
23
+ assert_equal [
24
+ GET_LOCAL, :test,
25
+ PUSH_INT, 1,
26
+ PUSH_INT, 1,
27
+ SET_KEY,
28
+ POP
29
+ ], bytecode
30
+ end
31
+
32
+ test "should compile lvalue HashAccessorList to GET_KEY, SET_KEY" do
33
+ tree = HashAssignment.new("test[1][1] = 1", 0, [
34
+ Identifier.new("test", 0),
35
+ HashAccessorList.new("[1][1]", 4, [
36
+ HashAccessor.new("[1]", 4, [
37
+ Expression.new("1", 5, [
38
+ IntegerLiteral.new("1", 5)
39
+ ])
40
+ ]),
41
+ HashAccessor.new("[1]", 7, [
42
+ Expression.new("1", 8, [
43
+ IntegerLiteral.new("1", 8)
44
+ ])
45
+ ])
46
+ ]),
47
+ AssignmentOperator.new("=", 11),
48
+ Expression.new("1", 13, [
49
+ IntegerLiteral.new("1", 13)
50
+ ])
51
+ ])
52
+ bytecode = tree.compile
53
+ assert_equal [
54
+ GET_LOCAL, :test,
55
+ PUSH_INT, 1,
56
+ GET_KEY,
57
+ PUSH_INT, 1,
58
+ PUSH_INT, 1,
59
+ SET_KEY,
60
+ POP
61
+ ], bytecode
62
+ end
63
+
64
+ test "should compile rvalue HashAccess to GET_KEY" do
65
+ tree = HashAccess.new("test[1]", 0, [
66
+ Identifier.new("test", 0),
67
+ HashAccessorList.new("[1]", 4, [
68
+ HashAccessor.new("[1]", 4, [
69
+ Expression.new("1", 5, [
70
+ IntegerLiteral.new("1", 5)
71
+ ])
72
+ ])
73
+ ])
74
+ ])
75
+ bytecode = tree.compile
76
+ assert_equal [
77
+ GET_LOCAL, :test,
78
+ PUSH_INT, 1,
79
+ GET_KEY
80
+ ], bytecode
81
+ end
82
+
83
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class HashAccessorTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile HashAccessor to GET_KEY" do
8
+ tree = HashAccessor.new("[1]", 0, [
9
+ Expression.new("1", 1, [
10
+ IntegerLiteral.new("1", 1)
11
+ ])
12
+ ])
13
+ bytecode = tree.compile_get
14
+ assert_equal [
15
+ PUSH_INT, 1,
16
+ GET_KEY
17
+ ], bytecode
18
+ end
19
+
20
+ test "should compile HashAccessor to SET_KEY" do
21
+ tree = HashAccessor.new("[1]", 0, [
22
+ Expression.new("1", 1, [
23
+ IntegerLiteral.new("1", 1)
24
+ ])
25
+ ])
26
+ bytecode = tree.compile_set
27
+ assert_equal [
28
+ PUSH_INT, 1,
29
+ SET_KEY
30
+ ], bytecode
31
+ end
32
+
33
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class HashLiteralTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile HashLiteral" do
8
+ tree = HashLiteral.new("{1 => 2}", 0, [
9
+ KeyValueList.new("1 => 2", 1, [
10
+ KeyValue.new("1 => 2", 1, [
11
+ Expression.new("1", 1, [
12
+ IntegerLiteral.new("1", 1)
13
+ ]),
14
+ Expression.new("2", 6, [
15
+ IntegerLiteral.new("2", 6)
16
+ ])
17
+ ])
18
+ ])
19
+ ])
20
+ bytecode = tree.compile
21
+ assert_equal [
22
+ PUSH_HASH,
23
+ PUSH_INT, 1,
24
+ PUSH_INT, 2,
25
+ SET_KEY
26
+ ], bytecode
27
+ end
28
+
29
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class KeyValueListTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile KeyValueList" do
8
+ tree = KeyValueList.new("1 => 2, 2 => 3", 0, [
9
+ KeyValue.new("1 => 2", 0, [
10
+ Expression.new("1", 0, [
11
+ IntegerLiteral.new("1", 0)
12
+ ]),
13
+ Expression.new("2", 5, [
14
+ IntegerLiteral.new("2", 5)
15
+ ])
16
+ ]),
17
+ KeyValue.new("2 => 3", 8, [
18
+ Expression.new("2", 8, [
19
+ IntegerLiteral.new("2", 8)
20
+ ]),
21
+ Expression.new("3", 13, [
22
+ IntegerLiteral.new("3", 13)
23
+ ])
24
+ ])
25
+ ])
26
+ bytecode = tree.compile
27
+ assert_equal [
28
+ PUSH_INT, 1,
29
+ PUSH_INT, 2,
30
+ SET_KEY,
31
+ PUSH_INT, 2,
32
+ PUSH_INT, 3,
33
+ SET_KEY
34
+ ], bytecode
35
+ end
36
+
37
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class KeyValueTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile KeyValue" do
8
+ tree = KeyValue.new("1 => 2", 0...6, [
9
+ Expression.new("1", 0...1, [
10
+ IntegerLiteral.new("1", 0...1)
11
+ ]),
12
+ Expression.new("2", 5...6, [
13
+ IntegerLiteral.new("2", 5...6)
14
+ ])
15
+ ])
16
+ bytecode = tree.compile
17
+ assert_equal [
18
+ PUSH_INT, 1,
19
+ PUSH_INT, 2,
20
+ SET_KEY
21
+ ], bytecode
22
+ end
23
+
24
+ end
@@ -0,0 +1,103 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class IdentifierTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile identifier with Assignment as parent to SET_LOCAL" do
8
+ tree = Assignment.new("test = 2", 0, [
9
+ Identifier.new('test', 0)
10
+ ])
11
+ bytecode = tree.elements.first.compile
12
+ assert_equal [SET_LOCAL, :test], bytecode
13
+ end
14
+
15
+ test "should compile identifier with FunctionDefinition as parent to SET_LOCAL" do
16
+ tree = FunctionDefinition.new("test = 2", 0, [
17
+ Identifier.new('test', 0)
18
+ ])
19
+ bytecode = tree.elements.first.compile
20
+ assert_equal [SET_LOCAL, :test], bytecode
21
+ end
22
+
23
+ test "should compile identifier with Expression as parent to GET_LOCAL" do
24
+ tree = Expression.new("test = 2", 0, [
25
+ Identifier.new('test', 0)
26
+ ])
27
+ bytecode = tree.elements.first.compile
28
+ assert_equal [GET_LOCAL, :test], bytecode
29
+ end
30
+
31
+ test "should compile identifier with MultitiveExpression as parent to GET_LOCAL" do
32
+ tree = MultitiveExpression.new("test = 2", 0, [
33
+ Identifier.new('test', 0)
34
+ ])
35
+ bytecode = tree.elements.first.compile
36
+ assert_equal [GET_LOCAL, :test], bytecode
37
+ end
38
+
39
+ test "should compile identifier with AdditiveExpression as parent to GET_LOCAL" do
40
+ tree = AdditiveExpression.new("test = 2", 0, [
41
+ Identifier.new('test', 0)
42
+ ])
43
+ bytecode = tree.elements.first.compile
44
+ assert_equal [GET_LOCAL, :test], bytecode
45
+ end
46
+
47
+ test "should compile identifier with ComparativeExpression as parent to GET_LOCAL" do
48
+ tree = ComparativeExpression.new("test = 2", 0, [
49
+ Identifier.new('test', 0)
50
+ ])
51
+ bytecode = tree.elements.first.compile
52
+ assert_equal [GET_LOCAL, :test], bytecode
53
+ end
54
+
55
+ test "should compile identifier with Assignment as parent to SET_GLOBAL" do
56
+ tree = Assignment.new("$test = 2", 0, [
57
+ Identifier.new('$test', 0)
58
+ ])
59
+ bytecode = tree.elements.first.compile
60
+ assert_equal [SET_GLOBAL, :$test], bytecode
61
+ end
62
+
63
+ test "should compile identifier with FunctionDefinition as parent to SET_GLOBAL" do
64
+ tree = FunctionDefinition.new("$test = 2", 0, [
65
+ Identifier.new('$test', 0)
66
+ ])
67
+ bytecode = tree.elements.first.compile
68
+ assert_equal [SET_GLOBAL, :$test], bytecode
69
+ end
70
+
71
+ test "should compile identifier with Expression as parent to GET_GLOBAL" do
72
+ tree = Expression.new("$test = 2", 0, [
73
+ Identifier.new('$test', 0)
74
+ ])
75
+ bytecode = tree.elements.first.compile
76
+ assert_equal [GET_GLOBAL, :$test], bytecode
77
+ end
78
+
79
+ test "should compile identifier with MultitiveExpression as parent to GET_GLOBAL" do
80
+ tree = MultitiveExpression.new("$test = 2", 0, [
81
+ Identifier.new('$test', 0)
82
+ ])
83
+ bytecode = tree.elements.first.compile
84
+ assert_equal [GET_GLOBAL, :$test], bytecode
85
+ end
86
+
87
+ test "should compile identifier with AdditiveExpression as parent to GET_GLOBAL" do
88
+ tree = AdditiveExpression.new("$test = 2", 0, [
89
+ Identifier.new('$test', 0)
90
+ ])
91
+ bytecode = tree.elements.first.compile
92
+ assert_equal [GET_GLOBAL, :$test], bytecode
93
+ end
94
+
95
+ test "should compile identifier with ComarativeExpression as parent to GET_GLOBAL" do
96
+ tree = ComparativeExpression.new("$test = 2", 0, [
97
+ Identifier.new('$test', 0)
98
+ ])
99
+ bytecode = tree.elements.first.compile
100
+ assert_equal [GET_GLOBAL, :$test], bytecode
101
+ end
102
+
103
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class FalseLiteralTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile FalseLiteral" do
8
+ tree = FalseLiteral.new("false", 0)
9
+ bytecode = tree.compile
10
+ assert_equal [PUSH_BOOL, false], bytecode
11
+ end
12
+
13
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class IntegerLiteralTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile FloatLiteral" do
8
+ tree = FloatLiteral.new("99.00019", 0)
9
+ bytecode = tree.compile
10
+ assert_equal [PUSH_FLOAT, 99.00019], bytecode
11
+ end
12
+
13
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class IntegerLiteralTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile IntegerLiteral" do
8
+ tree = IntegerLiteral.new("99", 0)
9
+ bytecode = tree.compile
10
+ assert_equal [PUSH_INT, 99], bytecode
11
+ end
12
+
13
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class NilLiteralTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile NilLiteral" do
8
+ tree = NilLiteral.new("nil", 0)
9
+ bytecode = tree.compile
10
+ assert_equal [PUSH_NIL], bytecode
11
+ end
12
+
13
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class StringLiteralTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile StringLiteral" do
8
+ tree = StringLiteral.new('"test"', 0)
9
+ bytecode = tree.compile
10
+ assert_equal [PUSH_STRING, 'test'], bytecode
11
+ end
12
+
13
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper.rb'))
2
+
3
+ class TrueLiteralTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceCompiler
6
+
7
+ test "should compile TrueLiteral" do
8
+ tree = TrueLiteral.new("true", 0)
9
+ bytecode = tree.compile
10
+ assert_equal [PUSH_BOOL, true], bytecode
11
+ end
12
+
13
+ end