From b4c221095fd5d4f1fba45d101346bda1fddf337b Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Sun, 28 May 2023 18:54:06 +0800 Subject: [PATCH] ... --- src/ceval.h | 17 +++++++++++++++++ src/compiler.h | 30 ++++++++++++++++++++++++++++++ src/lexer.h | 19 +++++++++++++++---- src/opcodes.h | 5 +++++ 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/ceval.h b/src/ceval.h index 2242f751..7fff5274 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -588,6 +588,23 @@ __NEXT_STEP:; const Str& spec = CAST(Str&, co_consts[byte.arg]); PUSH(format(spec, _0)); } 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 DEBUG_EXTRA_CHECK default: throw std::runtime_error(fmt(OP_NAMES[byte.op], " is not implemented")); diff --git a/src/compiler.h b/src/compiler.h index f63eedf0..1e49a025 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -736,6 +736,36 @@ __SUBSCR_END: case TK("try"): compile_try_except(); 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"): EXPR_TUPLE(false); ctx()->emit(OP_ASSERT, BC_NOARG, kw_line); diff --git a/src/lexer.h b/src/lexer.h index b73e3b81..6055831f 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -21,6 +21,7 @@ constexpr const char* kTokens[] = { /*****************************************/ ".", ",", ":", ";", "#", "(", ")", "[", "]", "{", "}", "**", "=", ">", "<", "...", "->", "?", "@", "==", "!=", ">=", "<=", + "++", "--", /** SPEC_BEGIN **/ "$goto", "$label", /** KW_BEGIN **/ @@ -411,7 +412,13 @@ struct Lexer { 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 '>': { if(matchchar('=')) add_token(TK(">=")); else if(matchchar('>')) add_token_2('=', TK(">>"), TK(">>=")); @@ -425,9 +432,13 @@ struct Lexer { return true; } case '-': { - if(matchchar('=')) add_token(TK("-=")); - else if(matchchar('>')) add_token(TK("->")); - else add_token(TK("-")); + if(matchchar('-')){ + add_token(TK("--")); + }else{ + if(matchchar('=')) add_token(TK("-=")); + else if(matchchar('>')) add_token(TK("->")); + else add_token(TK("-")); + } return true; } case '!': diff --git a/src/opcodes.h b/src/opcodes.h index 778fe15f..22ffbd4f 100644 --- a/src/opcodes.h +++ b/src/opcodes.h @@ -115,4 +115,9 @@ OPCODE(RE_RAISE) /**************************/ OPCODE(SETUP_DOCSTRING) OPCODE(FORMAT_STRING) +/**************************/ +OPCODE(INC_FAST) +OPCODE(DEC_FAST) +OPCODE(INC_GLOBAL) +OPCODE(DEC_GLOBAL) #endif \ No newline at end of file