Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
...
  • Loading branch information
blueloveTH committed Apr 24, 2023
1 parent 07a0122 commit 5765760
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 608 deletions.
164 changes: 46 additions & 118 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PocketPy
# pocketpy: python interpreter in 1 file

<p>
<a title="Build" href="https://github.com/blueloveTH/pocketpy/actions/workflows" ><img src="https://github.com/blueloveTH/pocketpy/actions/workflows/main.yml/badge.svg" /></a>
Expand All @@ -8,21 +8,57 @@
<img alt="GitHub release" src="https://img.shields.io/github/release/blueloveth/pocketpy.svg"></a>
<a title="Pub" href="https://pub.dev/packages/pocketpy" ><img src="https://img.shields.io/pub/v/pocketpy" /></a>
</p>
PocketPy is a lightweight(~8000 LOC) Python interpreter for game engines/apps.


---
#### The first production-ready version is `v1.0.0`, which will be available soon.

---

**English |** [**简体中文**](README_zh.md)

pkpy is a lightweight(~8000 LOC) Python interpreter for game engine/apps, built on C++17 with STL.

It is extremely easy to embed. Including a compiler, optimizer and bytecode virtual machine. All of them are available in a single header file `pocketpy.h`, without external dependencies.

Please see https://pocketpy.dev for details or try [Live Demo](https://pocketpy.dev/static/web/).

![sample_img](docs/sample.png)
## Quick start

Download the `pocketpy.h` on our GitHub release page.
And `#include` it in your project.

https://github.com/blueloveTH/pocketpy/releases/latest

### Compile flags

To compile it with your project, these flags must be set:

+ `--std=c++17` flag must be set
+ Exception must be enabled
+ RTTI is not required

!!!
For maximum performance, we recommend to use `clang++` with `-O2` flag.
!!!

### Example

```cpp
#include "pocketpy.h"

using namespace pkpy;

int main(){
// Create a virtual machine
VM* vm = new VM();

// Hello world!
vm->exec("print('Hello world!')", "main.py", EXEC_MODE);

// Create a list
vm->exec("a = [1, 2, 3]", "main.py", EXEC_MODE);

// Eval the sum of the list
PyObject* result = vm->exec("sum(a)", "<eval>", EVAL_MODE);
std::cout << CAST(int, result); // 6
return 0;
}
```

## Features

Expand All @@ -49,114 +85,6 @@ Please see https://pocketpy.dev for details or try [Live Demo](https://pocketpy.
| Generator | `yield i` | YES |
| Decorator | `@cache` | YES |

## Getting Started

#### C/C++

For C/C++ developers, you can download the `pocketpy.h` on our GitHub release page.

https://github.com/blueloveTH/pocketpy/releases/latest

