Refactor examples

This commit is contained in:
Mahbub Alam 2023-10-23 20:37:27 -04:00
parent 53276a299a
commit 39c92bb088
8 changed files with 103 additions and 19 deletions

1
examples/.gitignore vendored
View File

@ -1 +0,0 @@
pocketpy.h

35
examples/1_type_cast.cpp Normal file
View File

@ -0,0 +1,35 @@
/**
* This example demonstrates the type casting feature of PocketPy.
* It creates a virtual machine and cast PyObject* to different types.
*/
#include "pocketpy.h"
using namespace pkpy;
int main(){
// Create a virtual machine
VM* vm = new VM();
PyObject* str_obj = py_var(vm, "hello world");
// Cast PyObject* to Str type
Str& str = py_cast<Str&>(vm, str_obj);
std::cout << "string: " << str.c_str() << std::endl; // hello world
PyObject* int_obj = py_var(vm, 10);
// Cast PyObject* to Int type
int int_var = py_cast<int>(vm, int_obj);
std::cout << "int: " << int_var << std::endl; // 10
PyObject* float_obj = py_var(vm, 10.5);
// Cast PyObject* to double type
double float_var = py_cast<double>(vm, float_obj);
std::cout << "float: " << float_var << std::endl; // 10.5
// Dispose the virtual machine
delete vm;
return 0;
}

View File

@ -0,0 +1,36 @@
/**
* This example demonstrate the process of creating a python module and
* bind a function to it, as well as the procedure for calling the function.
*/
#include "pocketpy.h"
using namespace pkpy;
int main(){
// Create a virtual machine
VM* vm = new VM();
// Create a module
PyObject* math_module = vm->new_module("math");
// Bind a function named "add" to the module
vm->bind(math_module, "add(a: int, b: int) -> int",
[](VM* vm, ArgsView args){
int a = py_cast<int>(vm, args[0]);
int b = py_cast<int>(vm, args[1]);
return py_var(vm, a + b);
});
// Call the "add" function
PyObject* f_sum = math_module->attr("add");
PyObject* result = vm->call(f_sum, py_var(vm, 4), py_var(vm, 5));
std::cout << "Sum: " << py_cast<int>(vm, result) << std::endl; // 9
// Dispose the virtual machine
delete vm;
return 0;
}

View File

@ -1,9 +1,9 @@
/**
* Calculate average of a list of numbers.
* This example illustrate the process of creating a python module and
* bind a function to it, as well as the procedure for calling the function.
* Additionally, it outlines the creating of a list.
* This example demonstrates how to build a List object and how to access its elements.
* It creates a python module named "math_utils" and bind a function named "average" to it.
* It exercises creating a List and accessing its elements.
* It also demonstrates how to cast a python object to a C++ type.
*/
#include "pocketpy.h"

View File

@ -1,8 +1,8 @@
/**
* This example illustrate use of Dict and List in PocketPy.
* This example illustrate use of Dict in PocketPy.
* It creates a python module named "employee" and bind four functions to it.
* It exercises setting and getting elements in a Dict and List.
* It exercises setting and getting elements in a Dict.
*/

View File

@ -0,0 +1,26 @@
/**
* 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;
}

View File

@ -1,12 +0,0 @@
# TODO
- variations of print function
- variable declaration
- string, list, tuple, dictionary
- basic operations
- if else
- for loop, while loop
- json loads, dumps
- exception handling
clang++ --std=c++17 cjson.cpp -o cjson