Skip to content
This repository has been archived by the owner on Jun 3, 2018. It is now read-only.

Commit

Permalink
Add transpile examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmartinelli committed Jan 17, 2016
1 parent 1c69c73 commit 1a25c54
Showing 1 changed file with 59 additions and 63 deletions.
122 changes: 59 additions & 63 deletions README.md
Expand Up @@ -5,43 +5,13 @@
[![Code Health](https://landscape.io/github/lukasmartinelli/py14/master/landscape.svg?style=flat)](https://landscape.io/github/lukasmartinelli/py14/master)
[![Dependency Status](https://gemnasium.com/lukasmartinelli/py14.svg)](https://gemnasium.com/lukasmartinelli/py14)

Try it out online: http://py14.lukasmartinelli.ch/

This is a little experiment that shows how far you can go with the
C++ 14 `auto` return type and templates.

C++14 has such powerful type deduction that it is possible to transpile
Python into C++ without worrying about the missing type annotations in python.

## Trying it out

Requirements:

- clang 3.5

Transpiling:

```
./py14.py fib.py > fib.cpp
```

Compiling:

```
clang++ -Wall -Wextra -std=c++14 -Ipy14/runtime fib.cpp
```
Run regression tests:

```
cd regtests
make
```

Run tests

```
pip install -r requirements.txt
py.test --cov=py14
```

## How it works

Consider a `map` implementation.
Expand Down Expand Up @@ -80,53 +50,79 @@ In order to define the results vector we need to:
4. This results in the fully specified value type of the results vector
`decltype(fun(std::declval<typename decltype(values)::value_type>()))`
## Trying it out
One might support simple structures like this Python class.
Requirements:
```python
class Person:
def __init__(self, prename, name):
self.prename = prename
self.name = name
- clang 3.5
def full_name(self):
return self.prename + " " + self.name
Transpiling:
def give_dog(self, dog):
self.dog = dog
```
./py14.py fib.py > fib.cpp
```
Compiling:
```
clang++ -Wall -Wextra -std=c++14 -Ipy14/runtime fib.cpp
```
Run regression tests:
if __name__ == "__main__":
person = Person("Lukas", "Martinelli", "Bello")
person.give_dog("Hasso")
print(person.full_name())
```
cd regtests
make
```
We need to create a wrapper `makePerson` to parametrize the Person class.
Run tests
```
pip install -r requirements.txt
py.test --cov=py14
```
## More Examples
**Factorial**
```python
def factorial(num):
if num <= 1:
return num
return factorial(num-1) * num
```

```c++
template <typename T1, typename T2, typename T3>
struct Person {
T1 prename;
T2 name;
T3 dog;
auto full_name() { return this->prename + " "s + this->name; }
void give_dog(T3 dog) { this->dog = dog; }
};
template <typename T1, typename T2, typename T3>
auto makePerson(T1 prename, T2 name, T3 dog) {
return Person<T1, T2, T3>{prename, name, dog};
template <typename T1> auto factorial(T1 num) {
if (num <= 1) {
return num;
}
return factorial(num - 1) * num;
}
```
**Probability Density Function (PDF)**
```python
def pdf(x, mean, std_dev):
term1 = 1.0 / ((2 * math.pi) ** 0.5)
term2 = (math.e ** (-1.0 * (x-mean) ** 2.0 / 2.0 * (std_dev ** 2.0)))
return term1 * term2
```

int main() {
auto person = makePerson("Lukas"s, "Martinelli"s, "Bello"s);
person.give_dog("Hasso");
std::cout << person.full_name() << std::endl;
```c++
template <typename T1, typename T2, typename T3>
auto pdf(T1 x, T2 mean, T3 std_dev) {
auto term1 = 1.0 / std::pow(2 * py14::math::pi, 0.5);
auto term2 = std::pow(py14::math::e, -1.0 * std::pow(x - mean, 2.0) / 2.0 *
std::pow(std_dev, 2.0));
return term1 * term2;
}
```
## Working Features
Language Overview
Only bare functions using the basic language features are supported.
- [ ] classes
- [x] functions
Expand Down

0 comments on commit 1a25c54

Please sign in to comment.