pocketpy/examples/5_python_scripting.cpp
Mahbub Alam 6f8bf82e9a
Examples for beginners (#169)
* Add examples folder

* Draft change

* Add draft changes

* Add examples

* Refactor examples

---------

Co-authored-by: Mahbub Alam <alam.mahbub214@gmail.com>
2023-10-24 17:59:58 +08:00

27 lines
542 B
C++

/**
* This example demonstrate the use of PocketPy as a scripting language.
* It creates a virtual machine and execute a python script.
*/
#include "pocketpy.h"
using namespace pkpy;
int main(){
// Create a virtual machine
VM* vm = new VM();
// Print "hello world" to the console
vm->exec("print('hello world')"); // hello world
// List comprehension
vm->exec("l = [i*i for i in range(1, 6)]");
vm->exec("print(l)"); // [1, 4, 9, 16, 25]
// Dispose the virtual machine
delete vm;
return 0;
}