This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Terra1's profile picture

python interpreter written in lua

Started by Terra1, 21 April 2015 - 02:27 AM
Terra1 #1
Posted 21 April 2015 - 04:27 AM
I need help making a python interpreter in CC using lua to help people transition from python to lua but I am confused
I have already came up with the idea of a bunch of read commands built into a text editor but that would be complicated and buggy and bulky and slow.
Can somebody help me.
Bomb Bloke #2
Posted 21 April 2015 - 04:42 AM
Hrm, are you wanting to make an IDE (eg a text editor, one which maybe can call on an interpretor), or just the interpreter? They're two different tasks - I recommend sorting out the interpreter first.

That said, this sounds like one of those "if you have to ask, it's not for you"-type projects.
SquidDev #3
Posted 21 April 2015 - 07:35 AM
I started something a while back, though trust me it is a BIG job. Instead of parsing Python in Lua, parse Python in Python, and convert it to Lua. Python comes with this functionality called AST - Abstract Syntax Trees. This site gives you a pretty good description of how it all works, so some things like primitives (numbers, strings, etc..) are easy to convert, though lists and dictionaries will be harder. This is because you will have to write large portions of Python's object system before you start doing much. In fact, even numbers are objects, so primitives aren't simple.

So to start with you'd want to create the objecting system and core functionality - the int, str, list, dict, etc… classes. Then __builtins__ - so everything has functionality. I wouldn't bother implementing everything in Python - if this is for beginners then they don't really need the func_* methods, or locals(). Don't go for a rewrite of Python, just a very small subset.

Notice we haven't done anything about conversion so far. The above is a pretty thankless task, hard to test and you get few visible results from it. I'd then look at converting basic expressions to Lua - simple numbers & strings, and then basic arithmetic. Then go on for more complex tasks such as lists, and then control constructs (if, while, for) and then functions, and then (maybe), classes.

This is a massive job, it will be incredibly cool when finished, just a very long road to get there.