6_type_checker.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * This example demonstrates how to create different types of objects and
  3. * how to check their types.
  4. */
  5. #include "pocketpy.h"
  6. using namespace pkpy;
  7. int main(){
  8. // Create a virtual machine
  9. VM* vm = new VM();
  10. // Create a module
  11. PyObject* type_checker = vm->new_module("type_checker");
  12. // Bind a function named "get_type" to the module
  13. vm->bind(type_checker, "get_type(obj: object) -> str",
  14. "Returns type of a python object", // docstring
  15. [](VM* vm, ArgsView args){
  16. PyObject* obj = args[0];
  17. if(is_type(obj, vm->tp_int)){
  18. return py_var(vm, "int");
  19. }
  20. else if(is_type(obj, vm->tp_str)){
  21. return py_var(vm, "str");
  22. }
  23. else if(is_type(obj, vm->tp_float)){
  24. return py_var(vm, "float");
  25. }
  26. else if(is_type(obj, vm->tp_bool)){
  27. return py_var(vm, "bool");
  28. }
  29. else if(is_type(obj, vm->tp_dict)){
  30. return py_var(vm, "dict");
  31. }
  32. else if(is_type(obj, vm->tp_list)){
  33. return py_var(vm, "list");
  34. }
  35. else if(is_type(obj, vm->tp_tuple)){
  36. return py_var(vm, "tuple");
  37. }
  38. else{
  39. return py_var(vm, "unknown");
  40. }
  41. });
  42. // Get the "get_type" function
  43. PyObject* f_get_type = type_checker->attr("get_type");
  44. // Create a dictionary
  45. Dict d(vm);
  46. d.set(py_var(vm, "key"), py_var(vm, "value"));
  47. // Create a list
  48. List l;
  49. l.push_back(py_var(vm, "list_element"));
  50. // Declare a two-element tuple
  51. Tuple t(2);
  52. t[0] = py_var(vm, "tuple_element_0");
  53. t[1] = py_var(vm, "tuple_element_1");
  54. // Create different types of objects
  55. PyObject* int_obj = py_var(vm, 1);
  56. PyObject* str_obj = py_var(vm, "hello");
  57. PyObject* float_obj = py_var(vm, 1.0);
  58. PyObject* bool_obj = py_var(vm, true);
  59. PyObject* dict_obj = py_var(vm, std::move(d));
  60. PyObject* list_obj = py_var(vm, std::move(l));
  61. PyObject* tuple_obj = py_var(vm, std::move(t));
  62. // Call the "get_type" function and print type of different objects
  63. std::cout << "Type of 1: " << CAST(Str&, vm->call(f_get_type, int_obj)) << std::endl;
  64. std::cout << "Type of \"hello\": " << CAST(Str&, vm->call(f_get_type, str_obj)) << std::endl;
  65. std::cout << "Type of 1.0: " << CAST(Str&, vm->call(f_get_type, float_obj)) << std::endl;
  66. std::cout << "Type of true: " << CAST(Str&, vm->call(f_get_type, bool_obj)) << std::endl;
  67. std::cout << "Type of {\"key\": \"value\" }: " << CAST(Str&, vm->call(f_get_type, dict_obj)) << std::endl;
  68. std::cout << "Type of [\"list_element\"]: " << CAST(Str&, vm->call(f_get_type, list_obj)) << std::endl;
  69. std::cout << "Type of (\"tuple_element_0\", \"tuple_element_1\"): " << CAST(Str&, vm->call(f_get_type, tuple_obj)) << std::endl;
  70. // Dispose the virtual machine
  71. delete vm;
  72. return 0;
  73. }