mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
add compile
This commit is contained in:
parent
43fb6a966b
commit
1cc24d40a2
@ -168,6 +168,8 @@ public:
|
|||||||
bool issubclass(Type cls, Type base);
|
bool issubclass(Type cls, Type base);
|
||||||
|
|
||||||
CodeObject_ compile(std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
|
CodeObject_ compile(std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
|
||||||
|
Str precompile(std::string_view source, const Str& filename, CompileMode mode);
|
||||||
|
|
||||||
PyObject* exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module=nullptr);
|
PyObject* exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module=nullptr);
|
||||||
PyObject* exec(std::string_view source);
|
PyObject* exec(std::string_view source);
|
||||||
PyObject* eval(std::string_view source);
|
PyObject* eval(std::string_view source);
|
||||||
|
@ -1244,6 +1244,7 @@ __EAT_DOTS_END:
|
|||||||
}else if constexpr(std::is_same_v<T, Str>){
|
}else if constexpr(std::is_same_v<T, Str>){
|
||||||
ss << 'S';
|
ss << 'S';
|
||||||
for(char c: arg) ss.write_hex((unsigned char)c);
|
for(char c: arg) ss.write_hex((unsigned char)c);
|
||||||
|
ss.write_hex((unsigned char)0);
|
||||||
}
|
}
|
||||||
ss << '\n';
|
ss << '\n';
|
||||||
}, token.value);
|
}, token.value);
|
||||||
|
@ -208,6 +208,22 @@ void init_builtins(VM* _vm) {
|
|||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_vm->bind(_vm->builtins, "compile(source: str, filename: str, mode: str) -> str", [](VM* vm, ArgsView args) {
|
||||||
|
const Str& source = CAST(Str&, args[0]);
|
||||||
|
const Str& filename = CAST(Str&, args[1]);
|
||||||
|
const Str& mode = CAST(Str&, args[2]);
|
||||||
|
if(mode == "exec"){
|
||||||
|
return VAR(vm->precompile(source, filename, EXEC_MODE));
|
||||||
|
}else if(mode == "eval"){
|
||||||
|
return VAR(vm->precompile(source, filename, EVAL_MODE));
|
||||||
|
}else if(mode == "single"){
|
||||||
|
return VAR(vm->precompile(source, filename, CELL_MODE));
|
||||||
|
}else{
|
||||||
|
vm->ValueError("compile() mode must be 'exec', 'eval' or 'single'");
|
||||||
|
return vm->None;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
_vm->bind(_vm->builtins, "exit(code=0)", [](VM* vm, ArgsView args) {
|
_vm->bind(_vm->builtins, "exit(code=0)", [](VM* vm, ArgsView args) {
|
||||||
std::exit(CAST(int, args[0]));
|
std::exit(CAST(int, args[0]));
|
||||||
return vm->None;
|
return vm->None;
|
||||||
@ -1643,9 +1659,16 @@ CodeObject_ VM::compile(std::string_view source, const Str& filename, CompileMod
|
|||||||
try{
|
try{
|
||||||
return compiler.compile();
|
return compiler.compile();
|
||||||
}catch(const Exception& e){
|
}catch(const Exception& e){
|
||||||
#if PK_DEBUG_FULL_EXCEPTION
|
_error(e.self());
|
||||||
std::cerr << e.summary() << std::endl;
|
return nullptr;
|
||||||
#endif
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Str VM::precompile(std::string_view source, const Str& filename, CompileMode mode){
|
||||||
|
Compiler compiler(this, source, filename, mode, false);
|
||||||
|
try{
|
||||||
|
return compiler.precompile();
|
||||||
|
}catch(const Exception& e){
|
||||||
_error(e.self());
|
_error(e.self());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user