tataru 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -0
  3. data/bin/console +15 -0
  4. data/bin/setup +8 -0
  5. data/lib/tataru.rb +70 -12
  6. data/lib/tataru/base_resource.rb +49 -0
  7. data/lib/tataru/base_resource_desc.rb +35 -0
  8. data/lib/tataru/compiler.rb +100 -0
  9. data/lib/tataru/create_subroutines.rb +31 -0
  10. data/lib/tataru/delete_subroutines.rb +42 -0
  11. data/lib/tataru/flattener.rb +81 -0
  12. data/lib/tataru/init_hash_compiler.rb +41 -0
  13. data/lib/tataru/instruction.rb +52 -7
  14. data/lib/tataru/instruction_hash.rb +54 -0
  15. data/lib/tataru/instructions/call_instruction.rb +19 -0
  16. data/lib/tataru/instructions/check_create_instruction.rb +27 -0
  17. data/lib/tataru/instructions/check_delete_instruction.rb +20 -0
  18. data/lib/tataru/instructions/check_instruction.rb +26 -0
  19. data/lib/tataru/instructions/check_update_instruction.rb +27 -0
  20. data/lib/tataru/instructions/clear_instruction.rb +12 -0
  21. data/lib/tataru/instructions/compare_instruction.rb +16 -0
  22. data/lib/tataru/instructions/create_instruction.rb +20 -0
  23. data/lib/tataru/instructions/delete_instruction.rb +14 -0
  24. data/lib/tataru/instructions/end_instruction.rb +12 -0
  25. data/lib/tataru/instructions/goto_if_instruction.rb +26 -0
  26. data/lib/tataru/instructions/immediate_mode_instruction.rb +12 -0
  27. data/lib/tataru/instructions/init_instruction.rb +27 -0
  28. data/lib/tataru/instructions/key_instruction.rb +12 -0
  29. data/lib/tataru/instructions/mark_deletable_instruction.rb +13 -0
  30. data/lib/tataru/instructions/read_instruction.rb +28 -0
  31. data/lib/tataru/instructions/rescmp_instruction.rb +34 -0
  32. data/lib/tataru/instructions/resource_instruction.rb +15 -0
  33. data/lib/tataru/instructions/return_instruction.rb +16 -0
  34. data/lib/tataru/instructions/update_instruction.rb +16 -0
  35. data/lib/tataru/instructions/value_instruction.rb +15 -0
  36. data/lib/tataru/instructions/value_rom_instruction.rb +23 -0
  37. data/lib/tataru/instructions/value_update_instruction.rb +18 -0
  38. data/lib/tataru/memory.rb +16 -0
  39. data/lib/tataru/quest.rb +43 -0
  40. data/lib/tataru/representation.rb +22 -0
  41. data/lib/tataru/representations/array_representation.rb +18 -0
  42. data/lib/tataru/representations/hash_representation.rb +20 -0
  43. data/lib/tataru/representations/literal_representation.rb +9 -0
  44. data/lib/tataru/representations/output_representation.rb +19 -0
  45. data/lib/tataru/representations/resource_representation.rb +49 -0
  46. data/lib/tataru/resolver.rb +39 -0
  47. data/lib/tataru/resource_dsl.rb +21 -71
  48. data/lib/tataru/resource_type_pool.rb +22 -0
  49. data/lib/tataru/rom_reader.rb +47 -0
  50. data/lib/tataru/runner.rb +48 -0
  51. data/lib/tataru/sub_planner.rb +41 -0
  52. data/lib/tataru/subroutine_compiler.rb +52 -0
  53. data/lib/tataru/top_dsl.rb +35 -0
  54. data/lib/tataru/update_subroutines.rb +111 -0
  55. data/lib/tataru/version.rb +1 -1
  56. data/spec/compiler_spec.rb +181 -0
  57. data/spec/flattener_spec.rb +88 -0
  58. data/spec/init_hash_compiler_spec.rb +85 -0
  59. data/spec/instruction_hash_spec.rb +63 -0
  60. data/spec/instruction_spec.rb +36 -0
  61. data/spec/instructions/call_instruction_spec.rb +28 -0
  62. data/spec/instructions/check_create_instruction_spec.rb +67 -0
  63. data/spec/instructions/check_delete_instruction_spec.rb +47 -0
  64. data/spec/instructions/check_update_instruction_spec.rb +67 -0
  65. data/spec/instructions/clear_instruction_spec.rb +16 -0
  66. data/spec/instructions/compare_instruction_spec.rb +29 -0
  67. data/spec/instructions/create_instruction_spec.rb +62 -0
  68. data/spec/instructions/delete_instruction_spec.rb +20 -0
  69. data/spec/instructions/end_instruction_spec.rb +15 -0
  70. data/spec/instructions/goto_if_instruction_spec.rb +42 -0
  71. data/spec/instructions/init_instruction_spec.rb +16 -0
  72. data/spec/instructions/key_instruction_spec.rb +15 -0
  73. data/spec/instructions/mark_deletable_instruction_spec.rb +20 -0
  74. data/spec/instructions/read_instruction_spec.rb +34 -0
  75. data/spec/instructions/rescmp_instruction_spec.rb +113 -0
  76. data/spec/instructions/return_instruction_spec.rb +28 -0
  77. data/spec/instructions/update_instruction_spec.rb +39 -0
  78. data/spec/instructions/value_instruction_spec.rb +27 -0
  79. data/spec/instructions/value_rom_instruction_spec.rb +170 -0
  80. data/spec/instructions/value_update_instruction_spec.rb +35 -0
  81. data/spec/quest_spec.rb +9 -0
  82. data/spec/representations/array_representation_spec.rb +29 -0
  83. data/spec/representations/hash_representation_spec.rb +29 -0
  84. data/spec/representations/literal_representation_spec.rb +10 -0
  85. data/spec/representations/output_representation_spec.rb +11 -0
  86. data/spec/representations/resource_representation_spec.rb +50 -0
  87. data/spec/resource_dsl_spec.rb +71 -0
  88. data/spec/runner_spec.rb +99 -0
  89. data/spec/spec_helper.rb +401 -0
  90. data/spec/subroutine_compiler_spec.rb +39 -0
  91. data/spec/taru_spec.rb +616 -0
  92. data/spec/top_dsl_spec.rb +68 -0
  93. data/tataru.gemspec +45 -0
  94. metadata +145 -27
  95. data/Rakefile +0 -8
  96. data/lib/tataru/default_resource_finder.rb +0 -10
  97. data/lib/tataru/execution_step.rb +0 -79
  98. data/lib/tataru/planner.rb +0 -93
  99. data/lib/tataru/requirements.rb +0 -67
  100. data/lib/tataru/requirements_dsl.rb +0 -33
  101. data/lib/tataru/resource.rb +0 -52
  102. data/lib/tataru/state.rb +0 -64
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::ClearInstruction do
6
+ it 'clears temp' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::ClearInstruction.new
9
+
10
+ mem.hash[:temp] = { something: 'haha' }
11
+ instr.memory = mem
12
+ instr.run
13
+
14
+ expect(mem.hash[:temp]).to eq({})
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::CompareInstruction do
6
+ it 'sets to 1 if equal' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::CompareInstruction.new('abc')
9
+
10
+ mem.hash[:temp] = { result: 'abc' }
11
+
12
+ instr.memory = mem
13
+ instr.run
14
+
15
+ expect(mem.hash[:temp][:result]).to eq 1
16
+ end
17
+
18
+ it 'sets to 0 if not equal' do
19
+ mem = Tataru::Memory.new
20
+ instr = Tataru::Instructions::CompareInstruction.new('def')
21
+
22
+ mem.hash[:temp] = { result: 'abc' }
23
+
24
+ instr.memory = mem
25
+ instr.run
26
+
27
+ expect(mem.hash[:temp][:result]).to eq 0
28
+ end
29
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::CreateInstruction do
6
+ it 'sets remote id when needs remote id is true' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::CreateInstruction.new
9
+
10
+ allow_any_instance_of(Tataru::BaseResourceDesc).to receive(:needs_remote_id?) { true }
11
+ expect_any_instance_of(Tataru::BaseResource).to receive(:create).with({ someprop: 'abc' })
12
+ allow_any_instance_of(Tataru::BaseResource).to receive(:remote_id) { 'someid' }
13
+
14
+ mem.hash[:temp] = {
15
+ resource_name: 'thing',
16
+ resource_desc: 'Tataru::BaseResourceDesc',
17
+ properties: { someprop: 'abc' }
18
+ }
19
+ mem.hash[:remote_ids] = {}
20
+ instr.memory = mem
21
+ instr.run
22
+
23
+ expect(mem.hash[:remote_ids]['thing']).to eq 'someid'
24
+ end
25
+
26
+ it 'does not set remote id if does not need remote id' do
27
+ mem = Tataru::Memory.new
28
+ instr = Tataru::Instructions::CreateInstruction.new
29
+
30
+ allow_any_instance_of(Tataru::BaseResourceDesc).to receive(:needs_remote_id?) { false }
31
+ expect_any_instance_of(Tataru::BaseResource).to receive(:create).with({ someprop: 'def' })
32
+
33
+ mem.hash[:temp] = {
34
+ resource_name: 'thing',
35
+ resource_desc: 'Tataru::BaseResourceDesc',
36
+ properties: { someprop: 'def' }
37
+ }
38
+ mem.hash[:remote_ids] = {}
39
+ instr.memory = mem
40
+
41
+ instr.run
42
+ end
43
+
44
+ xit 'throws error if remote_id already set' do
45
+ mem = Tataru::Memory.new
46
+ instr = Tataru::Instructions::CreateInstruction.new
47
+
48
+ allow_any_instance_of(Tataru::BaseResourceDesc).to receive(:needs_remote_id?) { true }
49
+ expect_any_instance_of(Tataru::BaseResource).to receive(:create).with({ someprop: 'abc' })
50
+ allow_any_instance_of(Tataru::BaseResource).to receive(:remote_id) { 'someid' }
51
+
52
+ mem.hash[:temp] = {
53
+ resource_name: 'thing',
54
+ resource_desc: 'Tataru::BaseResourceDesc',
55
+ properties: { someprop: 'abc' }
56
+ }
57
+ mem.hash[:remote_ids] = { 'thing' => 'def' }
58
+ instr.memory = mem
59
+
60
+ expect { instr.run }.to raise_error
61
+ end
62
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::DeleteInstruction do
6
+ it 'calls delete on the resource' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::DeleteInstruction.new
9
+
10
+ mem.hash[:temp] = {
11
+ resource_name: 'thing',
12
+ resource_desc: 'Tataru::BaseResourceDesc'
13
+ }
14
+ mem.hash[:remote_ids] = { 'thing' => 'hello' }
15
+ instr.memory = mem
16
+
17
+ expect_any_instance_of(Tataru::BaseResource).to receive(:delete)
18
+ instr.run
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::EndInstruction do
6
+ it 'sets end to true' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::EndInstruction.new
9
+
10
+ instr.memory = mem
11
+ instr.run
12
+
13
+ expect(mem.end).to eq true
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::GotoIfInstruction do
6
+ it 'branches if result is non zero' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::GotoIfInstruction.new(5)
9
+
10
+ mem.hash[:temp] = { result: 1 }
11
+
12
+ instr.memory = mem
13
+ instr.run
14
+
15
+ expect(mem.program_counter).to eq 4
16
+ end
17
+
18
+ it 'does nothing if result is zero' do
19
+ mem = Tataru::Memory.new
20
+ instr = Tataru::Instructions::GotoIfInstruction.new(6)
21
+
22
+ mem.hash[:temp] = { result: 0 }
23
+
24
+ instr.memory = mem
25
+ instr.run
26
+
27
+ expect(mem.program_counter).to eq 0
28
+ end
29
+
30
+ it 'goes to label if param is label' do
31
+ mem = Tataru::Memory.new
32
+ instr = Tataru::Instructions::GotoIfInstruction.new('function')
33
+
34
+ mem.hash[:labels] = { 'function' => 10 }
35
+ mem.hash[:temp] = { result: 1 }
36
+
37
+ instr.memory = mem
38
+ instr.run
39
+
40
+ expect(mem.program_counter).to eq 9
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::InitInstruction do
6
+ it 'sets hashes' do
7
+ instr = Tataru::Instructions::InitInstruction.new
8
+ mem = Tataru::Memory.new
9
+ instr.memory = mem
10
+ instr.run
11
+ expect(mem.hash.key? :remote_ids).to eq true
12
+ expect(mem.hash.key? :outputs).to eq true
13
+ expect(mem.hash.key? :labels).to eq true
14
+ expect(mem.hash.key? :deleted).to eq true
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::KeyInstruction do
6
+ it 'set a key' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::KeyInstruction.new('meow')
9
+
10
+ instr.memory = mem
11
+ instr.run
12
+
13
+ expect(mem.hash[:temp]).to eq(_key: 'meow')
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::MarkDeletableInstruction do
6
+ it 'set a resource as deletable' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::MarkDeletableInstruction.new
9
+
10
+ mem.hash[:temp] = {
11
+ resource_name: 'thing',
12
+ resource_desc: 'Tataru::BaseResourceDesc'
13
+ }
14
+ mem.hash[:remote_ids] = { 'thing' => 'hello' }
15
+ instr.memory = mem
16
+ instr.run
17
+
18
+ expect(mem.hash[:remote_ids]).to eq('_deletable_thing' => 'hello')
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::ReadInstruction do
6
+ it 'sets hashes' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::ReadInstruction.new
9
+
10
+ mem.hash[:temp] = {
11
+ resource_name: 'thing',
12
+ resource_desc: 'Tataru::BaseResourceDesc'
13
+ }
14
+ mem.hash[:remote_ids] = { 'thing' => 'hello' }
15
+ instr.memory = mem
16
+
17
+ expect_any_instance_of(Tataru::BaseResourceDesc).to receive(:immutable_fields) { [:prop1] }
18
+ expect_any_instance_of(Tataru::BaseResourceDesc).to receive(:mutable_fields) { [:prop2] }
19
+
20
+ expect_any_instance_of(Tataru::BaseResource).to receive(:read).with([:prop1, :prop2]) do
21
+ {
22
+ prop1: 'info',
23
+ prop2: '1234',
24
+ extra: 'notseen'
25
+ }
26
+ end
27
+ instr.run
28
+
29
+ expect(mem.hash[:temp]['thing']).to eq(
30
+ prop1: 'info',
31
+ prop2: '1234'
32
+ )
33
+ end
34
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::RescmpInstruction do
6
+ # compares temp with rom properties
7
+ # sets no_change, update, recreate
8
+
9
+ it 'sets temp result to no_change if there are no changes' do
10
+ mem = Tataru::Memory.new
11
+ instr = Tataru::Instructions::RescmpInstruction.new
12
+
13
+ allow_any_instance_of(Tataru::BaseResourceDesc).to receive(:mutable_fields) { [:prop] }
14
+
15
+ mem.hash[:rom] = {
16
+ 'thing' => {
17
+ type: :hash,
18
+ references: {
19
+ :prop => 'literal'
20
+ }
21
+ },
22
+ 'literal' => {
23
+ type: :literal,
24
+ value: 'info'
25
+ }
26
+ }
27
+
28
+ mem.hash[:temp] = {
29
+ resource_name: 'thing',
30
+ resource_desc: 'Tataru::BaseResourceDesc',
31
+ 'thing' => {
32
+ prop: 'info'
33
+ }
34
+ }
35
+
36
+ mem.hash[:update_action] = {}
37
+
38
+ instr.memory = mem
39
+ instr.run
40
+
41
+ expect(mem.hash[:update_action]['thing']).to eq :no_change
42
+ end
43
+
44
+ it 'sets temp result to modify if there are changes to mutable' do
45
+ mem = Tataru::Memory.new
46
+ instr = Tataru::Instructions::RescmpInstruction.new
47
+
48
+ allow_any_instance_of(Tataru::BaseResourceDesc).to receive(:mutable_fields) { [:prop] }
49
+
50
+ mem.hash[:rom] = {
51
+ 'thing' => {
52
+ type: :hash,
53
+ references: {
54
+ :prop => 'literal'
55
+ }
56
+ },
57
+ 'literal' => {
58
+ type: :literal,
59
+ value: 'AAAA'
60
+ }
61
+ }
62
+
63
+ mem.hash[:temp] = {
64
+ resource_name: 'thing',
65
+ resource_desc: 'Tataru::BaseResourceDesc',
66
+ 'thing' => {
67
+ prop: 'BBBB'
68
+ }
69
+ }
70
+
71
+ mem.hash[:update_action] = {}
72
+
73
+ instr.memory = mem
74
+ instr.run
75
+
76
+ expect(mem.hash[:update_action]['thing']).to eq :modify
77
+ end
78
+
79
+ it 'sets temp result to recreate if there are changes to immutable' do
80
+ mem = Tataru::Memory.new
81
+ instr = Tataru::Instructions::RescmpInstruction.new
82
+
83
+ allow_any_instance_of(Tataru::BaseResourceDesc).to receive(:immutable_fields) { [:prop] }
84
+
85
+ mem.hash[:rom] = {
86
+ 'thing' => {
87
+ type: :hash,
88
+ references: {
89
+ :prop => 'literal'
90
+ }
91
+ },
92
+ 'literal' => {
93
+ type: :literal,
94
+ value: 'AAAAA'
95
+ }
96
+ }
97
+
98
+ mem.hash[:temp] = {
99
+ resource_name: 'thing',
100
+ resource_desc: 'Tataru::BaseResourceDesc',
101
+ 'thing' => {
102
+ prop: 'BBNBB'
103
+ }
104
+ }
105
+
106
+ mem.hash[:update_action] = {}
107
+
108
+ instr.memory = mem
109
+ instr.run
110
+
111
+ expect(mem.hash[:update_action]['thing']).to eq :recreate
112
+ end
113
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::ReturnInstruction do
6
+ it 'return to top of stack' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::ReturnInstruction.new
9
+
10
+ mem.call_stack = [1, 2, 3]
11
+ instr.memory = mem
12
+ instr.run
13
+
14
+ expect(mem.program_counter).to eq 3
15
+ end
16
+
17
+ it 'sets error if no more stack' do
18
+ mem = Tataru::Memory.new
19
+ mem.call_stack = []
20
+ instr = Tataru::Instructions::ReturnInstruction.new
21
+
22
+ instr.memory = mem
23
+ instr.run
24
+
25
+ expect(mem.program_counter).to eq 0
26
+ expect(mem.error).to eq 'At bottom of stack'
27
+ end
28
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tataru'
4
+
5
+ describe Tataru::Instructions::UpdateInstruction do
6
+ it 'calls update' do
7
+ mem = Tataru::Memory.new
8
+ instr = Tataru::Instructions::UpdateInstruction.new
9
+
10
+ mem.hash[:temp] = {
11
+ resource_name: 'thing',
12
+ resource_desc: 'Tataru::BaseResourceDesc',
13
+ properties: { 'someprop' => 'somevalue' }
14
+ }
15
+
16
+ mem.hash[:remote_ids] = { 'thing' => 'hello' }
17
+ instr.memory = mem
18
+ expect_any_instance_of(Tataru::BaseResource).to receive(:update).with('someprop' => 'somevalue')
19
+
20
+ instr.run
21
+ end
22
+
23
+ xit 'should throw error if an immutable prop is changed' do
24
+ mem = Tataru::Memory.new
25
+ instr = Tataru::Instructions::UpdateInstruction.new
26
+
27
+ expect_any_instance_of(Tataru::BaseResourceDesc).to receive(:immutable_fields) { ['someprop'] }
28
+
29
+ mem.hash[:temp] = {
30
+ resource_name: 'thing',
31
+ resource_desc: 'Tataru::BaseResourceDesc',
32
+ properties: { 'someprop' => 'somevalue' }
33
+ }
34
+ mem.hash[:remote_ids] = { 'thing' => 'hello' }
35
+ instr.memory = mem
36
+
37
+ expect { instr.run }.to raise_error 'immutable value changed'
38
+ end
39
+ end