Skip to content

Commit

Permalink
readme and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
omyyys committed Jul 18, 2022
1 parent fad82ac commit 7987f62
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Pycom: A Python Compiler

## Usage

> ./pycom (flags) [source file]
### Flags
* -i (bool):\
Print additional information about compilation (such as time taken). Defaults to off.

* -r (bool):\
Run the generated executable automatically after compilation. Defaults to off.

* -rd (bool):\
Run the generated executable automatically after compilation, and then delete it. Defaults to off.

* -o [output] (string):\
The string specified after the flag will be the name of the generated executable. Defaults to the name of the Python file that was passed in.

## What is Pycom?

Pycom is effectively a compiler for Python code, bringing it down to a native executable with C++ as 'intermediate representation'. It supports all of the BASIC-like syntax of Python along with a lot of the standard library and inbuilt functions. To see what is currently supported and not supported, check the 'Examples' section below.

## Why and when use Pycom?

Python is slow. While many optimisations and new implementations of it have vastly improved its speed, generating native code that can run as a standalone executable from Python code has never really been done. As a result, no matter what, Python code has never hit levels of speed and portability that C/C++. Pycom aims to tackle this.

Due to Pycom (currenly) not supporting all Python features from all versions, you should only really use it if you want to run simple applications that require high speed (again, check 'Examples')

## Examples

What Pycom supports and is good at:

High iteration loops:

```
def main():
for i in range(1, 1000001):
if i % 3 == 0:
print(i)
return 0
```
10 changes: 8 additions & 2 deletions compiler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import tokenise

# TODO: Implement variables
# TODO: Implement some Python builtins into the output as default - especially input() and len()

invopmap = {v: k for k, v in tokenise.tokmap.items()}

implemented = [
Expand Down Expand Up @@ -35,10 +38,12 @@
'void print(int istr){std::cout << istr << std::endl;}',
'void print(float fstr){std::cout << fstr << std::endl;}',
'void print(long long int llistr){std::cout << llistr << std::endl;}',
'void print(long double ldstr){std::cout << ldstr << std::endl;}'
'void print(long double ldstr){std::cout << ldstr << std::endl;}',
'int len(std::string str){return str.length();}',
'std::string input(std::string prompt){std::cout << prompt; std::string x; std::cin >> x; return x;}',
]

types = ["str", "int", "float"]
types = ["str", "int", "float", "None"]

includes = ["iostream", "string", "headers/range.hpp", "sstream"]
using = ["util::lang::range"]
Expand All @@ -47,6 +52,7 @@
"str": "std::string",
"int": "long long int",
"float": "long double",
"None": "void"
}

def findlastkw(tokens, currentind):
Expand Down
7 changes: 4 additions & 3 deletions source.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def main():
print("Hello, World!")
print("h")
print("df")
for i in range(1, 1000001):
if i % 3 == 0:
print(i)

return 0

0 comments on commit 7987f62

Please sign in to comment.