gobstones 0.0.1.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/.gitignore +1 -3
- data/.ruby-version +1 -1
- data/.travis.yml +1 -1
- data/CHANGELOG +22 -2
- data/Gemfile +3 -3
- data/Gemfile.lock +32 -27
- data/bin/gobstones +1 -1
- data/examples/.gitkeep +0 -0
- data/gobstones.gemspec +2 -2
- data/lib/gobstones/cli/board_template +1 -1
- data/lib/gobstones/cli/printer.rb +1 -1
- data/lib/gobstones/cli/runner.rb +8 -2
- data/lib/gobstones/extensions/all.rb +2 -1
- data/lib/gobstones/extensions/boolean.rb +1 -1
- data/lib/gobstones/extensions/fixnum.rb +1 -1
- data/lib/gobstones/extensions/string.rb +9 -0
- data/lib/gobstones/lang/all.rb +1 -1
- data/lib/gobstones/lang/commands/all.rb +3 -2
- data/lib/gobstones/lang/commands/boom_cmd.rb +6 -3
- data/lib/gobstones/lang/commands/command_block.rb +1 -1
- data/lib/gobstones/lang/commands/conditional_cmd.rb +5 -1
- data/lib/gobstones/lang/commands/ir_al_origen_cmd.rb +1 -1
- data/lib/gobstones/lang/commands/mover_cmd.rb +4 -2
- data/lib/gobstones/lang/commands/multiple_assignment.rb +35 -0
- data/lib/gobstones/lang/commands/poner_cmd.rb +8 -4
- data/lib/gobstones/lang/commands/procedure_call.rb +1 -9
- data/lib/gobstones/lang/commands/repeat_with_cmd.rb +10 -6
- data/lib/gobstones/lang/commands/sacar_cmd.rb +9 -7
- data/lib/gobstones/lang/commands/{assignments.rb → single_assignment.rb} +5 -3
- data/lib/gobstones/lang/commands/skip_cmd.rb +2 -2
- data/lib/gobstones/lang/commands/vaciar_tablero_cmd.rb +1 -1
- data/lib/gobstones/lang/commands/while_cmd.rb +1 -1
- data/lib/gobstones/lang/definitions/all.rb +1 -1
- data/lib/gobstones/lang/definitions/definition.rb +47 -5
- data/lib/gobstones/lang/definitions/definition_call.rb +17 -5
- data/lib/gobstones/lang/definitions/function.rb +9 -0
- data/lib/gobstones/lang/definitions/main.rb +1 -1
- data/lib/gobstones/lang/definitions/no_return_statement.rb +7 -3
- data/lib/gobstones/lang/definitions/procedure.rb +5 -20
- data/lib/gobstones/lang/definitions/return_from_function.rb +12 -2
- data/lib/gobstones/lang/definitions/return_from_main.rb +5 -1
- data/lib/gobstones/lang/definitions/var_tuple.rb +13 -1
- data/lib/gobstones/lang/expressions/all.rb +1 -1
- data/lib/gobstones/lang/expressions/arithmetic_expressions.rb +1 -1
- data/lib/gobstones/lang/expressions/boolean_expressions.rb +3 -2
- data/lib/gobstones/lang/expressions/enclosed_by_parens_expression.rb +17 -0
- data/lib/gobstones/lang/expressions/function_call.rb +1 -1
- data/lib/gobstones/lang/expressions/one_arg_expression.rb +16 -4
- data/lib/gobstones/lang/expressions/primitive_functions.rb +18 -12
- data/lib/gobstones/lang/expressions/two_arg_expression.rb +7 -3
- data/lib/gobstones/lang/expressions/type_bound_functions.rb +1 -1
- data/lib/gobstones/lang/expressions/var_name.rb +9 -3
- data/lib/gobstones/lang/literals/all.rb +1 -1
- data/lib/gobstones/lang/literals/colors.rb +1 -1
- data/lib/gobstones/lang/literals/literal.rb +31 -21
- data/lib/gobstones/lang/literals/number.rb +1 -1
- data/lib/gobstones/lang/program.rb +13 -4
- data/lib/gobstones/modules/equal_by_class.rb +1 -1
- data/lib/gobstones/parser/ast/ast.rb +12 -8
- data/lib/gobstones/parser/grammar/gobstones.treetop +4 -4
- data/lib/gobstones/parser/parse_error.rb +1 -1
- data/lib/gobstones/runner/all.rb +1 -2
- data/lib/gobstones/runner/board.rb +13 -4
- data/lib/gobstones/runner/cell.rb +10 -0
- data/lib/gobstones/runner/errors/all.rb +1 -1
- data/lib/gobstones/runner/errors/definition_not_found_error.rb +1 -1
- data/lib/gobstones/runner/execution_context.rb +66 -16
- data/lib/gobstones/runner/head.rb +17 -3
- data/spec/gobstones_lang_test_objects.rb +75 -0
- data/spec/lang/commands/boom_cmd_spec.rb +3 -3
- data/spec/lang/commands/cmd_block_spec.rb +14 -14
- data/spec/lang/commands/if_cmd_spec.rb +21 -20
- data/spec/lang/commands/ir_al_origen_cmd_spec.rb +3 -3
- data/spec/lang/commands/mover_cmd_spec.rb +12 -14
- data/spec/lang/commands/multiple_assignment_spec.rb +37 -0
- data/spec/lang/commands/poner_cmd_spec.rb +13 -14
- data/spec/lang/commands/procedure_call_spec.rb +19 -24
- data/spec/lang/commands/procedure_spec.rb +32 -36
- data/spec/lang/commands/repeat_with_cmd_spec.rb +39 -20
- data/spec/lang/commands/sacar_cmd_spec.rb +17 -16
- data/spec/lang/commands/single_assignment_spec.rb +13 -0
- data/spec/lang/commands/skip_cmd_spec.rb +2 -2
- data/spec/lang/commands/vaciar_tablero_cmd_spec.rb +7 -6
- data/spec/lang/commands/while_cmd_spec.rb +21 -15
- data/spec/lang/definitions/no_return_statement_spec.rb +10 -0
- data/spec/lang/definitions/var_tuple_spec.rb +16 -0
- data/spec/lang/expressions/arithmetic_expressions_spec.rb +15 -15
- data/spec/lang/expressions/boolean_expressions_spec.rb +35 -25
- data/spec/lang/expressions/comparison_expressions_spec.rb +25 -28
- data/spec/lang/expressions/enclosed_by_parens_expression_spec.rb +11 -0
- data/spec/lang/expressions/function_call_spec.rb +29 -0
- data/spec/lang/expressions/primitive_functions_spec.rb +60 -62
- data/spec/lang/expressions/type_bound_functions_spec.rb +13 -13
- data/spec/lang/expressions/var_name_spec.rb +20 -8
- data/spec/lang/literals/booleans_spec.rb +5 -7
- data/spec/lang/literals/colors_spec.rb +4 -4
- data/spec/lang/literals/directions_spec.rb +12 -12
- data/spec/lang/literals/numbers_spec.rb +2 -2
- data/spec/matchers/parse_matcher.rb +9 -10
- data/spec/parser/arithmetic_expressions_spec.rb +19 -19
- data/spec/parser/assignments_spec.rb +24 -10
- data/spec/parser/boolean_expressions_spec.rb +18 -18
- data/spec/parser/command_block_spec.rb +17 -19
- data/spec/parser/data_types_spec.rb +23 -23
- data/spec/parser/function_calls_spec.rb +13 -12
- data/spec/parser/function_definitions_spec.rb +13 -18
- data/spec/parser/gobstones_program_spec.rb +15 -15
- data/spec/parser/if_command_spec.rb +13 -12
- data/spec/parser/main_definition_spec.rb +12 -12
- data/spec/parser/nested_expressions_spec.rb +16 -20
- data/spec/parser/primitive_expressions_spec.rb +27 -33
- data/spec/parser/procedure_calls_spec.rb +12 -12
- data/spec/parser/procedure_definitions_spec.rb +10 -16
- data/spec/parser/repeat_with_command_spec.rb +7 -10
- data/spec/parser/simple_commands_spec.rb +10 -10
- data/spec/parser/treetop_parser_spec.rb +11 -10
- data/spec/parser/var_tuple_spec.rb +7 -11
- data/spec/parser/while_command_spec.rb +9 -9
- data/spec/runner/board_spec.rb +23 -27
- data/spec/runner/cell_spec.rb +34 -38
- data/spec/runner/execution_context_spec.rb +38 -24
- data/spec/runner/head_spec.rb +54 -63
- data/spec/spec_helper.rb +4 -1
- data/spec/type_checker_spec.rb +13 -13
- metadata +33 -18
- data/lib/gobstones/lang/expressions/parentheses_expression.rb +0 -13
- data/spec/lang/commands/assignments_spec.rb +0 -13
@@ -0,0 +1,29 @@
|
|
1
|
+
describe FunctionCall do
|
2
|
+
|
3
|
+
it "evaluates an existing function and returns its result when calling it" do
|
4
|
+
return_statement = ReturnFromFunction.new [42.to_gbs_num]
|
5
|
+
my_function = Function.new 'myFunction', no_arguments, empty_body, return_statement
|
6
|
+
program = Program.new [my_function], no_return_statement
|
7
|
+
context = program_context_for program
|
8
|
+
call = FunctionCall.new 'myFunction', []
|
9
|
+
|
10
|
+
result = call.evaluate context
|
11
|
+
|
12
|
+
expect(result).to eq(42.to_gbs_num)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "evaluates an existing function with some arguments" do
|
16
|
+
number_a, number_b, number_c = 'a'.to_var_name, 'b'.to_var_name, 'c'.to_var_name
|
17
|
+
return_statement = ReturnFromFunction.new [Add.new(Add.new(number_a, number_b), number_c)]
|
18
|
+
args = VarTuple.new [number_a, number_b, number_c]
|
19
|
+
my_function = Function.new 'myFunction', args, empty_body, return_statement
|
20
|
+
program = Program.new [my_function], no_return_statement
|
21
|
+
context = program_context_for program
|
22
|
+
call = FunctionCall.new 'myFunction', [1.to_gbs_num, 2.to_gbs_num, 3.to_gbs_num]
|
23
|
+
|
24
|
+
result = call.evaluate context
|
25
|
+
|
26
|
+
expect(result).to eq(6.to_gbs_num)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -1,126 +1,124 @@
|
|
1
1
|
describe "primitive functions" do
|
2
2
|
|
3
|
-
let(:context) {
|
4
|
-
let(:black) { Negro.new }
|
5
|
-
let(:west) { Oeste.new }
|
3
|
+
let(:context) { clean_context }
|
6
4
|
|
7
5
|
describe "nroBolitas() function" do
|
8
6
|
|
9
|
-
it "
|
10
|
-
expect(NroBolitas.new(
|
7
|
+
it "evaluates correctly in a clean context" do
|
8
|
+
expect(NroBolitas.new(negro).evaluate(context)).to eq(0.to_gbs_num)
|
11
9
|
end
|
12
10
|
|
13
|
-
it "
|
14
|
-
5.times { context.head.put
|
15
|
-
expect(NroBolitas.new(
|
11
|
+
it "evaluates correctly in a context with some data" do
|
12
|
+
5.times { context.head.put negro }
|
13
|
+
expect(NroBolitas.new(negro).evaluate(context)).to eq(5.to_gbs_num)
|
16
14
|
end
|
17
15
|
|
18
16
|
end
|
19
17
|
|
20
18
|
describe "hayBolitas() function" do
|
21
19
|
|
22
|
-
it "
|
23
|
-
expect(HayBolitas.new(
|
20
|
+
it "evaluates correctly in a clean context" do
|
21
|
+
expect(HayBolitas.new(negro).evaluate(context)).to eq(false_value)
|
24
22
|
end
|
25
23
|
|
26
|
-
it "
|
27
|
-
context.head.put
|
28
|
-
expect(HayBolitas.new(
|
24
|
+
it "evaluates correctly in a context with some data" do
|
25
|
+
context.head.put negro
|
26
|
+
expect(HayBolitas.new(negro).evaluate(context)).to eq(true_value)
|
29
27
|
end
|
30
28
|
|
31
29
|
end
|
32
30
|
|
33
31
|
describe "puedeMover() function" do
|
34
32
|
|
35
|
-
it "
|
36
|
-
expect(PuedeMover.new(
|
33
|
+
it "evaluates correctly in a clean context" do
|
34
|
+
expect(PuedeMover.new(oeste).evaluate(context)).to eq(false_value)
|
37
35
|
end
|
38
36
|
|
39
|
-
it "
|
40
|
-
context.head.move(
|
41
|
-
expect(PuedeMover.new(
|
37
|
+
it "evaluates correctly in a modified context" do
|
38
|
+
context.head.move(este)
|
39
|
+
expect(PuedeMover.new(oeste).evaluate(context)).to eq(true_value)
|
42
40
|
end
|
43
41
|
|
44
42
|
end
|
45
43
|
|
46
44
|
describe "siguiente() function" do
|
47
45
|
|
48
|
-
it "
|
49
|
-
expect(Siguiente.new(15.to_gbs_num).evaluate).to eq(16.to_gbs_num)
|
46
|
+
it "evaluates correctly for numbers" do
|
47
|
+
expect(Siguiente.new(15.to_gbs_num).evaluate(context)).to eq(16.to_gbs_num)
|
50
48
|
end
|
51
49
|
|
52
|
-
it "
|
53
|
-
expect(Siguiente.new(
|
54
|
-
expect(Siguiente.new(
|
55
|
-
expect(Siguiente.new(
|
56
|
-
expect(Siguiente.new(
|
50
|
+
it "evaluates correctly for colors" do
|
51
|
+
expect(Siguiente.new(azul).evaluate(context)).to eq(negro)
|
52
|
+
expect(Siguiente.new(negro).evaluate(context)).to eq(rojo)
|
53
|
+
expect(Siguiente.new(rojo).evaluate(context)).to eq(verde)
|
54
|
+
expect(Siguiente.new(verde).evaluate(context)).to eq(azul)
|
57
55
|
end
|
58
56
|
|
59
|
-
it "
|
60
|
-
expect(Siguiente.new(
|
61
|
-
expect(Siguiente.new(
|
62
|
-
expect(Siguiente.new(
|
63
|
-
expect(Siguiente.new(
|
57
|
+
it "evaluates correctly for directions" do
|
58
|
+
expect(Siguiente.new(norte).evaluate(context)).to eq(este)
|
59
|
+
expect(Siguiente.new(este).evaluate(context)).to eq(sur)
|
60
|
+
expect(Siguiente.new(sur).evaluate(context)).to eq(oeste)
|
61
|
+
expect(Siguiente.new(oeste).evaluate(context)).to eq(norte)
|
64
62
|
end
|
65
63
|
|
66
|
-
it "
|
67
|
-
expect(Siguiente.new(
|
68
|
-
expect(Siguiente.new(
|
64
|
+
it "evaluates correctly for booleans" do
|
65
|
+
expect(Siguiente.new(true_value).evaluate(context)).to eq(false_value)
|
66
|
+
expect(Siguiente.new(false_value).evaluate(context)).to eq(true_value)
|
69
67
|
end
|
70
68
|
|
71
69
|
end
|
72
70
|
|
73
71
|
describe "previo() function" do
|
74
72
|
|
75
|
-
it "
|
76
|
-
expect(Previo.new(43.to_gbs_num).evaluate).to eq(42.to_gbs_num)
|
73
|
+
it "evaluates correctly for numbers" do
|
74
|
+
expect(Previo.new(43.to_gbs_num).evaluate(context)).to eq(42.to_gbs_num)
|
77
75
|
end
|
78
76
|
|
79
|
-
it "
|
80
|
-
expect(Previo.new(
|
81
|
-
expect(Previo.new(
|
82
|
-
expect(Previo.new(
|
83
|
-
expect(Previo.new(
|
77
|
+
it "evaluates correctly for colors" do
|
78
|
+
expect(Previo.new(azul).evaluate(context)).to eq(verde)
|
79
|
+
expect(Previo.new(negro).evaluate(context)).to eq(azul)
|
80
|
+
expect(Previo.new(rojo).evaluate(context)).to eq(negro)
|
81
|
+
expect(Previo.new(verde).evaluate(context)).to eq(rojo)
|
84
82
|
end
|
85
83
|
|
86
|
-
it "
|
87
|
-
expect(Previo.new(
|
88
|
-
expect(Previo.new(
|
89
|
-
expect(Previo.new(
|
90
|
-
expect(Previo.new(
|
84
|
+
it "evaluates correctly for directions" do
|
85
|
+
expect(Previo.new(norte).evaluate(context)).to eq(oeste)
|
86
|
+
expect(Previo.new(este).evaluate(context)).to eq(norte)
|
87
|
+
expect(Previo.new(sur).evaluate(context)).to eq(este)
|
88
|
+
expect(Previo.new(oeste).evaluate(context)).to eq(sur)
|
91
89
|
end
|
92
90
|
|
93
|
-
it "
|
94
|
-
expect(Previo.new(
|
95
|
-
expect(Previo.new(
|
91
|
+
it "evaluates correctly for booleans" do
|
92
|
+
expect(Previo.new(true_value).evaluate(context)).to eq(false_value)
|
93
|
+
expect(Previo.new(false_value).evaluate(context)).to eq(true_value)
|
96
94
|
end
|
97
95
|
|
98
96
|
end
|
99
97
|
|
100
98
|
describe "opuesto() function" do
|
101
99
|
|
102
|
-
it "
|
103
|
-
expect(Opuesto.new(23.to_gbs_num).evaluate).to eq(-23.to_gbs_num)
|
104
|
-
expect(Opuesto.new(-42.to_gbs_num).evaluate).to eq(42.to_gbs_num)
|
100
|
+
it "evaluates correctly for numbers" do
|
101
|
+
expect(Opuesto.new(23.to_gbs_num).evaluate(context)).to eq(-23.to_gbs_num)
|
102
|
+
expect(Opuesto.new(-42.to_gbs_num).evaluate(context)).to eq(42.to_gbs_num)
|
105
103
|
end
|
106
104
|
|
107
|
-
it "
|
108
|
-
expect(Opuesto.new(
|
109
|
-
expect(Opuesto.new(
|
110
|
-
expect(Opuesto.new(
|
111
|
-
expect(Opuesto.new(
|
105
|
+
it "evaluates correctly for directions" do
|
106
|
+
expect(Opuesto.new(norte).evaluate(context)).to eq(sur)
|
107
|
+
expect(Opuesto.new(este).evaluate(context)).to eq(oeste)
|
108
|
+
expect(Opuesto.new(sur).evaluate(context)).to eq(norte)
|
109
|
+
expect(Opuesto.new(oeste).evaluate(context)).to eq(este)
|
112
110
|
end
|
113
111
|
|
114
|
-
it "
|
115
|
-
expect(Opuesto.new(
|
116
|
-
expect(Opuesto.new(
|
112
|
+
it "evaluates correctly for booleans" do
|
113
|
+
expect(Opuesto.new(true_value).evaluate(context)).to eq(false_value)
|
114
|
+
expect(Opuesto.new(false_value).evaluate(context)).to eq(true_value)
|
117
115
|
end
|
118
116
|
|
119
|
-
it "
|
120
|
-
expect { Opuesto.new(
|
117
|
+
it "fails when evaluating for colors" do
|
118
|
+
expect { Opuesto.new(verde).evaluate(context) }.
|
121
119
|
to raise_error(GobstonesTypeError, "colors don't have opposite")
|
122
120
|
end
|
123
121
|
|
124
122
|
end
|
125
123
|
|
126
|
-
end
|
124
|
+
end
|
@@ -2,38 +2,38 @@ describe "type bound functions" do
|
|
2
2
|
|
3
3
|
describe "boolean" do
|
4
4
|
|
5
|
-
it "
|
6
|
-
expect(MinBool.new.evaluate).to eq(
|
5
|
+
it "evaluates minBool() to False" do
|
6
|
+
expect(MinBool.new.evaluate).to eq(false_value)
|
7
7
|
end
|
8
8
|
|
9
|
-
it "
|
10
|
-
expect(MaxBool.new.evaluate).to eq(
|
9
|
+
it "evaluates maxBool() to True" do
|
10
|
+
expect(MaxBool.new.evaluate).to eq(true_value)
|
11
11
|
end
|
12
12
|
|
13
13
|
end
|
14
14
|
|
15
15
|
describe "colors" do
|
16
16
|
|
17
|
-
it "
|
18
|
-
expect(MinColor.new.evaluate).to eq(
|
17
|
+
it "evaluates minColor() to Azul" do
|
18
|
+
expect(MinColor.new.evaluate).to eq(azul)
|
19
19
|
end
|
20
20
|
|
21
|
-
it "
|
22
|
-
expect(MaxColor.new.evaluate).to eq(
|
21
|
+
it "evaluates maxColor() to Verde" do
|
22
|
+
expect(MaxColor.new.evaluate).to eq(verde)
|
23
23
|
end
|
24
24
|
|
25
25
|
end
|
26
26
|
|
27
27
|
describe "directions" do
|
28
28
|
|
29
|
-
it "
|
30
|
-
expect(MinDir.new.evaluate).to eq(
|
29
|
+
it "evaluates minDir() to Norte" do
|
30
|
+
expect(MinDir.new.evaluate).to eq(norte)
|
31
31
|
end
|
32
32
|
|
33
|
-
it "
|
34
|
-
expect(MaxDir.new.evaluate).to eq(
|
33
|
+
it "evaluates maxDir() to Oeste" do
|
34
|
+
expect(MaxDir.new.evaluate).to eq(oeste)
|
35
35
|
end
|
36
36
|
|
37
37
|
end
|
38
38
|
|
39
|
-
end
|
39
|
+
end
|
@@ -1,16 +1,28 @@
|
|
1
1
|
describe VarName do
|
2
2
|
|
3
|
-
let(:context) {
|
4
|
-
let(:
|
3
|
+
let(:context) { clean_context }
|
4
|
+
let(:variable_name) { 'var'.to_var_name }
|
5
5
|
|
6
|
-
it "
|
7
|
-
context.set
|
8
|
-
expect(
|
6
|
+
it "returns the associated value if it was defined in the context" do
|
7
|
+
context.set variable_name, 42.to_gbs_num
|
8
|
+
expect(variable_name.evaluate(context)).to eq(42.to_gbs_num)
|
9
9
|
end
|
10
10
|
|
11
|
-
it "
|
12
|
-
expect {
|
11
|
+
it "raises an error if there is no definition in context" do
|
12
|
+
expect { variable_name.evaluate context }
|
13
13
|
.to raise_error(UndefinedVariableError)
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
describe "#named?" do
|
17
|
+
|
18
|
+
it "is named 'var'" do
|
19
|
+
expect(variable_name.named? 'var').to be true
|
20
|
+
end
|
21
|
+
|
22
|
+
it "is not named 'blah'" do
|
23
|
+
expect(variable_name.named? 'blah').to be false
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -1,13 +1,11 @@
|
|
1
1
|
describe Boolean do
|
2
2
|
|
3
|
-
it "
|
4
|
-
|
5
|
-
expect(bool.evaluate).to eq(bool)
|
3
|
+
it "evaluates true to itself" do
|
4
|
+
expect(true_value.evaluate).to eq(true_value)
|
6
5
|
end
|
7
6
|
|
8
|
-
it "
|
9
|
-
|
10
|
-
expect(bool.evaluate).to eq(bool)
|
7
|
+
it "evaluates false to itself" do
|
8
|
+
expect(false_value.evaluate).to eq(false_value)
|
11
9
|
end
|
12
10
|
|
13
|
-
end
|
11
|
+
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
describe "colors" do
|
2
2
|
|
3
|
-
let(:all) { [
|
3
|
+
let(:all) { [azul, negro, rojo, verde] }
|
4
4
|
|
5
|
-
it "
|
5
|
+
it "evaluates any color to itself" do
|
6
6
|
all.each { |color| expect(color.evaluate).to eq(color) }
|
7
7
|
end
|
8
8
|
|
9
|
-
it "
|
9
|
+
it "returns the string representation" do
|
10
10
|
expect(all.map(&:to_s)).to eq(['Azul', 'Negro', 'Rojo', 'Verde'])
|
11
11
|
end
|
12
12
|
|
13
|
-
end
|
13
|
+
end
|
@@ -1,45 +1,45 @@
|
|
1
1
|
describe "directions" do
|
2
2
|
|
3
|
-
let(:all) { [
|
3
|
+
let(:all) { [norte, sur, este, oeste] }
|
4
4
|
|
5
|
-
it "
|
5
|
+
it "evaluates any direction to itself" do
|
6
6
|
all.each { |dir| expect(dir.evaluate).to eq(dir) }
|
7
7
|
end
|
8
8
|
|
9
|
-
it "
|
9
|
+
it "returns the string representation" do
|
10
10
|
expect(all.map(&:to_s)).to eq(['Norte', 'Sur', 'Este', 'Oeste'])
|
11
11
|
end
|
12
12
|
|
13
13
|
describe Norte do
|
14
14
|
|
15
|
-
it "
|
16
|
-
expect(
|
15
|
+
it "returns Sur as the opposite direction" do
|
16
|
+
expect(norte.opposite).to eq(sur)
|
17
17
|
end
|
18
18
|
|
19
19
|
end
|
20
20
|
|
21
21
|
describe Este do
|
22
22
|
|
23
|
-
it "
|
24
|
-
expect(
|
23
|
+
it "returns Oeste as the opposite direction" do
|
24
|
+
expect(este.opposite).to eq(oeste)
|
25
25
|
end
|
26
26
|
|
27
27
|
end
|
28
28
|
|
29
29
|
describe Sur do
|
30
30
|
|
31
|
-
it "
|
32
|
-
expect(
|
31
|
+
it "returns Norte as the opposite direction" do
|
32
|
+
expect(sur.opposite).to eq(norte)
|
33
33
|
end
|
34
34
|
|
35
35
|
end
|
36
36
|
|
37
37
|
describe Oeste do
|
38
38
|
|
39
|
-
it "
|
40
|
-
expect(
|
39
|
+
it "returns Este as the opposite direction" do
|
40
|
+
expect(oeste.opposite).to eq(este)
|
41
41
|
end
|
42
42
|
|
43
43
|
end
|
44
44
|
|
45
|
-
end
|
45
|
+
end
|
@@ -5,8 +5,6 @@ PARSER = Gobstones::Parser::TreetopParser.new
|
|
5
5
|
|
6
6
|
RSpec::Matchers.define :be_parsed_as do |grammar_elem|
|
7
7
|
|
8
|
-
@valid_nodes = [:program, :definition, :main, :expression, :command, :var_tuple ]
|
9
|
-
|
10
8
|
chain :and_fail do
|
11
9
|
@expect_parser_results = false
|
12
10
|
end
|
@@ -17,8 +15,9 @@ RSpec::Matchers.define :be_parsed_as do |grammar_elem|
|
|
17
15
|
end
|
18
16
|
|
19
17
|
match do |actual|
|
18
|
+
valid_nodes = [:program, :definition, :main, :expression, :command, :var_tuple]
|
20
19
|
fail 'wrong expectation' if @expect_parser_results.nil?
|
21
|
-
fail 'grammar elem not supported'
|
20
|
+
fail 'grammar elem not supported' unless valid_nodes.include?(grammar_elem)
|
22
21
|
|
23
22
|
begin
|
24
23
|
parse send("#{grammar_elem}_code_to_program", actual)
|
@@ -41,7 +40,7 @@ RSpec::Matchers.define :be_parsed_as do |grammar_elem|
|
|
41
40
|
end
|
42
41
|
|
43
42
|
def main_node_to_program(node)
|
44
|
-
Program.new
|
43
|
+
Program.new no_definitions, node
|
45
44
|
end
|
46
45
|
|
47
46
|
def definition_code_to_program(code)
|
@@ -49,7 +48,7 @@ RSpec::Matchers.define :be_parsed_as do |grammar_elem|
|
|
49
48
|
end
|
50
49
|
|
51
50
|
def definition_node_to_program(node)
|
52
|
-
Program.new [node], Main.new(
|
51
|
+
Program.new [node], Main.new(empty_body, no_return_statement)
|
53
52
|
end
|
54
53
|
|
55
54
|
def expression_code_to_program(code)
|
@@ -57,8 +56,8 @@ RSpec::Matchers.define :be_parsed_as do |grammar_elem|
|
|
57
56
|
end
|
58
57
|
|
59
58
|
def expression_node_to_program(node)
|
60
|
-
assign = SingleAssignment.new
|
61
|
-
main_node_to_program Main.new(
|
59
|
+
assign = SingleAssignment.new 'x'.to_var_name, node
|
60
|
+
main_node_to_program Main.new(CommandBlock.new([assign]), no_return_statement)
|
62
61
|
end
|
63
62
|
|
64
63
|
def command_code_to_program(code)
|
@@ -66,7 +65,7 @@ RSpec::Matchers.define :be_parsed_as do |grammar_elem|
|
|
66
65
|
end
|
67
66
|
|
68
67
|
def command_node_to_program(node)
|
69
|
-
main_node_to_program Main.new(
|
68
|
+
main_node_to_program Main.new(CommandBlock.new([node]), no_return_statement)
|
70
69
|
end
|
71
70
|
|
72
71
|
def var_tuple_code_to_program(code)
|
@@ -74,11 +73,11 @@ RSpec::Matchers.define :be_parsed_as do |grammar_elem|
|
|
74
73
|
end
|
75
74
|
|
76
75
|
def var_tuple_node_to_program(node)
|
77
|
-
Program.new
|
76
|
+
Program.new no_definitions, Main.new(empty_body, ReturnFromMain.new(node))
|
78
77
|
end
|
79
78
|
|
80
79
|
def parse(code)
|
81
80
|
@value = PARSER.parse code
|
82
81
|
end
|
83
82
|
|
84
|
-
end
|
83
|
+
end
|