A interpreter for programs written in the Apartment List dialect of Karel.
Add this line to your application's Gemfile:
gem 'karel-interpreter'
The interpreter takes a single file as an argument and prints out the final state of the Karel grid after executing the program.
# square_dance.krl
move
put
turn_left
move
turn_left
move
put
turn_left
move
% karel square_dance.krl
location: (0, 0)
direction: right
tokens: [{"location"=>"(-1, 0)", "count"=>1}, {"location"=>"(0, 1)", "count"=>1}]
operations: 9
The interpreter optionally takes a Karel configuration file (-i FILE
) to initialize
tokens on the Karel grid. The file format looks like
tokens: [{"location": "(-1, 0)", "count": 1}, {"location": "(0, 1)", "count": 2}]
The above initialization file will place one token at location (-1, 0) and two tokens at (0, 1)
# initial_tokens.kin
tokens: [{"location": "(-1, 0)", "count": 1}, {"location": "(0, 1)", "count": 2}]
# noop_program.krl
# Do nothing
% karel -i initial_tokens.kin noop_program.krl
location: (0, 0)
direction: up
tokens: [{"location"=>"(-1, 0)", "count"=>1}, {"location"=>"(0, 1)", "count"=>2}]
operations: 0
- Location - location of Karel when the program finishes
- Direction - direction Karel is facing when the program finishes
- Tokens - location and counts of all tokens left on the grid
- Operation - number of built-in commands executed
Karel is a creature that lives on an infinite two dimensional grid. Using built-in commands as the basic building blocks, rudimentary control flow, and user-defined commands, programs can move Karel around the grid and place and remove tokens from squares on the grid.
The Karel language is case-sensitive and indentation-sensitive. All built-in commands and reserved words for control flow and user-defined commands are lower case. Statements within the body of control flow branches and user-defined commands must be indented two spaces beyond the containing branch/command definition. Only one statement is allowed per line. Blank lines are permitted.
Comments begin with a #
and continue to the end of the line. Comments can be placed
on their own line or after a statement.
The following commands are available out of the box.
move
- Move Karel one square forward in the direction she is facingturn_left
- Turn Karel left (CCW) 90 degreestoken?
- Returntrue
if there is one or more tokens on Karel's current location, otherwise falsepick
- Pick up one token from Karel's current location. If there are no tokens, this command will crash the program.put
- Put one token down on Karel's current location.
Karel responds to if
/else
branching and while
loops. The condition evaluated for
these two control flow structures can be negated with a !
(no spaces are allowed between
the negation operator and the condition)
if token?
pick
end
if token?
pick
else
move
end
if !token?
put
end
if token?
pick
if token?
move
end
end
# This will safely remove all tokens from a square.
while token?
pick
end
The Karel language supports user-defined commands. These commands do not take arguments and do not return a value. Commands are defined with the following syntax
def <command-name>
<statement-1>
<statement-2>
...
end
def turn_around
turn_left
turn_left
end
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.
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.
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 code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Karel::Interpreter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.