karel-interpreter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +154 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/karel +4 -0
- data/karel-interpreter.gemspec +26 -0
- data/lib/karel/command/batch.rb +29 -0
- data/lib/karel/command/if.rb +42 -0
- data/lib/karel/command/move.rb +14 -0
- data/lib/karel/command/not.rb +20 -0
- data/lib/karel/command/on_token.rb +14 -0
- data/lib/karel/command/pick.rb +13 -0
- data/lib/karel/command/program.rb +17 -0
- data/lib/karel/command/put.rb +13 -0
- data/lib/karel/command/response.rb +25 -0
- data/lib/karel/command/turn_left.rb +13 -0
- data/lib/karel/command/user_defined.rb +20 -0
- data/lib/karel/command/while.rb +37 -0
- data/lib/karel/model/compass.rb +32 -0
- data/lib/karel/model/location.rb +15 -0
- data/lib/karel/model/token_container.rb +37 -0
- data/lib/karel/utils/parser.rb +100 -0
- data/lib/karel/version.rb +5 -0
- data/lib/karel.rb +50 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e536365aaca04663111b4b551fabe28846f3a850
|
4
|
+
data.tar.gz: 8884b11c61f588d982c07d45e6297b3b3ce7a7f4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a27a642da2819853c99948a14e35dd00c8f719a3ea78eeb48bca40ba60e9839a7a2fcbe7aa5801d35fde63113b4b1b466481623ee322463f8544dee1486b3b55
|
7
|
+
data.tar.gz: b9709c9ab0c17d66cf350d5e1fa277231178a53d44621321b002bcb0e4fe635852606f12e1bb7b8394ae2aa56eb8c5cad8eb6f7e57b6cce6741116a5b7d5d5b1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at collier@apartmentlist.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Tom Collier
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
# Karel Interpreter
|
2
|
+
|
3
|
+
A interpreter for programs written in the Apartment List dialect of Karel.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'karel-interpreter'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
The interpreter takes a single file as an argument and prints out the final state
|
16
|
+
of the Karel grid after executing the program.
|
17
|
+
|
18
|
+
### Example
|
19
|
+
|
20
|
+
```
|
21
|
+
# square_dance.krl
|
22
|
+
move
|
23
|
+
put
|
24
|
+
turn_left
|
25
|
+
move
|
26
|
+
turn_left
|
27
|
+
move
|
28
|
+
put
|
29
|
+
turn_left
|
30
|
+
move
|
31
|
+
```
|
32
|
+
|
33
|
+
```
|
34
|
+
% karel square_dance.krl
|
35
|
+
location: (0, 0)
|
36
|
+
direction: right
|
37
|
+
tokens: [{"location"=>"(-1, 0)", "count"=>1}, {"location"=>"(0, 1)", "count"=>1}]
|
38
|
+
operations: 9
|
39
|
+
```
|
40
|
+
|
41
|
+
### Output
|
42
|
+
|
43
|
+
* Location - location of Karel when the program finishes
|
44
|
+
* Direction - direction Karel is facing when the program finishes
|
45
|
+
* Tokens - location and counts of all tokens left on the grid
|
46
|
+
* Operation - number of built-in commands executed
|
47
|
+
|
48
|
+
## Karel Language Specification: Apartment List Dialect
|
49
|
+
|
50
|
+
Karel is a creature that lives on an infinite two dimensional grid. Using built-in
|
51
|
+
commands as the basic building blocks, rudimentary control flow, and user-defined
|
52
|
+
commands, programs can move Karel around the grid and place and remove tokens from
|
53
|
+
squares on the grid.
|
54
|
+
|
55
|
+
The Karel language is case-sensitive and indentation-sensitive. All built-in commands
|
56
|
+
and reserved words for control flow and user-defined commands are lower case. Statements
|
57
|
+
within the body of control flow branches and user-defined commands must be indented
|
58
|
+
two spaces beyond the containing branch/command definition. Only one statement is
|
59
|
+
allowed per line. Blank lines are permitted.
|
60
|
+
|
61
|
+
Comments begin with a `#` and continue to the end of the line. Comments can be placed
|
62
|
+
on their own line or after a statement.
|
63
|
+
|
64
|
+
### Built-in commands
|
65
|
+
|
66
|
+
The following commands are available out of the box.
|
67
|
+
|
68
|
+
* `move` - Move Karel one square forward in the direction she is facing
|
69
|
+
* `turn_left` - Turn Karel left (CCW) 90 degrees
|
70
|
+
* `token?` - Return `true` if there is one or more tokens on Karel's current location, otherwise false
|
71
|
+
* `pick` - Pick up one token from Karel's current location. If there are no tokens, this command will crash the program.
|
72
|
+
* `put` - Put one token down on Karel's current location.
|
73
|
+
|
74
|
+
### Control flow
|
75
|
+
|
76
|
+
Karel responds to `if`/`else` branching and `while` loops. The condition evaluated for
|
77
|
+
these two control flow structures can be negated with a `!` (no spaces are allowed between
|
78
|
+
the negation operator and the condition)
|
79
|
+
|
80
|
+
#### Example: a basic `if` statement
|
81
|
+
|
82
|
+
```
|
83
|
+
if token?
|
84
|
+
pick
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
#### Example: `if`/`else`
|
89
|
+
|
90
|
+
```
|
91
|
+
if token?
|
92
|
+
pick
|
93
|
+
else
|
94
|
+
move
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
#### Example: negated condition
|
99
|
+
|
100
|
+
```
|
101
|
+
if !token?
|
102
|
+
put
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
#### Example: nested `if`
|
107
|
+
|
108
|
+
```
|
109
|
+
if token?
|
110
|
+
pick
|
111
|
+
if token?
|
112
|
+
move
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
#### Example: nested `while`
|
118
|
+
|
119
|
+
```
|
120
|
+
# This will safely remove all tokens from a square.
|
121
|
+
while token?
|
122
|
+
pick
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
126
|
+
### User-defined commands
|
127
|
+
|
128
|
+
The Karel language supports user-defined commands, though these commands do not take
|
129
|
+
arguments and do not return a value. Commands are defined as follows
|
130
|
+
|
131
|
+
```
|
132
|
+
def turn_around
|
133
|
+
turn_left
|
134
|
+
turn_left
|
135
|
+
end
|
136
|
+
```
|
137
|
+
|
138
|
+
## Development
|
139
|
+
|
140
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
141
|
+
|
142
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
143
|
+
|
144
|
+
## Contributing
|
145
|
+
|
146
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/karel-interpreter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
147
|
+
|
148
|
+
## License
|
149
|
+
|
150
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
151
|
+
|
152
|
+
## Code of Conduct
|
153
|
+
|
154
|
+
Everyone interacting in the Karel::Interpreter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/karel-interpreter/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "karel/interpreter"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/karel
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "karel/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "karel-interpreter"
|
8
|
+
spec.version = Karel::Interpreter::VERSION
|
9
|
+
spec.authors = ["Tom Collier"]
|
10
|
+
spec.email = ["collier@apartmentlist.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A ruby-based Karel interpreter}
|
13
|
+
spec.homepage = "https://www.github.com/apartmentlist/karel-interpreter"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = ["karel"]
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'response'
|
2
|
+
|
3
|
+
module Command
|
4
|
+
class Batch
|
5
|
+
def initialize(statements)
|
6
|
+
@statements = statements
|
7
|
+
end
|
8
|
+
|
9
|
+
def execute(compass, location, tokens)
|
10
|
+
operations_count = 0
|
11
|
+
response = Response.new(
|
12
|
+
compass: compass, location: location, tokens: tokens
|
13
|
+
)
|
14
|
+
@statements.each do |statement|
|
15
|
+
response = statement.execute(
|
16
|
+
response.compass, response.location, response.tokens
|
17
|
+
)
|
18
|
+
operations_count += response.operations_count
|
19
|
+
end
|
20
|
+
|
21
|
+
Response.new(
|
22
|
+
compass: response.compass,
|
23
|
+
location: response.location,
|
24
|
+
operations_count: operations_count,
|
25
|
+
tokens: response.tokens
|
26
|
+
)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'batch'
|
2
|
+
require_relative 'response'
|
3
|
+
|
4
|
+
module Command
|
5
|
+
class If
|
6
|
+
def initialize(condition_statement)
|
7
|
+
@condition_statement = condition_statement
|
8
|
+
@body_statements = []
|
9
|
+
@else_statements = []
|
10
|
+
@in_else = false
|
11
|
+
end
|
12
|
+
|
13
|
+
def else!
|
14
|
+
raise ArgumentError, 'unexpected else statement' if @in_else
|
15
|
+
@in_else = true
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_statement(statement)
|
19
|
+
if @in_else
|
20
|
+
@else_statements << statement
|
21
|
+
else
|
22
|
+
@body_statements << statement
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def execute(compass, location, tokens)
|
27
|
+
response = @condition_statement.execute(compass, location, tokens)
|
28
|
+
operations_count = response.operations_count
|
29
|
+
statements = response.return_value ? @body_statements : @else_statements
|
30
|
+
batch_response = Batch.new(statements).execute(
|
31
|
+
response.compass, response.location, response.tokens
|
32
|
+
)
|
33
|
+
|
34
|
+
Response.new(
|
35
|
+
compass: batch_response.compass,
|
36
|
+
location: batch_response.location,
|
37
|
+
operations_count: response.operations_count + batch_response.operations_count,
|
38
|
+
tokens: batch_response.tokens
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative 'response'
|
2
|
+
|
3
|
+
module Command
|
4
|
+
class Not
|
5
|
+
def initialize(statement)
|
6
|
+
@statement = statement
|
7
|
+
end
|
8
|
+
|
9
|
+
def execute(compass, location, tokens)
|
10
|
+
response = @statement.execute(compass, location, tokens)
|
11
|
+
Response.new(
|
12
|
+
compass: response.compass,
|
13
|
+
location: response.location,
|
14
|
+
operations_count: response.operations_count,
|
15
|
+
return_value: !response.return_value,
|
16
|
+
tokens: response.tokens
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative 'batch'
|
2
|
+
|
3
|
+
module Command
|
4
|
+
class Program
|
5
|
+
def initialize
|
6
|
+
@statements = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def add_statement(statement)
|
10
|
+
@statements << statement
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute(compass, location, tokens)
|
14
|
+
Batch.new(@statements).execute(compass, location, tokens)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Command
|
2
|
+
class Response
|
3
|
+
attr_reader *%i[
|
4
|
+
compass
|
5
|
+
location
|
6
|
+
operations_count
|
7
|
+
return_value
|
8
|
+
tokens
|
9
|
+
]
|
10
|
+
|
11
|
+
def initialize(
|
12
|
+
compass:,
|
13
|
+
location:,
|
14
|
+
tokens:,
|
15
|
+
operations_count: 1,
|
16
|
+
return_value: nil
|
17
|
+
)
|
18
|
+
@compass = compass
|
19
|
+
@location = location
|
20
|
+
@return_value = return_value
|
21
|
+
@tokens = tokens
|
22
|
+
@operations_count = operations_count
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative 'batch'
|
2
|
+
|
3
|
+
module Command
|
4
|
+
class UserDefined
|
5
|
+
attr_reader :name
|
6
|
+
|
7
|
+
def initialize(name)
|
8
|
+
@name = name
|
9
|
+
@statements = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_statement(statement)
|
13
|
+
@statements << statement
|
14
|
+
end
|
15
|
+
|
16
|
+
def execute(compass, location, tokens)
|
17
|
+
Batch.new(@statements).execute(compass, location, tokens)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative 'response'
|
2
|
+
|
3
|
+
module Command
|
4
|
+
class While
|
5
|
+
def initialize(condition_statement)
|
6
|
+
@condition_statement = condition_statement
|
7
|
+
@statements = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def add_statement(statement)
|
11
|
+
@statements << statement
|
12
|
+
end
|
13
|
+
|
14
|
+
def execute(compass, location, tokens)
|
15
|
+
response = @condition_statement.execute(compass, location, tokens)
|
16
|
+
operations_count = response.operations_count
|
17
|
+
while response.return_value
|
18
|
+
response = Batch.new(@statements).execute(
|
19
|
+
response.compass,
|
20
|
+
response.location,
|
21
|
+
response.tokens
|
22
|
+
)
|
23
|
+
operations_count += response.operations_count
|
24
|
+
response = @condition_statement.execute(
|
25
|
+
response.compass, response.location, response.tokens
|
26
|
+
)
|
27
|
+
operations_count += response.operations_count
|
28
|
+
end
|
29
|
+
Response.new(
|
30
|
+
compass: response.compass,
|
31
|
+
location: response.location,
|
32
|
+
tokens: response.tokens,
|
33
|
+
operations_count: operations_count
|
34
|
+
)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative 'location'
|
2
|
+
|
3
|
+
module Model
|
4
|
+
class Compass
|
5
|
+
DIRECTIONS = {
|
6
|
+
up: [0, 1],
|
7
|
+
left: [-1, 0],
|
8
|
+
down: [0, -1],
|
9
|
+
right: [1, 0]
|
10
|
+
}
|
11
|
+
|
12
|
+
attr_reader :direction
|
13
|
+
|
14
|
+
def initialize(direction)
|
15
|
+
@direction = direction
|
16
|
+
end
|
17
|
+
|
18
|
+
def translate_location(location)
|
19
|
+
Location.new(
|
20
|
+
location.x + DIRECTIONS[direction][0],
|
21
|
+
location.y + DIRECTIONS[direction][1]
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def turn
|
26
|
+
dir_index = DIRECTIONS.keys.index(direction)
|
27
|
+
dir_index += 1
|
28
|
+
dir_index %= DIRECTIONS.length
|
29
|
+
self.class.new(DIRECTIONS.keys[dir_index])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Model
|
2
|
+
class TokenContainer
|
3
|
+
def initialize
|
4
|
+
@tokens = {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def any?(location)
|
8
|
+
@tokens.key?(location)
|
9
|
+
end
|
10
|
+
|
11
|
+
def pick(location)
|
12
|
+
if !@tokens.key?(location)
|
13
|
+
fail "No tokens at #{location}"
|
14
|
+
elsif @tokens[location] == 1
|
15
|
+
@tokens.delete(location)
|
16
|
+
else
|
17
|
+
@tokens[location] -= 1
|
18
|
+
end
|
19
|
+
return self
|
20
|
+
end
|
21
|
+
|
22
|
+
def put(location)
|
23
|
+
@tokens[location] ||= 0
|
24
|
+
@tokens[location] += 1
|
25
|
+
return self
|
26
|
+
end
|
27
|
+
|
28
|
+
def as_json
|
29
|
+
@tokens.keys.sort.map do |location|
|
30
|
+
{
|
31
|
+
'location' => location.to_s,
|
32
|
+
'count' => @tokens[location]
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require_relative '../command/if'
|
2
|
+
require_relative '../command/move'
|
3
|
+
require_relative '../command/not'
|
4
|
+
require_relative '../command/on_token'
|
5
|
+
require_relative '../command/pick'
|
6
|
+
require_relative '../command/program'
|
7
|
+
require_relative '../command/put'
|
8
|
+
require_relative '../command/turn_left'
|
9
|
+
require_relative '../command/user_defined'
|
10
|
+
require_relative '../command/while'
|
11
|
+
|
12
|
+
|
13
|
+
module Command
|
14
|
+
NATIVE = {
|
15
|
+
'move' => Move.new,
|
16
|
+
'token?' => OnToken.new,
|
17
|
+
'pick' => Pick.new,
|
18
|
+
'put' => Put.new,
|
19
|
+
'turn_left' => TurnLeft.new
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
INDENTATION = ' '.freeze
|
24
|
+
|
25
|
+
module Utils
|
26
|
+
class Parser
|
27
|
+
def self.parse(file)
|
28
|
+
commands = Command::NATIVE.dup
|
29
|
+
stack = [Command::Program.new]
|
30
|
+
line_no = 0
|
31
|
+
File.readlines(file).each do |raw_line|
|
32
|
+
line = raw_line.rstrip.gsub(/ *#.*/, '')
|
33
|
+
line_no += 1
|
34
|
+
curr_indent = INDENTATION * (stack.length - 1)
|
35
|
+
udf_match = line.match(/^def ([\w]+)$/)
|
36
|
+
if_match = line.match(/^#{curr_indent}if (\!)?([\w?]+)$/)
|
37
|
+
while_match = line.match(/^#{curr_indent}while (\!)?([\w?]+)$/)
|
38
|
+
command_match = line.match(/^#{curr_indent}([\w?]+)$/)
|
39
|
+
|
40
|
+
if udf_match
|
41
|
+
if commands.key?(udf_match[1])
|
42
|
+
raise SyntaxError, "Line #{line_no}: command '#{udf_match[1]}' is already defined"
|
43
|
+
end
|
44
|
+
stack.push(Command::UserDefined.new(udf_match[1]))
|
45
|
+
elsif if_match
|
46
|
+
unless commands.key?(if_match[2])
|
47
|
+
raise SyntaxError, "Line #{line_no}: command '#{if_match[2]}' not recognized"
|
48
|
+
end
|
49
|
+
command = commands[if_match[2]]
|
50
|
+
condition = if_match[1] ? Command::Not.new(command) : command
|
51
|
+
stack.push(Command::If.new(condition))
|
52
|
+
elsif while_match
|
53
|
+
unless commands.key?(while_match[2])
|
54
|
+
raise SyntaxError, "Line #{line_no}: command '#{while_match[1]}' not recognized"
|
55
|
+
end
|
56
|
+
command = commands[while_match[2]]
|
57
|
+
condition = while_match[1] ? Command::Not.new(command) : command
|
58
|
+
stack.push(Command::While.new(condition))
|
59
|
+
elsif command_match
|
60
|
+
if commands.key?(command_match[1])
|
61
|
+
stack.last.add_statement(commands[command_match[1]])
|
62
|
+
else
|
63
|
+
raise SyntaxError, "Line #{line_no}: command '#{command_match[1]}' not recognized"
|
64
|
+
end
|
65
|
+
elsif stack.length > 1
|
66
|
+
prev_indent = INDENTATION * (stack.length - 2)
|
67
|
+
if line.match(/^#{prev_indent}else$/)
|
68
|
+
if stack.last.is_a?(Command::If)
|
69
|
+
begin
|
70
|
+
stack.last.else!
|
71
|
+
rescue ArgumentError => error
|
72
|
+
raise SyntaxError, "Line #{line_no}: #{error.message}"
|
73
|
+
end
|
74
|
+
else
|
75
|
+
raise SyntaxError, "Line #{line_no}: unexpected 'else' statement"
|
76
|
+
end
|
77
|
+
elsif line.match(/^#{prev_indent}end$/)
|
78
|
+
statement = stack.pop
|
79
|
+
if statement.is_a?(Command::UserDefined)
|
80
|
+
commands[statement.name] = statement
|
81
|
+
else
|
82
|
+
stack.last.add_statement(statement)
|
83
|
+
end
|
84
|
+
elsif line =~ /#{curr_indent} /
|
85
|
+
raise SyntaxError, "Line #{line_no}: invalid indentation"
|
86
|
+
else
|
87
|
+
raise SyntaxError, "Line #{line_no}: command '#{line}' not recognized"
|
88
|
+
end
|
89
|
+
elsif line =~ /#{curr_indent} /
|
90
|
+
raise SyntaxError, "Line #{line_no}: inconsistent indentation"
|
91
|
+
elsif line.strip != ''
|
92
|
+
raise SyntaxError, "Line #{line_no}: command '#{line}' not recognized"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
raise SyntaxError, 'Unexpected end of input' if stack.length > 1
|
97
|
+
stack.pop
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/lib/karel.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative 'karel/model/compass'
|
4
|
+
require_relative 'karel/model/location'
|
5
|
+
require_relative 'karel/model/token_container'
|
6
|
+
require_relative 'karel/utils/parser'
|
7
|
+
|
8
|
+
module Karel
|
9
|
+
def self.usage
|
10
|
+
puts <<~USAGE
|
11
|
+
Usage: #{$0} file
|
12
|
+
Interpret a Karel file and print the final state
|
13
|
+
|
14
|
+
Example:
|
15
|
+
% #{$0} move-up.krl
|
16
|
+
location: (10, 10)
|
17
|
+
direction: up
|
18
|
+
tokens: [{"location":"(10, 0)","count":1},{"location":"(5, 5)","count":2}]
|
19
|
+
operations: 42
|
20
|
+
USAGE
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.run(args)
|
24
|
+
if args.length != 1
|
25
|
+
usage
|
26
|
+
exit(1)
|
27
|
+
elsif !File.exist?(args.first)
|
28
|
+
puts "File '#{args.first}' not found"
|
29
|
+
else
|
30
|
+
program = Utils::Parser.parse(args.first)
|
31
|
+
response = program.execute(
|
32
|
+
Model::Compass.new(:up),
|
33
|
+
Model::Location.new(0, 0),
|
34
|
+
Model::TokenContainer.new
|
35
|
+
)
|
36
|
+
puts <<~STATE
|
37
|
+
location: #{response.location.to_s}
|
38
|
+
direction: #{response.compass.direction}
|
39
|
+
tokens: #{response.tokens.as_json}
|
40
|
+
operations: #{response.operations_count}
|
41
|
+
STATE
|
42
|
+
end
|
43
|
+
rescue SyntaxError => error
|
44
|
+
puts "Syntax error: #{error.message}"
|
45
|
+
exit(2)
|
46
|
+
rescue RuntimeError => error
|
47
|
+
puts "Run time error: #{error.message}"
|
48
|
+
exit(1)
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: karel-interpreter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Collier
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- collier@apartmentlist.com
|
58
|
+
executables:
|
59
|
+
- karel
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- exe/karel
|
74
|
+
- karel-interpreter.gemspec
|
75
|
+
- lib/karel.rb
|
76
|
+
- lib/karel/command/batch.rb
|
77
|
+
- lib/karel/command/if.rb
|
78
|
+
- lib/karel/command/move.rb
|
79
|
+
- lib/karel/command/not.rb
|
80
|
+
- lib/karel/command/on_token.rb
|
81
|
+
- lib/karel/command/pick.rb
|
82
|
+
- lib/karel/command/program.rb
|
83
|
+
- lib/karel/command/put.rb
|
84
|
+
- lib/karel/command/response.rb
|
85
|
+
- lib/karel/command/turn_left.rb
|
86
|
+
- lib/karel/command/user_defined.rb
|
87
|
+
- lib/karel/command/while.rb
|
88
|
+
- lib/karel/model/compass.rb
|
89
|
+
- lib/karel/model/location.rb
|
90
|
+
- lib/karel/model/token_container.rb
|
91
|
+
- lib/karel/utils/parser.rb
|
92
|
+
- lib/karel/version.rb
|
93
|
+
homepage: https://www.github.com/apartmentlist/karel-interpreter
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.6.11
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: A ruby-based Karel interpreter
|
117
|
+
test_files: []
|