change enum name in C++ code back

This commit is contained in:
方而静 2024-06-11 19:49:48 +08:00
parent e82517bf88
commit 1ac790dfb5
Signed by: szTom
GPG Key ID: 072D999D60C6473C
12 changed files with 43 additions and 43 deletions

View File

@ -51,7 +51,7 @@ struct Compiler {
} }
CodeEmitContext* ctx() noexcept{ return &contexts.back(); } CodeEmitContext* ctx() noexcept{ return &contexts.back(); }
pkpy_CompileMode mode() const noexcept{ return lexer.src->mode; } CompileMode mode() const noexcept{ return lexer.src->mode; }
NameScope name_scope() const noexcept; NameScope name_scope() const noexcept;
CodeObject_ push_global_context() noexcept; CodeObject_ push_global_context() noexcept;
@ -133,7 +133,7 @@ struct Compiler {
} }
public: public:
Compiler(VM* vm, std::string_view source, const Str& filename, pkpy_CompileMode mode, bool unknown_global_scope = false) noexcept; Compiler(VM* vm, std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope = false) noexcept;
[[nodiscard]] Error* compile(CodeObject_* out) noexcept; [[nodiscard]] Error* compile(CodeObject_* out) noexcept;
~Compiler(); ~Compiler();
}; };

View File

@ -281,9 +281,9 @@ public:
#endif #endif
#if PK_REGION("Source Execution Methods") #if PK_REGION("Source Execution Methods")
CodeObject_ compile(std::string_view source, const Str& filename, pkpy_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, pkpy_CompileMode mode); Str precompile(std::string_view source, const Str& filename, CompileMode mode);
PyVar exec(std::string_view source, Str filename, pkpy_CompileMode mode, PyObject* _module=nullptr); PyVar exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module=nullptr);
PyVar exec(std::string_view source); PyVar exec(std::string_view source);
PyVar eval(std::string_view source); PyVar eval(std::string_view source);

View File

