Lua-To-Python
Lua-To-Python copied to clipboard
Lua to python compiler
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
- [x] Single line comments
- [x] Multiline comments
- [x] Numbers
- [x] Strings
- [x] Nil types
- [x] Variable assignments
- [x] Addition
- [x] Multiplication
- [x] If statements
- [x] Nested if statements
- [x]
~=operator - [x]
==operator - [x]
whilekeyword - [x] Concat strings with
.. - [x] Subtract values
- [x]
>=operator - [x]
<=operator - [x] Boolean types
- [x]
functiondeclarations - [x]
return - [x]
notlogical operator - [x]
boolexpression in comparison - [x]
%operator - [x]
/operator - [x]
orlogical operator - [x]
andlogical operator - [x] Assign function return to variable
- [x] Double number support
- [x] Negative values
- [x] Anonymous functions
- [x] Variables with numbers in name
- [x] Table datatype
- [x] Support for accessing Table properties
- [x] Support for lteral notation (
['@!#']) in Table - [x] List as argument to Table constructor
- [x]
#operator for retrieving Table/String length - [x] Iterator for Table using
pairs/ipairs - [x]
_Gfor globals access - [x]
forkeyword - [x] Add multiple assignment support (
x, y, z = 1, 2, 3) - [x] Add multiple assignment support in for loop target+iterator
- [x] Add multiline line support to anonymous functions
- [ ]
repeatkeyword - [ ] Short circuit / tenary operator
- [ ]
localvariables - [ ] 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/