mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
some fix
This commit is contained in:
parent
01bce1daf8
commit
05369804a9
@ -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;
|
||||
|
@ -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));
|
||||
|
@ -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__");
|
||||
|
12
src/vm.h
12
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){
|
||||
|
Loading…
x
Reference in New Issue
Block a user