blueloveTH 3 лет назад
Родитель
Сommit
05369804a9
4 измененных файлов с 17 добавлено и 15 удалено
  1. 6 3
      src/compiler.h
  2. 0 10
      src/pocketpy.h
  3. 0 1
      src/str.h
  4. 11 1
      src/vm.h

+ 6 - 3
src/compiler.h

@@ -135,7 +135,7 @@ public:
     }
 
     void eatNumber() {
-        static const std::regex pattern("^[+-]?([0-9]+)(\\.[0-9]+)?");
+        static const std::regex pattern("^([0-9]+)(\\.[0-9]+)?");
         std::smatch m;
 
         const char* i = parser->token_start;
@@ -186,8 +186,6 @@ public:
                 case '<': parser->setNextTwoCharToken('=', TK("<"), TK("<=")); return;
                 case '+': parser->setNextTwoCharToken('=', TK("+"), TK("+=")); return;
                 case '-': {
-                    // if(isdigit(parser->peekChar())) eatNumber();
-                    // we cannot treat it as literal number, since we will fail on f(n-1) case
                     parser->setNextTwoCharToken('=', TK("-"), TK("-="));
                     return;
                 }
@@ -826,6 +824,11 @@ __LISTCOMP:
     }
 
     PyVar consumeLiteral(){
+        if(match(TK("-"))){
+            consume(TK("@num"));
+            PyVar val = parser->previous.value;
+            return vm->numNegated(val);
+        }
         if(match(TK("@num"))) goto __LITERAL_EXIT;
         if(match(TK("@str"))) goto __LITERAL_EXIT;
         if(match(TK("True"))) goto __LITERAL_EXIT;

+ 0 - 10
src/pocketpy.h

@@ -196,12 +196,6 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->PyInt(vm->PyInt_AS_C(args[0]) % vm->PyInt_AS_C(args[1]));
     });
 
-    _vm->bindMethod("int", "__neg__", [](VM* vm, PyVarList args) {
-        if(!args[0]->isType(vm->_tp_int))
-            vm->typeError("unsupported operand type(s) for " "-" );
-        return vm->PyInt(-1 * vm->PyInt_AS_C(args[0]));
-    });
-
     _vm->bindMethod("int", "__repr__", [](VM* vm, PyVarList args) {
         return vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0])));
     });
@@ -249,10 +243,6 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->None;
     });
 
-    _vm->bindMethod("float", "__neg__", [](VM* vm, PyVarList args) {
-        return vm->PyFloat(-1.0 * vm->PyFloat_AS_C(args[0]));
-    });
-
     _vm->bindMethod("float", "__repr__", [](VM* vm, PyVarList args) {
         _Float val = vm->PyFloat_AS_C(args[0]);
         if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val));

+ 0 - 1
src/str.h

@@ -152,7 +152,6 @@ const _Str& __new__ = _Str("__new__");
 const _Str& __iter__ = _Str("__iter__");
 const _Str& __str__ = _Str("__str__");
 const _Str& __repr__ = _Str("__repr__");
-const _Str& __neg__ = _Str("__neg__");
 const _Str& __module__ = _Str("__module__");
 const _Str& __getitem__ = _Str("__getitem__");
 const _Str& __setitem__ = _Str("__setitem__");

+ 11 - 1
src/vm.h

@@ -171,7 +171,7 @@ private:
             case OP_UNARY_NEGATIVE:
                 {
                     PyVar obj = frame->popValue(this);
-                    frame->push(call(obj, __neg__, {}));
+                    frame->push(numNegated(obj));
                 } break;
             case OP_UNARY_NOT:
                 {
@@ -586,6 +586,16 @@ public:
         UNREACHABLE();
     }
 
+    PyVar numNegated(const PyVar& obj){
+        if (obj->isType(_tp_int)){
+            return PyInt(-PyInt_AS_C(obj));
+        }else if(obj->isType(_tp_float)){
+            return PyFloat(-PyFloat_AS_C(obj));
+        }
+        typeError("unsupported operand type(s) for -");
+        return nullptr;
+    }
+
     int normalizedIndex(int index, int size){
         if(index < 0) index += size;
         if(index < 0 || index >= size){