diff --git a/src/compiler.h b/src/compiler.h index 1b28e9cf..c6055b7a 100644 --- a/src/compiler.h +++ b/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; diff --git a/src/pocketpy.h b/src/pocketpy.h index cbbe8ba2..e6c9ce8f 100644 --- a/src/pocketpy.h +++ b/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)); diff --git a/src/str.h b/src/str.h index 17b44f52..aef6ba04 100644 --- a/src/str.h +++ b/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__"); diff --git a/src/vm.h b/src/vm.h index 29e9d6c2..08f6f98b 100644 --- a/src/vm.h +++ b/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){