@ -17,7 +17,7 @@ enum {
}; };
struct SourceData : public pkpy_SourceData { struct SourceData : public pkpy_SourceData {
SourceData(std::string_view source, const Str& filename, pkpy_CompileMode mode) { SourceData(std::string_view source, const Str& filename, CompileMode mode) {
pkpy_SourceData__ctor(this, source.data(), source.size(), &filename, mode); pkpy_SourceData__ctor(this, source.data(), source.size(), &filename, mode);
} }

View File

@ -179,7 +179,7 @@ bool Compiler::match_newlines(bool* need_more_lines) noexcept{
consumed = true; consumed = true;
} }
if(need_more_lines) { if(need_more_lines) {
*need_more_lines = (mode() == PK_REPL_MODE && curr().type == TK("@eof")); *need_more_lines = (mode() == REPL_MODE && curr().type == TK("@eof"));
} }
return consumed; return consumed;
} }
@ -1040,7 +1040,7 @@ Error* Compiler::compile_stmt() noexcept{
/*************************************************/ /*************************************************/
case TK("=="): { case TK("=="): {
consume(TK("@id")); consume(TK("@id"));
if(mode() != PK_EXEC_MODE) return SyntaxError("'label' is only available in PK_EXEC_MODE"); if(mode() != EXEC_MODE) return SyntaxError("'label' is only available in EXEC_MODE");
if(!ctx()->add_label(prev().str())) { if(!ctx()->add_label(prev().str())) {
Str escaped(prev().str().escape()); Str escaped(prev().str().escape());
return SyntaxError("label %s already exists", escaped.c_str()); return SyntaxError("label %s already exists", escaped.c_str());
@ -1050,7 +1050,7 @@ Error* Compiler::compile_stmt() noexcept{
} break; } break;
case TK("->"): case TK("->"):
consume(TK("@id")); consume(TK("@id"));
if(mode() != PK_EXEC_MODE) return SyntaxError("'goto' is only available in PK_EXEC_MODE"); if(mode() != EXEC_MODE) return SyntaxError("'goto' is only available in EXEC_MODE");
ctx()->emit_(OP_GOTO, StrName(prev().sv()).index, prev().line); ctx()->emit_(OP_GOTO, StrName(prev().sv()).index, prev().line);
consume_end_stmt(); consume_end_stmt();
break; break;
@ -1081,7 +1081,7 @@ Error* Compiler::compile_stmt() noexcept{
} }
if(!is_typed_name) { if(!is_typed_name) {
ctx()->s_emit_top(); ctx()->s_emit_top();
if((mode() == PK_CELL_MODE || mode() == PK_REPL_MODE) && name_scope() == NAME_GLOBAL) { if((mode() == CELL_MODE || mode() == REPL_MODE) && name_scope() == NAME_GLOBAL) {
ctx()->emit_(OP_PRINT_EXPR, BC_NOARG, BC_KEEPLINE); ctx()->emit_(OP_PRINT_EXPR, BC_NOARG, BC_KEEPLINE);
} else { } else {
ctx()->emit_(OP_POP_TOP, BC_NOARG, BC_KEEPLINE); ctx()->emit_(OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
@ -1280,7 +1280,7 @@ Error* Compiler::read_literal(PyVar* out) noexcept{
} }
} }
Compiler::Compiler(VM* vm, std::string_view source, const Str& filename, pkpy_CompileMode mode, bool unknown_global_scope) noexcept: Compiler::Compiler(VM* vm, std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope) noexcept:
lexer(vm, std::make_shared<SourceData>(source, filename, mode)){ lexer(vm, std::make_shared<SourceData>(source, filename, mode)){
this->vm = vm; this->vm = vm;
this->unknown_global_scope = unknown_global_scope; this->unknown_global_scope = unknown_global_scope;
@ -1306,7 +1306,7 @@ Error* Compiler::compile(CodeObject_* out) noexcept{
advance(); // skip @sof, so prev() is always valid advance(); // skip @sof, so prev() is always valid
match_newlines(); // skip possible leading '\n' match_newlines(); // skip possible leading '\n'
if(mode() == PK_EVAL_MODE) { if(mode() == EVAL_MODE) {
check(EXPR_TUPLE()); check(EXPR_TUPLE());
ctx()->s_emit_top(); ctx()->s_emit_top();
consume(TK("@eof")); consume(TK("@eof"));
@ -1314,7 +1314,7 @@ Error* Compiler::compile(CodeObject_* out) noexcept{
check(pop_context()); check(pop_context());
*out = code; *out = code;
return NULL; return NULL;
} else if(mode() == PK_JSON_MODE) { } else if(mode() == JSON_MODE) {
check(EXPR()); check(EXPR());
Expr* e = ctx()->s_popx(); Expr* e = ctx()->s_popx();
if(!e->is_json_object()) return SyntaxError("expect a JSON object, literal or array"); if(!e->is_json_object()) return SyntaxError("expect a JSON object, literal or array");

View File

@ -145,7 +145,7 @@ Error* Lexer::eat_name() noexcept{
if(length == 0) return SyntaxError("@id contains invalid char"); if(length == 0) return SyntaxError("@id contains invalid char");
std::string_view name(token_start, length); std::string_view name(token_start, length);
if(src->mode == PK_JSON_MODE) { if(src->mode == JSON_MODE) {
if(name == "true") { if(name == "true") {
add_token(TK("True")); add_token(TK("True"));
} else if(name == "false") { } else if(name == "false") {
@ -238,7 +238,7 @@ Error* Lexer::eat_string_until(char quote, bool raw, Str* out) noexcept{
break; break;
} }
if(c == '\0') { if(c == '\0') {
if(quote3 && src->mode == PK_REPL_MODE) return NeedMoreLines(); if(quote3 && src->mode == REPL_MODE) return NeedMoreLines();
return SyntaxError("EOL while scanning string literal"); return SyntaxError("EOL while scanning string literal");
} }
if(c == '\n') { if(c == '\n') {
@ -375,7 +375,7 @@ Error* Lexer::lex_one_token(bool* eof) noexcept{
// line continuation character // line continuation character
char c = eatchar_include_newline(); char c = eatchar_include_newline();
if(c != '\n') { if(c != '\n') {
if(src->mode == PK_REPL_MODE && c == '\0') return NeedMoreLines(); if(src->mode == REPL_MODE && c == '\0') return NeedMoreLines();
return SyntaxError("expected newline after line continuation character"); return SyntaxError("expected newline after line continuation character");
} }
eat_spaces(); eat_spaces();

View File

@ -792,7 +792,7 @@ PyVar VM::__run_top_frame() {
auto it = __cached_codes.try_get(string); auto it = __cached_codes.try_get(string);
CodeObject_ code; CodeObject_ code;
if(it == nullptr) { if(it == nullptr) {
code = vm->compile(string, "<eval>", PK_EVAL_MODE, true); code = vm->compile(string, "<eval>", EVAL_MODE, true);
__cached_codes.insert(string, code); __cached_codes.insert(string, code);
} else { } else {
code = *it; code = *it;

View File

@ -173,7 +173,7 @@ bool VM::issubclass(Type cls, Type base) {
return false; return false;
} }
PyVar VM::exec(std::string_view source, Str filename, pkpy_CompileMode mode, PyObject* _module) { PyVar VM::exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module) {
if(_module == nullptr) _module = _main; if(_module == nullptr) _module = _main;
try { try {
#if PK_DEBUG_PRECOMPILED_EXEC == 1 #if PK_DEBUG_PRECOMPILED_EXEC == 1
@ -197,9 +197,9 @@ PyVar VM::exec(std::string_view source, Str filename, pkpy_CompileMode mode, PyO
return nullptr; return nullptr;
} }
PyVar VM::exec(std::string_view source) { return exec(source, "main.py", PK_EXEC_MODE); } PyVar VM::exec(std::string_view source) { return exec(source, "main.py", EXEC_MODE); }
PyVar VM::eval(std::string_view source) { return exec(source, "<eval>", PK_EVAL_MODE); } PyVar VM::eval(std::string_view source) { return exec(source, "<eval>", EVAL_MODE); }
PyObject* VM::new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled, PyTypeInfo::Vt vt) { PyObject* VM::new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled, PyTypeInfo::Vt vt) {
PyObject* obj = heap._new<Type>(tp_type, Type(_all_types.size())); PyObject* obj = heap._new<Type>(tp_type, Type(_all_types.size()));
@ -391,7 +391,7 @@ PyObject* VM::py_import(Str path, bool throw_err) {
// _lazy_modules.erase(it); // no need to erase // _lazy_modules.erase(it); // no need to erase
} }
auto _ = __import_context.scope(path, is_init); auto _ = __import_context.scope(path, is_init);
CodeObject_ code = compile(source, filename, PK_EXEC_MODE); CodeObject_ code = compile(source, filename, EXEC_MODE);
Str name_cpnt = path_cpnts.back(); Str name_cpnt = path_cpnts.back();
path_cpnts.pop_back(); path_cpnts.pop_back();
@ -606,12 +606,12 @@ PyVar VM::__py_exec_internal(const CodeObject_& code, PyVar globals, PyVar local
} }
void VM::py_exec(std::string_view source, PyVar globals, PyVar locals) { void VM::py_exec(std::string_view source, PyVar globals, PyVar locals) {
CodeObject_ code = vm->compile(source, "<exec>", PK_EXEC_MODE, true); CodeObject_ code = vm->compile(source, "<exec>", EXEC_MODE, true);
__py_exec_internal(code, globals, locals); __py_exec_internal(code, globals, locals);
} }
PyVar VM::py_eval(std::string_view source, PyVar globals, PyVar locals) { PyVar VM::py_eval(std::string_view source, PyVar globals, PyVar locals) {
CodeObject_ code = vm->compile(source, "<eval>", PK_EVAL_MODE, true); CodeObject_ code = vm->compile(source, "<eval>", EVAL_MODE, true);
return __py_exec_internal(code, globals, locals); return __py_exec_internal(code, globals, locals);
} }
@ -1358,7 +1358,7 @@ PyObject* VM::bind(PyObject* obj, const char* sig, const char* docstring, Native
int length = snprintf(buffer, sizeof(buffer), "def %s : pass", sig); int length = snprintf(buffer, sizeof(buffer), "def %s : pass", sig);
std::string_view source(buffer, length); std::string_view source(buffer, length);
// fn(a, b, *c, d=1) -> None // fn(a, b, *c, d=1) -> None
CodeObject_ co = compile(source, "<bind>", PK_EXEC_MODE); CodeObject_ co = compile(source, "<bind>", EXEC_MODE);
assert(co->func_decls.size() == 1); assert(co->func_decls.size() == 1);
FuncDecl_ decl = co->func_decls[0]; FuncDecl_ decl = co->func_decls[0];
@ -1805,12 +1805,12 @@ void VM::__breakpoint() {
std::string arg = line.substr(space + 1); std::string arg = line.substr(space + 1);
if(arg.empty()) continue; // ignore empty command if(arg.empty()) continue; // ignore empty command
if(cmd == "p" || cmd == "print") { if(cmd == "p" || cmd == "print") {
CodeObject_ code = compile(arg, "<stdin>", PK_EVAL_MODE, true); CodeObject_ code = compile(arg, "<stdin>", EVAL_MODE, true);
PyVar retval = vm->_exec(code.get(), frame_0->_module, frame_0->_callable, frame_0->_locals); PyVar retval = vm->_exec(code.get(), frame_0->_module, frame_0->_callable, frame_0->_locals);
stdout_write(vm->py_repr(retval)); stdout_write(vm->py_repr(retval));
stdout_write("\n"); stdout_write("\n");
} else if(cmd == "!") { } else if(cmd == "!") {
CodeObject_ code = compile(arg, "<stdin>", PK_EXEC_MODE, true); CodeObject_ code = compile(arg, "<stdin>", EXEC_MODE, true);
vm->_exec(code.get(), frame_0->_module, frame_0->_callable, frame_0->_locals); vm->_exec(code.get(), frame_0->_module, frame_0->_callable, frame_0->_locals);
} }
continue; continue;

View File

@ -107,7 +107,7 @@ void add_module_json(VM* vm) {
} else { } else {
sv = CAST(Str&, args[0]).sv(); sv = CAST(Str&, args[0]).sv();
} }
CodeObject_ code = vm->compile(sv, "<json>", PK_JSON_MODE); CodeObject_ code = vm->compile(sv, "<json>", JSON_MODE);
return vm->_exec(code, vm->callstack.top()._module); return vm->_exec(code, vm->callstack.top()._module);
}); });
@ -229,7 +229,7 @@ void add_module_dis(VM* vm) {
PyVar obj = args[0]; PyVar obj = args[0];
if(is_type(obj, vm->tp_str)) { if(is_type(obj, vm->tp_str)) {
const Str& source = CAST(Str, obj); const Str& source = CAST(Str, obj);
code = vm->compile(source, "<dis>", PK_EXEC_MODE); code = vm->compile(source, "<dis>", EXEC_MODE);
} }
PyVar f = obj; PyVar f = obj;
if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, obj).func; if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, obj).func;
@ -246,7 +246,7 @@ void add_module_gc(VM* vm) {
void add_module_enum(VM* vm) { void add_module_enum(VM* vm) {
PyObject* mod = vm->new_module("enum"); PyObject* mod = vm->new_module("enum");
CodeObject_ code = vm->compile(kPythonLibs__enum, "enum.py", PK_EXEC_MODE); CodeObject_ code = vm->compile(kPythonLibs__enum, "enum.py", EXEC_MODE);
vm->_exec(code, mod); vm->_exec(code, mod);
PyVar Enum = mod->attr("Enum"); PyVar Enum = mod->attr("Enum");
vm->_all_types[PK_OBJ_GET(Type, Enum)].on_end_subclass = [](VM* vm, PyTypeInfo* new_ti) { vm->_all_types[PK_OBJ_GET(Type, Enum)].on_end_subclass = [](VM* vm, PyTypeInfo* new_ti) {

View File

@ -226,11 +226,11 @@ void __init_builtins(VM* _vm) {
const Str& filename = CAST(Str&, args[1]); const Str& filename = CAST(Str&, args[1]);
const Str& mode = CAST(Str&, args[2]); const Str& mode = CAST(Str&, args[2]);
if(mode == "exec") { if(mode == "exec") {
return VAR(vm->precompile(source, filename, PK_EXEC_MODE)); return VAR(vm->precompile(source, filename, EXEC_MODE));
} else if(mode == "eval") { } else if(mode == "eval") {
return VAR(vm->precompile(source, filename, PK_EVAL_MODE)); return VAR(vm->precompile(source, filename, EVAL_MODE));
} else if(mode == "single") { } else if(mode == "single") {
return VAR(vm->precompile(source, filename, PK_CELL_MODE)); return VAR(vm->precompile(source, filename, CELL_MODE));
} else { } else {
vm->ValueError("compile() mode must be 'exec', 'eval' or 'single'"); vm->ValueError("compile() mode must be 'exec', 'eval' or 'single'");
return vm->None; return vm->None;
@ -1672,12 +1672,12 @@ void VM::__post_init_builtin_types() {
try { try {
// initialize dummy func_decl for exec/eval // initialize dummy func_decl for exec/eval
CodeObject_ dynamic_co = compile("def _(): pass", "<dynamic>", PK_EXEC_MODE); CodeObject_ dynamic_co = compile("def _(): pass", "<dynamic>", EXEC_MODE);
__dynamic_func_decl = dynamic_co->func_decls[0]; __dynamic_func_decl = dynamic_co->func_decls[0];
// initialize builtins // initialize builtins
CodeObject_ code = compile(kPythonLibs_builtins, "<builtins>", PK_EXEC_MODE); CodeObject_ code = compile(kPythonLibs_builtins, "<builtins>", EXEC_MODE);
this->_exec(code, this->builtins); this->_exec(code, this->builtins);
code = compile(kPythonLibs__set, "<set>", PK_EXEC_MODE); code = compile(kPythonLibs__set, "<set>", EXEC_MODE);
this->_exec(code, this->builtins); this->_exec(code, this->builtins);
} catch(TopLevelException e) { } catch(TopLevelException e) {
std::cerr << e.summary() << std::endl; std::cerr << e.summary() << std::endl;
@ -1704,7 +1704,7 @@ void VM::__post_init_builtin_types() {
#endif #endif
} }
CodeObject_ VM::compile(std::string_view source, const Str& filename, pkpy_CompileMode mode, bool unknown_global_scope) { CodeObject_ VM::compile(std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope) {
Compiler compiler(this, source, filename, mode, unknown_global_scope); Compiler compiler(this, source, filename, mode, unknown_global_scope);
CodeObject_ code; CodeObject_ code;
Error* err = compiler.compile(&code); Error* err = compiler.compile(&code);
@ -1726,7 +1726,7 @@ void VM::__compile_error(Error* err){
_error(__last_exception); _error(__last_exception);
} }
Str VM::precompile(std::string_view source, const Str& filename, pkpy_CompileMode mode) { Str VM::precompile(std::string_view source, const Str& filename, CompileMode mode) {
Compiler compiler(this, source, filename, mode, false); Compiler compiler(this, source, filename, mode, false);
Str out; Str out;
Error* err = compiler.lexer.precompile(&out); Error* err = compiler.lexer.precompile(&out);

View File

@ -60,7 +60,7 @@ bool pkpy_exec(pkpy_vm* vm_handle, const char* source) {
PK_ASSERT_NO_ERROR() PK_ASSERT_NO_ERROR()
PyVar res; PyVar res;
PK_PROTECTED( PK_PROTECTED(
CodeObject_ code = vm->compile(source, "main.py", PK_EXEC_MODE); CodeObject_ code = vm->compile(source, "main.py", EXEC_MODE);
res = vm->_exec(code, vm->_main); res = vm->_exec(code, vm->_main);
) )
return res != nullptr; return res != nullptr;
@ -76,7 +76,7 @@ bool pkpy_exec_2(pkpy_vm* vm_handle, const char* source, const char* filename, i
}else{ }else{
mod = vm->_modules[module].get(); // may raise mod = vm->_modules[module].get(); // may raise
} }
CodeObject_ code = vm->compile(source, filename, (pkpy_CompileMode)mode); CodeObject_ code = vm->compile(source, filename, (CompileMode)mode);
res = vm->_exec(code, mod); res = vm->_exec(code, mod);
) )
return res != nullptr; return res != nullptr;
@ -417,7 +417,7 @@ bool pkpy_eval(pkpy_vm* vm_handle, const char* source) {
VM* vm = (VM*)vm_handle; VM* vm = (VM*)vm_handle;
PK_ASSERT_NO_ERROR() PK_ASSERT_NO_ERROR()
PK_PROTECTED( PK_PROTECTED(
CodeObject_ co = vm->compile(source, "<eval>", PK_EVAL_MODE); CodeObject_ co = vm->compile(source, "<eval>", EVAL_MODE);
PyVar ret = vm->_exec(co, vm->_main); PyVar ret = vm->_exec(co, vm->_main);
vm->s_data.push(ret); vm->s_data.push(ret);
) )

View File

@ -12,7 +12,7 @@ REPL::REPL(VM* vm) : vm(vm) {
} }
bool REPL::input(std::string line) { bool REPL::input(std::string line) {
pkpy_CompileMode mode = PK_REPL_MODE; CompileMode mode = REPL_MODE;
if(need_more_lines) { if(need_more_lines) {
buffer += line; buffer += line;
buffer += '\n'; buffer += '\n';
@ -25,7 +25,7 @@ bool REPL::input(std::string line) {
need_more_lines = 0; need_more_lines = 0;
line = buffer; line = buffer;
buffer.clear(); buffer.clear();
mode = PK_CELL_MODE; mode = CELL_MODE;
} else { } else {
return true; return true;
} }

View File

@ -9,7 +9,7 @@ set_policy("build.warning", true)
option("pk_enable_profiler") option("pk_enable_profiler")
set_default(false) set_default(false)
add_defines("PK_ENABLE_PROFILER=1") add_defines("PK_ENABLE_PROFILER=1")
target("main") target("main")
set_default(true) set_default(true)
set_kind("binary") set_kind("binary")