Lua to Python
This is a project where I attempt to convert Lua to Python, by transforming Lua into Python AST and then running it.
Current status
- Variable assignments, basic datatypes and comparisons, if statements, while loops and functions are working.
Getting started
pip install -r requirements.txt
Usage: compile.py [OPTIONS] SOURCE_FILE
Options:
--strip_comments INTEGER Remove comments from tokens
--tokens INTEGER Show tokens generated by lexer
--ast INTEGER Show the internal AST (later transformed to Python
AST
--py_ast INTEGER Show Python AST
--py_code INTEGER Show generated Python code
--help Show this message and exit.
Example: ./compile.py --strip-comments=1 examples/functions.lua
Roadmap
- Single line comments
- Multiline comments
- Numbers
- Strings
- Nil types
- Variable assignments
- Addition
- Multiplication
- If statements
- Nested if statements
-
~=
operator -
==
operator -
while
keyword - Concat strings with
..
- Subtract values
-
>=
operator -
<=
operator - Boolean types
-
function
declarations -
return
-
not
logical operator -
bool
expression in comparison -
%
operator -
/
operator -
or
logical operator -
and
logical operator - Assign function return to variable
- Double number support
- Negative values
- Anonymous functions
- Variables with numbers in name
- Table datatype
- Support for accessing Table properties
- Support for lteral notation (
['@!#']
) in Table - List as argument to Table constructor
-
#
operator for retrieving Table/String length - Iterator for Table using
pairs
/ipairs
-
_G
for globals access -
for
keyword - Add multiple assignment support (
x, y, z = 1, 2, 3
) - Add multiple assignment support in for loop target+iterator
- Add multiline line support to anonymous functions
-
repeat
keyword - Short circuit / tenary operator
-
local
variables - Numbers beginning with
.
(ex.123
) - Undefined variables should return nil
- Dot property syntax in Table for string keys
- Function calls with single params should not require parens
- Metatable support for tables
- BUG: Function cannot call itself
- BUG: Function declaration in nested Table
- BUG: Nested attribute retrival with
["random"]["random2"]
- BUG: Decimal type key in Table
- BUG: Table construct in function (ex:
pairs({a=1, b=2, c=3})
References
- https://drew.ltd/blog/posts/2020-7-18.html - Many thanks for Drew and his excellent articles on how to build a programming language
- https://greentreesnakes.readthedocs.io/en/latest/
- https://github.com/python/cpython/blob/master/Lib/ast.py
- https://learnxinyminutes.com/docs/lua/