Added _indexOf

This commit is contained in:
2023-02-12 14:32:26 +00:00
parent ab2cd5dc93
commit 8653a2663f
4 changed files with 59 additions and 7 deletions

View File

@@ -629,6 +629,53 @@ static int nativeGetValues(Toy_Interpreter* interpreter, Toy_LiteralArray* argum
return 1; 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) { static int nativeMap(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
//no arguments //no arguments
if (arguments->count != 2) { if (arguments->count != 2) {
@@ -1400,7 +1447,7 @@ int Toy_hookCompound(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_L
{"_forEach", nativeForEach}, //array, dictionary {"_forEach", nativeForEach}, //array, dictionary
{"_getKeys", nativeGetKeys}, //dictionary {"_getKeys", nativeGetKeys}, //dictionary
{"_getValues", nativeGetValues}, //dictionary {"_getValues", nativeGetValues}, //dictionary
// {"_indexOf", native}, //array, string {"_indexOf", nativeIndexOf}, //array
// {"_insert", native}, //array, dictionary, string // {"_insert", native}, //array, dictionary, string
{"_map", nativeMap}, //array, dictionary {"_map", nativeMap}, //array, dictionary
{"_reduce", nativeReduce}, //array, dictionary {"_reduce", nativeReduce}, //array, dictionary

View File

@@ -10,7 +10,7 @@
#define TOY_VERSION_BUILD __DATE__ " " __TIME__ #define TOY_VERSION_BUILD __DATE__ " " __TIME__
//platform/compiler-specific instructions //platform/compiler-specific instructions
#if defined(__linux__) #if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
#define TOY_API extern #define TOY_API extern
@@ -22,10 +22,6 @@
#define TOY_API __declspec(dllexport) #define TOY_API __declspec(dllexport)
#endif #endif
#elif defined(__MINGW32__)
#define TOY_API extern
#else #else
#define TOY_API extern #define TOY_API extern

View File

@@ -3,7 +3,7 @@
//NOTE: you need both font AND background for these to work //NOTE: you need both font AND background for these to work
//platform/compiler-specific instructions //platform/compiler-specific instructions
#if defined(__linux__) || defined(__MINGW32__) #if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
//fonts color //fonts color
#define TOY_CC_FONT_BLACK "\033[30;" #define TOY_CC_FONT_BLACK "\033[30;"

View File

@@ -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
{ {
//test map with toy functions //test map with toy functions