BLUELOVETH 2 ani în urmă
părinte
comite
b4c221095f
4 a modificat fișierele cu 67 adăugiri și 4 ștergeri
  1. 17 0
      src/ceval.h
  2. 30 0
      src/compiler.h
  3. 15 4
      src/lexer.h
  4. 5 0
      src/opcodes.h

+ 17 - 0
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"));

+ 30 - 0
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);

+ 15 - 4
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 '!':

+ 5 - 0
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