gobstones 0.0.1.1
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 +7 -0
- data/.gitignore +6 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.simplecov +3 -0
- data/.travis.yml +4 -0
- data/CHANGELOG +4 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +47 -0
- data/LICENSE +675 -0
- data/README.md +17 -0
- data/Rakefile +4 -0
- data/bin/gobstones +12 -0
- data/gobstones.gemspec +24 -0
- data/lib/gobstones/cli/board_template +39 -0
- data/lib/gobstones/cli/printer.rb +106 -0
- data/lib/gobstones/cli/runner.rb +52 -0
- data/lib/gobstones/extensions/all.rb +2 -0
- data/lib/gobstones/extensions/boolean.rb +17 -0
- data/lib/gobstones/extensions/fixnum.rb +9 -0
- data/lib/gobstones/lang/all.rb +5 -0
- data/lib/gobstones/lang/commands/all.rb +13 -0
- data/lib/gobstones/lang/commands/assignments.rb +29 -0
- data/lib/gobstones/lang/commands/boom_cmd.rb +28 -0
- data/lib/gobstones/lang/commands/command_block.rb +37 -0
- data/lib/gobstones/lang/commands/conditional_cmd.rb +27 -0
- data/lib/gobstones/lang/commands/if_cmd.rb +38 -0
- data/lib/gobstones/lang/commands/ir_al_origen_cmd.rb +19 -0
- data/lib/gobstones/lang/commands/mover_cmd.rb +25 -0
- data/lib/gobstones/lang/commands/poner_cmd.rb +31 -0
- data/lib/gobstones/lang/commands/procedure_call.rb +23 -0
- data/lib/gobstones/lang/commands/repeat_with_cmd.rb +73 -0
- data/lib/gobstones/lang/commands/sacar_cmd.rb +31 -0
- data/lib/gobstones/lang/commands/skip_cmd.rb +19 -0
- data/lib/gobstones/lang/commands/vaciar_tablero_cmd.rb +19 -0
- data/lib/gobstones/lang/commands/while_cmd.rb +25 -0
- data/lib/gobstones/lang/definitions/all.rb +4 -0
- data/lib/gobstones/lang/definitions/definition.rb +32 -0
- data/lib/gobstones/lang/definitions/definition_call.rb +24 -0
- data/lib/gobstones/lang/definitions/function.rb +14 -0
- data/lib/gobstones/lang/definitions/main.rb +26 -0
- data/lib/gobstones/lang/definitions/no_return_statement.rb +15 -0
- data/lib/gobstones/lang/definitions/procedure.rb +44 -0
- data/lib/gobstones/lang/definitions/return_from_function.rb +22 -0
- data/lib/gobstones/lang/definitions/return_from_main.rb +22 -0
- data/lib/gobstones/lang/definitions/var_tuple.rb +26 -0
- data/lib/gobstones/lang/expressions/all.rb +8 -0
- data/lib/gobstones/lang/expressions/arithmetic_expressions.rb +52 -0
- data/lib/gobstones/lang/expressions/boolean_expressions.rb +29 -0
- data/lib/gobstones/lang/expressions/comparison_expressions.rb +45 -0
- data/lib/gobstones/lang/expressions/function_call.rb +15 -0
- data/lib/gobstones/lang/expressions/one_arg_expression.rb +21 -0
- data/lib/gobstones/lang/expressions/parentheses_expression.rb +13 -0
- data/lib/gobstones/lang/expressions/primitive_functions.rb +68 -0
- data/lib/gobstones/lang/expressions/two_arg_expression.rb +34 -0
- data/lib/gobstones/lang/expressions/type_bound_functions.rb +66 -0
- data/lib/gobstones/lang/expressions/var_name.rb +31 -0
- data/lib/gobstones/lang/literals/all.rb +4 -0
- data/lib/gobstones/lang/literals/booleans.rb +109 -0
- data/lib/gobstones/lang/literals/colors.rb +94 -0
- data/lib/gobstones/lang/literals/directions.rb +137 -0
- data/lib/gobstones/lang/literals/literal.rb +63 -0
- data/lib/gobstones/lang/literals/number.rb +49 -0
- data/lib/gobstones/lang/program.rb +32 -0
- data/lib/gobstones/modules/equal_by_class.rb +13 -0
- data/lib/gobstones/parser/ast/ast.rb +210 -0
- data/lib/gobstones/parser/grammar/gobstones.treetop +316 -0
- data/lib/gobstones/parser/parse_error.rb +17 -0
- data/lib/gobstones/parser/treetop_parser.rb +73 -0
- data/lib/gobstones/runner/all.rb +6 -0
- data/lib/gobstones/runner/board.rb +57 -0
- data/lib/gobstones/runner/cell.rb +60 -0
- data/lib/gobstones/runner/errors/all.rb +8 -0
- data/lib/gobstones/runner/errors/boom_error.rb +11 -0
- data/lib/gobstones/runner/errors/definition_not_found_error.rb +19 -0
- data/lib/gobstones/runner/errors/empty_cell_error.rb +11 -0
- data/lib/gobstones/runner/errors/gobstones_runtime_error.rb +11 -0
- data/lib/gobstones/runner/errors/gobstones_type_error.rb +11 -0
- data/lib/gobstones/runner/errors/out_of_board_error.rb +11 -0
- data/lib/gobstones/runner/errors/undefined_variable_error.rb +11 -0
- data/lib/gobstones/runner/errors/wrong_arguments_error.rb +11 -0
- data/lib/gobstones/runner/execution_context.rb +89 -0
- data/lib/gobstones/runner/head.rb +101 -0
- data/lib/gobstones/type_check_result.rb +16 -0
- data/spec/lang/commands/assignments_spec.rb +13 -0
- data/spec/lang/commands/boom_cmd_spec.rb +8 -0
- data/spec/lang/commands/cmd_block_spec.rb +21 -0
- data/spec/lang/commands/if_cmd_spec.rb +49 -0
- data/spec/lang/commands/ir_al_origen_cmd_spec.rb +16 -0
- data/spec/lang/commands/mover_cmd_spec.rb +38 -0
- data/spec/lang/commands/poner_cmd_spec.rb +29 -0
- data/spec/lang/commands/procedure_call_spec.rb +44 -0
- data/spec/lang/commands/procedure_spec.rb +67 -0
- data/spec/lang/commands/repeat_with_cmd_spec.rb +41 -0
- data/spec/lang/commands/sacar_cmd_spec.rb +34 -0
- data/spec/lang/commands/skip_cmd_spec.rb +12 -0
- data/spec/lang/commands/vaciar_tablero_cmd_spec.rb +13 -0
- data/spec/lang/commands/while_cmd_spec.rb +37 -0
- data/spec/lang/expressions/arithmetic_expressions_spec.rb +108 -0
- data/spec/lang/expressions/boolean_expressions_spec.rb +56 -0
- data/spec/lang/expressions/comparison_expressions_spec.rb +285 -0
- data/spec/lang/expressions/primitive_functions_spec.rb +126 -0
- data/spec/lang/expressions/type_bound_functions_spec.rb +39 -0
- data/spec/lang/expressions/var_name_spec.rb +16 -0
- data/spec/lang/literals/booleans_spec.rb +13 -0
- data/spec/lang/literals/colors_spec.rb +13 -0
- data/spec/lang/literals/directions_spec.rb +45 -0
- data/spec/lang/literals/numbers_spec.rb +8 -0
- data/spec/matchers/parse_matcher.rb +84 -0
- data/spec/parser/arithmetic_expressions_spec.rb +129 -0
- data/spec/parser/assignments_spec.rb +39 -0
- data/spec/parser/boolean_expressions_spec.rb +111 -0
- data/spec/parser/command_block_spec.rb +50 -0
- data/spec/parser/data_types_spec.rb +67 -0
- data/spec/parser/function_calls_spec.rb +37 -0
- data/spec/parser/function_definitions_spec.rb +50 -0
- data/spec/parser/gobstones_program_spec.rb +55 -0
- data/spec/parser/if_command_spec.rb +46 -0
- data/spec/parser/main_definition_spec.rb +42 -0
- data/spec/parser/nested_expressions_spec.rb +39 -0
- data/spec/parser/primitive_expressions_spec.rb +105 -0
- data/spec/parser/procedure_calls_spec.rb +36 -0
- data/spec/parser/procedure_definitions_spec.rb +39 -0
- data/spec/parser/repeat_with_command_spec.rb +23 -0
- data/spec/parser/simple_commands_spec.rb +62 -0
- data/spec/parser/treetop_parser_spec.rb +109 -0
- data/spec/parser/var_tuple_spec.rb +30 -0
- data/spec/parser/while_command_spec.rb +30 -0
- data/spec/runner/board_spec.rb +85 -0
- data/spec/runner/cell_spec.rb +72 -0
- data/spec/runner/execution_context_spec.rb +64 -0
- data/spec/runner/head_spec.rb +139 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/type_checker_spec.rb +37 -0
- metadata +242 -0
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'gobstones/lang/literals/literal'
|
2
|
+
|
3
|
+
module Gobstones
|
4
|
+
|
5
|
+
module Lang
|
6
|
+
|
7
|
+
class Boolean < Literal
|
8
|
+
|
9
|
+
def return_type
|
10
|
+
:Boolean
|
11
|
+
end
|
12
|
+
|
13
|
+
def opposite
|
14
|
+
self.not
|
15
|
+
end
|
16
|
+
|
17
|
+
def previous
|
18
|
+
self.not
|
19
|
+
end
|
20
|
+
|
21
|
+
def next
|
22
|
+
self.not
|
23
|
+
end
|
24
|
+
|
25
|
+
def not
|
26
|
+
raise 'subclass responsibility'
|
27
|
+
end
|
28
|
+
|
29
|
+
def if_true(block, context)
|
30
|
+
raise 'subclass responsibility'
|
31
|
+
end
|
32
|
+
|
33
|
+
def if_false(block, context)
|
34
|
+
raise 'subclass responsibility'
|
35
|
+
end
|
36
|
+
|
37
|
+
def is_true?
|
38
|
+
raise 'subclass responsibility'
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
class True < Boolean
|
44
|
+
|
45
|
+
def <(other)
|
46
|
+
false
|
47
|
+
end
|
48
|
+
|
49
|
+
def and(other)
|
50
|
+
other
|
51
|
+
end
|
52
|
+
|
53
|
+
def or(other)
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
def not
|
58
|
+
False.new
|
59
|
+
end
|
60
|
+
|
61
|
+
def if_true(block, context)
|
62
|
+
block.evaluate context
|
63
|
+
end
|
64
|
+
|
65
|
+
def if_false(block, context)
|
66
|
+
# nothing to do, I'm true :)
|
67
|
+
end
|
68
|
+
|
69
|
+
def is_true?
|
70
|
+
true
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
class False < Boolean
|
76
|
+
|
77
|
+
def <(other)
|
78
|
+
self != other
|
79
|
+
end
|
80
|
+
|
81
|
+
def and(other)
|
82
|
+
self
|
83
|
+
end
|
84
|
+
|
85
|
+
def or(other)
|
86
|
+
other
|
87
|
+
end
|
88
|
+
|
89
|
+
def not
|
90
|
+
True.new
|
91
|
+
end
|
92
|
+
|
93
|
+
def if_true(block, context)
|
94
|
+
# nothing to do, I'm false :)
|
95
|
+
end
|
96
|
+
|
97
|
+
def if_false(block, context)
|
98
|
+
block.evaluate context
|
99
|
+
end
|
100
|
+
|
101
|
+
def is_true?
|
102
|
+
false
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'gobstones/lang/literals/literal'
|
2
|
+
require 'gobstones/runner/errors/gobstones_type_error'
|
3
|
+
|
4
|
+
module Gobstones
|
5
|
+
|
6
|
+
module Lang
|
7
|
+
|
8
|
+
class Color < Literal
|
9
|
+
|
10
|
+
def self.order
|
11
|
+
[Azul, Negro, Rojo, Verde]
|
12
|
+
end
|
13
|
+
|
14
|
+
def <(other)
|
15
|
+
self.class.order.index(self.class) < self.class.order.index(other.class)
|
16
|
+
end
|
17
|
+
|
18
|
+
def opposite
|
19
|
+
raise Gobstones::Runner::GobstonesTypeError, "colors don't have opposite"
|
20
|
+
end
|
21
|
+
|
22
|
+
def return_type
|
23
|
+
:Color
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
class Azul < Color
|
29
|
+
|
30
|
+
def previous
|
31
|
+
Verde.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def next
|
35
|
+
Negro.new
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_s
|
39
|
+
'Azul'
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
class Negro < Color
|
45
|
+
|
46
|
+
def previous
|
47
|
+
Azul.new
|
48
|
+
end
|
49
|
+
|
50
|
+
def next
|
51
|
+
Rojo.new
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_s
|
55
|
+
'Negro'
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
class Rojo < Color
|
61
|
+
|
62
|
+
def previous
|
63
|
+
Negro.new
|
64
|
+
end
|
65
|
+
|
66
|
+
def next
|
67
|
+
Verde.new
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_s
|
71
|
+
'Rojo'
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
class Verde < Color
|
77
|
+
|
78
|
+
def previous
|
79
|
+
Rojo.new
|
80
|
+
end
|
81
|
+
|
82
|
+
def next
|
83
|
+
Azul.new
|
84
|
+
end
|
85
|
+
|
86
|
+
def to_s
|
87
|
+
'Verde'
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require 'gobstones/lang/literals/literal'
|
2
|
+
|
3
|
+
module Gobstones
|
4
|
+
|
5
|
+
module Lang
|
6
|
+
|
7
|
+
class Direction < Literal
|
8
|
+
|
9
|
+
def self.order
|
10
|
+
[Norte, Este, Sur, Oeste]
|
11
|
+
end
|
12
|
+
|
13
|
+
def <(other)
|
14
|
+
self.class.order.index(self.class) < self.class.order.index(other.class)
|
15
|
+
end
|
16
|
+
|
17
|
+
def return_type
|
18
|
+
:Direction
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
class Norte < Direction
|
24
|
+
|
25
|
+
def can_move?(head)
|
26
|
+
head.can_move_north?
|
27
|
+
end
|
28
|
+
|
29
|
+
def move(head)
|
30
|
+
head.move_north
|
31
|
+
end
|
32
|
+
|
33
|
+
def opposite
|
34
|
+
Sur.new
|
35
|
+
end
|
36
|
+
|
37
|
+
def previous
|
38
|
+
Oeste.new
|
39
|
+
end
|
40
|
+
|
41
|
+
def next
|
42
|
+
Este.new
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s
|
46
|
+
'Norte'
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
class Este < Direction
|
52
|
+
|
53
|
+
def can_move?(head)
|
54
|
+
head.can_move_east?
|
55
|
+
end
|
56
|
+
|
57
|
+
def move(head)
|
58
|
+
head.move_east
|
59
|
+
end
|
60
|
+
|
61
|
+
def opposite
|
62
|
+
Oeste.new
|
63
|
+
end
|
64
|
+
|
65
|
+
def previous
|
66
|
+
Norte.new
|
67
|
+
end
|
68
|
+
|
69
|
+
def next
|
70
|
+
Sur.new
|
71
|
+
end
|
72
|
+
|
73
|
+
def to_s
|
74
|
+
'Este'
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
class Sur < Direction
|
80
|
+
|
81
|
+
def can_move?(head)
|
82
|
+
head.can_move_south?
|
83
|
+
end
|
84
|
+
|
85
|
+
def move(head)
|
86
|
+
head.move_south
|
87
|
+
end
|
88
|
+
|
89
|
+
def opposite
|
90
|
+
Norte.new
|
91
|
+
end
|
92
|
+
|
93
|
+
def previous
|
94
|
+
Este.new
|
95
|
+
end
|
96
|
+
|
97
|
+
def next
|
98
|
+
Oeste.new
|
99
|
+
end
|
100
|
+
|
101
|
+
def to_s
|
102
|
+
'Sur'
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
class Oeste < Direction
|
108
|
+
|
109
|
+
def can_move?(head)
|
110
|
+
head.can_move_west?
|
111
|
+
end
|
112
|
+
|
113
|
+
def move(head)
|
114
|
+
head.move_west
|
115
|
+
end
|
116
|
+
|
117
|
+
def opposite
|
118
|
+
Este.new
|
119
|
+
end
|
120
|
+
|
121
|
+
def previous
|
122
|
+
Sur.new
|
123
|
+
end
|
124
|
+
|
125
|
+
def next
|
126
|
+
Norte.new
|
127
|
+
end
|
128
|
+
|
129
|
+
def to_s
|
130
|
+
'Oeste'
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'gobstones/modules/equal_by_class'
|
2
|
+
require 'gobstones/runner/errors/gobstones_type_error'
|
3
|
+
|
4
|
+
module Gobstones
|
5
|
+
|
6
|
+
module Lang
|
7
|
+
|
8
|
+
class Literal
|
9
|
+
|
10
|
+
include Comparable
|
11
|
+
include Gobstones::EqualByClass
|
12
|
+
|
13
|
+
def evaluate(context=nil)
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def <=>(other)
|
18
|
+
self == other ? 0 : (self < other ? -1 : 1)
|
19
|
+
end
|
20
|
+
|
21
|
+
def <(other)
|
22
|
+
raise 'subclass responsibility'
|
23
|
+
end
|
24
|
+
|
25
|
+
def if_true(block, context)
|
26
|
+
raise Gobstones::Runner::GobstonesTypeError, "#{self} is not a boolean"
|
27
|
+
end
|
28
|
+
|
29
|
+
def if_false(block, context)
|
30
|
+
raise Gobstones::Runner::GobstonesTypeError, "#{self} is not a boolean"
|
31
|
+
end
|
32
|
+
|
33
|
+
def is_true?
|
34
|
+
raise Gobstones::Runner::GobstonesTypeError, "#{self} is not a boolean"
|
35
|
+
end
|
36
|
+
|
37
|
+
def same_type_as(other)
|
38
|
+
self.return_type == other.return_type
|
39
|
+
end
|
40
|
+
|
41
|
+
def return_type
|
42
|
+
raise 'subclass responsibility'
|
43
|
+
end
|
44
|
+
|
45
|
+
OPERATORS_MAPPING = {
|
46
|
+
:equal => :==,
|
47
|
+
:not_equal => '!='.to_sym,
|
48
|
+
:less_than => :<,
|
49
|
+
:less_equal => :<=,
|
50
|
+
:greater_than => :>, :greater_equal => :>=
|
51
|
+
}
|
52
|
+
|
53
|
+
OPERATORS_MAPPING.each do |gbs_op, ruby_op|
|
54
|
+
define_method gbs_op do |other|
|
55
|
+
send(ruby_op, other) ? True.new : False.new
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'gobstones/lang/literals/literal'
|
2
|
+
|
3
|
+
module Gobstones
|
4
|
+
|
5
|
+
module Lang
|
6
|
+
|
7
|
+
class Number < Literal
|
8
|
+
|
9
|
+
attr_reader :value
|
10
|
+
|
11
|
+
def initialize(num)
|
12
|
+
@value = num
|
13
|
+
end
|
14
|
+
|
15
|
+
def == other
|
16
|
+
super(other) && value == other.value
|
17
|
+
end
|
18
|
+
|
19
|
+
def <(other)
|
20
|
+
value < other.value
|
21
|
+
end
|
22
|
+
|
23
|
+
def return_type
|
24
|
+
:Number
|
25
|
+
end
|
26
|
+
|
27
|
+
[:+, :-, :*, :/, :%, :**].each do |selector|
|
28
|
+
define_method selector do |other|
|
29
|
+
self.class.new value.send(selector, other.value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def opposite
|
34
|
+
self.class.new(-value)
|
35
|
+
end
|
36
|
+
|
37
|
+
def previous
|
38
|
+
self.class.new(value - 1)
|
39
|
+
end
|
40
|
+
|
41
|
+
def next
|
42
|
+
self.class.new(value + 1)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|