Git Product home page Git Product logo

lupy's Introduction

LuPy

What is this project?

LuPy is a simple translator that produces Lua equivalent for a subset of Python programs.
It supports most of the script-like yntax of Python, i.e anything without classes and imports.
It also doesnt support stray expressions, since they aren't allowed in Lua.

Standard library

For now LuPy supports only a few functions from standard library: print() - translates to itself in Lua.
dict() - translates into table declaration. len() - translates into #, which when placed before table returns length of it.

Any other function are not officially supported, but would work in unsafe mode if they have the same name in both languages. For example type() function works like that.

Python collections vs Lua tables

LuPy translates all the collections (lists, dictionaries) from Python into Lua tables. Since Python arrays start from zero and their Lua counterpart start from one, LuPy explicitly defines that the first element of array starts from zero in translated Lua code.

Stray expressions

Stray expressions, e.g. expressions without assignment are semantically incorrect for Lua, thus LuPy doesn't translate programs with them.

Example:

# allowed
a = 2 + 2

# allowed
func()

# forbidden
2 + 2

Installation

This project stores dependencies in requirements.txt file.
To install them use:

pip install -r requirements.txt

Usage

Command line

python main.py -i ./input -o ./output

Arguments:
-i <path> - path to input file or directory. Default: './input'.
-o <path> - path to output directory. Default: './output.
-unsafe - turns off semantical checks

Python

To translate string:

from lupy import translate

translate("print('Hello World!')")

To process file or folder:

from lupy import process

# process one file and save to working dir
process("test.py" "./")
# process ./input folder and save result into ./output folder
process("./input", "./output")

Implementation

Lexer

LuPy uses regular expression based lexer for converting python code into tokens.
Lexer also does all the preprocessing of data, converting names (e.g. identifiers, function names, numbers, strings and etc.) into easily parsable tokens, since only type of this tokens is importnant and content can be ommited for parsing stage.

Parser

LuPy uses Earley Parser for checking that input is a part of supported subset of Python and building AST of the program.
You can find grammar used in LuPy: here.

Sematics and Safe mode

LuPy does a list of checks on AST before generating a Lua program from it to ensure semantical correctness of original code. This stage can be skipped by running LuPy in unsafe mode.

Checks:

  • Variable must be declared (i.e. assigned) before it's used in expressions.
  • Function must be declared before it's called for the first time
  • Function call must have the same amount of parameters as function declaration (default values aren't supported in LuPy)
  • When function is called all variables used by this function (i.e. global scope variables) must be declared
  • When function is called all the functions called inside of it must be declared

lupy's People

Contributors

healear avatar powersl1d3 avatar rmdeuce avatar sveta290700 avatar vladocc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.