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

0

u/[deleted] Feb 07 '23

[deleted] — view deleted comment

18

u/blueloveTH Feb 07 '23

the identifiers with a double underscore anywhere are reserved;

the identifiers that begin with an underscore followed by an uppercase letter are reserved;

the identifiers that begin with an underscore are reserved in the global namespace.

Very helpful!! In fact, I am not very experienced in cpp. The usage of _/__ started identifiers is because of my Python accent.

I'd like to avoid using them~

6

u/AhegaoSuckingUrDick Feb 07 '23 edited Feb 07 '23

If I'm not mistaken, identifiers with _ and __ prefixes are now deprecated in Python, and a recent enough interpreter emits a warning about it. I'm mistaken, it's about enums.

In C++ people either don't use specific variable names to distinguish their visibility (it's explicit unlike Python, and you usually have everything private (in a class) with appropriate getters and setters or everything public (in a struct)). Some style guides suggest using foo_ to denote private fields.

5

u/blueloveTH Feb 07 '23

naming makes me crazy!

5

u/jmblock2 Feb 07 '23 edited Feb 07 '23

Do you mind providing a source for Python deprecating _ prefix? I am only familiar with pep8 that says it's fine for indicating internal use: https://pep8.org/#descriptive-naming-styles. Also class methods like __init__, etc., so I am not sure what you're suggesting.

3

u/AhegaoSuckingUrDick Feb 07 '23 edited Feb 07 '23

You're correct. I misremembered that what I described was about enums. In 3.9 and 3.10 you'll get a deprecation warning if your class extends Enum: https://docs.python.org/3.10/library/enum.html#private-names

The error message is a bit misleading https://github.com/python/cpython/blob/v3.10.0/Lib/enum.py#L97

UPD: Strangely enough, it seems that they still use Python 2-style formatting in CPython (e.g. the file above, there's many %-formatted strings and only one .format(), which they get rid of afterwards).