This commit is contained in:
BLUELOVETH 2023-05-28 18:54:06 +08:00
parent 0bd413b337
commit b4c221095f
4 changed files with 67 additions and 4 deletions

View File

@ -588,6 +588,23 @@ __NEXT_STEP:;
const Str& spec = CAST(Str&, co_consts[byte.arg]); const Str& spec = CAST(Str&, co_consts[byte.arg]);
PUSH(format(spec, _0)); PUSH(format(spec, _0));
} DISPATCH(); } DISPATCH();
/*****************************************/
TARGET(INC_FAST)
_0 = frame->_locals[byte.arg];
if(_0 == PY_NULL) vm->NameError(co->varnames[byte.arg]);
frame->_locals[byte.arg] = VAR(CAST(i64, _0) + 1);
DISPATCH();
TARGET(DEC_FAST)
_0 = frame->_locals[byte.arg];
if(_0 == PY_NULL) vm->NameError(co->varnames[byte.arg]);
frame->_locals[byte.arg] = VAR(CAST(i64, _0) - 1);
DISPATCH();
// TARGET(INC_GLOBAL)
// _name = StrName(byte.arg);
// _0 = frame->f_globals().try_get(_name);
// if(_0 == nullptr) vm->NameError(_name);
// frame->f_globals().try_set()
#if !PK_ENABLE_COMPUTED_GOTO #if !PK_ENABLE_COMPUTED_GOTO
#if DEBUG_EXTRA_CHECK #if DEBUG_EXTRA_CHECK
default: throw std::runtime_error(fmt(OP_NAMES[byte.op], " is not implemented")); default: throw std::runtime_error(fmt(OP_NAMES[byte.op], " is not implemented"));

View File

@ -736,6 +736,36 @@ __SUBSCR_END:
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());
switch(name_scope()){
case NAME_LOCAL:
int namei = ctx()->add_varname(name);
ctx()->emit(OP_INC_FAST, namei, prev().line);
break;
case NAME_GLOBAL:
ctx()->emit(OP_INC_GLOBAL, name.index, prev().line);
break;
default: SyntaxError(); break;
}
consume_end_stmt();
}
case TK("--"):{
consume(TK("@id"));
StrName name(prev().sv());
switch(name_scope()){
case NAME_LOCAL:
int namei = ctx()->add_varname(name);
ctx()->emit(OP_DEC_FAST, namei, prev().line);
break;
case NAME_GLOBAL:
ctx()->emit(OP_DEC_GLOBAL, name.index, prev().line);
break;
default: SyntaxError(); break;
}
consume_end_stmt();
}
case TK("assert"): case TK("assert"):
EXPR_TUPLE(false); EXPR_TUPLE(false);
ctx()->emit(OP_ASSERT, BC_NOARG, kw_line); ctx()->emit(OP_ASSERT, BC_NOARG, kw_line);

View File

@ -21,6 +21,7 @@ constexpr const char* kTokens[] = {
/*****************************************/ /*****************************************/
".", ",", ":", ";", "#", "(", ")", "[", "]", "{", "}", ".", ",", ":", ";", "#", "(", ")", "[", "]", "{", "}",
"**", "=", ">", "<", "...", "->", "?", "@", "==", "!=", ">=", "<=", "**", "=", ">", "<", "...", "->", "?", "@", "==", "!=", ">=", "<=",
"++", "--",
/** SPEC_BEGIN **/ /** SPEC_BEGIN **/
"$goto", "$label", "$goto", "$label",
/** KW_BEGIN **/ /** KW_BEGIN **/
@ -411,7 +412,13 @@ struct Lexer {
return true; return true;
} }
case '=': add_token_2('=', TK("="), TK("==")); return true; case '=': add_token_2('=', TK("="), TK("==")); return true;
case '+': add_token_2('=', TK("+"), TK("+=")); return true; case '+':
if(matchchar('+')){
add_token(TK("++"));
}else{
add_token_2('=', TK("+"), TK("+="));
}
return true;
case '>': { case '>': {
if(matchchar('=')) add_token(TK(">=")); if(matchchar('=')) add_token(TK(">="));
else if(matchchar('>')) add_token_2('=', TK(">>"), TK(">>=")); else if(matchchar('>')) add_token_2('=', TK(">>"), TK(">>="));
@ -425,9 +432,13 @@ struct Lexer {
return true; return true;
} }
case '-': { case '-': {
if(matchchar('-')){
add_token(TK("--"));
}else{
if(matchchar('=')) add_token(TK("-=")); if(matchchar('=')) add_token(TK("-="));
else if(matchchar('>')) add_token(TK("->")); else if(matchchar('>')) add_token(TK("->"));
else add_token(TK("-")); else add_token(TK("-"));
}
return true; return true;
} }
case '!': case '!':

View File

@ -115,4 +115,9 @@ OPCODE(RE_RAISE)
/**************************/ /**************************/
OPCODE(SETUP_DOCSTRING) OPCODE(SETUP_DOCSTRING)
OPCODE(FORMAT_STRING) OPCODE(FORMAT_STRING)
/**************************/
OPCODE(INC_FAST)
OPCODE(DEC_FAST)
OPCODE(INC_GLOBAL)
OPCODE(DEC_GLOBAL)
#endif #endif