Check [C-API](https://pocketpy.dev/c-api/vm/) for references. For further customization, you can use [C++ API](https://pocketpy.dev/getting-started/cpp/).

```cpp
#include "pocketpy.h"

int main(){
// Create a virtual machine
auto vm = pkpy_new_vm();

// Hello world!
pkpy_vm_exec(vm, "print('Hello world!')");

// Create a list
pkpy_vm_exec(vm, "a = [1, 2, 3]");

// Eval the sum of the list
char* result = pkpy_vm_eval(vm, "sum(a)");
printf("%s", result); // 6

// Free the resources
pkpy_delete(result);
pkpy_delete(vm);
return 0;
}
```

#### Unity Engine

PocketPy for Unity can be installed via Unity Asset Store.

https://assetstore.unity.com/packages/slug/241120

```csharp
using UnityEngine;

public class Test01 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// Create a virtual machine
pkpy.VM vm = new pkpy.VM();

// Create a list
vm.exec("a = [1, 2, 3]");

// Eval the sum of the list
string result = vm.eval("sum(a)");
Debug.Log(result); // 6
// Print to the standard output
vm.exec("print(a)");
pkpy.PyOutput o = vm.read_output();
Debug.Log(o.stdout); // [1, 2, 3]
// Create a binding
vm.bind("builtins", "test", (double x) => x+1);
Debug.Log(vm.eval("test(3.14)")); // '4.14'
}
}
```

#### Flutter

Run the following script to install this plugin.

```
flutter pub add pocketpy
```

See https://pocketpy.dev/getting-started/flutter/

#### Pre-compiled Libs

You can download `artifact.zip` from [Github Release](https://github.com/blueloveTH/pocketpy/releases/latest) page. In this archive, there are pre-compiled libraries for many platforms. The file structure is as follows.

```
- android/
- arm64-v8a/
- libpocketpy.so
- armeabi-v7a/
- libpocketpy.so
- x86_64/
- libpocketpy.so
- linux/
- x86_64/
- pocketpy
- pocketpy.so
- macos/
- pocketpy.bundle/
- web/
- lib/
- pocketpy.js
- pocketpy.wasm
- windows/
- x86_64/
- pocketpy.dll
- pocketpy.exe
```

## Contribution

All kinds of contributions are welcome.
Expand All @@ -183,5 +111,5 @@ Check our [Coding Style Guide](https://pocketpy.dev/coding_style_guide/) if you

## License

PocketPy is licensed under the [MIT License](http://opensource.org/licenses/MIT).
pkpy is licensed under the [MIT License](http://opensource.org/licenses/MIT).

134 changes: 27 additions & 107 deletions README_zh.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PocketPy
# pocketpy: python interpreter in 1 file

<p>
<a title="Build" href="https://github.com/blueloveTH/pocketpy/actions/workflows" ><img src="https://github.com/blueloveTH/pocketpy/actions/workflows/main.yml/badge.svg" /></a>
Expand All @@ -8,46 +8,16 @@
<img alt="GitHub release" src="https://img.shields.io/github/release/blueloveth/pocketpy.svg"></a>
<a title="Pub" href="https://pub.dev/packages/pocketpy" ><img src="https://img.shields.io/pub/v/pocketpy" /></a>
</p>

PocketPy是一个轻量级的Python解释器,为嵌入至游戏引擎而设计。
pocketpy是一个轻量级的Python解释器,为嵌入至游戏引擎而设计,基于C++17和STL。

它包含一个编译器和基于字节码的虚拟机,以及交互式命令窗的实现。所有功能均集成在单个头文件`pocketpy.h`中,不包含外部依赖项,能很方便地嵌入至你的应用。

你可以 [在浏览器中体验](https://pocketpy.dev/static/web/) PocketPy的交互式界面(REPL)。

![sample_img](docs/sample.png)

## 支持的语法特性

| 特性 | 示例 | 支持 |
| ------------ | ------------------------------- | ---- |
| 分支 | `if..else..elif` | YES |
| 循环 | `for/while/break/continue` | YES |
| 函数 | `def f(x,*args,y=1):` | YES |
| 类与继承 | `class A(B):` | YES |
| 列表 | `[1, 2, 'a']` | YES |
| 列表生成式 | `[i for i in range(5)]` | YES |
| 切片 | `a[1:2], a[:2], a[1:]` | YES |
| 元组 | `(1, 2, 'a')` | YES |
| 字典 | `{'a': 1, 'b': 2}` | YES |
| 格式化字符串 | `f'value is {x}'` | YES |
| 序列解包 | `a, b = 1, 2` | YES |
| 异常 | `raise/try..catch` | YES |
| 动态分发 | `eval()/exec()` | YES |
| 反射 | `hasattr()/getattr()/setattr()` | YES |
| 导入模块 | `import/from..import` | YES |
| 上下文管理器 | `with <expr> as <id>:` | YES |
| 类型标注 | `def f(a:int, b:float=1)` | YES |
| 生成器 | `yield i` | YES |
| 装饰器 | `@cache` | YES |
你可以 [在浏览器中体验](https://pocketpy.dev/static/web/) pocketpy的交互式界面(REPL)。

## 快速上手

根据你所使用的语言和平台选择对应的插件。

#### C/C++

你可以在 [Github Release](https://github.com/blueloveTH/pocketpy/releases/latest) 页面下载`pocketpy.h`,并加入到你的工程中。请参考[C-API](https://pocketpy.dev/c-api/vm/)相关的说明,对于C++,你也可以使用`VM`类的方法操作虚拟机。
你可以在 [Github Release](https://github.com/blueloveTH/pocketpy/releases/latest) 页面下载`pocketpy.h`
并加入到你的工程中。请参阅 https://pocketpy.dev 以获取更详细的文档。

```cpp
#include "pocketpy.h"
Expand All @@ -73,79 +43,29 @@ int main(){
}
```

#### Unity Engine

你可以在Unity资源商店下载PocketPy的C#插件,支持Windows/MacOS/Android/iOS平台。

https://assetstore.unity.com/packages/slug/241120

```csharp
using UnityEngine;

public class Test01 : MonoBehaviour
{
void Start()
{
// 创建一个虚拟机
pkpy.VM vm = new pkpy.VM();

// 构造一个列表
vm.exec("a = [1, 2, 3]");

// 对列表进行求和
string result = vm.eval("sum(a)");
Debug.Log(result); // 6
// 打印变量`a`,并读取标准输出
vm.exec("print(a)");
pkpy.PyOutput o = vm.read_output();
Debug.Log(o.stdout); // [1, 2, 3]
// 构造一个函数绑定
vm.bind("builtins", "test", (double x) => x+1);
Debug.Log(vm.eval("test(3.14)")); // '4.14'
}
}
```

#### Flutter

使用下列命令安装pocketpy的[Flutter插件](https://pub.dev/packages/pocketpy),支持Windows/Android/iOS/Web平台。

```
flutter pub add pocketpy
```

详细配置请参考 https://pocketpy.dev/getting-started/flutter/

#### 预编译库

你可以在 [Github Release](https://github.com/blueloveTH/pocketpy/releases/latest) 页面下载`artifact.zip`,这个压缩包中包含了一套预编译库。结构如下。

```
- android/
- arm64-v8a/
- libpocketpy.so
- armeabi-v7a/
- libpocketpy.so
- x86_64/
- libpocketpy.so
- linux/
- x86_64/
- pocketpy
- pocketpy.so
- macos/
- pocketpy.bundle/
- web/
- lib/
- pocketpy.js
- pocketpy.wasm
- windows/
- x86_64/
- pocketpy.dll
- pocketpy.exe
```
## 支持的语法特性

| 特性 | 示例 | 支持 |
| ------------ | ------------------------------- | ---- |
| 分支 | `if..else..elif` | YES |
| 循环 | `for/while/break/continue` | YES |
| 函数 | `def f(x,*args,y=1):` | YES |
| 类与继承 | `class A(B):` | YES |
| 列表 | `[1, 2, 'a']` | YES |
| 列表生成式 | `[i for i in range(5)]` | YES |
| 切片 | `a[1:2], a[:2], a[1:]` | YES |
| 元组 | `(1, 2, 'a')` | YES |
| 字典 | `{'a': 1, 'b': 2}` | YES |
| 格式化字符串 | `f'value is {x}'` | YES |
| 序列解包 | `a, b = 1, 2` | YES |
| 异常 | `raise/try..catch` | YES |
| 动态分发 | `eval()/exec()` | YES |
| 反射 | `hasattr()/getattr()/setattr()` | YES |
| 导入模块 | `import/from..import` | YES |
| 上下文管理器 | `with <expr> as <id>:` | YES |
| 类型标注 | `def f(a:int, b:float=1)` | YES |
| 生成器 | `yield i` | YES |
| 装饰器 | `@cache` | YES |

## 参考

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ icon: home
label: Welcome
---

# Welcome to PocketPy
# Welcome to pocketpy

pkpy is a lightweight(~8000 LOC) Python interpreter for game engine/apps.

Expand Down
Binary file added docs/plugins.zip
Binary file not shown.
35 changes: 0 additions & 35 deletions docs/plugins/c.md

This file was deleted.

Loading

0 comments on commit 5765760

Please sign in to comment.