blueloveTH 3 лет назад
Родитель
Сommit
385002035c
3 измененных файлов с 41 добавлено и 11 удалено
  1. 1 1
      src/main.cpp
  2. 38 8
      src/pocketpy.h
  3. 2 2
      src/vm.h

+ 1 - 1
src/main.cpp

@@ -45,7 +45,7 @@ extern "C" {
 
     __EXPORT
     bool repl_input(const char* line){
-        return pkpy_input_repl(_repl, line);
+        return pkpy_repl_input(_repl, line);
     }
 }
 

+ 38 - 8
src/pocketpy.h

@@ -547,7 +547,24 @@ void __addModuleSys(VM* vm){
     vm->setAttr(mod, "version", vm->PyStr(PK_VERSION));
 }
 
+
 extern "C" {
+    __EXPORT
+    struct PyObjectDump: public PkExportedResource{
+        const char* type;   // "int", "str", "float" ...
+        const char* string; // __str__ representation
+        const char* repr;   // __repr__ representation
+
+        PyObjectDump(const char* _type, const char* _string, const char* _repr):
+            type(strdup(_type)), string(strdup(_string)), repr(strdup(_repr)){}
+
+        ~PyObjectDump(){
+            delete[] type;
+            delete[] string;
+            delete[] repr;
+        }
+    };
+
     __EXPORT
     VM* pkpy_new_vm(PrintFn _stdout, PrintFn _stderr){
         VM* vm = new VM();
@@ -570,9 +587,23 @@ extern "C" {
     }
 
     __EXPORT
-    void pkpy_exec(VM* vm, const char* source){
+    bool pkpy_exec(VM* vm, const char* source){
         _Code code = compile(vm, source, "main.py");
-        if(code != nullptr) vm->exec(code);
+        if(code == nullptr) return false;
+        return vm->exec(code) != nullptr;
+    }
+
+    __EXPORT
+    PyObjectDump* pkpy_eval(VM* vm, const char* source){
+        _Code code = compile(vm, source, "<eval>", EVAL_MODE);
+        if(code == nullptr) return nullptr;
+        PyVar ret = vm->exec(code);
+        if(ret == nullptr) return nullptr;
+        return new PyObjectDump(
+            ret->getTypeName().c_str(),
+            vm->PyStr_AS_C(vm->asStr(ret)).c_str(),
+            vm->PyStr_AS_C(vm->asRepr(ret)).c_str()
+        );
     }
 
     __EXPORT
@@ -581,16 +612,15 @@ extern "C" {
     }
 
     __EXPORT
-    bool pkpy_input_repl(REPL* r, const char* line){
+    bool pkpy_repl_input(REPL* r, const char* line){
         return r->input(line);
     }
 
     __EXPORT
-    void pkpy_add_module(VM* vm, const char* name, const char* source){
+    bool pkpy_add_module(VM* vm, const char* name, const char* source){
         _Code code = compile(vm, source, name + _Str(".py"));
-        if(code != nullptr){
-            PyVar _m = vm->newModule(name);
-            vm->exec(code, _m);
-        }
+        if(code == nullptr) return false;
+        PyVar _m = vm->newModule(name);
+        return vm->exec(code, _m) != nullptr;
     }
 }

+ 2 - 2
src/vm.h

@@ -435,7 +435,7 @@ public:
         return call(getAttr(obj, func), args);
     }
 
-    PyVar exec(const _Code& code, PyVar _module=nullptr){
+    PyVarOrNull exec(const _Code& code, PyVar _module=nullptr){
         if(_module == nullptr) _module = _main;
         try {
             return _exec(code, _module);
@@ -447,7 +447,7 @@ public:
                 _stderr(this, re.what());
             }
             _stderr(this, "\n");
-            return None;
+            return nullptr;
         }
     }