From 108d2fe969d30f962cc88c82fdccaa852f00535a Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 30 Aug 2024 15:37:13 +0800 Subject: [PATCH] change to 0-based --- include/pocketpy/pocketpy.h | 4 ++-- src/public/modules.c | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 443f23db..15fb556a 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -115,13 +115,13 @@ PK_EXPORT bool py_eval(const char* source, py_Ref module) PY_RAISE PY_RETURN; /// Example: /// `py_newstr(py_r0(), "abc");` /// `py_newint(py_r1(), 123);` -/// `py_smartexec("print(_1, _2)", NULL, py_r0(), py_r1());` +/// `py_smartexec("print(_0, _1)", NULL, py_r0(), py_r1());` /// `// "abc 123" will be printed`. PK_EXPORT bool py_smartexec(const char* source, py_Ref module, ...) PY_RAISE PY_RETURN; /// Evaluate a source string with smart interpretation. /// Example: /// `py_newstr(py_r0(), "abc");` -/// `py_smarteval("len(_1)", NULL, py_r0());` +/// `py_smarteval("len(_)", NULL, py_r0());` /// `int res = py_toint(py_retval());` /// `// res will be 3`. PK_EXPORT bool py_smarteval(const char* source, py_Ref module, ...) PY_RAISE PY_RETURN; diff --git a/src/public/modules.c b/src/public/modules.c index d17c5059..556de346 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -536,14 +536,14 @@ static bool // [globals, locals, code] CodeObject* co = py_touserdata(py_peek(-1)); py_StackRef locals = py_peek(-2); - int max_index = 0; + int max_index = -1; c11__foreach(Bytecode, &co->codes, bc) { if(bc->op == OP_LOAD_NAME) { c11_sv name = py_name2sv(bc->arg); if(name.data[0] != '_') continue; int index; if(name.size == 1) { - index = 1; + index = 0; } else if(name.size == 2 && isdigit(name.data[1])) { index = name.data[1] - '0'; } else { @@ -553,17 +553,17 @@ static bool } } - if(max_index == 0) { return ValueError("no placeholder found in the source"); } + if(max_index == -1) return ValueError("no placeholder found in the source"); - for(int i = 1; i <= max_index; i++) { + for(int i = 0; i <= max_index; i++) { py_Ref val = va_arg(args, py_Ref); char buf[3]; buf[0] = '_'; buf[1] = '0' + i; buf[2] = '\0'; py_dict_setitem_by_str(locals, buf, val); - if(i == 1) { - // _ => _1 + if(i == 0) { + // _ => _0 py_dict_setitem_by_str(locals, "_", val); } }