remove unused bc

This commit is contained in:
blueloveTH 2024-06-07 01:08:05 +08:00
parent 8b2b8ab83a
commit 97d6f384f4
9 changed files with 15 additions and 98 deletions

View File

@ -1,23 +0,0 @@
---
icon: dot
title: Increment Statement
---
pkpy provides `++i` and `--j` statements to operate a simple named `int` variable.
+ `++i` is equivalent to `i+=1`, but much faster
+ `--j` is equivalent to `j-=1`, but much faster
## Example
```python
a = 1
++a
assert a == 2
def f(a):
--a
return a
assert f(3) == 2
```

View File

@ -149,8 +149,4 @@ OPCODE(POP_EXCEPTION)
/**************************/ /**************************/
OPCODE(FORMAT_STRING) OPCODE(FORMAT_STRING)
/**************************/ /**************************/
OPCODE(INC_FAST)
OPCODE(DEC_FAST)
OPCODE(INC_GLOBAL)
OPCODE(DEC_GLOBAL)
#endif #endif

View File

@ -16,7 +16,7 @@ def enumerate(iterable, start=0):
n = start n = start
for elem in iterable: for elem in iterable:
yield n, elem yield n, elem
++n n += 1
def sum(iterable): def sum(iterable):
res = 0 res = 0

File diff suppressed because one or more lines are too long

View File

@ -864,31 +864,6 @@ void Compiler::compile_stmt() {
case TK("try"): compile_try_except(); break; case TK("try"): compile_try_except(); break;
case TK("pass"): consume_end_stmt(); break; case TK("pass"): consume_end_stmt(); break;
/*************************************************/ /*************************************************/
case TK("++"): {
consume(TK("@id"));
StrName name(prev().sv());
NameScope scope = name_scope();
bool is_global = ctx()->global_names.contains(name.sv());
if(is_global) scope = NAME_GLOBAL;
switch(scope) {
case NAME_LOCAL: ctx()->emit_(OP_INC_FAST, ctx()->add_varname(name), prev().line); break;
case NAME_GLOBAL: ctx()->emit_(OP_INC_GLOBAL, name.index, prev().line); break;
default: SyntaxError(); break;
}
consume_end_stmt();
break;
}
case TK("--"): {
consume(TK("@id"));
StrName name(prev().sv());
switch(name_scope()) {
case NAME_LOCAL: ctx()->emit_(OP_DEC_FAST, ctx()->add_varname(name), prev().line); break;
case NAME_GLOBAL: ctx()->emit_(OP_DEC_GLOBAL, name.index, prev().line); break;
default: SyntaxError(); break;
}
consume_end_stmt();
break;
}
case TK("assert"): { case TK("assert"): {
EXPR(); // condition EXPR(); // condition
ctx()->emit_expr(); ctx()->emit_expr();

View File

@ -1127,33 +1127,6 @@ PyVar VM::__run_top_frame() {
} }
DISPATCH() DISPATCH()
/*****************************************/ /*****************************************/
case OP_INC_FAST: {
PyVar* p = &frame->_locals[byte.arg];
if(*p == PY_NULL) vm->NameError(frame->co->varnames[byte.arg]);
*p = VAR(CAST(i64, *p) + 1);
}
DISPATCH()
case OP_DEC_FAST: {
PyVar* p = &frame->_locals[byte.arg];
if(*p == PY_NULL) vm->NameError(frame->co->varnames[byte.arg]);
*p = VAR(CAST(i64, *p) - 1);
}
DISPATCH()
case OP_INC_GLOBAL: {
StrName _name(byte.arg);
PyVar* p = frame->f_globals().try_get_2_likely_found(_name);
if(p == nullptr) vm->NameError(_name);
*p = VAR(CAST(i64, *p) + 1);
}
DISPATCH()
case OP_DEC_GLOBAL: {
StrName _name(byte.arg);
PyVar* p = frame->f_globals().try_get_2_likely_found(_name);
if(p == nullptr) vm->NameError(_name);
*p = VAR(CAST(i64, *p) - 1);
}
DISPATCH()
/*****************************************/
default: PK_UNREACHABLE() default: PK_UNREACHABLE()
} }
} }

View File

@ -758,15 +758,11 @@ static std::string _opcode_argstr(VM* vm, int i, Bytecode byte, const CodeObject
case OP_BEGIN_CLASS: case OP_BEGIN_CLASS:
case OP_GOTO: case OP_GOTO:
case OP_DELETE_GLOBAL: case OP_DELETE_GLOBAL:
case OP_INC_GLOBAL:
case OP_DEC_GLOBAL:
case OP_STORE_CLASS_ATTR: case OP_STORE_CLASS_ATTR:
case OP_FOR_ITER_STORE_GLOBAL: ss << " (" << StrName(byte.arg).sv() << ")"; break; case OP_FOR_ITER_STORE_GLOBAL: ss << " (" << StrName(byte.arg).sv() << ")"; break;
case OP_LOAD_FAST: case OP_LOAD_FAST:
case OP_STORE_FAST: case OP_STORE_FAST:
case OP_DELETE_FAST: case OP_DELETE_FAST:
case OP_INC_FAST:
case OP_DEC_FAST:
case OP_FOR_ITER_STORE_FAST: case OP_FOR_ITER_STORE_FAST:
case OP_LOAD_SUBSCR_FAST: case OP_LOAD_SUBSCR_FAST:
case OP_STORE_SUBSCR_FAST: ss << " (" << co->varnames[byte.arg].sv() << ")"; break; case OP_STORE_SUBSCR_FAST: ss << " (" << co->varnames[byte.arg].sv() << ")"; break;

View File

@ -1,15 +1,15 @@
a = 1 # a = 1
++a # ++a
assert a == 2 # assert a == 2
++a; ++a; --a; # ++a; ++a; --a;
assert a == 3 # assert a == 3
def f(a): # def f(a):
++a # ++a
++a # ++a
--a # --a
return a # return a
assert f(3) == 4 # assert f(3) == 4
assert f(-2) == -1 # assert f(-2) == -1

View File

@ -91,7 +91,7 @@ except UnboundLocalError:
g = 1 g = 1
def f(): def f():
global g global g
++g g += 1
f(); f() f(); f()
assert g == 3 assert g == 3