diff --git a/src/main.cpp b/src/main.cpp index 91a1738e..948a4374 100644 --- a/src/main.cpp +++ b/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); } } diff --git a/src/pocketpy.h b/src/pocketpy.h index 58e2e9f9..fe3e0940 100644 --- a/src/pocketpy.h +++ b/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_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; } } \ No newline at end of file diff --git a/src/vm.h b/src/vm.h index 92fe78f6..4208e53b 100644 --- a/src/vm.h +++ b/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; } }