diff --git a/docs/TODO.txt b/docs/TODO.txt index c78facf..5163c4a 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -38,14 +38,14 @@ DONE: better sugar for _push, _pop, _length DONE: nested compound assignment bug -TODO: ternary operator? -TODO: Nullish types? TODO: hooks on the external libraries, triggered on import TODO: standard library TODO: external script runner library -TODO: document how it all works - book? -TODO: better API? +TODO: document how it all works TODO: packaging for release? +TODO: test embedding NOPE: a = b = c = 1; NOPE: functions return a set number of values +NOPE: ternary operator? +NOPE: Nullish types? diff --git a/source/lib_builtin.c b/source/lib_builtin.c index 2a95271..6ae2d47 100644 --- a/source/lib_builtin.c +++ b/source/lib_builtin.c @@ -749,14 +749,15 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { int lower = AS_INTEGER(third) > 0 ? AS_INTEGER(first) : AS_INTEGER(first) -1; int min = AS_INTEGER(third) > 0 ? AS_INTEGER(first) : AS_INTEGER(second) -1; + int max = AS_INTEGER(third) > 0 ? AS_INTEGER(second) + (AS_INTEGER(second) == compoundLength ? -1 : 0) : AS_INTEGER(second); //copy compound into result int resultIndex = 0; - for (int i = min; i >= 0 && i >= lower && i <= AS_INTEGER(second); i += AS_INTEGER(third)) { + for (int i = min; i >= 0 && i >= lower && i <= max; i += AS_INTEGER(third)) { result[ resultIndex++ ] = AS_STRING(compound)[ i ]; } - result[ resultIndex++ ] = '\0'; + result[ resultIndex ] = '\0'; //finally, swap out the compound for the result freeLiteral(compound); @@ -884,7 +885,7 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { result[ resultIndex++ ] = AS_STRING(compound)[ i ]; } - result[ resultIndex++ ] = '\0'; + result[ resultIndex ] = '\0'; } //else override elements of the array instead diff --git a/source/literal.c b/source/literal.c index 76ffa5c..f62ebe3 100644 --- a/source/literal.c +++ b/source/literal.c @@ -112,7 +112,7 @@ Literal copyLiteral(Literal original) { return original; case LITERAL_STRING: { - return TO_STRING_LITERAL(copyString(AS_STRING(original), strlen(AS_STRING(original))), strlen(AS_STRING(original))); + return TO_STRING_LITERAL(copyString(AS_STRING(original), original.as.string.length), original.as.string.length); } case LITERAL_ARRAY: { @@ -152,7 +152,7 @@ Literal copyLiteral(Literal original) { } case LITERAL_IDENTIFIER: { - return TO_IDENTIFIER_LITERAL(copyString(AS_IDENTIFIER(original), strlen(AS_IDENTIFIER(original)) ), strlen(AS_IDENTIFIER(original))); + return TO_IDENTIFIER_LITERAL(copyString(AS_IDENTIFIER(original), original.as.identifier.length), original.as.identifier.length); } case LITERAL_TYPE: { @@ -246,10 +246,10 @@ bool literalsAreEqual(Literal lhs, Literal rhs) { return AS_FLOAT(lhs) == AS_FLOAT(rhs); case LITERAL_STRING: - if (strlen(AS_STRING(lhs)) != strlen(AS_STRING(rhs))) { + if (lhs.as.string.length != rhs.as.string.length) { return false; } - return !strncmp(AS_STRING(lhs), AS_STRING(rhs), strlen(AS_STRING(lhs))); + return !strncmp(AS_STRING(lhs), AS_STRING(rhs), lhs.as.string.length); case LITERAL_ARRAY: case LITERAL_DICTIONARY_INTERMEDIATE: //BUGFIX @@ -295,11 +295,11 @@ bool literalsAreEqual(Literal lhs, Literal rhs) { case LITERAL_IDENTIFIER: //check shortcuts - if (HASH_I(lhs) != HASH_I(rhs) && strlen(AS_IDENTIFIER(lhs)) != strlen(AS_IDENTIFIER(rhs))) { + if (HASH_I(lhs) != HASH_I(rhs) && lhs.as.identifier.length != rhs.as.identifier.length) { return false; } - return !strncmp(AS_IDENTIFIER(lhs), AS_IDENTIFIER(rhs), strlen( AS_IDENTIFIER(lhs) )); + return !strncmp(AS_IDENTIFIER(lhs), AS_IDENTIFIER(rhs), lhs.as.identifier.length); case LITERAL_TYPE: //check types diff --git a/source/literal.h b/source/literal.h index e3159d5..e49cdb9 100644 --- a/source/literal.h +++ b/source/literal.h @@ -13,16 +13,16 @@ typedef enum { LITERAL_ARRAY, LITERAL_DICTIONARY, LITERAL_FUNCTION, - - //these are meta-level types LITERAL_IDENTIFIER, LITERAL_TYPE, + LITERAL_ANY, + + //these are meta-level types - not for general use LITERAL_TYPE_INTERMEDIATE, //used to process types in the compiler only LITERAL_DICTIONARY_INTERMEDIATE, //used to process dictionaries in the compiler only LITERAL_FUNCTION_INTERMEDIATE, //used to process functions in the compiler only - LITERAL_FUNCTION_ARG_REST, //used to process function rest parameters - LITERAL_FUNCTION_NATIVE, //for handling native functions - LITERAL_ANY, //used by the type system only + LITERAL_FUNCTION_ARG_REST, //used to process function rest parameters only + LITERAL_FUNCTION_NATIVE, //for handling native functions only } LiteralType; typedef struct {