Browse Source

Merge pull request #283 from 16bit-ykiko/doc

Update README.
BLUELOVETH 1 năm trước cách đây
mục cha
commit
475be8ce80
1 tập tin đã thay đổi với 14 bổ sung20 xóa
  1. 14 20
      README.md

+ 14 - 20
README.md

@@ -91,39 +91,33 @@ python scripts/run_tests.py
 ### Example
 ### Example
 
 
 ```cpp
 ```cpp
-#include "pocketpy.h"
+#include <pybind11/pybind11.h>
 
 
-using namespace pkpy;
+namespace py = pybind11;
 
 
-int main(){
-    // Create a virtual machine
-    VM* vm = new VM();
+int main() {
+    // Start the interpreter
+    py::scoped_interpreter guard{};
 
 
     // Hello world!
     // Hello world!
-    vm->exec("print('Hello world!')");
+    py::exec("print('Hello world!')");
 
 
     // Create a list
     // Create a list
-    vm->exec("a = [1, 2, 3]");
+    py::exec("a = [1, 2, 3]");
 
 
     // Eval the sum of the list
     // Eval the sum of the list
-    PyVar result = vm->eval("sum(a)");
-    std::cout << "Sum of the list: "<< py_cast<int>(vm, result) << std::endl;   // 6
+    auto result = py::eval("sum(a)");
+    std::cout << "Sum of the list: " << result.cast<int>() << std::endl;  // 6
 
 
     // Bindings
     // Bindings
-    vm->bind(vm->_main, "add(a: int, b: 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);
-      });
+    auto m = py::module_::__main__();
+    m.def("add", [](int a, int b) {
+        return a + b;
+    });
 
 
     // Call the function
     // Call the function
-    PyVar f_add = vm->_main->attr("add");
-    result = vm->call(f_add, py_var(vm, 3), py_var(vm, 7));
-    std::cout << "Sum of 2 variables: "<< py_cast<int>(vm, result) << std::endl;   // 10
+    std::cout << "Sum of 2 variables: " << m.attr("add")(1, 2).cast<int>() << std::endl;  // 10
 
 
-    // Dispose the virtual machine
-    delete vm;
     return 0;
     return 0;
 }
 }
 ```
 ```