change to 0-based

This commit is contained in:
blueloveTH 2024-08-30 15:37:13 +08:00
parent 4e5021089c
commit 108d2fe969
2 changed files with 8 additions and 8 deletions

View File

@ -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;

View File

@ -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);
}
}