mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
add import *
This commit is contained in:
parent
7ebf5f1e88
commit
d6451b55d6
@ -301,6 +301,14 @@ PyVar VM::run_frame(Frame* frame){
|
||||
frame->push(*ext_mod);
|
||||
}
|
||||
} continue;
|
||||
case OP_STORE_ALL_NAMES: {
|
||||
PyVar obj = frame->pop_value(this);
|
||||
for(auto& [name, value]: obj->attr().items()){
|
||||
Str s = name.str();
|
||||
if(s.empty() || s[0] == '_') continue;
|
||||
frame->f_globals().set(name, value);
|
||||
}
|
||||
}; continue;
|
||||
case OP_YIELD_VALUE: return _py_op_yield;
|
||||
// TODO: using "goto" inside with block may cause __exit__ not called
|
||||
case OP_WITH_ENTER: call(frame->pop_value(this), __enter__); continue;
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include "vm.h"
|
||||
|
||||
struct CType{
|
||||
PY_CLASS(c, type)
|
||||
PY_CLASS(c, _type)
|
||||
|
||||
const char* name; // must be a literal
|
||||
int size;
|
||||
@ -15,7 +15,7 @@ struct CType{
|
||||
vm->bind_method<0>(type, "__repr__", [](VM* vm, pkpy::Args& args) {
|
||||
CType& self = vm->py_cast<CType>(args[0]);
|
||||
StrStream ss;
|
||||
ss << "<c.type '" << self.name << "'>";
|
||||
ss << "<c._type '" << self.name << "'>";
|
||||
return vm->PyStr(ss.str());
|
||||
});
|
||||
}
|
||||
@ -44,7 +44,7 @@ constexpr CType ctype_t(const char name[]){
|
||||
}
|
||||
|
||||
struct Pointer{
|
||||
PY_CLASS(c, ptr)
|
||||
PY_CLASS(c, _ptr)
|
||||
|
||||
void* ptr;
|
||||
CType _ctype;
|
||||
@ -165,7 +165,7 @@ void add_module_c(VM* vm){
|
||||
Pointer& p = vm->py_cast<Pointer>(args[0]);
|
||||
return vm->new_object<Pointer>(strdup(p.cast<char*>()), ctype_t("char"));
|
||||
}else{
|
||||
vm->TypeError("strdup() argument must be 'str' or 'c.ptr'");
|
||||
vm->TypeError("strdup() argument must be 'str' or 'c._ptr'");
|
||||
return vm->None;
|
||||
}
|
||||
});
|
||||
|
@ -745,6 +745,12 @@ __LISTCOMP:
|
||||
void compile_from_import() {
|
||||
Token tkmodule = _compile_import();
|
||||
consume(TK("import"));
|
||||
if (match(TK("*"))) {
|
||||
if(name_scope() != NAME_GLOBAL) SyntaxError("import * can only be used in global scope");
|
||||
emit(OP_STORE_ALL_NAMES);
|
||||
consume_end_stmt();
|
||||
return;
|
||||
}
|
||||
do {
|
||||
emit(OP_DUP_TOP_VALUE);
|
||||
consume(TK("@id"));
|
||||
@ -971,7 +977,7 @@ __LISTCOMP:
|
||||
consume_end_stmt();
|
||||
// If last op is not an assignment, pop the result.
|
||||
uint8_t last_op = co()->codes.back().op;
|
||||
if( last_op!=OP_STORE_NAME && last_op!=OP_STORE_REF && last_op!=OP_INPLACE_BINARY_OP && last_op!=OP_INPLACE_BITWISE_OP){
|
||||
if( last_op!=OP_STORE_NAME && last_op!=OP_STORE_REF && last_op!=OP_INPLACE_BINARY_OP && last_op!=OP_INPLACE_BITWISE_OP && last_op!=OP_STORE_ALL_NAMES){
|
||||
if(last_op == OP_BUILD_TUPLE_REF) co()->codes.back().op = OP_BUILD_TUPLE;
|
||||
if(mode()==REPL_MODE && name_scope() == NAME_GLOBAL) emit(OP_PRINT_EXPR, -1, true);
|
||||
emit(OP_POP_TOP, -1, true);
|
||||
|
@ -85,5 +85,6 @@ OPCODE(INPLACE_BITWISE_OP)
|
||||
|
||||
OPCODE(SETUP_CLOSURE)
|
||||
OPCODE(SETUP_DECORATOR)
|
||||
OPCODE(STORE_ALL_NAMES)
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user