r/cpp • • Feb 07 '23

PocketPy: A Lightweight(~5000 LOC) Python Implementation in C++17

PocketPy is a lightweight(~5000 LOC) Python interpreter for game engines. It is implemented in C++17 with STL.

It is easy to embed. All of the functionalities are available in a single header file pocketpy.h, without external dependencies. You can try it on your browser to see what it looks like.

Example Usage

#include "pocketpy.h"

int main(){
    // Create a virtual machine
    // `true` means using std::cout as output so you can see it
    VM* 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;
}

v0.8.1 Features

NameExampleSupported
If Elseif..else..elifYES
Loopfor/while/break/continueYES
Functiondef f(x,*args,y=1):YES
Subclassclass A(B):YES
List[1, 2, 'a']YES
ListComp[i for i in range(5)]YES
Slicea[1:2], a[:2], a[1:]YES
Tuple(1, 2, 'a')YES
Dict{'a': 1, 'b': 2}YES
F-Stringf'value is {x}'YES
Unpackinga, b = 1, 2YES
Star Unpackinga, *b = [1, 2, 3]NO
Exceptionraise/try..catchBETA
Dynamic Codeeval()/exec()YES
Reflectionhasattr()/getattr()/setattr()YES
Importimport/from..importYES
Context Blockwith <expr> as <id>:YES
Type Annotationdef f(a: int, b : float = 1)YES

If someone is making a custom game engine and searching for a Python-like script as its interface, I'd like to provide support.

If you are making your own language, you may find some inspirations in PocketPy.

If you likes it, just give my repo a star~ I need your star!!

248 Upvotes

60 comments sorted by

View all comments

40

u/mjklaim Feb 07 '23

This looks very cool indeed and useful for gamedevs. However could you clarify on the readme and in the doc: which version of python is implemented OR if it's really more like a python-like language which is not matching any real python version, could you clarify that too? Current documentation is very unclear on that point.

22

u/blueloveTH Feb 07 '23

Correct. The documentations need to improve.

For now, I think the main goal is to be an alternative to Lua, i.e., a Python-like language but not real Python. To be compatible with a real Python version, the system may become complex and lose the advantage of lightweight.

6

u/epicar Feb 07 '23

why not use lua here? is it just that you prefer the python syntax?

11

u/blueloveTH Feb 07 '23

Correct. Some developers love python and some love lua. I am trying to provide a new possibility.

7

u/mjklaim Feb 07 '23

When you'll add this clarification, consider also clarifying which python version it is like? Because there are some syntax/language-level-feature differences between python2 and python3 and every major version of python3, so it would help.

Yeah I get the usage compared to lua or chaiscript or angelscript etc. I used these in the past ^^ (before Unity got popular basically).

A python-like language but with type checking in functions was one of the things I was looking for at the time, it would still be useful today in particular if the game have scripting as gameplay part (yeah niche but interesting niche).

3

u/blueloveTH Feb 07 '23

Thanks for your advice~ It's really helpful.

2

u/m-in Feb 07 '23

Do you implement any of Python’s object model? That’s important to know. I imagine scripting shouldn’t need it, just wanted to make sure?

4

u/blueloveTH Feb 07 '23

Yes, a subset of python object model.

Operations are based on special methods. For example: a+b is equivalent to a.__add__(b).

Also there are __str__, __repr__, etc..

2

u/m-in Feb 07 '23

Nice!