diff --git a/examples/.gitignore b/examples/.gitignore deleted file mode 100644 index 3c9a1c23..00000000 --- a/examples/.gitignore +++ /dev/null @@ -1 +0,0 @@ -pocketpy.h diff --git a/examples/1_type_cast.cpp b/examples/1_type_cast.cpp new file mode 100644 index 00000000..3ff45a7f --- /dev/null +++ b/examples/1_type_cast.cpp @@ -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(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(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(vm, float_obj); + std::cout << "float: " << float_var << std::endl; // 10.5 + + // Dispose the virtual machine + delete vm; + return 0; +} diff --git a/examples/2_module_and_binding.cpp b/examples/2_module_and_binding.cpp new file mode 100644 index 00000000..dc897087 --- /dev/null +++ b/examples/2_module_and_binding.cpp @@ -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(vm, args[0]); + int b = py_cast(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(vm, result) << std::endl; // 9 + + // Dispose the virtual machine + delete vm; + return 0; +} diff --git a/examples/average.cpp b/examples/3_list_operation.cpp similarity index 80% rename from examples/average.cpp rename to examples/3_list_operation.cpp index 128c87f5..d96d63ea 100644 --- a/examples/average.cpp +++ b/examples/3_list_operation.cpp @@ -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" diff --git a/examples/employee.cpp b/examples/4_dictionary_operation.cpp similarity index 97% rename from examples/employee.cpp rename to examples/4_dictionary_operation.cpp index 7a35e692..3e43878d 100644 --- a/examples/employee.cpp +++ b/examples/4_dictionary_operation.cpp @@ -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. */ diff --git a/examples/5_python_scripting.cpp b/examples/5_python_scripting.cpp new file mode 100644 index 00000000..735e615b --- /dev/null +++ b/examples/5_python_scripting.cpp @@ -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; +} diff --git a/examples/type_checker.cpp b/examples/6_type_checker.cpp similarity index 100% rename from examples/type_checker.cpp rename to examples/6_type_checker.cpp diff --git a/examples/README.md b/examples/README.md deleted file mode 100644 index be5db77c..00000000 --- a/examples/README.md +++ /dev/null @@ -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