From 0e932f24cca98f3f19315f9fab3491d549e183b3 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 5 Feb 2023 13:29:44 +0000 Subject: [PATCH] Added _toString() --- repl/lib_compound.c | 49 ++++++++++++++++++++++++++++++++++- test/scripts/lib/compound.toy | 10 +++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/repl/lib_compound.c b/repl/lib_compound.c index e514025..2b2ca90 100644 --- a/repl/lib_compound.c +++ b/repl/lib_compound.c @@ -142,6 +142,53 @@ static int nativeToLower(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen return 1; } +static char* toStringUtilObject = NULL; +static void toStringUtil(const char* input) { + int len = strlen(input) + 1; + + if (len > TOY_MAX_STRING_LENGTH) { + len = TOY_MAX_STRING_LENGTH; + } + + toStringUtilObject = TOY_ALLOCATE(char, len); + + snprintf(toStringUtilObject, len, "%s", input); +} + +static int nativeToString(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { + //no arguments + if (arguments->count != 1) { + interpreter->errorOutput("Incorrect number of arguments to _toString\n"); + return -1; + } + + //get the argument + Toy_Literal selfLiteral = Toy_popLiteralArray(arguments); + + //parse to a value + Toy_Literal selfLiteralIdn = selfLiteral; + if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) { + Toy_freeLiteral(selfLiteralIdn); + } + + //print it to a custom function + Toy_printLiteralCustom(selfLiteral, toStringUtil); + + //create the resulting string and push it + Toy_Literal result = TOY_TO_STRING_LITERAL(Toy_createRefString(toStringUtilObject)); //internal copy + + Toy_pushLiteralArray(&interpreter->stack, result); + + //cleanup + TOY_FREE_ARRAY(char, toStringUtilObject, Toy_lengthRefString( TOY_AS_STRING(result) ) + 1); + toStringUtilObject = NULL; + + Toy_freeLiteral(result); + Toy_freeLiteral(selfLiteral); + + return 1; +} + static int nativeToUpper(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { //no arguments if (arguments->count != 1) { @@ -325,7 +372,7 @@ int Toy_hookCompound(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_L // {"_some", native}, //array, dictionary, string // {"_sort", native}, //array {"_toLower", nativeToLower}, //string - // {"_toString", native}, //array, dictionary + {"_toString", nativeToString}, //array, dictionary {"_toUpper", nativeToUpper}, //string {"_trim", nativeTrim}, //string {NULL, NULL} diff --git a/test/scripts/lib/compound.toy b/test/scripts/lib/compound.toy index e4075ba..4ee9c4e 100644 --- a/test/scripts/lib/compound.toy +++ b/test/scripts/lib/compound.toy @@ -32,6 +32,16 @@ import compound; } +//test toString +{ + var a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; + + var s = a.toString(); + + assert s == "[[1,2,3],[4,5,6],[7,8,9]]", "array._toString() failed"; +} + + //test toUpper { assert "Hello World".toUpper() == "HELLO WORLD", "_toUpper() failed";