Educational programming language re-implemented in Perl.
- Invented by Richard E. Pattis at Stanford in the early
Karel The Robot: A
Gentle Introduction to the Art of Programming
- Named after
Čapek, the author of
- Its Czech version became quite popular in Czechoslovakia.
- It was the first programming language I learned.
- I implemented it years ago in Perl
- as an interpreter,
- as a monolithic unmaintainable script.
- I wanted to try better.
Karel the Robot—the Details
- The robot lives in a grid.
- The grid can contain walls and marks (originally beepers).
- The robot can’t walk through walls.
- The robot understands the following commands:
Karel the Robot—the Details (2)
The robot can learn new commands
- using sequences of already known ones,
- and also the following control keywords and conditions:
- repeat N times
- if ( + else )
- done, command, end
- there is
- there is no
- ( not ) facing
- Perl-like comments also supported
Example - Turning Right
repeat 3 x
done # <- Here ends the repeat.
Calling a Function
while there's no wall
if there's no wall
right # User-defined.
- The robot remembers where a function was called from and
returns back once the function has finished.
The Killer Feature
while there's no mark
while there's a wall
if there's no wall
follow-right-hand # Recursion!
- Like when calling another function, the robot remembers
from where it recursed from and returns back.
- Turing complete.
- Implement increment and addition.
- Write a program that fills all the accessible grid squares
with marks (+ demo).
Tested on Humans
- My 8 years old son was excited.
- I was able to explain “off-by-one error” to him.
- Version 0.03 on its way to CPAN.
- Simple text UI is included in the distribution (demo).
- On GitHub, a web GUI written in Dancer2 and Template::Toolkit emerges.
- Debugging: almost done
- propagation of symbol positions and length: grammar → stack
- propagation of the source code: parser → stack