From ad39476a35c50dfd46331d985521dfc3a151c9bf Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sat, 6 Aug 2022 12:20:04 +0900 Subject: [PATCH] Clean up function, method repring --- src/obj_function.c | 57 ++++++++++-------------------- test/test.krk | 2 +- test/testBoundMethodEdgecases.krk | 6 ++++ test/testClassMethodAssignment.krk | 4 +-- test/testStrReprOrdering.krk | 6 ++++ 5 files changed, 33 insertions(+), 42 deletions(-) diff --git a/src/obj_function.c b/src/obj_function.c index 6b0a267..43223b9 100644 --- a/src/obj_function.c +++ b/src/obj_function.c @@ -22,25 +22,15 @@ static KrkTuple * functionArgs(KrkCodeObject * _self) { } for (short i = 0; i < _self->keywordArgs; ++i) { - struct StringBuilder sb = {0}; - pushStringBuilderStr(&sb, AS_CSTRING(_self->keywordArgNames.values[i]), AS_STRING(_self->keywordArgNames.values[i])->length); - pushStringBuilder(&sb,'='); - tuple->values.values[tuple->values.count++] = finishStringBuilder(&sb); + tuple->values.values[tuple->values.count++] = krk_stringFromFormat("%S=", AS_STRING(_self->keywordArgNames.values[i])); } if (_self->obj.flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_ARGS) { - struct StringBuilder sb = {0}; - pushStringBuilder(&sb, '*'); - pushStringBuilderStr(&sb, AS_CSTRING(_self->requiredArgNames.values[_self->requiredArgs]), AS_STRING(_self->requiredArgNames.values[_self->requiredArgs])->length); - tuple->values.values[tuple->values.count++] = finishStringBuilder(&sb); + tuple->values.values[tuple->values.count++] = krk_stringFromFormat("*%S", AS_STRING(_self->requiredArgNames.values[_self->requiredArgs])); } if (_self->obj.flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_KWS) { - struct StringBuilder sb = {0}; - pushStringBuilder(&sb, '*'); - pushStringBuilder(&sb, '*'); - pushStringBuilderStr(&sb, AS_CSTRING(_self->keywordArgNames.values[_self->keywordArgs]), AS_STRING(_self->keywordArgNames.values[_self->keywordArgs])->length); - tuple->values.values[tuple->values.count++] = finishStringBuilder(&sb); + tuple->values.values[tuple->values.count++] = krk_stringFromFormat("**%S", AS_STRING(_self->keywordArgNames.values[_self->keywordArgs])); } krk_pop(); @@ -151,9 +141,6 @@ KRK_Method(function,_ip_to_line) { KRK_Method(function,__str__) { METHOD_TAKES_NONE(); - struct StringBuilder sb = {0}; - pushStringBuilderStr(&sb, "")); - pushStringBuilderStr(&sb, AS_CSTRING(name), AS_STRING(name)->length); + krk_push(name); - pushStringBuilderStr(&sb," at ", 4); + struct StringBuilder sb = {0}; + krk_pushStringBuilderFormat(&sb, "", AS_STRING(name), (void*)AS_OBJECT(self)); - char address[100]; - size_t len = snprintf(address, 100, "%p", (void*)AS_OBJECT(self)); - pushStringBuilderStr(&sb, address, len); + krk_pop(); - pushStringBuilder(&sb,'>'); - - return finishStringBuilder(&sb); + return krk_finishStringBuilder(&sb); } KRK_Method(function,__file__) { @@ -222,14 +206,12 @@ KRK_Method(codeobject,__str__) { if (!IS_STRING(s)) return NONE_VAL(); krk_push(s); - size_t len = AS_STRING(s)->length + sizeof(""); - char * tmp = malloc(len); - snprintf(tmp, len, "", AS_CSTRING(s)); - s = OBJECT_VAL(krk_copyString(tmp,len-1)); - free(tmp); + struct StringBuilder sb = {0}; + krk_pushStringBuilderFormat(&sb, "", AS_STRING(s), (void*)self); krk_pop(); - return s; + + return krk_finishStringBuilder(&sb); } KRK_Method(codeobject,_ip_to_line) { @@ -329,17 +311,14 @@ KRK_Method(method,__str__) { if (!IS_STRING(s)) return NONE_VAL(); krk_push(s); - KrkClass * type = krk_getType(self->receiver); - krk_push(self->receiver); - KrkValue reprVal = krk_callDirect(type->_reprer, 1); + struct StringBuilder sb = {0}; + krk_pushStringBuilderFormat(&sb, "receiver); + if (IS_OBJECT(self->receiver)) krk_pushStringBuilderFormat(&sb, " at %p", (void*)AS_OBJECT(self->receiver)); + krk_pushStringBuilder(&sb, '>'); - size_t len = AS_STRING(s)->length + AS_STRING(reprVal)->length + sizeof("") + 1; - char * tmp = malloc(len); - snprintf(tmp, len, "", AS_CSTRING(s), AS_CSTRING(reprVal)); - s = OBJECT_VAL(krk_copyString(tmp,len-1)); - free(tmp); krk_pop(); - return s; + + return krk_finishStringBuilder(&sb); } KRK_Method(method,__file__) { diff --git a/test/test.krk b/test/test.krk index b1c405f..fd0a2b0 100755 --- a/test/test.krk +++ b/test/test.krk @@ -112,7 +112,7 @@ let test = Test() test.doAThing() test.foo = "bar" print(test.foo) -print('' +method.__str__ = __str__ + class Callable(): def __init__(self,name): self.name = name diff --git a/test/testClassMethodAssignment.krk b/test/testClassMethodAssignment.krk index 21784eb..34c52e6 100644 --- a/test/testClassMethodAssignment.krk +++ b/test/testClassMethodAssignment.krk @@ -14,10 +14,10 @@ def noargs(): Foo.other = other Foo.noargs = noargs -print('' +method.__str__ = __str__ + class Foo(object): def __str__(self): return ''