Changed how string/identifier lengths are determined in copyLiteral

This commit is contained in:
2022-09-15 16:36:42 +01:00
parent bb6e850548
commit 7eb16e51bb
4 changed files with 19 additions and 18 deletions

View File

@@ -38,14 +38,14 @@ DONE: better sugar for _push, _pop, _length
DONE: nested compound assignment bug DONE: nested compound assignment bug
TODO: ternary operator?
TODO: Nullish types?
TODO: hooks on the external libraries, triggered on import TODO: hooks on the external libraries, triggered on import
TODO: standard library TODO: standard library
TODO: external script runner library TODO: external script runner library
TODO: document how it all works - book? TODO: document how it all works
TODO: better API?
TODO: packaging for release? TODO: packaging for release?
TODO: test embedding
NOPE: a = b = c = 1; NOPE: a = b = c = 1;
NOPE: functions return a set number of values NOPE: functions return a set number of values
NOPE: ternary operator?
NOPE: Nullish types?

View File

@@ -749,14 +749,15 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) {
int lower = AS_INTEGER(third) > 0 ? AS_INTEGER(first) : AS_INTEGER(first) -1; 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 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 //copy compound into result
int resultIndex = 0; 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++ ] = AS_STRING(compound)[ i ];
} }
result[ resultIndex++ ] = '\0'; result[ resultIndex ] = '\0';
//finally, swap out the compound for the result //finally, swap out the compound for the result
freeLiteral(compound); freeLiteral(compound);
@@ -884,7 +885,7 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) {
result[ resultIndex++ ] = AS_STRING(compound)[ i ]; result[ resultIndex++ ] = AS_STRING(compound)[ i ];
} }
result[ resultIndex++ ] = '\0'; result[ resultIndex ] = '\0';
} }
//else override elements of the array instead //else override elements of the array instead

View File

@@ -112,7 +112,7 @@ Literal copyLiteral(Literal original) {
return original; return original;
case LITERAL_STRING: { 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: { case LITERAL_ARRAY: {
@@ -152,7 +152,7 @@ Literal copyLiteral(Literal original) {
} }
case LITERAL_IDENTIFIER: { 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: { case LITERAL_TYPE: {
@@ -246,10 +246,10 @@ bool literalsAreEqual(Literal lhs, Literal rhs) {
return AS_FLOAT(lhs) == AS_FLOAT(rhs); return AS_FLOAT(lhs) == AS_FLOAT(rhs);
case LITERAL_STRING: case LITERAL_STRING:
if (strlen(AS_STRING(lhs)) != strlen(AS_STRING(rhs))) { if (lhs.as.string.length != rhs.as.string.length) {
return false; 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_ARRAY:
case LITERAL_DICTIONARY_INTERMEDIATE: //BUGFIX case LITERAL_DICTIONARY_INTERMEDIATE: //BUGFIX
@@ -295,11 +295,11 @@ bool literalsAreEqual(Literal lhs, Literal rhs) {
case LITERAL_IDENTIFIER: case LITERAL_IDENTIFIER:
//check shortcuts //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 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: case LITERAL_TYPE:
//check types //check types

View File

@@ -13,16 +13,16 @@ typedef enum {
LITERAL_ARRAY, LITERAL_ARRAY,
LITERAL_DICTIONARY, LITERAL_DICTIONARY,
LITERAL_FUNCTION, LITERAL_FUNCTION,
//these are meta-level types
LITERAL_IDENTIFIER, LITERAL_IDENTIFIER,
LITERAL_TYPE, 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_TYPE_INTERMEDIATE, //used to process types in the compiler only
LITERAL_DICTIONARY_INTERMEDIATE, //used to process dictionaries 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_INTERMEDIATE, //used to process functions in the compiler only
LITERAL_FUNCTION_ARG_REST, //used to process function rest parameters LITERAL_FUNCTION_ARG_REST, //used to process function rest parameters only
LITERAL_FUNCTION_NATIVE, //for handling native functions LITERAL_FUNCTION_NATIVE, //for handling native functions only
LITERAL_ANY, //used by the type system only
} LiteralType; } LiteralType;
typedef struct { typedef struct {