diff --git a/docs/TODO.txt b/docs/TODO.txt index 57093d2..044d2d9 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -37,8 +37,8 @@ DONE: maximum recursion/function depth TODO: nested compound assignment -TODO: better sugar for _push and _pop -TODO: ternary operator +TODO: better sugar for _push, _pop, _length +TODO: ternary operator? TODO: Nullish types? TODO: hooks on the external libraries, triggered on import TODO: standard library diff --git a/scripts/test/index-arrays.toy b/scripts/test/index-arrays.toy index 8908a55..5a7e659 100644 --- a/scripts/test/index-arrays.toy +++ b/scripts/test/index-arrays.toy @@ -50,4 +50,15 @@ } +//test indexing with variables +{ + var week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]; + + var first = 1; + var second = 2; + var third = -1; + + assert week[first:second:third] == ["wednesday", "tuesday"], "indexing with variables failed"; +} + print "All good"; diff --git a/scripts/test/index-dictionaries.toy b/scripts/test/index-dictionaries.toy index b929c9e..192d48d 100644 --- a/scripts/test/index-dictionaries.toy +++ b/scripts/test/index-dictionaries.toy @@ -50,4 +50,13 @@ } +//test indexing with variables +{ + var d = ["one":1, "two":2, "three":3]; + + var first = "two"; + + assert d[first] == 2, "indexing with variables failed"; +} + print "All good"; diff --git a/scripts/test/index-strings.toy b/scripts/test/index-strings.toy index e0896eb..64c8648 100644 --- a/scripts/test/index-strings.toy +++ b/scripts/test/index-strings.toy @@ -29,4 +29,12 @@ numbers[::-2] = "abc"; assert numbers == "01234c6b8a", "string weird manipulation failed"; +//test indexing with variables +var first = 11; +var second = 15; +var third = -1; + +assert greeting[first:second:third] == "dlrow", "indexing with variables failed"; + + print "All good"; diff --git a/source/lib_builtin.c b/source/lib_builtin.c index 58121b7..f3ca621 100644 --- a/source/lib_builtin.c +++ b/source/lib_builtin.c @@ -260,6 +260,24 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { //dictionary - no slicing if (IS_DICTIONARY(compound)) { + if (IS_IDENTIFIER(first)) { + Literal idn = first; + parseIdentifierToValue(interpreter, &first); + freeLiteral(idn); + } + + if (IS_IDENTIFIER(second)) { + Literal idn = second; + parseIdentifierToValue(interpreter, &second); + freeLiteral(idn); + } + + if (IS_IDENTIFIER(third)) { + Literal idn = third; + parseIdentifierToValue(interpreter, &third); + freeLiteral(idn); + } + value = getLiteralDictionary(AS_DICTIONARY(compound), first); //dictionary @@ -314,8 +332,6 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { //array - slicing if (IS_ARRAY(compound)) { - value = getLiteralArray(AS_ARRAY(compound), first); - //array slice if (IS_NULL(op)) { //parse out the booleans & their defaults @@ -324,6 +340,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { freeLiteral(first); first = TO_INTEGER_LITERAL(0); } + + if (IS_IDENTIFIER(first)) { + Literal idn = first; + parseIdentifierToValue(interpreter, &first); + freeLiteral(idn); + } } if (!IS_NULL(second)) { @@ -331,6 +353,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { freeLiteral(second); second = TO_INTEGER_LITERAL(AS_ARRAY(compound)->count - 1); } + + if (IS_IDENTIFIER(second)) { + Literal idn = second; + parseIdentifierToValue(interpreter, &second); + freeLiteral(idn); + } } if (IS_NULL(third) || IS_BOOLEAN(third)) { @@ -338,6 +366,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { third = TO_INTEGER_LITERAL(1); } + if (IS_IDENTIFIER(third)) { + Literal idn = third; + parseIdentifierToValue(interpreter, &third); + freeLiteral(idn); + } + //handle each null case if (IS_NULL(first) || !IS_INTEGER(first)) { //something is weird - skip out @@ -405,13 +439,19 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { } //array slice assignment - else if (!strcmp( AS_STRING(op), "=")) { + if (IS_STRING(op) && !strcmp( AS_STRING(op), "=")) { //parse out the booleans & their defaults if (!IS_NULL(first)) { if (IS_BOOLEAN(first)) { freeLiteral(first); first = TO_INTEGER_LITERAL(0); } + + if (IS_IDENTIFIER(first)) { + Literal idn = first; + parseIdentifierToValue(interpreter, &first); + freeLiteral(idn); + } } if (!IS_NULL(second)) { @@ -419,6 +459,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { freeLiteral(second); second = TO_INTEGER_LITERAL(AS_INTEGER(first)); } + + if (IS_IDENTIFIER(second)) { + Literal idn = second; + parseIdentifierToValue(interpreter, &second); + freeLiteral(idn); + } } if (IS_NULL(third) || IS_BOOLEAN(third)) { @@ -426,6 +472,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { third = TO_INTEGER_LITERAL(1); } + if (IS_IDENTIFIER(third)) { + Literal idn = third; + parseIdentifierToValue(interpreter, &third); + freeLiteral(idn); + } + //handle each null case if (IS_NULL(first) || !IS_INTEGER(first)) { //something is weird - skip out @@ -547,31 +599,39 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { compound = TO_ARRAY_LITERAL(result); } - else if (IS_STRING(op) && !strcmp( AS_STRING(op), "+=")) { + if (IS_IDENTIFIER(first)) { + Literal idn = first; + parseIdentifierToValue(interpreter, &first); + freeLiteral(idn); + } + + value = getLiteralArray(AS_ARRAY(compound), first); + + if (IS_STRING(op) && !strcmp( AS_STRING(op), "+=")) { Literal lit = addition(interpreter, value, assign); setLiteralArray(AS_ARRAY(compound), first, lit); freeLiteral(lit); } - else if (IS_STRING(op) && !strcmp( AS_STRING(op), "-=")) { + if (IS_STRING(op) && !strcmp( AS_STRING(op), "-=")) { Literal lit = subtraction(interpreter, value, assign); setLiteralArray(AS_ARRAY(compound), first, lit); freeLiteral(lit); } - else if (IS_STRING(op) && !strcmp( AS_STRING(op), "*=")) { + if (IS_STRING(op) && !strcmp( AS_STRING(op), "*=")) { Literal lit = multiplication(interpreter, value, assign); setLiteralArray(AS_ARRAY(compound), first, lit); freeLiteral(lit); } - else if (IS_STRING(op) && !strcmp( AS_STRING(op), "/=")) { + if (IS_STRING(op) && !strcmp( AS_STRING(op), "/=")) { Literal lit = division(interpreter, value, assign); setLiteralArray(AS_ARRAY(compound), first, lit); freeLiteral(lit); } - else if (IS_STRING(op) && !strcmp( AS_STRING(op), "%=")) { + if (IS_STRING(op) && !strcmp( AS_STRING(op), "%=")) { Literal lit = modulo(interpreter, value, assign); setLiteralArray(AS_ARRAY(compound), first, lit); freeLiteral(lit); @@ -588,6 +648,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { freeLiteral(first); first = TO_INTEGER_LITERAL(0); } + + if (IS_IDENTIFIER(first)) { + Literal idn = first; + parseIdentifierToValue(interpreter, &first); + freeLiteral(idn); + } } if (!IS_NULL(second)) { @@ -595,6 +661,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { freeLiteral(second); second = TO_INTEGER_LITERAL(strlen(AS_STRING(compound))); } + + if (IS_IDENTIFIER(second)) { + Literal idn = second; + parseIdentifierToValue(interpreter, &second); + freeLiteral(idn); + } } if (IS_NULL(third) || IS_BOOLEAN(third)) { @@ -602,6 +674,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { third = TO_INTEGER_LITERAL(1); } + if (IS_IDENTIFIER(third)) { + Literal idn = third; + parseIdentifierToValue(interpreter, &third); + freeLiteral(idn); + } + //handle each null case if (IS_NULL(first) || !IS_INTEGER(first)) { //something is weird - skip out @@ -680,6 +758,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { freeLiteral(first); first = TO_INTEGER_LITERAL(0); } + + if (IS_IDENTIFIER(first)) { + Literal idn = first; + parseIdentifierToValue(interpreter, &first); + freeLiteral(idn); + } } if (!IS_NULL(second)) { @@ -687,6 +771,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { freeLiteral(second); second = TO_INTEGER_LITERAL(strlen(AS_STRING(compound))); } + + if (IS_IDENTIFIER(second)) { + Literal idn = second; + parseIdentifierToValue(interpreter, &second); + freeLiteral(idn); + } } if (IS_NULL(third) || IS_BOOLEAN(third)) { @@ -694,6 +784,12 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { third = TO_INTEGER_LITERAL(1); } + if (IS_IDENTIFIER(first)) { + Literal idn = first; + parseIdentifierToValue(interpreter, &first); + freeLiteral(idn); + } + //handle each null case if (IS_NULL(first) || !IS_INTEGER(first)) { //something is weird - skip out @@ -797,7 +893,7 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { } else if (IS_STRING(op) && !strcmp( AS_STRING(op), "+=")) { - Literal tmp = addition(interpreter, compound, value); + Literal tmp = addition(interpreter, compound, assign); freeLiteral(compound); compound = tmp; //don't clear tmp }