diff --git a/repl/lib_compound.c b/repl/lib_compound.c index 895bcba..b65e594 100644 --- a/repl/lib_compound.c +++ b/repl/lib_compound.c @@ -629,6 +629,53 @@ static int nativeGetValues(Toy_Interpreter* interpreter, Toy_LiteralArray* argum return 1; } +static int nativeIndexOf(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { + //no arguments + if (arguments->count != 2) { + interpreter->errorOutput("Incorrect number of arguments to _indexOf\n"); + return -1; + } + + //get the args + Toy_Literal valueLiteral = Toy_popLiteralArray(arguments); + Toy_Literal selfLiteral = Toy_popLiteralArray(arguments); + + //parse to value if needed + Toy_Literal selfLiteralIdn = selfLiteral; + if (TOY_IS_IDENTIFIER(selfLiteral) && Toy_parseIdentifierToValue(interpreter, &selfLiteral)) { + Toy_freeLiteral(selfLiteralIdn); + } + + Toy_Literal valueLiteralIdn = valueLiteral; + if (TOY_IS_IDENTIFIER(valueLiteral) && Toy_parseIdentifierToValue(interpreter, &valueLiteral)) { + Toy_freeLiteral(valueLiteralIdn); + } + + //check type + if (!TOY_IS_ARRAY(selfLiteral)) { + interpreter->errorOutput("Incorrect argument type passed to indexOf\n"); + Toy_freeLiteral(selfLiteral); + Toy_freeLiteral(valueLiteral); + return -1; + } + + //search the array for the matching literal + Toy_Literal resultLiteral = TOY_TO_NULL_LITERAL; + for (int i = 0; i < TOY_AS_ARRAY(selfLiteral)->count; i++) { + if (Toy_literalsAreEqual(valueLiteral, TOY_AS_ARRAY(selfLiteral)->literals[i])) { + resultLiteral = TOY_TO_INTEGER_LITERAL(i); + break; + } + } + + //return the result and clean up + Toy_pushLiteralArray(&interpreter->stack, resultLiteral); + Toy_freeLiteral(resultLiteral); + Toy_freeLiteral(selfLiteral); + + return 1; +} + static int nativeMap(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { //no arguments if (arguments->count != 2) { @@ -1400,7 +1447,7 @@ int Toy_hookCompound(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_L {"_forEach", nativeForEach}, //array, dictionary {"_getKeys", nativeGetKeys}, //dictionary {"_getValues", nativeGetValues}, //dictionary - // {"_indexOf", native}, //array, string + {"_indexOf", nativeIndexOf}, //array // {"_insert", native}, //array, dictionary, string {"_map", nativeMap}, //array, dictionary {"_reduce", nativeReduce}, //array, dictionary diff --git a/source/toy_common.h b/source/toy_common.h index 3b1b415..c0c671a 100644 --- a/source/toy_common.h +++ b/source/toy_common.h @@ -10,7 +10,7 @@ #define TOY_VERSION_BUILD __DATE__ " " __TIME__ //platform/compiler-specific instructions -#if defined(__linux__) +#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__) #define TOY_API extern @@ -22,10 +22,6 @@ #define TOY_API __declspec(dllexport) #endif -#elif defined(__MINGW32__) - -#define TOY_API extern - #else #define TOY_API extern diff --git a/source/toy_console_colors.h b/source/toy_console_colors.h index 9671002..c1428f7 100644 --- a/source/toy_console_colors.h +++ b/source/toy_console_colors.h @@ -3,7 +3,7 @@ //NOTE: you need both font AND background for these to work //platform/compiler-specific instructions -#if defined(__linux__) || defined(__MINGW32__) +#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__) //fonts color #define TOY_CC_FONT_BLACK "\033[30;" diff --git a/test/scripts/lib/compound.toy b/test/scripts/lib/compound.toy index 3ea3bcf..d0d9256 100644 --- a/test/scripts/lib/compound.toy +++ b/test/scripts/lib/compound.toy @@ -155,6 +155,15 @@ import compound; } +//test indexOf +{ + var a = [1, 2, 42, 3]; + + //results are zero-indexed + assert a.indexOf(42) == 2, "_indexOf failed"; +} + + //test map { //test map with toy functions