Sen descrición

BLUELOVETH 8f3f08503a up %!s(int64=2) %!d(string=hai) anos
.github 970d48f90c up %!s(int64=2) %!d(string=hai) anos
benchmarks 8f3f08503a up %!s(int64=2) %!d(string=hai) anos
docs 24b2140959 update readme %!s(int64=3) %!d(string=hai) anos
plugins d7dddf8c22 update flutter plugin %!s(int64=3) %!d(string=hai) anos
python 6a9d220433 update gc %!s(int64=2) %!d(string=hai) anos
scripts 818ee2981e up %!s(int64=2) %!d(string=hai) anos
src 8f3f08503a up %!s(int64=2) %!d(string=hai) anos
tests 7ce783360f up %!s(int64=2) %!d(string=hai) anos
web f884351c4e Update index.js %!s(int64=2) %!d(string=hai) anos
.gitattributes 0891000a46 init %!s(int64=3) %!d(string=hai) anos
.gitignore 6a27bc8bda up %!s(int64=2) %!d(string=hai) anos
LICENSE ea3fc010f3 change license %!s(int64=3) %!d(string=hai) anos
README.md 7611cf6410 Update README.md %!s(int64=2) %!d(string=hai) anos
README_zh.md 0c2e239e97 fix https://github.com/blueloveTH/pocketpy/issues/41 %!s(int64=3) %!d(string=hai) anos
amalgamate.py 7324f897b5 up %!s(int64=2) %!d(string=hai) anos
build.py 970d48f90c up %!s(int64=2) %!d(string=hai) anos
compile_flags.txt 3b13b1a988 up %!s(int64=3) %!d(string=hai) anos
preprocess.py 6714a3f784 update gc %!s(int64=2) %!d(string=hai) anos
run_profile.sh fba313ce0d Update run_profile.sh %!s(int64=2) %!d(string=hai) anos
run_profile_test.sh 970d48f90c up %!s(int64=2) %!d(string=hai) anos
run_tests.sh 7a07eefed0 up %!s(int64=3) %!d(string=hai) anos

README.md

PocketPy

GitHub GitHub release

PocketPy is a lightweight(~6000 LOC) Python interpreter for game engines/apps.

English | 简体中文

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.

sample_img

Features

Name Example Supported
If Else if..else..elif YES
Loop for/while/break/continue YES
Function def f(x,*args,y=1): YES
Subclass class A(B): YES
List [1, 2, 'a'] YES
ListComp [i for i in range(5)] YES
Slice a[1:2], a[:2], a[1:] YES
Tuple (1, 2, 'a') YES
Dict {'a': 1, 'b': 2} YES
F-String f'value is {x}' YES
Unpacking a, b = 1, 2 YES
Star Unpacking a, *b = [1, 2, 3] YES
Exception raise/try..catch YES
Dynamic Code eval()/exec() YES
Reflection hasattr()/getattr()/setattr() YES
Import import/from..import YES
Context Block with <expr> as <id>: YES
Type Annotation def f(a:int, b:float=1) YES
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 for references. For further customization, you can use C++ API.

#include "pocketpy.h"

int main(){
    // Create a virtual machine
    auto vm = pkpy_new_vm(true);
    
    // 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

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 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.

  • Submit a Pull Request
    • fix a bug
    • add a new feature
  • Open an Issue
    • any suggestions
    • any questions

Check our Coding Style Guide if you want to contribute C++ code.

Reference

The official implementation of Python programming language.

An excellent learning material. It illustrates how Python's virtual machine works.

License

PocketPy is licensed under the MIT License.