Introduction:
I use Lego Mindstroms(NXT) robotics kit and I could not find a way to
program my bots in python. I pondered about learning the NXT byte code
format and writing a backend for the
PyPy
project. But instead I quickly hacked up a simple translator in couple
of hours that enables one to write NXT programs in python like syntax
and converts it to
NXC.
Is this python really ?
Well it resembles python, but none of the useful syntax is supported
yet.
Currently none of the following are supported:
- Dynamic typing (I don't think this will ever be
supported)
- No function generators, iterators, imports, lambda
functions ect
- No support for object oriented programming
What is supported:
- All functions in NXC (Since it is just a wrapper)
- For, While, if statements.
- Functions.
One of the big todo items is to do data flow analysis to determine what
type each variable is.
- Only ints and longs for now
- longs should contain the string "long" in variable name (I
know, pretty lame)
To Do:
1) Data Flow Analysis: I have some idea on how to do this. I just need
to spend enough time to get this working.
2) Support object oriented programming
3) Add a library of functions so that it becomes very easy program NXT
in python
Example:
Here is a line following
program in python like language
To generate NXC code, just run
python translate.py <input.py> > output.nxc
Here is the translated
NXC code.
Download:
Source:
translate.py
The above file is under Apache license as I used some code from pyJS by
James Tauber that converts python to java script. I did not want to
write all of the AST walker functions and reuse some existing code.