Skip to main content

Strongly typed Python to C++ transpiler.

Project description

buildstatus coverage

🐁 Mys

🚧 🚧 🚧 🚧 🚧 Under construction - DO NOT USE 🚧 🚧 🚧 🚧 🚧

The Mys (/maɪs/) programming language - an attempt to create a strongly typed Python-like language that produces fast binaries.

Project homepage: https://github.com/eerimoq/mys

🚧 🚧 🚧 🚧 🚧 Under construction - DO NOT USE 🚧 🚧 🚧 🚧 🚧

Installation

$ pip install mys

You must also have a recent version of g++ installed.

Tutorial

First of all, create a package called foo and enter it. This package is used in throughout the tutorial.

$ mys new foo
$ cd foo

Two files are create; Package.toml and src/main.mys. Package.toml contains the package configuration and src/main.mys the application source code.

The source code is the hello world application.

def main():
    print('Hello, world!')

Build and run it with mys run. All build output is found in the build directory.

$ mys run
Hello, world!

Now, replace the code in src/main.mys with the code below.

def func_1(a: int) -> (int, str):
     return 2 * a, 'Bar'

def func_2(a: int, b: int=1) -> int:
     return a * b

def func_3(a: Optional[int]) -> int:
    if a is None:
        return 0
    else:
        return 2 * a

def func_4(a: int) -> {int: [float]}:
    return {10 * a: [7.5, -1.0]}

def func_5():
    try:
        raise Exception()
    except:
        print('func_5(): An exception occurred.')

def main(args: [str]):
    value = int(args[1])
    print('func_1(value):', func_1(value))
    print('func_2(value):', func_2(value))
    print('func_3(None): ', func_3(None))
    print('func_3(value):', func_3(value))
    print('func_4(value):', func_4(value))
    func_5()

Build and run it.

$ mys run 5
func_1(value): (5, 'Bar')
func_2(value): 5
func_3(None):  0
func_3(value): 10
func_4(value): {50: [7.5, -1,0]}
func_5(): An exception occurred.

Built-in functions and classes

Built-in functions and classes

abs()

all()

any()

bool()

bytearray()

bytes()

chr()

dict()

divmod()

enumerate()

f32()

f64()

float()

format()

int()

len()

list()

min()

max()

open()

ord()

print()

range()

reversed()

round()

s8()

s16()

s32()

s64()

set()

str()

sum()

tuple()

u8()

u16()

u32()

u64()

zip()

All built-ins aims to behave like their Python counterparts, with the following differences.

  • abs() only supports integer and floating point numbers.

  • all() and any() only supports lists of bool().

  • u8(), u16(), u32(), u64(), s8(), s16(), s32() and s64() behaves like int().

  • f32() and f64() behaves like float().

  • min() and max() only supports lists of integer and floating point numbers, and a fixed number of integer and floating points parameters.

  • sum() only supports lists of integer and floating point numbers.

Types

Variables may all be set to None if declared as Optional.

Type

Example

Comment

int

1, -1000

An integer. Usually 32 or 64 bits.

u8

5, 200

An 8 bits unsigned integer.

u16

5, 200

A 16 bits unsigned integer.

u32

5, 200

A 32 bits unsigned integer.

u64

5, 200

A 64 bits unsigned integer.

s8

-5, 100

An 8 bits signed integer.

s16

-5, 100

A 16 bits signed integer.

s32

-5, 100

A 32 bits signed integer.

s64

-5, 100

A 64 bits signed integer.

float

5.5, -100.0

A floating point number. Usually 32 bits.

f32

5.3, -100.0

A 32 bits floating point number.

f64

5.3, -100.0

A 64 bits floating point number.

str

'Hi!'

A unicode string. Immutable.

bytes

b'\x00\x43'

A sequence of bytes. Immutable.

bytearray

A sequence of bytes.

tuple(T1, T2, ...)

(5.0, 5, 'foo')

A tuple with items of types T1, T2, etc. Immutable.

list(T)

[5, 10, 1]

A list with items of type T.

dict(TK, TV)

{5: 'a', -1: 'b'}

A dictionary with keys of type TK and values of type TV.

set(T)

A set with items of type T.

Major differences to Python

  • All variables must have a known type at compile time. The same applies to function parameters and return value.

  • Threads can run in parallel. No GIL exists.

    WARNING: Data races will occur when multiple threads uses a variable at the same time, which will likely make the program crash.

  • Integers and floats have a platform dependent maximum size, usually 32 or 64 bits.

  • The built-in function super() does not exists. Base class constructors are called implicitly.

  • Decorators does not exist.

  • Variable function arguments *args and **kwargs are not supported, except to some built-in functions.

  • Async is not supported.

  • The majority of the standard library is not implemented.

Text editor settings

Visual Code

Use the Python language for *.mys files by modifying your files.associations setting.

See the official Visual Code guide for more detils.

"files.associations": {
    "*.mys": "python"
}

Emacs

Use the Python mode for *.mys files by adding the following to your .emacs configuration file.

(add-to-list 'auto-mode-alist '("\\.mys\\'" . python-mode))

Performance

ToDo.

Build process

  1. Use Python’s parser to transform the source code to an Abstract Syntax Tree (AST).

  2. Generate C++ code from the AST.

  3. Compile the C++ code with g++.

  4. Link the program with g++.

Similar projects

https://github.com/konchunas/pyrs

https://github.com/lukasmartinelli/py14

https://github.com/shedskin/shedskin

https://github.com/pradyun/Py2C

https://github.com/mbdevpl/transpyle

http://numba.pydata.org/

https://github.com/Nuitka/Nuitka

https://github.com/QQuick/Transcrypt

https://github.com/pyjs/pyjs

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mys-0.7.0.tar.gz (10.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page